diff options
Diffstat (limited to 'src/main/java/com/keuin/ohmyvanillamc/OhMyVanillaMinecraft.java')
-rw-r--r-- | src/main/java/com/keuin/ohmyvanillamc/OhMyVanillaMinecraft.java | 60 |
1 files changed, 56 insertions, 4 deletions
diff --git a/src/main/java/com/keuin/ohmyvanillamc/OhMyVanillaMinecraft.java b/src/main/java/com/keuin/ohmyvanillamc/OhMyVanillaMinecraft.java index a3d5c90..f76321a 100644 --- a/src/main/java/com/keuin/ohmyvanillamc/OhMyVanillaMinecraft.java +++ b/src/main/java/com/keuin/ohmyvanillamc/OhMyVanillaMinecraft.java @@ -1,15 +1,31 @@ package com.keuin.ohmyvanillamc; +import com.google.gson.Gson; +import com.google.gson.GsonBuilder; +import com.google.gson.JsonIOException; import net.fabricmc.api.ModInitializer; import net.minecraft.server.network.EntityTrackerEntry; +import java.io.*; import java.lang.reflect.Field; +import java.nio.charset.StandardCharsets; +import java.util.logging.Logger; public class OhMyVanillaMinecraft implements ModInitializer { - public static boolean disableFishSchooling = true; - public static boolean disablePhantomSpawning = false; - public static boolean disableWanderingTraderSpawning = false; + private static final Logger LOGGER = Logger.getLogger("OhMyVanillaMinecraft"); + + private static OmvmConfiguration configuration = null; + private static final OmvmConfiguration defaultConfiguration = new OmvmConfiguration( + false, + false, + false, + false + ); + + public static OmvmConfiguration getConfiguration() { + return configuration != null ? configuration : defaultConfiguration; + } private static void disableEntityTrackerEntrySpamming() { try { @@ -35,7 +51,43 @@ public class OhMyVanillaMinecraft implements ModInitializer { // However, some things (like resources) may still be uninitialized. // Proceed with mild caution. - System.out.println("OhMyVanillaMinecraft is loading..."); + LOGGER.info("Loading configuration..."); + + // load configuration + + final String fileName = "omvm.json"; + final File file = new File(fileName); + if (!file.exists()) { + LOGGER.info("Configuration file does not exist! Use default configuration."); + try (FileOutputStream fileOutputStream = new FileOutputStream(file)) { + final String jsonString = (new GsonBuilder().setPrettyPrinting().create()).toJson(defaultConfiguration); + try (OutputStreamWriter outputStreamWriter = new OutputStreamWriter(fileOutputStream)) { + outputStreamWriter.write(jsonString); + } + } catch (FileNotFoundException e) { + LOGGER.severe("Cannot write default configuration to file `" + fileName + "`: " + e); + } catch (IOException e) { + LOGGER.severe("Failed to write default configuration to file `" + fileName + "`: " + e); + } + } else { + try (FileInputStream fileInputStream = new FileInputStream(file)) { + try (InputStreamReader inputStreamReader = new InputStreamReader(fileInputStream, StandardCharsets.UTF_8)) { + try (BufferedReader reader = new BufferedReader(inputStreamReader)) { + configuration = (new Gson()).fromJson(reader, OmvmConfiguration.class); + } + } + } catch (IOException e) { + LOGGER.severe("Failed to read config file `" + fileName + "`: " + e); + } catch (JsonIOException e) { + LOGGER.severe("Failed to decode config json file `" + fileName + "`: " + e); + } + } + + if (configuration == null) { + configuration = defaultConfiguration; + } + + LOGGER.info("Configuration: \n==========\n" + configuration + "\n=========="); } } |