Fixed and checked support for all available config types
This commit is contained in:
@@ -31,7 +31,7 @@ public class ConfigBase
|
||||
public final String modName;
|
||||
public final int configVersion;
|
||||
|
||||
/*
|
||||
/**
|
||||
What the config works with
|
||||
|
||||
Enum
|
||||
@@ -40,23 +40,29 @@ public class ConfigBase
|
||||
Integer
|
||||
Double
|
||||
Long
|
||||
// Float (to be tested)
|
||||
Float
|
||||
String
|
||||
Map<String, ?> // The ? should be another value from above
|
||||
|
||||
// Below, "T" should be a value from above
|
||||
List<T>
|
||||
ArrayList<T>
|
||||
Map<String, T>
|
||||
HashMap<String, T>
|
||||
*/
|
||||
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(Double.class);
|
||||
acceptableInputs.add(Long.class);
|
||||
// acceptableInputs.add(Float.class);
|
||||
acceptableInputs.add(String.class);
|
||||
acceptableInputs.add(Map.class); // TODO[CONFIG]: This is handled separately to check the first input is String and the second input is valid
|
||||
acceptableInputs.add(HashMap.class);
|
||||
}
|
||||
public static final List<Class<?>> acceptableInputs = new ArrayList<Class<?>>() {{
|
||||
add(Boolean.class);
|
||||
add(Byte.class);
|
||||
add(Short.class);
|
||||
add(Integer.class);
|
||||
add(Double.class);
|
||||
add(Long.class);
|
||||
add(Float.class);
|
||||
add(String.class);
|
||||
add(List.class);
|
||||
add(ArrayList.class);
|
||||
add(Map.class); // TODO[CONFIG]: This is handled separately to check the first input is String and the second input is valid
|
||||
add(HashMap.class);
|
||||
}};
|
||||
|
||||
/** Disables the minimum and maximum of a variable */
|
||||
public boolean disableMinMax = false; // Very fun to use
|
||||
@@ -64,16 +70,17 @@ public class ConfigBase
|
||||
|
||||
|
||||
public ConfigBase(String modID, String modName, Class<?> config, int configVersion) {
|
||||
LOGGER.info("Initialising config for " + modName);
|
||||
this.modID = modID;
|
||||
this.modName = modName;
|
||||
this.configVersion = configVersion;
|
||||
|
||||
addAcceptableInputs(); // Add all the acceptable stuff to the acceptableInputs list
|
||||
initNestedClass(config, ""); // Init root category
|
||||
|
||||
// File handling (load from file)
|
||||
this.configFileINSTANCE = new ConfigFileHandling(this);
|
||||
this.configFileINSTANCE.loadFromFile();
|
||||
LOGGER.info("Config for " + modName + " initialised");
|
||||
}
|
||||
|
||||
private void initNestedClass(Class<?> config, String category) {
|
||||
@@ -115,12 +122,14 @@ public class ConfigBase
|
||||
if (Clazz.isEnum())
|
||||
return true;
|
||||
for(Class<?> i: acceptableInputs) {
|
||||
if(Clazz == i)
|
||||
if(i == Clazz)
|
||||
return true;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
|
||||
|
||||
/**
|
||||
* Used for checking that all the lang files for the config exist
|
||||
*
|
||||
|
||||
@@ -50,7 +50,13 @@ public class ConfigFileHandling {
|
||||
}
|
||||
}
|
||||
|
||||
config.save();
|
||||
|
||||
try {
|
||||
config.save();
|
||||
} catch (Exception e) {
|
||||
// If it fails to save, crash game
|
||||
SingletonInjector.INSTANCE.get(IMinecraftClientWrapper.class).crashMinecraft("Failed to save config at ["+ ConfigPath.toString() +"]", e);
|
||||
}
|
||||
config.close();
|
||||
}
|
||||
/**
|
||||
@@ -81,7 +87,13 @@ public class ConfigFileHandling {
|
||||
}
|
||||
}
|
||||
|
||||
config.save();
|
||||
|
||||
try {
|
||||
config.save();
|
||||
} catch (Exception e) {
|
||||
// If it fails to save, crash game
|
||||
SingletonInjector.INSTANCE.get(IMinecraftClientWrapper.class).crashMinecraft("Failed to save config at ["+ ConfigPath.toString() +"]", e);
|
||||
}
|
||||
config.close();
|
||||
}
|
||||
|
||||
@@ -103,9 +115,9 @@ public class ConfigFileHandling {
|
||||
|
||||
if (ConfigTypeConverters.convertObjects.containsKey(entry.getType())) {
|
||||
workConfig.set(entry.getNameWCategory(), ConfigTypeConverters.convertToString(entry.getType(), entry.getTrueValue()));
|
||||
} else {
|
||||
workConfig.set(entry.getNameWCategory(), entry.getTrueValue());
|
||||
return;
|
||||
}
|
||||
workConfig.set(entry.getNameWCategory(), entry.getTrueValue());
|
||||
}
|
||||
|
||||
// Loads an entry when only given the entry
|
||||
@@ -125,20 +137,24 @@ public class ConfigFileHandling {
|
||||
try {
|
||||
if (entry.getType().isEnum()) {
|
||||
entry.setWithoutSaving((T) ( workConfig.getEnum(entry.getNameWCategory(), (Class<? extends Enum>) entry.getType()) ));
|
||||
} else if (ConfigTypeConverters.convertObjects.containsKey(entry.getType())) {
|
||||
entry.setWithoutSaving((T) ConfigTypeConverters.convertFromString(entry.getType(), workConfig.get(entry.getNameWCategory())));
|
||||
} else {
|
||||
if (entry.getType() == workConfig.get(entry.getNameWCategory()).getClass()) { // If the types are the same
|
||||
entry.setWithoutSaving((T) workConfig.get(entry.getNameWCategory()));
|
||||
|
||||
if (entry.isValid() == 0) return;
|
||||
else if (entry.isValid() == -1) entry.setWithoutSaving(entry.getMin());
|
||||
else if (entry.isValid() == 1) entry.setWithoutSaving(entry.getMax());
|
||||
} else {
|
||||
LOGGER.warn("Entry ["+ entry.getNameWCategory() +"] is invalid. Expected " + entry.getType() + " but got " + workConfig.get(entry.getNameWCategory()).getClass() + ". Using default value.");
|
||||
saveEntry(entry, workConfig);
|
||||
}
|
||||
return;
|
||||
}
|
||||
if (ConfigTypeConverters.convertObjects.containsKey(entry.getType())) {
|
||||
entry.setWithoutSaving((T) ConfigTypeConverters.convertFromString(entry.getType(), workConfig.get(entry.getNameWCategory())));
|
||||
return;
|
||||
}
|
||||
|
||||
if (entry.getType() == workConfig.get(entry.getNameWCategory()).getClass()) { // If the types are the same
|
||||
entry.setWithoutSaving((T) workConfig.get(entry.getNameWCategory()));
|
||||
|
||||
if (entry.isValid() == 0) return;
|
||||
else if (entry.isValid() == -1) entry.setWithoutSaving(entry.getMin());
|
||||
else if (entry.isValid() == 1) entry.setWithoutSaving(entry.getMax());
|
||||
return;
|
||||
}
|
||||
|
||||
LOGGER.warn("Entry ["+ entry.getNameWCategory() +"] is invalid. Expected " + entry.getType() + " but got " + workConfig.get(entry.getNameWCategory()).getClass() + ". Using default value.");
|
||||
saveEntry(entry, workConfig);
|
||||
} catch (Exception e) {
|
||||
// e.printStackTrace();
|
||||
LOGGER.warn("Entry ["+entry.getNameWCategory()+"] had an invalid value when loading the config. Using default value.");
|
||||
|
||||
@@ -16,6 +16,9 @@ import java.util.Map;
|
||||
public class ConfigTypeConverters {
|
||||
// Once you've made a converter add it to here where the first value is the type you want to convert and the 2nd value is the converter
|
||||
public static final Map<Class, ConverterBase> convertObjects = new HashMap<Class, ConverterBase>() {{
|
||||
put(Float.class, new FloatConverter());
|
||||
put(Byte.class, new ByteConverter());
|
||||
|
||||
put(Map.class, new MapConverter());
|
||||
put(HashMap.class, new MapConverter());
|
||||
}};
|
||||
@@ -48,6 +51,16 @@ public class ConfigTypeConverters {
|
||||
|
||||
|
||||
|
||||
// Float and Bytes are a bit wack with the config parser
|
||||
// So we just store them as strings
|
||||
public static class FloatConverter extends ConverterBase {
|
||||
@Override public String convertToString(Object item) { return ((Float) item).toString(); }
|
||||
@Override public Float convertFromString(String s) { return Float.valueOf(s); }
|
||||
}
|
||||
public static class ByteConverter extends ConverterBase {
|
||||
@Override public String convertToString(Object item) { return ((Byte) item).toString(); }
|
||||
@Override public Byte convertFromString(String s) { return Byte.valueOf(s); }
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
@@ -18,7 +18,7 @@ public abstract class AbstractConfigType<T, S> { // The S is the class that is e
|
||||
|
||||
protected ConfigEntryAppearance appearance;
|
||||
|
||||
public AbstractConfigType(ConfigEntryAppearance appearance, T value) {
|
||||
protected AbstractConfigType(ConfigEntryAppearance appearance, T value) {
|
||||
this.appearance = appearance;
|
||||
this.value = value;
|
||||
}
|
||||
@@ -41,15 +41,12 @@ public abstract class AbstractConfigType<T, S> { // The S is the class that is e
|
||||
}
|
||||
|
||||
|
||||
/** Should not be needed for anything other than the gui/file handling */
|
||||
public String getCategory() {
|
||||
return this.category;
|
||||
}
|
||||
/** Should not be needed for anything other than the gui/file handling */
|
||||
public String getName() {
|
||||
return this.name;
|
||||
}
|
||||
/** Should not be needed for anything other than the gui/file handling */
|
||||
public String getNameWCategory() {
|
||||
return (this.category.isEmpty() ? "" : this.category + ".") + this.name;
|
||||
}
|
||||
|
||||
@@ -11,7 +11,7 @@ import com.seibel.lod.core.config.types.enums.ConfigEntryAppearance;
|
||||
public class ConfigCategory extends AbstractConfigType<Class, ConfigCategory> {
|
||||
public String destination; // Where the category goes to
|
||||
|
||||
public ConfigCategory(ConfigEntryAppearance appearance, Class value, String destination) {
|
||||
private ConfigCategory(ConfigEntryAppearance appearance, Class value, String destination) {
|
||||
super(appearance, value);
|
||||
this.destination = destination;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user