From 6f7b1ac223d36335e3715c29f9f7906fc0d7a13d Mon Sep 17 00:00:00 2001 From: coolGi2007 Date: Sat, 26 Mar 2022 20:07:12 +1030 Subject: [PATCH] Allowed use of hash map rather than using MultiOption --- src/main/java/com/seibel/lod/core/Config.java | 8 +- .../seibel/lod/core/config/ConfigBase.java | 48 +++++++++--- .../core/config/file/ConfigFileHandling.java | 48 +++++++++--- .../com/seibel/lod/core/util/MultiOption.java | 76 ------------------- 4 files changed, 79 insertions(+), 101 deletions(-) delete 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 e38aab678..5976c3918 100644 --- a/src/main/java/com/seibel/lod/core/Config.java +++ b/src/main/java/com/seibel/lod/core/Config.java @@ -3,9 +3,11 @@ 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; +import java.util.HashMap; +import java.util.Map; + /** * This handles any configuration the user has access to. * @author coolGi2007 @@ -48,8 +50,8 @@ 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)) + public static ConfigEntry> testMultiOption = new ConfigEntry.Builder>() + .set(new HashMap() {{put("overworld", true); put("nether", false);}}) .build(); 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 cbf99cd39..f8d6551c9 100644 --- a/src/main/java/com/seibel/lod/core/config/ConfigBase.java +++ b/src/main/java/com/seibel/lod/core/config/ConfigBase.java @@ -1,13 +1,12 @@ package com.seibel.lod.core.config; -import com.seibel.lod.core.ModInfo; +import com.seibel.lod.core.api.ClientApi; import com.seibel.lod.core.config.file.ConfigFileHandling; -import com.seibel.lod.core.util.SingletonHandler; -import com.seibel.lod.core.wrapperInterfaces.minecraft.IMinecraftWrapper; import java.io.File; import java.lang.reflect.Field; import java.util.ArrayList; +import java.util.HashMap; import java.util.List; /** @@ -28,13 +27,24 @@ public class ConfigBase { Byte Map or MultiOption */ - + public static final List> acceptableInputs = new ArrayList<>(); + private static void addAcceptableInputs() { + acceptableInputs.add(Boolean.class); + acceptableInputs.add(Byte.class); + acceptableInputs.add(Short.class); + acceptableInputs.add(Integer.class); + acceptableInputs.add(Long.class); + acceptableInputs.add(Float.class); + acceptableInputs.add(String.class); + acceptableInputs.add(HashMap.class); + } public static final List> entries = new ArrayList>(); public static final List categories = new ArrayList<>(); public static void init(Class config) { - categories.add(""); // Add root category to category list + addAcceptableInputs(); // Add all of the acceptable stuff to the acceptableInputs list +// categories.add(""); // Add root category to category list initNestedClass(config, ""); // Init root category // File handling (load from file) @@ -44,23 +54,37 @@ public class ConfigBase { private static void initNestedClass(Class config, String category) { // Put all the entries in entries - for (Field field : config.getFields()) - { + for (Field field : config.getFields()) { if (ConfigEntry.class.isAssignableFrom(field.getType())) { // If item is type ConfigEntry try { - entries.add((ConfigEntry) field.get(field.getType())); // Add to entries + if (isAcceptableType(((ConfigEntry) field.get(field.getType())).get().getClass())) { + entries.add((ConfigEntry) field.get(field.getType())); // Add to entries + entries.get(entries.size() - 1).category = category; + entries.get(entries.size() - 1).name = field.getName(); + } else { + ClientApi.LOGGER.error("Invalid variable type at [" + (category.isEmpty() ? "" : category + ".") + field.getName() + "]."); + ClientApi.LOGGER.error("Type [" + ((ConfigEntry) field.get(field.getType())).get().getClass() + "] is not one of these types [" + acceptableInputs.toString() + "]"); + } } catch (IllegalAccessException exception) { exception.printStackTrace(); } - entries.get(entries.size() - 1).category = category; - entries.get(entries.size() - 1).name = field.getName(); } - if (field.isAnnotationPresent(ConfigAnnotations.Category.class)) { // If it's a category then init the stuff inside it and put it in the category list + if (field.isAnnotationPresent(ConfigAnnotations.Category.class)) { // If it's a category then init the stuff inside it and put it in the category list String NCategory = (category.isEmpty() ? "" : category + ".") + field.getName(); categories.add(NCategory); initNestedClass(field.getType(), NCategory); } - } + } + } + + private static boolean isAcceptableType(Class Class) { + if (Class.isEnum()) + return true; + for(Class i: acceptableInputs) { + if(Class == i) + return true; + } + return false; } } 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 0dbcae557..16217adaa 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,14 +4,16 @@ 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; +import org.json.simple.JSONObject; +import org.json.simple.parser.JSONParser; +import org.json.simple.parser.ParseException; -import java.io.File; import java.io.IOException; import java.nio.file.Files; import java.nio.file.Path; +import java.util.HashMap; /** * Handles all stuff to do with the files @@ -55,7 +57,6 @@ public class ConfigFileHandling { for (ConfigEntry entry : ConfigBase.entries) { createComment(entry, config); loadEntry(entry, config); - System.out.println(entry.get()); } config.close(); @@ -68,10 +69,10 @@ public class ConfigFileHandling { } @SuppressWarnings("unchecked") public static void saveEntry(ConfigEntry entry, CommentedFileConfig workConfig) { - if (!entry.get().getClass().isAssignableFrom(MultiOption.class)) { + if (!entry.get().getClass().isAssignableFrom(HashMap.class)) { workConfig.set(entry.getNameWCategory(), entry.get()); } else { - workConfig.set(entry.getNameWCategory(), ((MultiOption) entry.get()).getAsString()); + workConfig.set(entry.getNameWCategory(), getStringFromHashMap((HashMap) entry.get())); } } public static void loadEntry(ConfigEntry entry) { @@ -90,14 +91,11 @@ public class ConfigFileHandling { 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 if (entry.get().getClass().isAssignableFrom(HashMap.class)) { + entry.setWTSave((T) getHashMapFromString(workConfig.get(entry.getNameWCategory()))); } else { entry.setWTSave(workConfig.get(entry.getNameWCategory())); } - System.out.println(workConfig.get(entry.getNameWCategory()).getClass().toString()); // entry.setWTSave(workConfig.get(entry.getNameWCategory())); } else { saveEntry(entry, workConfig); @@ -129,4 +127,34 @@ public class ConfigFileHandling { } } } + + + + + // Stuff for converting HashMap's and String's + public static String getStringFromHashMap(HashMap item) { + JSONObject jsonObject = new JSONObject(); + + for (int i=0; i< item.size(); i++) { + jsonObject.put(item.keySet().toArray()[i], item.get(item.keySet().toArray()[i])); + } + + return jsonObject.toJSONString(); + } + + public static HashMap getHashMapFromString(String s) { + HashMap map = new HashMap<>(); + + 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++) { + map.put((String) jsonObject.keySet().toArray()[i], (type) jsonObject.get(jsonObject.keySet().toArray()[i])); + } + return map; + } } diff --git a/src/main/java/com/seibel/lod/core/util/MultiOption.java b/src/main/java/com/seibel/lod/core/util/MultiOption.java deleted file mode 100644 index b06fbd8c7..000000000 --- a/src/main/java/com/seibel/lod/core/util/MultiOption.java +++ /dev/null @@ -1,76 +0,0 @@ -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; - } -}