refactor config files so that module configs are separate

This commit is contained in:
janeptrv 2020-10-10 18:57:22 -04:00
parent 35ff8912de
commit 17e135f3d3
29 changed files with 333 additions and 408 deletions

View file

@ -126,15 +126,19 @@ public class PetroleumMod implements ModInitializer {
public static String getServerAddress() { public static String getServerAddress() {
if (client != null && client.getServer() != null) { if (client != null && client.getServer() != null) {
IntegratedServer server = client.getServer(); IntegratedServer server = client.getServer();
if (!server.isRemote()) {
return "localhost";
}
if (server.isRemote() && !server.getServerIp().isEmpty()) { if (server.isRemote() && !server.getServerIp().isEmpty()) {
return server.getServerIp(); return server.getServerIp();
} }
} }
return null; return null;
} }
@Override @Override
public void onInitialize() { public void onInitialize() {
ConfigManager.initConfig(); ConfigManager.initConfig();
// always update mod data // always update mod data

View file

@ -50,7 +50,7 @@ public class PModMenuScreen extends Screen {
if (ConfigManager.getConfig().isPresent()) { if (ConfigManager.getConfig().isPresent()) {
ConfigManager.getConfig().get().disableModule("petroleum.modmenu"); ConfigManager.getConfig().get().disableModule("petroleum.modmenu");
this.buttons.forEach(button -> ((PMovableButton) button).updateCoordinate()); this.buttons.forEach(button -> ((PMovableButton) button).updateCoordinate());
ConfigManager.save(); ConfigManager.saveGlobalConfig();
} }
super.onClose(); super.onClose();
} }

View file

@ -5,7 +5,7 @@ import net.minecraft.client.gui.widget.EntryListWidget;
import net.minecraft.client.util.math.MatrixStack; import net.minecraft.client.util.math.MatrixStack;
import net.minecraft.text.LiteralText; import net.minecraft.text.LiteralText;
import net.minecraft.text.Text; 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. * The type P module config entry.
@ -41,7 +41,7 @@ public class PModuleConfigEntry extends EntryListWidget.Entry<PModuleConfigEntry
// option should be centered or otherwise offset // option should be centered or otherwise offset
// but not extend past the side of the pane // but not extend past the side of the pane
int fontHeight = MinecraftClient.getInstance().textRenderer.fontHeight; int fontHeight = MinecraftClient.getInstance().textRenderer.fontHeight;
MinecraftClient.getInstance().textRenderer.drawWithShadow(matrices, new LiteralText(option.toStringValue()), x, y + fontHeight + 4, 0xFFFFFF); MinecraftClient.getInstance().textRenderer.drawWithShadow(matrices, new LiteralText(option.getStringValue()), x, y + fontHeight + 4, 0xFFFFFF);
} }
} }
} }

View file

