Added a way to put multiple options into 1 item and added some code for standalone jar

This commit is contained in:
coolGi2007
2022-03-25 17:26:26 +10:30
parent 2fa54347ea
commit db2636946e
7 changed files with 190 additions and 10 deletions
@@ -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<MultiOption<Boolean>> testMultiOption = new ConfigEntry.Builder<MultiOption<Boolean>>()
.set(new MultiOption<Boolean>().set("overworld", true).set("nether", false))
.build();
public static class Graphics
{
@@ -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;
}
}
@@ -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<String, Boolean> or MultiOption
*/
public static final List<ConfigEntry<?>> entries = new ArrayList<ConfigEntry<?>>();
public static final List<String> 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) {
@@ -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 <T> void loadEntry(ConfigEntry<T> entry, CommentedFileConfig workConfig) {
if (workConfig.contains(entry.getNameWCategory())) {
if (entry.get().getClass().isEnum()) {
// Safe cast due to above checking that <T> is indeed a Enum
// And the second cast back to <T> is safe due to the template
entry.setWTSave((T)(
workConfig.getEnum(entry.getNameWCategory(), (Class<? extends Enum>) entry.get().getClass())
));
// Safe cast due to above checking that <T> is indeed a Enum
// And the second cast back to <T> is safe due to the template
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 {
entry.setWTSave(workConfig.get(entry.getNameWCategory()));
}
@@ -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();
}
}
@@ -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);
}
}
@@ -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<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;
}
}