From db2636946efe455bff128a19a91868d257ccc9d0 Mon Sep 17 00:00:00 2001 From: coolGi2007 Date: Fri, 25 Mar 2022 17:26:26 +1030 Subject: [PATCH] Added a way to put multiple options into 1 item and added some code for standalone jar --- src/main/java/com/seibel/lod/core/Config.java | 5 ++ .../java/com/seibel/lod/core/JarMain.java | 30 ++++++++ .../seibel/lod/core/config/ConfigBase.java | 17 ++++- .../core/config/file/ConfigFileHandling.java | 22 ++++-- .../lod/core/jar/JarDependencySetup.java | 12 +++ .../config/ConfigWrapper.java | 38 ++++++++++ .../com/seibel/lod/core/util/MultiOption.java | 76 +++++++++++++++++++ 7 files changed, 190 insertions(+), 10 deletions(-) create mode 100644 src/main/java/com/seibel/lod/core/JarMain.java create mode 100644 src/main/java/com/seibel/lod/core/jar/JarDependencySetup.java create mode 100644 src/main/java/com/seibel/lod/core/jar/wrapperInterfaces/config/ConfigWrapper.java create mode 100644 src/main/java/com/seibel/lod/core/util/MultiOption.java diff --git a/src/main/java/com/seibel/lod/core/Config.java b/src/main/java/com/seibel/lod/core/Config.java index b7bfe127c..e38aab678 100644 --- a/src/main/java/com/seibel/lod/core/Config.java +++ b/src/main/java/com/seibel/lod/core/Config.java @@ -3,6 +3,7 @@ package com.seibel.lod.core; import com.seibel.lod.core.config.*; import com.seibel.lod.core.enums.config.*; import com.seibel.lod.core.enums.rendering.*; +import com.seibel.lod.core.util.MultiOption; import com.seibel.lod.core.wrapperInterfaces.config.ILodConfigWrapperSingleton; /** @@ -47,6 +48,10 @@ public class Config .set(ILodConfigWrapperSingleton.IClient.OPTIONS_BUTTON_DEFAULT) .build(); + public static ConfigEntry> testMultiOption = new ConfigEntry.Builder>() + .set(new MultiOption().set("overworld", true).set("nether", false)) + .build(); + public static class Graphics { diff --git a/src/main/java/com/seibel/lod/core/JarMain.java b/src/main/java/com/seibel/lod/core/JarMain.java new file mode 100644 index 000000000..ac53996a2 --- /dev/null +++ b/src/main/java/com/seibel/lod/core/JarMain.java @@ -0,0 +1,30 @@ +package com.seibel.lod.core; + +import com.seibel.lod.core.jar.JarDependencySetup; + +import java.io.InputStream; + +/** + * The main class when you run the standalone jar + * + * @author coolGi + */ +public class JarMain { + public static void main(String[] args){ + JarDependencySetup.createInitialBindings(); + System.out.println("Why are you running the jar, this isn't done yet >:("); + } + + /** Get a file within the mods resources */ + public static InputStream accessFile(String resource) { + + // this is the path within the jar file + InputStream input = JarMain.class.getResourceAsStream("/resources/" + resource); + if (input == null) { + // this is how we load file within editor (eg eclipse) + input = JarMain.class.getClassLoader().getResourceAsStream(resource); + } + + return input; + } +} diff --git a/src/main/java/com/seibel/lod/core/config/ConfigBase.java b/src/main/java/com/seibel/lod/core/config/ConfigBase.java index 4f2d229e2..cbf99cd39 100644 --- a/src/main/java/com/seibel/lod/core/config/ConfigBase.java +++ b/src/main/java/com/seibel/lod/core/config/ConfigBase.java @@ -17,6 +17,19 @@ import java.util.List; * @author Ran */ public class ConfigBase { + /* + What the config works with + + Enum + Integer + Boolean + Double + Float + Byte + Map or MultiOption + */ + + public static final List> entries = new ArrayList>(); public static final List categories = new ArrayList<>(); @@ -26,10 +39,6 @@ public class ConfigBase { // File handling (load from file) ConfigFileHandling.loadFromFile(); - - // Temporary to see stuff - System.out.println(entries); - System.out.println(categories); } private static void initNestedClass(Class config, String category) { diff --git a/src/main/java/com/seibel/lod/core/config/file/ConfigFileHandling.java b/src/main/java/com/seibel/lod/core/config/file/ConfigFileHandling.java index c0e0fd8d3..0dbcae557 100644 --- a/src/main/java/com/seibel/lod/core/config/file/ConfigFileHandling.java +++ b/src/main/java/com/seibel/lod/core/config/file/ConfigFileHandling.java @@ -4,6 +4,7 @@ import com.electronwill.nightconfig.core.file.CommentedFileConfig; import com.seibel.lod.core.ModInfo; import com.seibel.lod.core.config.ConfigBase; import com.seibel.lod.core.config.ConfigEntry; +import com.seibel.lod.core.util.MultiOption; import com.seibel.lod.core.util.SingletonHandler; import com.seibel.lod.core.wrapperInterfaces.minecraft.IMinecraftWrapper; @@ -65,8 +66,13 @@ public class ConfigFileHandling { saveEntry(entry, config); config.close(); } + @SuppressWarnings("unchecked") public static void saveEntry(ConfigEntry entry, CommentedFileConfig workConfig) { - workConfig.set(entry.getNameWCategory(), entry.get()); + if (!entry.get().getClass().isAssignableFrom(MultiOption.class)) { + workConfig.set(entry.getNameWCategory(), entry.get()); + } else { + workConfig.set(entry.getNameWCategory(), ((MultiOption) entry.get()).getAsString()); + } } public static void loadEntry(ConfigEntry entry) { loadConfig(config); @@ -79,11 +85,15 @@ public class ConfigFileHandling { public static void loadEntry(ConfigEntry entry, CommentedFileConfig workConfig) { if (workConfig.contains(entry.getNameWCategory())) { if (entry.get().getClass().isEnum()) { - // Safe cast due to above checking that is indeed a Enum - // And the second cast back to is safe due to the template - entry.setWTSave((T)( - workConfig.getEnum(entry.getNameWCategory(), (Class) entry.get().getClass()) - )); + // Safe cast due to above checking that is indeed a Enum + // And the second cast back to is safe due to the template + entry.setWTSave((T) ( + workConfig.getEnum(entry.getNameWCategory(), (Class) entry.get().getClass()) + )); + } else if (entry.get().getClass().isAssignableFrom(MultiOption.class)) { + entry.setWTSave( + (T) new MultiOption().getFromString(workConfig.get(entry.getNameWCategory())) + ); } else { entry.setWTSave(workConfig.get(entry.getNameWCategory())); } diff --git a/src/main/java/com/seibel/lod/core/jar/JarDependencySetup.java b/src/main/java/com/seibel/lod/core/jar/JarDependencySetup.java new file mode 100644 index 000000000..1181276c2 --- /dev/null +++ b/src/main/java/com/seibel/lod/core/jar/JarDependencySetup.java @@ -0,0 +1,12 @@ +package com.seibel.lod.core.jar; + +import com.seibel.lod.core.jar.wrapperInterfaces.config.ConfigWrapper; +import com.seibel.lod.core.util.SingletonHandler; +import com.seibel.lod.core.wrapperInterfaces.config.IConfigWrapper; + +public class JarDependencySetup { + public static void createInitialBindings() { + SingletonHandler.bind(IConfigWrapper.class, ConfigWrapper.INSTANCE); + ConfigWrapper.init(); + } +} diff --git a/src/main/java/com/seibel/lod/core/jar/wrapperInterfaces/config/ConfigWrapper.java b/src/main/java/com/seibel/lod/core/jar/wrapperInterfaces/config/ConfigWrapper.java new file mode 100644 index 000000000..be754fe7f --- /dev/null +++ b/src/main/java/com/seibel/lod/core/jar/wrapperInterfaces/config/ConfigWrapper.java @@ -0,0 +1,38 @@ +package com.seibel.lod.core.jar.wrapperInterfaces.config; + +import com.seibel.lod.core.JarMain; +import com.seibel.lod.core.wrapperInterfaces.config.IConfigWrapper; +import org.json.simple.JSONObject; +import org.json.simple.parser.JSONParser; +import org.json.simple.parser.ParseException; + +public class ConfigWrapper implements IConfigWrapper { + public static final ConfigWrapper INSTANCE = new ConfigWrapper(); + private static JSONObject jsonObject = new JSONObject(); + + public static void init() { + try { + Object obj = new JSONParser().parse(JarMain.accessFile("assets/lod/lang/en_us.json").toString()); + jsonObject = (JSONObject) obj; + } catch (ParseException e) { + e.printStackTrace(); + } + } + + @Override + public boolean LangExists(String str) { + try { + if (jsonObject.get(str) == null) + return false; + else + return true; + } catch (Exception e) { + return false; + } + } + + @Override + public String getFromLang(String str) { + return (String) jsonObject.get(str); + } +} diff --git a/src/main/java/com/seibel/lod/core/util/MultiOption.java b/src/main/java/com/seibel/lod/core/util/MultiOption.java new file mode 100644 index 000000000..b06fbd8c7 --- /dev/null +++ b/src/main/java/com/seibel/lod/core/util/MultiOption.java @@ -0,0 +1,76 @@ +package com.seibel.lod.core.util; + +import org.json.simple.JSONObject; +import org.json.simple.parser.JSONParser; +import org.json.simple.parser.ParseException; + +import java.util.ArrayList; +import java.util.HashMap; +import java.util.List; +import java.util.Map; + +/** + * A way to add a config option that contains multiple items + * This could be used for multiple dimensions + * + * @author coolGi + */ +// TODO: Add support for enum's +public class MultiOption { +// Map items = new HashMap<>(); // TODO: Later on use this rather than using 2 lists + List itemNames = new ArrayList<>(); + List itemValues = new ArrayList<>(); + + public MultiOption(List itemNames, List itemValues) { + this.itemNames = itemNames; + this.itemValues = itemValues; + } + + public MultiOption() { + + } + + public String getAsString() { + JSONObject jsonObject = new JSONObject(); + + for (int i=0; i< itemNames.size(); i++) { + jsonObject.put(itemNames.get(i), itemValues.get(i)); + } + + return jsonObject.toJSONString(); + } + + public MultiOption getFromString(String s) { + List tmpItemNames = new ArrayList<>(); + List tmpItemValues = new ArrayList<>(); + + JSONObject jsonObject = null; + try { + jsonObject = (JSONObject) new JSONParser().parse(s); + } catch (ParseException p) { + p.printStackTrace(); + } + + for (int i = 0; i < jsonObject.keySet().toArray().length; i++) { + tmpItemNames.add((String) jsonObject.keySet().toArray()[i]); + tmpItemValues.add((T) jsonObject.get(jsonObject.keySet().toArray()[i])); + } + + this.itemNames = tmpItemNames; + this.itemValues = tmpItemValues; + return this; + } + + public T get(String item) { + return itemValues.get(itemNames.indexOf(item)); + } + public MultiOption set(String item, T value) { + if (itemNames.contains(item)) { + itemValues.set(itemNames.indexOf(item), value); + } else { + itemNames.add(item); + itemValues.add(value); + } + return this; + } +}