Allowed use of hash map rather than using MultiOption

This commit is contained in:
coolGi2007
2022-03-26 20:07:12 +10:30
parent db2636946e
commit 6f7b1ac223
4 changed files with 79 additions and 101 deletions
@@ -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<MultiOption<Boolean>> testMultiOption = new ConfigEntry.Builder<MultiOption<Boolean>>()
.set(new MultiOption<Boolean>().set("overworld", true).set("nether", false))
public static ConfigEntry<HashMap<String, Boolean>> testMultiOption = new ConfigEntry.Builder<HashMap<String, Boolean>>()
.set(new HashMap<String, Boolean>() {{put("overworld", true); put("nether", false);}})
.build();
@@ -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<String, Boolean> or MultiOption
*/
public static final List<Class<?>> 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<ConfigEntry<?>> entries = new ArrayList<ConfigEntry<?>>();
public static final List<String> 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;
}
}
@@ -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<String, ?>) entry.get()));
}
}
public static void loadEntry(ConfigEntry<?> entry) {
@@ -90,14 +91,11 @@ public class ConfigFileHandling {
entry.setWTSave((T) (
workConfig.getEnum(entry.getNameWCategory(), (Class<? extends Enum>) entry.get().getClass())
));
} else if (entry.get().getClass().isAssignableFrom(MultiOption.class)) {
entry.setWTSave(
(T) new MultiOption<T>().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<String, ?> 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 <type> HashMap<String, ?> getHashMapFromString(String s) {
HashMap<String, type> 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;
}
}
@@ -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<T> {
// Map<String, T> items = new HashMap<>(); // TODO: Later on use this rather than using 2 lists
List<String> itemNames = new ArrayList<>();
List<T> itemValues = new ArrayList<>();
public MultiOption(List<String> itemNames, List<T> 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<String> tmpItemNames = new ArrayList<>();
List<T> 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;
}
}