Add classic config GUI part 2/3
This commit is contained in:
@@ -31,15 +31,15 @@ public class Config
|
||||
// |-> Buffers
|
||||
// |-> Debugging
|
||||
|
||||
public static ConfigCategory<Client> client = new ConfigCategory.Builder<Client>().set(new Client()).build();
|
||||
public static ConfigCategory client = new ConfigCategory.Builder().set(Client.class).build();
|
||||
|
||||
public static class Client
|
||||
{
|
||||
public static ConfigCategory<Graphics> graphics = new ConfigCategory.Builder<Graphics>().set(new Graphics()).build();
|
||||
public static ConfigCategory graphics = new ConfigCategory.Builder().set(Graphics.class).build();
|
||||
|
||||
public static ConfigCategory<WorldGenerator> worldGenerator = new ConfigCategory.Builder<WorldGenerator>().set(new WorldGenerator()).build();
|
||||
public static ConfigCategory worldGenerator = new ConfigCategory.Builder().set(WorldGenerator.class).build();
|
||||
|
||||
public static ConfigCategory<Advanced> advanced = new ConfigCategory.Builder<Advanced>().set(new Advanced()).build();
|
||||
public static ConfigCategory advanced = new ConfigCategory.Builder().set(Advanced.class).build();
|
||||
|
||||
|
||||
public static ConfigEntry<Boolean> optionsButton = new ConfigEntry.Builder<Boolean>()
|
||||
@@ -47,18 +47,18 @@ public class Config
|
||||
.set(ILodConfigWrapperSingleton.IClient.OPTIONS_BUTTON_DEFAULT)
|
||||
.build();
|
||||
|
||||
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();
|
||||
// public static ConfigEntry<HashMap<String, Boolean>> testMultiOption = new ConfigEntry.Builder<HashMap<String, Boolean>>()
|
||||
// .set(new HashMap<>() {{put("overworld", true); put("nether", false);}})
|
||||
// .build();
|
||||
|
||||
|
||||
public static class Graphics
|
||||
{
|
||||
public static ConfigCategory<Quality> quality = new ConfigCategory.Builder<Quality>().set(new Quality()).build();
|
||||
public static ConfigCategory quality = new ConfigCategory.Builder().set(Quality.class).build();
|
||||
|
||||
public static ConfigCategory<FogQuality> fogQuality = new ConfigCategory.Builder<FogQuality>().set(new FogQuality()).build();
|
||||
public static ConfigCategory fogQuality = new ConfigCategory.Builder().set(FogQuality.class).build();
|
||||
|
||||
public static ConfigCategory<AdvancedGraphics> advancedGraphics = new ConfigCategory.Builder<AdvancedGraphics>().set(new AdvancedGraphics()).build();
|
||||
public static ConfigCategory advancedGraphics = new ConfigCategory.Builder().set(AdvancedGraphics.class).build();
|
||||
|
||||
|
||||
public static class Quality
|
||||
@@ -176,11 +176,11 @@ public class Config
|
||||
|
||||
public static class Advanced
|
||||
{
|
||||
public static ConfigCategory<Threading> threading = new ConfigCategory.Builder<Threading>().set(new Threading()).build();
|
||||
public static ConfigCategory threading = new ConfigCategory.Builder().set(Threading.class).build();
|
||||
|
||||
public static ConfigCategory<Debugging> debugging = new ConfigCategory.Builder<Debugging>().set(new Debugging()).build();
|
||||
public static ConfigCategory debugging = new ConfigCategory.Builder().set(Debugging.class).build();
|
||||
|
||||
public static ConfigCategory<Buffers> buffers = new ConfigCategory.Builder<Buffers>().set(new Buffers()).build();
|
||||
public static ConfigCategory buffers = new ConfigCategory.Builder().set(Buffers.class).build();
|
||||
|
||||
|
||||
public static class Threading
|
||||
|
||||
@@ -11,6 +11,7 @@ import java.lang.reflect.Field;
|
||||
import java.util.ArrayList;
|
||||
import java.util.HashMap;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
|
||||
/**
|
||||
* Indexes and sets everything up for the file handling and gui
|
||||
@@ -42,10 +43,10 @@ public class ConfigBase {
|
||||
acceptableInputs.add(Long.class);
|
||||
// acceptableInputs.add(Float.class);
|
||||
acceptableInputs.add(String.class);
|
||||
acceptableInputs.add(HashMap.class);
|
||||
acceptableInputs.add(HashMap.class); // TODO[CONFIG]: This is handled separately to check the first input is String and the second input is valid
|
||||
}
|
||||
|
||||
public static final List<AbstractConfigType<?>> entries = new ArrayList<>();
|
||||
public static final List<AbstractConfigType<?, ?>> entries = new ArrayList<>();
|
||||
|
||||
public static void init(Class<?> config) {
|
||||
addAcceptableInputs(); // Add all of the acceptable stuff to the acceptableInputs list
|
||||
@@ -62,12 +63,12 @@ public class ConfigBase {
|
||||
for (Field field : config.getFields()) {
|
||||
if (AbstractConfigType.class.isAssignableFrom(field.getType())) {
|
||||
try {
|
||||
entries.add((AbstractConfigType<?>) field.get(field.getType()));
|
||||
entries.add((AbstractConfigType<?, ?>) field.get(field.getType()));
|
||||
} catch (IllegalAccessException exception) {
|
||||
exception.printStackTrace();
|
||||
}
|
||||
|
||||
AbstractConfigType<?> entry = entries.get(entries.size() - 1);
|
||||
AbstractConfigType<?, ?> entry = entries.get(entries.size() - 1);
|
||||
entry.category = category;
|
||||
entry.name = field.getName();
|
||||
|
||||
@@ -75,21 +76,26 @@ public class ConfigBase {
|
||||
if (!isAcceptableType(((ConfigEntry<?>) entry).get().getClass())) {
|
||||
ClientApi.LOGGER.error("Invalid variable type at [" + (category.isEmpty() ? "" : category + ".") + field.getName() + "].");
|
||||
ClientApi.LOGGER.error("Type [" + ((ConfigEntry<?>) entry).get().getClass() + "] is not one of these types [" + acceptableInputs.toString() + "]");
|
||||
entries.remove(entries.size() -1); // Delete the entry if it is invalid so the game can still run
|
||||
}
|
||||
}
|
||||
|
||||
if (ConfigCategory.class.isAssignableFrom(field.getType())) { // If it's a category then init the stuff inside it and put it in the category list
|
||||
initNestedClass((entry.getType()), ((ConfigCategory) entry).getDestination());
|
||||
if (((ConfigCategory) entry).getDestination() == null)
|
||||
((ConfigCategory) entry).destination = ((ConfigCategory) entry).getNameWCategory();
|
||||
if (entry.get() != null) {
|
||||
initNestedClass(((ConfigCategory) entry).get(), ((ConfigCategory) entry).getDestination());
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private static boolean isAcceptableType(Class<?> Class) {
|
||||
if (Class.isEnum())
|
||||
private static boolean isAcceptableType(Class<?> Clazz) {
|
||||
if (Clazz.isEnum())
|
||||
return true;
|
||||
for(Class<?> i: acceptableInputs) {
|
||||
if(Class == i)
|
||||
if(Clazz == i)
|
||||
return true;
|
||||
}
|
||||
return false;
|
||||
|
||||
@@ -23,29 +23,32 @@ import java.util.HashMap;
|
||||
*/
|
||||
public class ConfigFileHandling {
|
||||
public static final Path ConfigPath = SingletonHandler.get(IMinecraftWrapper.class).getGameDirectory().toPath().resolve("config").resolve(ModInfo.NAME+".toml");
|
||||
public static final CommentedFileConfig config = CommentedFileConfig.builder(ConfigPath.toFile()).autosave().build();
|
||||
|
||||
/** Saves the config to the file */
|
||||
public static void saveToFile() {
|
||||
if (!Files.exists(ConfigPath))
|
||||
CommentedFileConfig config = CommentedFileConfig.builder(ConfigPath.toFile()).build();
|
||||
if (!Files.exists(ConfigPath)) // Try to check if the config exists
|
||||
try {
|
||||
Files.createFile(ConfigPath);
|
||||
} catch (IOException ex) {
|
||||
ex.printStackTrace();
|
||||
}
|
||||
|
||||
loadConfig(config);
|
||||
|
||||
for (AbstractConfigType<?> entry : ConfigBase.entries) {
|
||||
for (AbstractConfigType<?, ?> entry : ConfigBase.entries) {
|
||||
if (ConfigEntry.class.isAssignableFrom(entry.getClass())) {
|
||||
createComment((ConfigEntry<?>) entry, config);
|
||||
saveEntry((ConfigEntry<?>) entry, config);
|
||||
}
|
||||
}
|
||||
|
||||
config.save();
|
||||
config.close();
|
||||
}
|
||||
/** Loads the config from the file */
|
||||
public static void loadFromFile() {
|
||||
CommentedFileConfig config = CommentedFileConfig.builder(ConfigPath.toFile()).build();
|
||||
// Attempt to load the file and if it fails then save config to file
|
||||
try {
|
||||
if (Files.exists(ConfigPath))
|
||||
@@ -55,25 +58,32 @@ public class ConfigFileHandling {
|
||||
return;
|
||||
}
|
||||
} catch (Exception e) {
|
||||
e.printStackTrace();
|
||||
saveToFile();
|
||||
return;
|
||||
}
|
||||
|
||||
// Load all the entries
|
||||
for (AbstractConfigType<?> entry : ConfigBase.entries) {
|
||||
for (AbstractConfigType<?, ?> entry : ConfigBase.entries) {
|
||||
if (ConfigEntry.class.isAssignableFrom(entry.getClass())) {
|
||||
createComment((ConfigEntry<?>) entry, config);
|
||||
loadEntry((ConfigEntry<?>) entry, config);
|
||||
}
|
||||
}
|
||||
|
||||
config.save();
|
||||
config.close();
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
// Save an entry when only given the entry
|
||||
public static void saveEntry(ConfigEntry<?> entry) {
|
||||
CommentedFileConfig config = CommentedFileConfig.builder(ConfigPath.toFile()).build();
|
||||
loadConfig(config);
|
||||
saveEntry(entry, config);
|
||||
config.save();
|
||||
config.close();
|
||||
}
|
||||
// Save an entry
|
||||
@@ -90,6 +100,7 @@ public class ConfigFileHandling {
|
||||
|
||||
// Loads an entry when only given the entry
|
||||
public static void loadEntry(ConfigEntry<?> entry) {
|
||||
CommentedFileConfig config = CommentedFileConfig.builder(ConfigPath.toFile()).autosave().build();
|
||||
loadConfig(config);
|
||||
loadEntry(entry, config);
|
||||
config.close();
|
||||
@@ -112,7 +123,6 @@ public class ConfigFileHandling {
|
||||
} else {
|
||||
entry.setWTSave(workConfig.get(entry.getNameWCategory()));
|
||||
}
|
||||
// entry.setWTSave(workConfig.get(entry.getNameWCategory()));
|
||||
} else {
|
||||
saveEntry(entry, workConfig);
|
||||
}
|
||||
@@ -120,21 +130,28 @@ public class ConfigFileHandling {
|
||||
|
||||
// Creates the comment for an entry when only given the entry
|
||||
public static void createComment(ConfigEntry<?> entry) {
|
||||
CommentedFileConfig config = CommentedFileConfig.builder(ConfigPath.toFile()).autosave().build();
|
||||
loadConfig(config);
|
||||
createComment(entry, config);
|
||||
config.close();
|
||||
}
|
||||
// Creates a comment for an entry
|
||||
public static void createComment(ConfigEntry<?> entry, CommentedFileConfig workConfig) {
|
||||
if (!entry.getAppearance().showInFile)
|
||||
return;
|
||||
workConfig.setComment(entry.getNameWCategory(), entry.getComment());
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
/** Does config.load(); but with error checking */
|
||||
public static void loadConfig(CommentedFileConfig config) {
|
||||
try {
|
||||
config.load();
|
||||
} catch (Exception e) {
|
||||
System.out.println("Loading file failed because of this expectation:\n"+e);
|
||||
try { // Now try remaking the file
|
||||
try { // Now try remaking the file and loading it
|
||||
Files.deleteIfExists(ConfigPath);
|
||||
Files.createFile(ConfigPath);
|
||||
config.load();
|
||||
@@ -146,9 +163,6 @@ public class ConfigFileHandling {
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
// Stuff for converting HashMap's and String's (uses json)
|
||||
public static String getStringFromHashMap(HashMap<String, ?> item) {
|
||||
JSONObject jsonObject = new JSONObject();
|
||||
|
||||
@@ -7,7 +7,7 @@ import com.seibel.lod.core.config.ConfigEntryAppearance;
|
||||
*
|
||||
* @author coolGi2007
|
||||
*/
|
||||
public abstract class AbstractConfigType<T> {
|
||||
public abstract class AbstractConfigType<T, S> { // The S is the class that is extending this
|
||||
public String category = ""; // This should only be set once in the init
|
||||
public String name; // This should only be set once in the init
|
||||
protected T value;
|
||||
@@ -55,18 +55,20 @@ public abstract class AbstractConfigType<T> {
|
||||
public Class<?> getType() {
|
||||
return value.getClass();
|
||||
}
|
||||
protected static abstract class Builder<T> {
|
||||
|
||||
protected static abstract class Builder<T, S> {
|
||||
protected ConfigEntryAppearance tmpAppearance = ConfigEntryAppearance.ALL;
|
||||
protected T tmpValue;
|
||||
|
||||
public this setAppearance(ConfigEntryAppearance newAppearance) {
|
||||
this.tmpAppearance = newAppearance;
|
||||
return this;
|
||||
}
|
||||
|
||||
public this set(T newValue) {
|
||||
// Put this into your own builder
|
||||
public S setAppearance(ConfigEntryAppearance newAppearance) {
|
||||
this.tmpAppearance = newAppearance;
|
||||
return (S) this;
|
||||
}
|
||||
public S set(T newValue) {
|
||||
this.tmpValue = newValue;
|
||||
return this;
|
||||
return (S) this;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,32 +1,34 @@
|
||||
package com.seibel.lod.core.config.types;
|
||||
|
||||
import com.seibel.lod.core.api.ClientApi;
|
||||
import com.seibel.lod.core.config.ConfigEntryAppearance;
|
||||
|
||||
public class ConfigCategory<T> extends AbstractConfigType<T> {
|
||||
private final String destination; // Where the category goes to
|
||||
public class ConfigCategory extends AbstractConfigType<Class, ConfigCategory> {
|
||||
public String destination; // Where the category goes to
|
||||
|
||||
public ConfigCategory(ConfigEntryAppearance appearance, T value, String destination) {
|
||||
public ConfigCategory(ConfigEntryAppearance appearance, Class value, String destination) {
|
||||
super(appearance, value);
|
||||
if (destination == null) {
|
||||
this.destination = getNameWCategory();
|
||||
} else {
|
||||
this.destination = destination;
|
||||
}
|
||||
this.destination = destination;
|
||||
}
|
||||
|
||||
public String getDestination() {
|
||||
return this.destination;
|
||||
}
|
||||
|
||||
public static class Builder<T> extends AbstractConfigType.Builder<T> {
|
||||
public static class Builder extends AbstractConfigType.Builder<Class, Builder> {
|
||||
private String tmpDestination = null;
|
||||
|
||||
public Builder<T> setDestination(String newDestination) {
|
||||
public Builder setDestination(String newDestination) {
|
||||
this.tmpDestination = newDestination;
|
||||
return this;
|
||||
}
|
||||
|
||||
public ConfigCategory<T> build() {
|
||||
public Builder setAppearance(ConfigEntryAppearance newAppearance) {
|
||||
this.tmpAppearance = newAppearance;
|
||||
return this;
|
||||
}
|
||||
|
||||
public ConfigCategory build() {
|
||||
return new ConfigCategory(tmpAppearance, tmpValue, tmpDestination);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -8,7 +8,7 @@ import com.seibel.lod.core.config.file.ConfigFileHandling;
|
||||
*
|
||||
* @author coolGi2007
|
||||
*/
|
||||
public class ConfigEntry<T> extends AbstractConfigType<T> {
|
||||
public class ConfigEntry<T> extends AbstractConfigType<T, ConfigEntry> {
|
||||
|
||||
private T defaultValue;
|
||||
private String comment;
|
||||
@@ -113,7 +113,7 @@ public class ConfigEntry<T> extends AbstractConfigType<T> {
|
||||
}
|
||||
}
|
||||
|
||||
public static class Builder<T> extends AbstractConfigType.Builder<T> {
|
||||
public static class Builder<T> extends AbstractConfigType.Builder<T, Builder> {
|
||||
private String tmpComment;
|
||||
private T tmpMin;
|
||||
private T tmpMax;
|
||||
|
||||
Reference in New Issue
Block a user