From 17e135f3d3af11f4f552ede2117f82bbcf4db3b1 Mon Sep 17 00:00:00 2001 From: janeptrv Date: Sat, 10 Oct 2020 18:57:22 -0400 Subject: [PATCH] refactor config files so that module configs are separate --- .../java/pm/j4/petroleum/PetroleumMod.java | 6 +- .../pm/j4/petroleum/gui/PModMenuScreen.java | 2 +- .../j4/petroleum/gui/PModuleConfigEntry.java | 4 +- .../pm/j4/petroleum/gui/PMovableButton.java | 12 +-- .../pm/j4/petroleum/gui/POptionsScreen.java | 3 +- .../j4/petroleum/modules/ExampleModule.java | 9 +- .../modules/bindings/BindingManager.java | 14 ++- .../pm/j4/petroleum/modules/xray/Xray.java | 13 +++ .../petroleum/util/config/ConfigHolder.java | 4 + .../petroleum/util/config/ConfigManager.java | 102 +++++++++++------- .../petroleum/util/config/GlobalConfig.java | 32 +++--- .../j4/petroleum/util/data/ModuleConfig.java | 7 ++ .../{config => data}/OptionSerializiable.java | 15 +-- .../util/module/ConfigurationOption.java | 68 ------------ .../j4/petroleum/util/module/ModuleBase.java | 23 ++-- .../petroleum/util/module/StringWritable.java | 48 --------- .../util/module/option/BooleanOption.java | 29 +++++ .../util/module/option/BooleanValue.java | 41 ------- .../module/option/ConfigurationOption.java | 27 +++++ .../util/module/option/DummyValue.java | 9 -- .../util/module/option/IntegerOption.java | 29 +++++ .../util/module/option/IntegerValue.java | 46 -------- .../util/module/option/KeybindOption.java | 42 ++++++++ .../util/module/option/KeybindValue.java | 39 ------- .../util/module/option/ListOption.java | 24 +++++ .../util/module/option/OptionTypeMatcher.java | 29 ----- .../util/module/option/StringOption.java | 30 ++++++ .../util/module/option/StringValue.java | 32 ------ src/main/resources/fabric.mod.json | 2 +- 29 files changed, 333 insertions(+), 408 deletions(-) create mode 100644 src/main/java/pm/j4/petroleum/modules/xray/Xray.java create mode 100644 src/main/java/pm/j4/petroleum/util/data/ModuleConfig.java rename src/main/java/pm/j4/petroleum/util/{config => data}/OptionSerializiable.java (57%) delete mode 100644 src/main/java/pm/j4/petroleum/util/module/ConfigurationOption.java delete mode 100644 src/main/java/pm/j4/petroleum/util/module/StringWritable.java create mode 100644 src/main/java/pm/j4/petroleum/util/module/option/BooleanOption.java delete mode 100644 src/main/java/pm/j4/petroleum/util/module/option/BooleanValue.java create mode 100644 src/main/java/pm/j4/petroleum/util/module/option/ConfigurationOption.java delete mode 100644 src/main/java/pm/j4/petroleum/util/module/option/DummyValue.java create mode 100644 src/main/java/pm/j4/petroleum/util/module/option/IntegerOption.java delete mode 100644 src/main/java/pm/j4/petroleum/util/module/option/IntegerValue.java create mode 100644 src/main/java/pm/j4/petroleum/util/module/option/KeybindOption.java delete mode 100644 src/main/java/pm/j4/petroleum/util/module/option/KeybindValue.java create mode 100644 src/main/java/pm/j4/petroleum/util/module/option/ListOption.java delete mode 100644 src/main/java/pm/j4/petroleum/util/module/option/OptionTypeMatcher.java create mode 100644 src/main/java/pm/j4/petroleum/util/module/option/StringOption.java delete mode 100644 src/main/java/pm/j4/petroleum/util/module/option/StringValue.java diff --git a/src/main/java/pm/j4/petroleum/PetroleumMod.java b/src/main/java/pm/j4/petroleum/PetroleumMod.java index 5f7f785..bba2464 100644 --- a/src/main/java/pm/j4/petroleum/PetroleumMod.java +++ b/src/main/java/pm/j4/petroleum/PetroleumMod.java @@ -126,15 +126,19 @@ public class PetroleumMod implements ModInitializer { public static String getServerAddress() { if (client != null && client.getServer() != null) { IntegratedServer server = client.getServer(); + if (!server.isRemote()) { + return "localhost"; + } if (server.isRemote() && !server.getServerIp().isEmpty()) { return server.getServerIp(); } } return null; } - + @Override public void onInitialize() { + ConfigManager.initConfig(); // always update mod data diff --git a/src/main/java/pm/j4/petroleum/gui/PModMenuScreen.java b/src/main/java/pm/j4/petroleum/gui/PModMenuScreen.java index 9f477cd..e9eac22 100644 --- a/src/main/java/pm/j4/petroleum/gui/PModMenuScreen.java +++ b/src/main/java/pm/j4/petroleum/gui/PModMenuScreen.java @@ -50,7 +50,7 @@ public class PModMenuScreen extends Screen { if (ConfigManager.getConfig().isPresent()) { ConfigManager.getConfig().get().disableModule("petroleum.modmenu"); this.buttons.forEach(button -> ((PMovableButton) button).updateCoordinate()); - ConfigManager.save(); + ConfigManager.saveGlobalConfig(); } super.onClose(); } diff --git a/src/main/java/pm/j4/petroleum/gui/PModuleConfigEntry.java b/src/main/java/pm/j4/petroleum/gui/PModuleConfigEntry.java index 58dc4f3..7a16310 100644 --- a/src/main/java/pm/j4/petroleum/gui/PModuleConfigEntry.java +++ b/src/main/java/pm/j4/petroleum/gui/PModuleConfigEntry.java @@ -5,7 +5,7 @@ import net.minecraft.client.gui.widget.EntryListWidget; import net.minecraft.client.util.math.MatrixStack; import net.minecraft.text.LiteralText; import net.minecraft.text.Text; -import pm.j4.petroleum.util.module.ConfigurationOption; +import pm.j4.petroleum.util.module.option.ConfigurationOption; /** * The type P module config entry. @@ -41,7 +41,7 @@ public class PModuleConfigEntry extends EntryListWidget.Entry mx && storedX - padding < mx && storedY + padding > my && storedY - padding < my) { this.expanded = !this.expanded; @@ -142,10 +142,8 @@ public class PMovableButton extends AbstractButtonWidget { } } - return false; - } else { - return false; } + return false; } /** diff --git a/src/main/java/pm/j4/petroleum/gui/POptionsScreen.java b/src/main/java/pm/j4/petroleum/gui/POptionsScreen.java index e20db0b..f1f91a5 100644 --- a/src/main/java/pm/j4/petroleum/gui/POptionsScreen.java +++ b/src/main/java/pm/j4/petroleum/gui/POptionsScreen.java @@ -109,7 +109,8 @@ public class POptionsScreen extends Screen { } configurableModules.forEach(module -> this.modules.addEntry(new POptionEntry(module, this.modules))); this.addButton(new ButtonWidget(this.width / 2 - 75, this.height - 30, 150, 20, ScreenTexts.DONE, (buttonWidget) -> { - ConfigManager.save(); + //TODO see PModMenuScreen + ConfigManager.saveAllModules(); assert this.client != null; this.client.openScreen(this.previousScreen); })); diff --git a/src/main/java/pm/j4/petroleum/modules/ExampleModule.java b/src/main/java/pm/j4/petroleum/modules/ExampleModule.java index ead9f11..0bb285a 100644 --- a/src/main/java/pm/j4/petroleum/modules/ExampleModule.java +++ b/src/main/java/pm/j4/petroleum/modules/ExampleModule.java @@ -3,9 +3,9 @@ package pm.j4.petroleum.modules; import java.util.HashMap; import java.util.Map; import net.minecraft.client.MinecraftClient; -import pm.j4.petroleum.util.module.ConfigurationOption; import pm.j4.petroleum.util.module.ModuleBase; -import pm.j4.petroleum.util.module.option.BooleanValue; +import pm.j4.petroleum.util.module.option.BooleanOption; +import pm.j4.petroleum.util.module.option.ConfigurationOption; /** * The type Example module. @@ -25,9 +25,8 @@ public class ExampleModule extends ModuleBase { @Override protected Map getDefaultConfig() { Map options = new HashMap<>(); - ConfigurationOption option = new ConfigurationOption<>(new BooleanValue(false)); - options.put("petroleum.example_b_one", option); - options.put("petroleum.example_b_two", option); + options.put("petroleum.example_b_one", new BooleanOption("example")); + options.put("petroleum.example_b_two", new BooleanOption("example")); return options; } diff --git a/src/main/java/pm/j4/petroleum/modules/bindings/BindingManager.java b/src/main/java/pm/j4/petroleum/modules/bindings/BindingManager.java index 775b1ed..bcd25eb 100644 --- a/src/main/java/pm/j4/petroleum/modules/bindings/BindingManager.java +++ b/src/main/java/pm/j4/petroleum/modules/bindings/BindingManager.java @@ -11,9 +11,8 @@ import pm.j4.petroleum.PetroleumMod; import pm.j4.petroleum.gui.PModuleConfigEntry; import pm.j4.petroleum.util.config.ConfigManager; import pm.j4.petroleum.util.config.GlobalConfig; -import pm.j4.petroleum.util.module.ConfigurationOption; import pm.j4.petroleum.util.module.ModuleBase; -import pm.j4.petroleum.util.module.option.KeybindValue; +import pm.j4.petroleum.util.module.option.KeybindOption; /** * The type Binding manager. @@ -41,10 +40,15 @@ public class BindingManager extends ModuleBase { public List getConfigEntries() { List entries = new ArrayList<>(); - Map, ModuleBase> mapped = new HashMap<>(); + Map mapped = new HashMap<>(); if (ConfigManager.getConfig().isPresent()) { Map binds = ConfigManager.getConfig().get().globalConfig.bindings; - binds.forEach((key, func) -> mapped.put(new ConfigurationOption<>(new KeybindValue(key)), func)); + + binds.forEach((key, func) -> { + KeybindOption o = new KeybindOption(func.getModuleName() + " " + func.getCategory()); + o.fromKeybind(key, func); + mapped.put(o, func); + }); } mapped.forEach((bind, module) -> { PModuleConfigEntry entry = new PModuleConfigEntry(bind, new TranslatableText(module.getModuleName())) { @@ -57,7 +61,7 @@ public class BindingManager extends ModuleBase { } if (this.option != null) { int fontHeight = MinecraftClient.getInstance().textRenderer.fontHeight; - MinecraftClient.getInstance().textRenderer.drawWithShadow(matrices, "Key Value: " + this.option.toStringValue(), x, y + fontHeight + 4, 0xFFFFFF); + MinecraftClient.getInstance().textRenderer.drawWithShadow(matrices, "Key Value: " + this.option.getStringValue(), x, y + fontHeight + 4, 0xFFFFFF); } } }; diff --git a/src/main/java/pm/j4/petroleum/modules/xray/Xray.java b/src/main/java/pm/j4/petroleum/modules/xray/Xray.java new file mode 100644 index 0000000..ff4b961 --- /dev/null +++ b/src/main/java/pm/j4/petroleum/modules/xray/Xray.java @@ -0,0 +1,13 @@ +package pm.j4.petroleum.modules.xray; + +import pm.j4.petroleum.util.module.ModuleBase; + +public class Xray extends ModuleBase { + public Xray() { + super("petroleum.xray", + "petroleum.render", + true, + false, + true); + } +} diff --git a/src/main/java/pm/j4/petroleum/util/config/ConfigHolder.java b/src/main/java/pm/j4/petroleum/util/config/ConfigHolder.java index a59fcc4..4d6d6e8 100644 --- a/src/main/java/pm/j4/petroleum/util/config/ConfigHolder.java +++ b/src/main/java/pm/j4/petroleum/util/config/ConfigHolder.java @@ -90,4 +90,8 @@ public class ConfigHolder { globalConfig.disableModule(module); } } + + public static void saveModule(ModuleBase module) { + + } } diff --git a/src/main/java/pm/j4/petroleum/util/config/ConfigManager.java b/src/main/java/pm/j4/petroleum/util/config/ConfigManager.java index e83b5e4..a9e2e5a 100644 --- a/src/main/java/pm/j4/petroleum/util/config/ConfigManager.java +++ b/src/main/java/pm/j4/petroleum/util/config/ConfigManager.java @@ -10,6 +10,9 @@ import pm.j4.petroleum.PetroleumMod; import pm.j4.petroleum.modules.bindings.BindingInfo; import pm.j4.petroleum.modules.menu.ModMenu; import pm.j4.petroleum.util.data.ButtonInformation; +import pm.j4.petroleum.util.data.ModuleConfig; +import pm.j4.petroleum.util.data.OptionSerializiable; +import pm.j4.petroleum.util.module.ModuleBase; /** * The type Config manager. @@ -19,13 +22,9 @@ public class ConfigManager { * The constant GSON. */ public static final Gson GSON = new GsonBuilder() - .registerTypeAdapter(GlobalConfig.class, SerializationHelper.getSerializer()) - .registerTypeAdapter(GlobalConfig.class, SerializationHelper.getDeserializer()) + .registerTypeAdapter(GlobalConfig.class, SerializationHelper.getGlobalSerializer()) + .registerTypeAdapter(GlobalConfig.class, SerializationHelper.getGlobalDeserializer()) .setFieldNamingPolicy(FieldNamingPolicy.LOWER_CASE_WITH_UNDERSCORES).setPrettyPrinting().create(); - /** - * The constant file. - */ - private static File file; /** * The constant config. */ @@ -34,11 +33,12 @@ public class ConfigManager { /** * Prepare config file. */ - private static void prepareConfigFile() { - if (file != null) { - return; + private static File prepareConfigFile(String path, String filename) { + if(path != "") { + File directory = new File(FabricLoader.getInstance().getConfigDir().toString(), path); + if (!directory.exists()) directory.mkdir(); } - file = new File(FabricLoader.getInstance().getConfigDir().toString(), "petroleum.json"); + return new File(FabricLoader.getInstance().getConfigDir().toString(), path + filename); } /** @@ -53,54 +53,90 @@ public class ConfigManager { config.globalConfig = new DefaultConfig(); config.serverConfigs = new HashMap<>(); - load(); + config = load("", "petroleum.json", ConfigHolder.class); + initModules(); + } + + public static void initModules() { + PetroleumMod.getActiveMods().forEach(module -> { + ModuleConfig options = load("modules/", module.getModuleName() + ".json", ModuleConfig.class); + if(options != null && options.options != null) { + options.options.forEach((key, option) -> { + if (module.hasOption(option.key)) { + module.setConfigOption(option.key, option.value); + } + }); + } + }); } /** * Load. */ - private static void load() { - prepareConfigFile(); + private static T load(String path, String filename, Class tClass) { + File file = prepareConfigFile(path, filename); try { if (!file.exists()) { - save(); + save(path, filename, tClass.newInstance()); } if (file.exists()) { BufferedReader reader = new BufferedReader(new FileReader(file)); - ConfigHolder parsedConfig = null; + T parsedConfig = null; try { - parsedConfig = GSON.fromJson(reader, ConfigHolder.class); + parsedConfig = GSON.fromJson(reader, tClass); } catch (Exception e) { System.out.println("Couldn't parse config file"); e.printStackTrace(); } if (parsedConfig != null) { - config = parsedConfig; + return parsedConfig; } } - } catch (FileNotFoundException e) { - System.out.println("Couldn't load Petroleum configuration"); + } catch (FileNotFoundException | InstantiationException | IllegalAccessException e) { + System.out.println("Couldn't load configuration file at " + path); e.printStackTrace(); } + return null; + } + + public static T deserializeElement(JsonElement element, Class tClass) { + return GSON.fromJson(element, tClass); } /** * Save. */ - public static void save() { - prepareConfigFile(); - - String json = GSON.toJson(config); + private static void save(String path, String filename, T data) { + File file = prepareConfigFile(path, filename); + String json = GSON.toJson(data); try (FileWriter fileWriter = new FileWriter(file)) { + System.out.println("FILE WRITE ATTEMPT"); + System.out.println(path); + System.out.println(json); fileWriter.write(json); } catch (IOException e) { - System.out.println("Couldn't save Petroleum configuration"); + System.out.println("Couldn't save configuration file at " + path); e.printStackTrace(); } } + public static void saveModule(ModuleBase b) { + ModuleConfig c = new ModuleConfig(); + c.options = GlobalConfig.serializeModuleConfiguration(b); + save("modules/", b.getModuleName() + ".json", c); + } + + public static void saveAllModules() { + List mods = PetroleumMod.getActiveMods(); + mods.forEach(ConfigManager::saveModule); + } + + public static void saveGlobalConfig() { + save("","petroleum.json", config); + } + /** * Gets config. * @@ -122,7 +158,7 @@ class SerializationHelper { /** * The constant s. */ - private static final JsonSerializer s = (src, typeOfSrc, ctx) -> { + private static final JsonSerializer GLOBAL_CONFIG_JSON_SERIALIZER = (src, typeOfSrc, ctx) -> { JsonObject jsonConfig = new JsonObject(); JsonArray bindings = ctx.serialize(src.serializeBindings()).getAsJsonArray(); @@ -131,12 +167,6 @@ class SerializationHelper { JsonArray modules = ctx.serialize(src.enabledModules).getAsJsonArray(); jsonConfig.add("enabled_modules", modules); - JsonObject moduleConfigs = new JsonObject(); - PetroleumMod.getActiveMods().forEach(module -> { - moduleConfigs.add(module.getModuleName(), ctx.serialize(src.serializeModuleConfiguration(module))); - }); - jsonConfig.add("module_configuration", moduleConfigs); - JsonObject tabCoordinates = new JsonObject(); ModMenu.getButtons().forEach((category, coordinates) -> { tabCoordinates.add(category, ctx.serialize(coordinates)); @@ -149,7 +179,7 @@ class SerializationHelper { /** * The constant ds. */ - private static final JsonDeserializer ds = ((json, typeOfT, ctx) -> { + private static final JsonDeserializer GLOBAL_CONFIG_JSON_DESERIALIZER = ((json, typeOfT, ctx) -> { JsonObject obj = json.getAsJsonObject(); List bindings = new ArrayList<>(); @@ -191,8 +221,8 @@ class SerializationHelper { * * @return the serializer */ - public static JsonSerializer getSerializer() { - return s; + public static JsonSerializer getGlobalSerializer() { + return GLOBAL_CONFIG_JSON_SERIALIZER; } /** @@ -200,7 +230,7 @@ class SerializationHelper { * * @return the deserializer */ - public static JsonDeserializer getDeserializer() { - return ds; + public static JsonDeserializer getGlobalDeserializer() { + return GLOBAL_CONFIG_JSON_DESERIALIZER; } } \ No newline at end of file diff --git a/src/main/java/pm/j4/petroleum/util/config/GlobalConfig.java b/src/main/java/pm/j4/petroleum/util/config/GlobalConfig.java index ea4896f..ffa62c7 100644 --- a/src/main/java/pm/j4/petroleum/util/config/GlobalConfig.java +++ b/src/main/java/pm/j4/petroleum/util/config/GlobalConfig.java @@ -7,9 +7,9 @@ import net.minecraft.client.util.InputUtil; import pm.j4.petroleum.PetroleumMod; import pm.j4.petroleum.modules.bindings.BindingInfo; import pm.j4.petroleum.util.data.ButtonInformation; -import pm.j4.petroleum.util.module.ConfigurationOption; +import pm.j4.petroleum.util.data.OptionSerializiable; import pm.j4.petroleum.util.module.ModuleBase; -import pm.j4.petroleum.util.module.option.OptionTypeMatcher; +import pm.j4.petroleum.util.module.option.ConfigurationOption; /** * The type Global config. @@ -65,15 +65,19 @@ public class GlobalConfig extends Config { */ private void convertBinding(BindingInfo info) { Optional match = PetroleumMod.getMod(info.attachedModuleName); - match.ifPresent(moduleBase -> setBinding(new KeyBinding( - info.translationKey, - info.type, - info.key, - info.category - ), + match.ifPresent(moduleBase -> setBinding(reconstructBinding(info), moduleBase)); } + public static KeyBinding reconstructBinding(BindingInfo info) { + return new KeyBinding( + info.translationKey, + info.type, + info.key, + info.category + ); + } + /** * Extract binding binding info. * @@ -81,7 +85,7 @@ public class GlobalConfig extends Config { * @param f the f * @return the binding info */ - private BindingInfo extractBinding(KeyBinding b, ModuleBase f) { + public static BindingInfo extractBinding(KeyBinding b, ModuleBase f) { BindingInfo res = new BindingInfo(); res.attachedModuleName = f.getModuleName(); @@ -120,11 +124,11 @@ public class GlobalConfig extends Config { * @param module the module * @return the list */ - public List serializeModuleConfiguration(ModuleBase module) { - List opts = new ArrayList<>(); + public static Map serializeModuleConfiguration(ModuleBase module) { + Map opts = new HashMap<>(); Map configuration = module.getModuleConfiguration(); configuration.forEach((key, value) -> { - opts.add(new OptionSerializiable(value.getValueType(), value.toStringValue(), key)); + opts.put(key, new OptionSerializiable(key, value.toJson())); }); return opts; } @@ -137,7 +141,9 @@ public class GlobalConfig extends Config { */ public void deserializeModuleConfiguration(List opts, ModuleBase module) { opts.forEach(option -> { - module.setConfigOption(option.key, new ConfigurationOption<>(OptionTypeMatcher.match(option.type, option.value))); + if (module.hasOption(option.key)) { + module.setConfigOption(option.key, option.value); + } }); } diff --git a/src/main/java/pm/j4/petroleum/util/data/ModuleConfig.java b/src/main/java/pm/j4/petroleum/util/data/ModuleConfig.java new file mode 100644 index 0000000..c96ead0 --- /dev/null +++ b/src/main/java/pm/j4/petroleum/util/data/ModuleConfig.java @@ -0,0 +1,7 @@ +package pm.j4.petroleum.util.data; + +import java.util.Map; + +public class ModuleConfig { + public Map options; +} diff --git a/src/main/java/pm/j4/petroleum/util/config/OptionSerializiable.java b/src/main/java/pm/j4/petroleum/util/data/OptionSerializiable.java similarity index 57% rename from src/main/java/pm/j4/petroleum/util/config/OptionSerializiable.java rename to src/main/java/pm/j4/petroleum/util/data/OptionSerializiable.java index 0ef5c37..c57f57e 100644 --- a/src/main/java/pm/j4/petroleum/util/config/OptionSerializiable.java +++ b/src/main/java/pm/j4/petroleum/util/data/OptionSerializiable.java @@ -1,4 +1,6 @@ -package pm.j4.petroleum.util.config; +package pm.j4.petroleum.util.data; + +import com.google.gson.JsonElement; /** * The type Option serializiable. @@ -7,24 +9,17 @@ public class OptionSerializiable { /** * Instantiates a new Option serializiable. * - * @param type the type * @param value the value * @param key the key */ - public OptionSerializiable(String type, String value, String key) { - this.type = type; + public OptionSerializiable(String key, JsonElement value) { this.value = value; this.key = key; } - - /** - * The Type. - */ - public final String type; /** * The Value. */ - public final String value; + public final JsonElement value; /** * The Key. */ diff --git a/src/main/java/pm/j4/petroleum/util/module/ConfigurationOption.java b/src/main/java/pm/j4/petroleum/util/module/ConfigurationOption.java deleted file mode 100644 index f103d62..0000000 --- a/src/main/java/pm/j4/petroleum/util/module/ConfigurationOption.java +++ /dev/null @@ -1,68 +0,0 @@ -package pm.j4.petroleum.util.module; - -/** - * The type Configuration option. - * - * @param the type parameter - */ -public class ConfigurationOption { - - /** - * The Value. - */ - private T value; - - /** - * Instantiates a new Configuration option. - * - * @param initialValue the initial value - */ - public ConfigurationOption(T initialValue) { - this.value = initialValue; - } - - /** - * Sets value. - * - * @param value the value - */ - public void setValue(T value) { - this.value = value; - } - - /** - * Gets value. - * - * @return the value - */ - public T getValue() { - return this.value; - } - - /** - * To string value string. - * - * @return the string - */ - public String toStringValue() { - return this.value.getStringValue(); - } - - /** - * Sets from string value. - * - * @param value the value - */ - public void setFromStringValue(String value) { - this.value.toStringValue(value); - } - - /** - * Gets value type. - * - * @return the value type - */ - public String getValueType() { - return this.value.getType(); - } -} diff --git a/src/main/java/pm/j4/petroleum/util/module/ModuleBase.java b/src/main/java/pm/j4/petroleum/util/module/ModuleBase.java index 7b95e68..6869f60 100644 --- a/src/main/java/pm/j4/petroleum/util/module/ModuleBase.java +++ b/src/main/java/pm/j4/petroleum/util/module/ModuleBase.java @@ -1,11 +1,13 @@ package pm.j4.petroleum.util.module; +import com.google.gson.JsonElement; import java.util.*; import net.minecraft.client.MinecraftClient; import net.minecraft.text.TranslatableText; import pm.j4.petroleum.gui.PModuleConfigEntry; import pm.j4.petroleum.util.config.ConfigHolder; import pm.j4.petroleum.util.config.ConfigManager; +import pm.j4.petroleum.util.module.option.ConfigurationOption; /** * The Basis for all mods, used so that modules all have a common activation point and settings. @@ -35,6 +37,7 @@ public abstract class ModuleBase { * Init. */ public void init() { + } /** @@ -154,14 +157,18 @@ public abstract class ModuleBase { * @param value the value * @return whether the operation was successful. */ - public boolean setConfigOption(String key, ConfigurationOption value) { + public boolean setConfigOption(String key, JsonElement value) { if (moduleOptions.containsKey(key)) { - moduleOptions.replace(key, value); + moduleOptions.get(key).fromJson(value); return true; } return false; } + public boolean hasOption(String key) { + return moduleOptions.containsKey(key); + } + /** * Gets default config. * @@ -171,18 +178,6 @@ public abstract class ModuleBase { return new HashMap<>(); } - /** - * Add config option. - * - * @param key the key - * @param value the value - */ - protected void addConfigOption(String key, ConfigurationOption value) { - if (!this.setConfigOption(key, value)) { - moduleOptions.put(key, value); - } - } - /** * Gets config option. * diff --git a/src/main/java/pm/j4/petroleum/util/module/StringWritable.java b/src/main/java/pm/j4/petroleum/util/module/StringWritable.java deleted file mode 100644 index 4a63652..0000000 --- a/src/main/java/pm/j4/petroleum/util/module/StringWritable.java +++ /dev/null @@ -1,48 +0,0 @@ -package pm.j4.petroleum.util.module; - -/** - * The type String writable. - * - * @param the type parameter - */ -public class StringWritable> { - /** - * The Value. - */ - public T value; - - /** - * Gets type. - * - * @return the type - */ - public String getType() { - return ""; - } - - /** - * Gets value. - * - * @return the value - */ - public T getValue() { - return this.value; - } - - /** - * Gets string value. - * - * @return the string value - */ - public String getStringValue() { - return ""; - } - - /** - * To string value. - * - * @param value the value - */ - public void toStringValue(String value) { - } -} diff --git a/src/main/java/pm/j4/petroleum/util/module/option/BooleanOption.java b/src/main/java/pm/j4/petroleum/util/module/option/BooleanOption.java new file mode 100644 index 0000000..8b797dd --- /dev/null +++ b/src/main/java/pm/j4/petroleum/util/module/option/BooleanOption.java @@ -0,0 +1,29 @@ +package pm.j4.petroleum.util.module.option; + +import com.google.gson.JsonElement; +import com.google.gson.JsonPrimitive; + +/** + * The type Boolean value. + */ +public class BooleanOption extends ConfigurationOption { + private boolean value; + public BooleanOption(String description) { + super(description); + } + + @Override + public String getStringValue() { + return Boolean.toString(value); + } + + @Override + public void fromJson(JsonElement e) { + this.value = e.getAsBoolean(); + } + + @Override + public JsonElement toJson() { + return new JsonPrimitive(value); + } +} diff --git a/src/main/java/pm/j4/petroleum/util/module/option/BooleanValue.java b/src/main/java/pm/j4/petroleum/util/module/option/BooleanValue.java deleted file mode 100644 index 1bec235..0000000 --- a/src/main/java/pm/j4/petroleum/util/module/option/BooleanValue.java +++ /dev/null @@ -1,41 +0,0 @@ -package pm.j4.petroleum.util.module.option; - -import pm.j4.petroleum.util.module.StringWritable; - -/** - * The type Boolean value. - */ -public class BooleanValue extends StringWritable { - /** - * Instantiates a new Boolean value. - * - * @param value the value - */ - public BooleanValue(String value) { - this.value = Boolean.valueOf(value); - } - - /** - * Instantiates a new Boolean value. - * - * @param value the value - */ - public BooleanValue(boolean value) { - this.value = Boolean.valueOf(value); - } - - @Override - public String getType() { - return "B"; - } - - @Override - public String getStringValue() { - return value.toString(); - } - - @Override - public void toStringValue(String value) { - this.value = Boolean.valueOf(value); - } -} diff --git a/src/main/java/pm/j4/petroleum/util/module/option/ConfigurationOption.java b/src/main/java/pm/j4/petroleum/util/module/option/ConfigurationOption.java new file mode 100644 index 0000000..0ec0f39 --- /dev/null +++ b/src/main/java/pm/j4/petroleum/util/module/option/ConfigurationOption.java @@ -0,0 +1,27 @@ +package pm.j4.petroleum.util.module.option; + +import com.google.gson.JsonElement; + +/** + * The type Configuration option. + * + */ +public abstract class ConfigurationOption { + private final String description; + + protected ConfigurationOption(String description) { + this.description = description; + } + + public final String getDescription() { + return this.description; + } + + public abstract String getStringValue(); + public abstract void fromJson(JsonElement e); + public abstract JsonElement toJson(); + + public void update() { + + } +} diff --git a/src/main/java/pm/j4/petroleum/util/module/option/DummyValue.java b/src/main/java/pm/j4/petroleum/util/module/option/DummyValue.java deleted file mode 100644 index 336a5c6..0000000 --- a/src/main/java/pm/j4/petroleum/util/module/option/DummyValue.java +++ /dev/null @@ -1,9 +0,0 @@ -package pm.j4.petroleum.util.module.option; - -import pm.j4.petroleum.util.module.StringWritable; - -/** - * The type Dummy value. - */ -public class DummyValue extends StringWritable { -} diff --git a/src/main/java/pm/j4/petroleum/util/module/option/IntegerOption.java b/src/main/java/pm/j4/petroleum/util/module/option/IntegerOption.java new file mode 100644 index 0000000..2d8faf2 --- /dev/null +++ b/src/main/java/pm/j4/petroleum/util/module/option/IntegerOption.java @@ -0,0 +1,29 @@ +package pm.j4.petroleum.util.module.option; + +import com.google.gson.JsonElement; +import com.google.gson.JsonPrimitive; + +/** + * The type Integer value. + */ +public class IntegerOption extends ConfigurationOption { + private int value; + protected IntegerOption(String description) { + super(description); + } + + @Override + public String getStringValue() { + return Integer.toString(value); + } + + @Override + public void fromJson(JsonElement e) { + this.value = e.getAsInt(); + } + + @Override + public JsonElement toJson() { + return new JsonPrimitive(value); + } +} diff --git a/src/main/java/pm/j4/petroleum/util/module/option/IntegerValue.java b/src/main/java/pm/j4/petroleum/util/module/option/IntegerValue.java deleted file mode 100644 index 90a0b42..0000000 --- a/src/main/java/pm/j4/petroleum/util/module/option/IntegerValue.java +++ /dev/null @@ -1,46 +0,0 @@ -package pm.j4.petroleum.util.module.option; - -import pm.j4.petroleum.util.module.StringWritable; - -/** - * The type Integer value. - */ -public class IntegerValue extends StringWritable { - /** - * The Type. - */ - public final String type = "I"; - - /** - * Instantiates a new Integer value. - * - * @param value the value - */ - public IntegerValue(String value) { - this.value = Integer.valueOf(value); - } - - /** - * Instantiates a new Integer value. - * - * @param value the value - */ - public IntegerValue(int value) { - this.value = Integer.valueOf(value); - } - - @Override - public String getType() { - return "I"; - } - - @Override - public String getStringValue() { - return value.toString(); - } - - @Override - public void toStringValue(String value) { - this.value = Integer.valueOf(value); - } -} diff --git a/src/main/java/pm/j4/petroleum/util/module/option/KeybindOption.java b/src/main/java/pm/j4/petroleum/util/module/option/KeybindOption.java new file mode 100644 index 0000000..eeab653 --- /dev/null +++ b/src/main/java/pm/j4/petroleum/util/module/option/KeybindOption.java @@ -0,0 +1,42 @@ +package pm.j4.petroleum.util.module.option; + +import com.google.gson.JsonElement; +import net.minecraft.client.options.KeyBinding; +import pm.j4.petroleum.modules.bindings.BindingInfo; +import pm.j4.petroleum.util.config.ConfigManager; +import pm.j4.petroleum.util.config.GlobalConfig; +import pm.j4.petroleum.util.module.ModuleBase; + +/** + * The type Keybind value. + */ +public class KeybindOption extends ConfigurationOption { + + private KeyBinding value; + private BindingInfo convertedValue; + public KeybindOption(String description) { + super(description); + } + + @Override + public String getStringValue() { + return value.getDefaultKey().getLocalizedText().getString(); + } + + @Override + public void fromJson(JsonElement e) { + BindingInfo bindingInfo = ConfigManager.deserializeElement(e, BindingInfo.class); + this.convertedValue = bindingInfo; + this.value = GlobalConfig.reconstructBinding(bindingInfo); + } + + @Override + public JsonElement toJson() { + return null; + } + + public void fromKeybind(KeyBinding bind, ModuleBase base) { + this.value = bind; + this.convertedValue = GlobalConfig.extractBinding(bind, base); + } +} diff --git a/src/main/java/pm/j4/petroleum/util/module/option/KeybindValue.java b/src/main/java/pm/j4/petroleum/util/module/option/KeybindValue.java deleted file mode 100644 index d4641ea..0000000 --- a/src/main/java/pm/j4/petroleum/util/module/option/KeybindValue.java +++ /dev/null @@ -1,39 +0,0 @@ -package pm.j4.petroleum.util.module.option; - -import net.minecraft.client.options.KeyBinding; -import pm.j4.petroleum.util.module.StringWritable; - -/** - * The type Keybind value. - */ -public class KeybindValue extends StringWritable { - /** - * Instantiates a new Keybind value. - * - * @param translationKey the translation key - * @param code the code - * @param category the category - */ - public KeybindValue(String translationKey, int code, String category) { - this.value = new KeyBinding(translationKey, code, category); - } - - /** - * Instantiates a new Keybind value. - * - * @param binding the binding - */ - public KeybindValue(KeyBinding binding) { - this.value = binding; - } - - @Override - public String getType() { - return "KB"; - } - - @Override - public String getStringValue() { - return this.value.getDefaultKey().getLocalizedText().getString(); - } -} diff --git a/src/main/java/pm/j4/petroleum/util/module/option/ListOption.java b/src/main/java/pm/j4/petroleum/util/module/option/ListOption.java new file mode 100644 index 0000000..c1f6b5a --- /dev/null +++ b/src/main/java/pm/j4/petroleum/util/module/option/ListOption.java @@ -0,0 +1,24 @@ +package pm.j4.petroleum.util.module.option; + +import com.google.gson.JsonElement; + +public class ListOption extends ConfigurationOption { + protected ListOption(String description) { + super(description); + } + + @Override + public String getStringValue() { + return "ListObject"; + } + + @Override + public void fromJson(JsonElement e) { + + } + + @Override + public JsonElement toJson() { + return null; + } +} diff --git a/src/main/java/pm/j4/petroleum/util/module/option/OptionTypeMatcher.java b/src/main/java/pm/j4/petroleum/util/module/option/OptionTypeMatcher.java deleted file mode 100644 index 0afc684..0000000 --- a/src/main/java/pm/j4/petroleum/util/module/option/OptionTypeMatcher.java +++ /dev/null @@ -1,29 +0,0 @@ -package pm.j4.petroleum.util.module.option; - -import pm.j4.petroleum.util.module.StringWritable; - -/** - * The type Option type matcher. - */ -public class OptionTypeMatcher { - /** - * Match string writable. - * - * @param type the type - * @param value the value - * @return the string writable - */ - public static StringWritable match(String type, String value) { - if (type == null || type.isEmpty()) { - return new DummyValue(); - } - switch (type) { - case "I": - return new IntegerValue(value); - case "B": - return new BooleanValue(value); - default: - return new DummyValue(); - } - } -} diff --git a/src/main/java/pm/j4/petroleum/util/module/option/StringOption.java b/src/main/java/pm/j4/petroleum/util/module/option/StringOption.java new file mode 100644 index 0000000..b276650 --- /dev/null +++ b/src/main/java/pm/j4/petroleum/util/module/option/StringOption.java @@ -0,0 +1,30 @@ +package pm.j4.petroleum.util.module.option; + +import com.google.gson.JsonElement; +import com.google.gson.JsonPrimitive; + +/** + * The type String value. + */ +public class StringOption extends ConfigurationOption { + + private String value; + + protected StringOption(String description) { + super(description); + } + + public String getStringValue() { + return value; + } + + @Override + public void fromJson(JsonElement e) { + this.value = e.getAsString(); + } + + @Override + public JsonElement toJson() { + return new JsonPrimitive(value); + } +} diff --git a/src/main/java/pm/j4/petroleum/util/module/option/StringValue.java b/src/main/java/pm/j4/petroleum/util/module/option/StringValue.java deleted file mode 100644 index 39fb3b3..0000000 --- a/src/main/java/pm/j4/petroleum/util/module/option/StringValue.java +++ /dev/null @@ -1,32 +0,0 @@ -package pm.j4.petroleum.util.module.option; - -import pm.j4.petroleum.util.module.StringWritable; - -/** - * The type String value. - */ -public class StringValue extends StringWritable { - /** - * Instantiates a new String value. - * - * @param value the value - */ - public StringValue(String value) { - this.value = value; - } - - @Override - public String getType() { - return "S"; - } - - @Override - public String getStringValue() { - return this.value; - } - - @Override - public void toStringValue(String value) { - this.value = value; - } -} diff --git a/src/main/resources/fabric.mod.json b/src/main/resources/fabric.mod.json index c3793af..849ab35 100644 --- a/src/main/resources/fabric.mod.json +++ b/src/main/resources/fabric.mod.json @@ -10,7 +10,7 @@ ], "contact": { "homepage": "https://j4.pm/", - "sources": "https://github.com/FabricMC/fabric-example-mod" + "sources": "https://gitdab.com/jane/petroleum" }, "license": "WTFPL",