Skip to content

Commit

Permalink
Merge pull request #27 from Protonull/better-loadconfig
Browse files Browse the repository at this point in the history
  • Loading branch information
Protonull authored Aug 29, 2024
2 parents 4f792f2 + 880c8f0 commit 43eda8d
Showing 1 changed file with 36 additions and 19 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -2,20 +2,21 @@

import com.google.common.eventbus.Subscribe;
import com.mojang.blaze3d.platform.InputConstants.Type;
import java.io.ByteArrayInputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import java.lang.reflect.Field;
import java.nio.file.Path;
import java.util.Properties;
import net.minecraft.client.KeyMapping;
import net.minecraft.client.Minecraft;
import net.minecraft.client.Options;
import net.minecraft.client.gui.Gui;
import net.minecraft.client.gui.screens.Screen;
import net.minecraft.client.renderer.entity.ItemRenderer;
import org.apache.commons.io.FileUtils;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.jetbrains.annotations.NotNull;
Expand Down Expand Up @@ -111,6 +112,12 @@ public final void enable() {

public abstract void registerKeyBinding(KeyMapping mapping);

/**
* Even though this will almost certainly be `.minecraft/config/` for all mod loaders, it's best practice
* nonetheless to use what the mod loader gives you.
*/
protected abstract @NotNull File getConfigFolder();

@Subscribe
private void tick(
final @NotNull ClientTickEvent event
Expand All @@ -121,27 +128,37 @@ private void tick(
}

private void loadConfig() {
Properties properties = new Properties();
Path gameDir = Minecraft.getInstance().gameDirectory.toPath();
File configFile = gameDir.resolve("config").resolve("civmodern.properties").toFile();
final File configDir = getConfigFolder();
final var configFile = new File(configDir, "civmodern.properties");
InputStream configReadStream;
try {
if (!configFile.exists()) {
InputStream resource = AbstractCivModernMod.class
.getResourceAsStream("/civmodern.properties");
byte[] buffer = new byte[resource.available()];
resource.read(buffer);
FileOutputStream fos = new FileOutputStream(configFile);
fos.write(buffer);
configReadStream = new FileInputStream(configFile);
}
catch (final FileNotFoundException ignored) {
final byte[] raw;
try (final InputStream defaultConfigResource = AbstractCivModernMod.class.getResourceAsStream("/civmodern.properties")) {
raw = defaultConfigResource.readAllBytes(); // Ignore highlighter
}

FileInputStream input = new FileInputStream(configFile);
properties.load(input);
} catch (IOException ex) {
ex.printStackTrace();
catch (final IOException e) {
throw new IllegalStateException("Could not read CivModern's default config resource!", e);
}
configDir.mkdirs(); // Just in case
try {
FileUtils.writeByteArrayToFile(configFile, raw);
}
catch (final IOException e) {
throw new IllegalStateException("Could not save CivModern's default config resource!", e);
}
configReadStream = new ByteArrayInputStream(raw);
}
final var properties = new Properties();
try {
properties.load(configReadStream);
}
catch (final IOException e) {
throw new IllegalStateException("Could not parse CivModern's default config resource!", e);
}

this.config = new CivMapConfig(configFile, properties);

}

private void loadRadar() {
Expand Down

0 comments on commit 43eda8d

Please sign in to comment.