@ -58,10 +58,6 @@ public class PMovableButton extends AbstractButtonWidget {
* The Stored y. * The Stored y.
*/ */
private int storedY; private int storedY;
/**
* The Padding.
*/
private final int padding = 5;
private final String category; private final String category;
@ -108,6 +104,10 @@ public class PMovableButton extends AbstractButtonWidget {
public void onRelease(double mouseX, double mouseY) { public void onRelease(double mouseX, double mouseY) {
int mx = (int) mouseX; int mx = (int) mouseX;
int my = (int) mouseY; int my = (int) mouseY;
/**
* The Padding.
*/
int padding = 5;
if (storedX + padding > mx && storedX - padding < mx && if (storedX + padding > mx && storedX - padding < mx &&
storedY + padding > my && storedY - padding < my) { storedY + padding > my && storedY - padding < my) {
this.expanded = !this.expanded; this.expanded = !this.expanded;
@ -142,10 +142,8 @@ public class PMovableButton extends AbstractButtonWidget {
} }
} }
return false;
} else {
return false;
} }
return false;
} }
/** /**

View file

@ -109,7 +109,8 @@ public class POptionsScreen extends Screen {
} }
configurableModules.forEach(module -> this.modules.addEntry(new POptionEntry(module, this.modules))); 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) -> { 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; assert this.client != null;
this.client.openScreen(this.previousScreen); this.client.openScreen(this.previousScreen);
})); }));

View file

@ -3,9 +3,9 @@ package pm.j4.petroleum.modules;
import java.util.HashMap; import java.util.HashMap;
import java.util.Map; import java.util.Map;
import net.minecraft.client.MinecraftClient; 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.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. * The type Example module.
@ -25,9 +25,8 @@ public class ExampleModule extends ModuleBase {
@Override @Override
protected Map<String, ConfigurationOption> getDefaultConfig() { protected Map<String, ConfigurationOption> getDefaultConfig() {
Map<String, ConfigurationOption> options = new HashMap<>(); Map<String, ConfigurationOption> options = new HashMap<>();
ConfigurationOption option = new ConfigurationOption<>(new BooleanValue(false)); options.put("petroleum.example_b_one", new BooleanOption("example"));
options.put("petroleum.example_b_one", option); options.put("petroleum.example_b_two", new BooleanOption("example"));
options.put("petroleum.example_b_two", option);
return options; return options;
} }

View file

@ -11,9 +11,8 @@ import pm.j4.petroleum.PetroleumMod;
import pm.j4.petroleum.gui.PModuleConfigEntry; import pm.j4.petroleum.gui.PModuleConfigEntry;
import pm.j4.petroleum.util.config.ConfigManager; import pm.j4.petroleum.util.config.ConfigManager;
import pm.j4.petroleum.util.config.GlobalConfig; 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.ModuleBase;
import pm.j4.petroleum.util.module.option.KeybindValue; import pm.j4.petroleum.util.module.option.KeybindOption;
/** /**
* The type Binding manager. * The type Binding manager.
@ -41,10 +40,15 @@ public class BindingManager extends ModuleBase {
public List<PModuleConfigEntry> getConfigEntries() { public List<PModuleConfigEntry> getConfigEntries() {
List<PModuleConfigEntry> entries = new ArrayList<>(); List<PModuleConfigEntry> entries = new ArrayList<>();
Map<ConfigurationOption<KeybindValue>, ModuleBase> mapped = new HashMap<>(); Map<KeybindOption, ModuleBase> mapped = new HashMap<>();
if (ConfigManager.getConfig().isPresent()) { if (ConfigManager.getConfig().isPresent()) {
Map<KeyBinding, ModuleBase> binds = ConfigManager.getConfig().get().globalConfig.bindings; Map<KeyBinding, ModuleBase> 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) -> { mapped.forEach((bind, module) -> {
PModuleConfigEntry entry = new PModuleConfigEntry(bind, new TranslatableText(module.getModuleName())) { PModuleConfigEntry entry = new PModuleConfigEntry(bind, new TranslatableText(module.getModuleName())) {
@ -57,7 +61,7 @@ public class BindingManager extends ModuleBase {
} }
if (this.option != null) { if (this.option != null) {
int fontHeight = MinecraftClient.getInstance().textRenderer.fontHeight; 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);
} }
} }
}; };

View file

@ -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);
}
}

View file

@ -90,4 +90,8 @@ public class ConfigHolder {
globalConfig.disableModule(module); globalConfig.disableModule(module);
} }
} }
public static void saveModule(ModuleBase module) {
}
} }

View file

@ -10,6 +10,9 @@ import pm.j4.petroleum.PetroleumMod;
import pm.j4.petroleum.modules.bindings.BindingInfo; import pm.j4.petroleum.modules.bindings.BindingInfo;
import pm.j4.petroleum.modules.menu.ModMenu; import pm.j4.petroleum.modules.menu.ModMenu;
import pm.j4.petroleum.util.data.ButtonInformation; 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. * The type Config manager.
@ -19,13 +22,9 @@ public class ConfigManager {
* The constant GSON. * The constant GSON.
*/ */
public static final Gson GSON = new GsonBuilder() public static final Gson GSON = new GsonBuilder()
.registerTypeAdapter(GlobalConfig.class, SerializationHelper.getSerializer()) .registerTypeAdapter(GlobalConfig.class, SerializationHelper.getGlobalSerializer())
.registerTypeAdapter(GlobalConfig.class, SerializationHelper.getDeserializer()) .registerTypeAdapter(GlobalConfig.class, SerializationHelper.getGlobalDeserializer())
.setFieldNamingPolicy(FieldNamingPolicy.LOWER_CASE_WITH_UNDERSCORES).setPrettyPrinting().create(); .setFieldNamingPolicy(FieldNamingPolicy.LOWER_CASE_WITH_UNDERSCORES).setPrettyPrinting().create();
/**
* The constant file.
*/
private static File file;
/** /**
* The constant config. * The constant config.
*/ */
@ -34,11 +33,12 @@ public class ConfigManager {
/** /**
* Prepare config file. * Prepare config file.
*/ */
private static void prepareConfigFile() { private static File prepareConfigFile(String path, String filename) {
if (file != null) { if(path != "") {
return; 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.globalConfig = new DefaultConfig();
config.serverConfigs = new HashMap<>(); 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. * Load.
*/ */
private static void load() { private static <T> T load(String path, String filename, Class<T> tClass) {
prepareConfigFile(); File file = prepareConfigFile(path, filename);
try { try {
if (!file.exists()) { if (!file.exists()) {
save(); save(path, filename, tClass.newInstance());
} }
if (file.exists()) { if (file.exists()) {
BufferedReader reader = new BufferedReader(new FileReader(file)); BufferedReader reader = new BufferedReader(new FileReader(file));
ConfigHolder parsedConfig = null; T parsedConfig = null;
try { try {
parsedConfig = GSON.fromJson(reader, ConfigHolder.class); parsedConfig = GSON.fromJson(reader, tClass);
} catch (Exception e) { } catch (Exception e) {
System.out.println("Couldn't parse config file"); System.out.println("Couldn't parse config file");
e.printStackTrace(); e.printStackTrace();
} }
if (parsedConfig != null) { if (parsedConfig != null) {
config = parsedConfig; return parsedConfig;
} }
} }
} catch (FileNotFoundException e) { } catch (FileNotFoundException | InstantiationException | IllegalAccessException e) {
System.out.println("Couldn't load Petroleum configuration"); System.out.println("Couldn't load configuration file at " + path);
e.printStackTrace(); e.printStackTrace();
} }
return null;
}
public static <T> T deserializeElement(JsonElement element, Class<T> tClass) {
return GSON.fromJson(element, tClass);
} }
/** /**
* Save. * Save.
*/ */
public static void save() { private static <T> void save(String path, String filename, T data) {
prepareConfigFile(); File file = prepareConfigFile(path, filename);
String json = GSON.toJson(config);
String json = GSON.toJson(data);
try (FileWriter fileWriter = new FileWriter(file)) { try (FileWriter fileWriter = new FileWriter(file)) {
System.out.println("FILE WRITE ATTEMPT");
System.out.println(path);
System.out.println(json);
fileWriter.write(json); fileWriter.write(json);
} catch (IOException e) { } catch (IOException e) {
System.out.println("Couldn't save Petroleum configuration"); System.out.println("Couldn't save configuration file at " + path);
e.printStackTrace(); 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<ModuleBase> mods = PetroleumMod.getActiveMods();
mods.forEach(ConfigManager::saveModule);
}
public static void saveGlobalConfig() {
save("","petroleum.json", config);
}
/** /**
* Gets config. * Gets config.
* *
@ -122,7 +158,7 @@ class SerializationHelper {
/** /**
* The constant s. * The constant s.
*/ */
private static final JsonSerializer<GlobalConfig> s = (src, typeOfSrc, ctx) -> { private static final JsonSerializer<GlobalConfig> GLOBAL_CONFIG_JSON_SERIALIZER = (src, typeOfSrc, ctx) -> {
JsonObject jsonConfig = new JsonObject(); JsonObject jsonConfig = new JsonObject();
JsonArray bindings = ctx.serialize(src.serializeBindings()).getAsJsonArray(); JsonArray bindings = ctx.serialize(src.serializeBindings()).getAsJsonArray();
@ -131,12 +167,6 @@ class SerializationHelper {
JsonArray modules = ctx.serialize(src.enabledModules).getAsJsonArray(); JsonArray modules = ctx.serialize(src.enabledModules).getAsJsonArray();
jsonConfig.add("enabled_modules", modules); 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(); JsonObject tabCoordinates = new JsonObject();
ModMenu.getButtons().forEach((category, coordinates) -> { ModMenu.getButtons().forEach((category, coordinates) -> {
tabCoordinates.add(category, ctx.serialize(coordinates)); tabCoordinates.add(category, ctx.serialize(coordinates));
@ -149,7 +179,7 @@ class SerializationHelper {
/** /**
* The constant ds. * The constant ds.
*/ */
private static final JsonDeserializer<GlobalConfig> ds = ((json, typeOfT, ctx) -> { private static final JsonDeserializer<GlobalConfig> GLOBAL_CONFIG_JSON_DESERIALIZER = ((json, typeOfT, ctx) -> {
JsonObject obj = json.getAsJsonObject(); JsonObject obj = json.getAsJsonObject();
List<BindingInfo> bindings = new ArrayList<>(); List<BindingInfo> bindings = new ArrayList<>();
@ -191,8 +221,8 @@ class SerializationHelper {
* *
* @return the serializer * @return the serializer
*/ */
public static JsonSerializer<GlobalConfig> getSerializer() { public static JsonSerializer<GlobalConfig> getGlobalSerializer() {
return s; return GLOBAL_CONFIG_JSON_SERIALIZER;
} }
/** /**
@ -200,7 +230,7 @@ class SerializationHelper {
* *
* @return the deserializer * @return the deserializer
*/ */
public static JsonDeserializer<GlobalConfig> getDeserializer() { public static JsonDeserializer<GlobalConfig> getGlobalDeserializer() {
return ds; return GLOBAL_CONFIG_JSON_DESERIALIZER;
} }
} }

View file

@ -7,9 +7,9 @@ import net.minecraft.client.util.InputUtil;
import pm.j4.petroleum.PetroleumMod; import pm.j4.petroleum.PetroleumMod;
import pm.j4.petroleum.modules.bindings.BindingInfo; import pm.j4.petroleum.modules.bindings.BindingInfo;
import pm.j4.petroleum.util.data.ButtonInformation; 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.ModuleBase;
import pm.j4.petroleum.util.module.option.OptionTypeMatcher; import pm.j4.petroleum.util.module.option.ConfigurationOption;
/** /**
* The type Global config. * The type Global config.
@ -65,15 +65,19 @@ public class GlobalConfig extends Config {
*/ */
private void convertBinding(BindingInfo info) { private void convertBinding(BindingInfo info) {
Optional<ModuleBase> match = PetroleumMod.getMod(info.attachedModuleName); Optional<ModuleBase> match = PetroleumMod.getMod(info.attachedModuleName);
match.ifPresent(moduleBase -> setBinding(new KeyBinding( match.ifPresent(moduleBase -> setBinding(reconstructBinding(info),
info.translationKey,
info.type,
info.key,
info.category
),
moduleBase)); moduleBase));
} }
public static KeyBinding reconstructBinding(BindingInfo info) {
return new KeyBinding(
info.translationKey,
info.type,
info.key,
info.category
);
}
/** /**
* Extract binding binding info. * Extract binding binding info.
* *
@ -81,7 +85,7 @@ public class GlobalConfig extends Config {
* @param f the f * @param f the f
* @return the binding info * @return the binding info
*/ */
private BindingInfo extractBinding(KeyBinding b, ModuleBase f) { public static BindingInfo extractBinding(KeyBinding b, ModuleBase f) {
BindingInfo res = new BindingInfo(); BindingInfo res = new BindingInfo();
res.attachedModuleName = f.getModuleName(); res.attachedModuleName = f.getModuleName();
@ -120,11 +124,11 @@ public class GlobalConfig extends Config {
* @param module the module * @param module the module
* @return the list * @return the list
*/ */
public List<OptionSerializiable> serializeModuleConfiguration(ModuleBase module) { public static Map<String, OptionSerializiable> serializeModuleConfiguration(ModuleBase module) {
List<OptionSerializiable> opts = new ArrayList<>(); Map<String, OptionSerializiable> opts = new HashMap<>();
Map<String, ConfigurationOption> configuration = module.getModuleConfiguration(); Map<String, ConfigurationOption> configuration = module.getModuleConfiguration();
configuration.forEach((key, value) -> { configuration.forEach((key, value) -> {
opts.add(new OptionSerializiable(value.getValueType(), value.toStringValue(), key)); opts.put(key, new OptionSerializiable(key, value.toJson()));
}); });
return opts; return opts;
} }
@ -137,7 +141,9 @@ public class GlobalConfig extends Config {
*/ */
public void deserializeModuleConfiguration(List<OptionSerializiable> opts, ModuleBase module) { public void deserializeModuleConfiguration(List<OptionSerializiable> opts, ModuleBase module) {
opts.forEach(option -> { 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);
}
}); });
} }

View file

@ -0,0 +1,7 @@
package pm.j4.petroleum.util.data;
import java.util.Map;
public class ModuleConfig {
public Map<String, OptionSerializiable> options;
}

View file

@ -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. * The type Option serializiable.
@ -7,24 +9,17 @@ public class OptionSerializiable {
/** /**
* Instantiates a new Option serializiable. * Instantiates a new Option serializiable.
* *
* @param type the type
* @param value the value * @param value the value
* @param key the key * @param key the key
*/ */
public OptionSerializiable(String type, String value, String key) { public OptionSerializiable(String key, JsonElement value) {
this.type = type;
this.value = value; this.value = value;
this.key = key; this.key = key;
} }
/**
* The Type.
*/
public final String type;
/** /**
* The Value. * The Value.
*/ */
public final String value; public final JsonElement value;
/** /**
* The Key. * The Key.
*/ */

View file

@ -1,68 +0,0 @@
package pm.j4.petroleum.util.module;
/**
* The type Configuration option.
*
* @param <T> the type parameter
*/
public class ConfigurationOption<T extends StringWritable> {
/**
* 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();
}
}

View file

@ -1,11 +1,13 @@
package pm.j4.petroleum.util.module; package pm.j4.petroleum.util.module;
import com.google.gson.JsonElement;
import java.util.*; import java.util.*;
import net.minecraft.client.MinecraftClient; import net.minecraft.client.MinecraftClient;
import net.minecraft.text.TranslatableText; import net.minecraft.text.TranslatableText;
import pm.j4.petroleum.gui.PModuleConfigEntry; import pm.j4.petroleum.gui.PModuleConfigEntry;
import pm.j4.petroleum.util.config.ConfigHolder; import pm.j4.petroleum.util.config.ConfigHolder;
import pm.j4.petroleum.util.config.ConfigManager; 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. * 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. * Init.
*/ */
public void init() { public void init() {
} }
/** /**
@ -154,14 +157,18 @@ public abstract class ModuleBase {
* @param value the value * @param value the value
* @return whether the operation was successful. * @return whether the operation was successful.
*/ */
public boolean setConfigOption(String key, ConfigurationOption value) { public boolean setConfigOption(String key, JsonElement value) {
if (moduleOptions.containsKey(key)) { if (moduleOptions.containsKey(key)) {
moduleOptions.replace(key, value); moduleOptions.get(key).fromJson(value);
return true; return true;
} }
return false; return false;
} }
public boolean hasOption(String key) {
return moduleOptions.containsKey(key);
}
/** /**
* Gets default config. * Gets default config.
* *
@ -171,18 +178,6 @@ public abstract class ModuleBase {
return new HashMap<>(); 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. * Gets config option.
* *

View file

@ -1,48 +0,0 @@
package pm.j4.petroleum.util.module;
/**
* The type String writable.
*
* @param <T> the type parameter
*/
public class StringWritable<T extends Comparable<T>> {
/**
* 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) {
}
}

View file

@ -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);
}
}

View file

@ -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<Boolean> {
/**
* 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);
}
}

View file

@ -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() {
}
}

View file

@ -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<Boolean> {
}

View file

@ -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);
}
}

View file

@ -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<Integer> {
/**
* 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);
}
}

View file

@ -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);
}
}

View file

@ -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<KeyBinding> {
/**
* 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();
}
}

View file

@ -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;
}
}

View file

@ -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();
}
}
}

View file

@ -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);
}
}

View file

@ -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<String> {
/**
* 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;
}
}

View file

@ -10,7 +10,7 @@
], ],
"contact": { "contact": {
"homepage": "https://j4.pm/", "homepage": "https://j4.pm/",
"sources": "https://github.com/FabricMC/fabric-example-mod" "sources": "https://gitdab.com/jane/petroleum"
}, },
"license": "WTFPL", "license": "WTFPL",