From f6d8c7da2f12d59b96d0f04248d4dc9f501e09bc Mon Sep 17 00:00:00 2001 From: xiaozhou233 Date: Sat, 7 Nov 2026 18:58:04 +0800 Subject: [PATCH] Feat: Add module value system - Add Value, BooleanValue, KeybindValue and ModeValue - Store values per module with defaults, reset, visibility and JSON encoding - Wire module keybinds to EventKey so bound keys toggle modules --- .../xiaozhou233/orangex/modules/Module.java | 100 ++++++++++++++- .../orangex/modules/ModuleManager.java | 11 ++ .../orangex/value/BooleanValue.java | 38 ++++++ .../orangex/value/KeybindValue.java | 58 +++++++++ .../xiaozhou233/orangex/value/ModeValue.java | 97 +++++++++++++++ .../cn/xiaozhou233/orangex/value/Value.java | 114 ++++++++++++++++++ 6 files changed, 415 insertions(+), 3 deletions(-) create mode 100644 OrangeX/src/main/java/cn/xiaozhou233/orangex/value/BooleanValue.java create mode 100644 OrangeX/src/main/java/cn/xiaozhou233/orangex/value/KeybindValue.java create mode 100644 OrangeX/src/main/java/cn/xiaozhou233/orangex/value/ModeValue.java create mode 100644 OrangeX/src/main/java/cn/xiaozhou233/orangex/value/Value.java diff --git a/OrangeX/src/main/java/cn/xiaozhou233/orangex/modules/Module.java b/OrangeX/src/main/java/cn/xiaozhou233/orangex/modules/Module.java index b51460d..5bf9a45 100644 --- a/OrangeX/src/main/java/cn/xiaozhou233/orangex/modules/Module.java +++ b/OrangeX/src/main/java/cn/xiaozhou233/orangex/modules/Module.java @@ -1,9 +1,21 @@ package cn.xiaozhou233.orangex.modules; import cn.xiaozhou233.orangex.OrangeX; +import cn.xiaozhou233.orangex.input.KeyCodes; +import cn.xiaozhou233.orangex.value.BooleanValue; +import cn.xiaozhou233.orangex.value.KeybindValue; +import cn.xiaozhou233.orangex.value.ModeValue; +import cn.xiaozhou233.orangex.value.Value; import cn.xiaozhou233.orangex.wrapper.api.Minecraft; +import com.google.gson.JsonObject; import lombok.Getter; +import java.util.ArrayList; +import java.util.Collections; +import java.util.LinkedHashMap; +import java.util.List; +import java.util.Map; + @Getter public abstract class Module { protected final Minecraft mc = OrangeX.getInstance().getMinecraft(); @@ -11,13 +23,95 @@ public abstract class Module { protected final ModuleCategory category; protected boolean enabled = false; + private final Map> valueMap = new LinkedHashMap<>(); + private final KeybindValue keybind; + public Module(String name, ModuleCategory category) { - this.name = name; - this.category = category; + this(name, category, KeyCodes.NONE); } public Module(String name, ModuleCategory category, int defaultKey) { - this(name, category); + this.name = name; + this.category = category; + this.keybind = new KeybindValue("keybind", defaultKey); + valueMap.put(keybind.getName(), keybind); + } + + /** + * Registers a value so it can be displayed by configuration UIs and + * persisted with the module. + * + * @return the same value, for fluent field initialisation. + */ + protected > V addValue(V value) { + if (valueMap.containsKey(value.getName())) { + throw new IllegalArgumentException( + "Module '" + name + "' already has a value named '" + value.getName() + "'"); + } + valueMap.put(value.getName(), value); + return value; + } + + protected BooleanValue addBoolean(String name, boolean defaultValue) { + return addValue(new BooleanValue(name, defaultValue)); + } + + protected KeybindValue addKeybind(String name, int defaultKey) { + return addValue(new KeybindValue(name, defaultKey)); + } + + protected ModeValue addMode(String name, String defaultMode, String... modes) { + return addValue(new ModeValue(name, defaultMode, modes)); + } + + public Value getValue(String name) { + return valueMap.get(name); + } + + public List> getValues() { + return Collections.unmodifiableList(new ArrayList<>(valueMap.values())); + } + + public List> getVisibleValues() { + List> visible = new ArrayList<>(); + for (Value value : valueMap.values()) { + if (value.isVisible()) { + visible.add(value); + } + } + return visible; + } + + public void resetValues() { + for (Value value : valueMap.values()) { + value.reset(); + } + } + + /** + * Encodes every value (including the module keybind) for configuration + * storage. + */ + public JsonObject valuesToJson() { + JsonObject json = new JsonObject(); + for (Value value : valueMap.values()) { + json.add(value.getName(), value.toJson()); + } + return json; + } + + /** + * Restores previously encoded values. Unknown keys are ignored. + */ + public void valuesFromJson(JsonObject json) { + if (json == null) { + return; + } + for (Value value : valueMap.values()) { + if (json.has(value.getName())) { + value.fromJson(json.get(value.getName())); + } + } } protected void onEnable() { diff --git a/OrangeX/src/main/java/cn/xiaozhou233/orangex/modules/ModuleManager.java b/OrangeX/src/main/java/cn/xiaozhou233/orangex/modules/ModuleManager.java index a780dc9..f97e80f 100644 --- a/OrangeX/src/main/java/cn/xiaozhou233/orangex/modules/ModuleManager.java +++ b/OrangeX/src/main/java/cn/xiaozhou233/orangex/modules/ModuleManager.java @@ -19,6 +19,7 @@ public class ModuleManager { public void init() { registerAll(); + OrangeX.getInstance().getEventBus().register(this); } public void registerAll() { @@ -46,4 +47,14 @@ public class ModuleManager { public List getAllModules() { return modules; } + + @Subscribe + public void onKey(EventKey event) { + for (Module module : modules) { + if (module.getKeybind().getValue() == event.getKey()) { + module.toggle(); + System.out.println("[ModuleManager] Toggled module: " + module.getName()); + } + } + } } \ No newline at end of file diff --git a/OrangeX/src/main/java/cn/xiaozhou233/orangex/value/BooleanValue.java b/OrangeX/src/main/java/cn/xiaozhou233/orangex/value/BooleanValue.java new file mode 100644 index 0000000..0f535ed --- /dev/null +++ b/OrangeX/src/main/java/cn/xiaozhou233/orangex/value/BooleanValue.java @@ -0,0 +1,38 @@ +package cn.xiaozhou233.orangex.value; + +import com.google.gson.JsonElement; +import com.google.gson.JsonPrimitive; + +/** + * An on/off value, for example "enabled" or "show background". + */ +public class BooleanValue extends Value { + + public BooleanValue(String name, boolean defaultValue) { + super(name, defaultValue); + } + + public boolean isEnabled() { + return getValue(); + } + + public void setEnabled(boolean enabled) { + setValue(enabled); + } + + public void toggle() { + setValue(!isEnabled()); + } + + @Override + public JsonElement toJson() { + return new JsonPrimitive(isEnabled()); + } + + @Override + public void fromJson(JsonElement json) { + if (isBoolean(json)) { + setValue(json.getAsBoolean()); + } + } +} diff --git a/OrangeX/src/main/java/cn/xiaozhou233/orangex/value/KeybindValue.java b/OrangeX/src/main/java/cn/xiaozhou233/orangex/value/KeybindValue.java new file mode 100644 index 0000000..93faf48 --- /dev/null +++ b/OrangeX/src/main/java/cn/xiaozhou233/orangex/value/KeybindValue.java @@ -0,0 +1,58 @@ +package cn.xiaozhou233.orangex.value; + +import cn.xiaozhou233.orangex.input.KeyCodes; +import com.google.gson.JsonElement; +import com.google.gson.JsonPrimitive; + +/** + * A single key binding. The stored key always uses the canonical 1.8.9 / + * LWJGL 2 numbering used by + * {@link cn.xiaozhou233.orangex.event.impl.EventKey}; {@link KeyCodes#NONE} + * means the binding is unbound. + */ +public class KeybindValue extends Value { + + public KeybindValue(String name, int defaultKey) { + super(name, normalize(defaultKey)); + } + + public int getKey() { + return getValue(); + } + + public void setKey(int key) { + setValue(normalize(key)); + } + + public boolean isBound() { + return getKey() != KeyCodes.NONE; + } + + public void clear() { + setKey(KeyCodes.NONE); + } + + /** + * @param key canonical 1.8.9 (LWJGL 2) key code. + * @return whether this binding is set and matches {@code key}. + */ + public boolean matches(int key) { + return isBound() && getKey() == key; + } + + private static int normalize(int key) { + return key < 0 ? KeyCodes.NONE : key; + } + + @Override + public JsonElement toJson() { + return new JsonPrimitive(getKey()); + } + + @Override + public void fromJson(JsonElement json) { + if (isNumber(json)) { + setKey(json.getAsInt()); + } + } +} diff --git a/OrangeX/src/main/java/cn/xiaozhou233/orangex/value/ModeValue.java b/OrangeX/src/main/java/cn/xiaozhou233/orangex/value/ModeValue.java new file mode 100644 index 0000000..5db41c0 --- /dev/null +++ b/OrangeX/src/main/java/cn/xiaozhou233/orangex/value/ModeValue.java @@ -0,0 +1,97 @@ +package cn.xiaozhou233.orangex.value; + +import com.google.gson.JsonElement; +import com.google.gson.JsonPrimitive; + +import java.util.ArrayList; +import java.util.Arrays; +import java.util.Collection; +import java.util.Collections; +import java.util.List; + +/** + * A value limited to a fixed list of named modes, for example + * "Vanilla" / "Packet" / "Spoof". + */ +public class ModeValue extends Value { + + private final List modes; + + public ModeValue(String name, String defaultMode, String... modes) { + this(name, defaultMode, Arrays.asList(modes)); + } + + public ModeValue(String name, String defaultMode, Collection modes) { + super(name, defaultMode); + if (modes == null || modes.isEmpty()) { + throw new IllegalArgumentException("Mode value '" + name + "' needs at least one mode"); + } + this.modes = Collections.unmodifiableList(new ArrayList<>(modes)); + if (!this.modes.contains(defaultMode)) { + throw new IllegalArgumentException( + "Default mode '" + defaultMode + "' is not in " + this.modes); + } + } + + public List getModes() { + return modes; + } + + public String getMode() { + return getValue(); + } + + public boolean is(String mode) { + return getValue().equals(mode); + } + + @Override + public void setValue(String newValue) { + if (!modes.contains(newValue)) { + throw new IllegalArgumentException( + "Unknown mode '" + newValue + "' for value '" + getName() + "'"); + } + super.setValue(newValue); + } + + public int getIndex() { + return modes.indexOf(getValue()); + } + + /** + * Selects the next mode, wrapping around at the end. + * + * @return the newly selected mode. + */ + public String next() { + String next = modes.get((getIndex() + 1) % modes.size()); + setValue(next); + return next; + } + + /** + * Selects the previous mode, wrapping around at the start. + * + * @return the newly selected mode. + */ + public String previous() { + String previous = modes.get((getIndex() - 1 + modes.size()) % modes.size()); + setValue(previous); + return previous; + } + + @Override + public JsonElement toJson() { + return new JsonPrimitive(getValue()); + } + + @Override + public void fromJson(JsonElement json) { + if (isString(json)) { + String mode = json.getAsString(); + if (modes.contains(mode)) { + setValue(mode); + } + } + } +} diff --git a/OrangeX/src/main/java/cn/xiaozhou233/orangex/value/Value.java b/OrangeX/src/main/java/cn/xiaozhou233/orangex/value/Value.java new file mode 100644 index 0000000..fbbce0f --- /dev/null +++ b/OrangeX/src/main/java/cn/xiaozhou233/orangex/value/Value.java @@ -0,0 +1,114 @@ +package cn.xiaozhou233.orangex.value; + +import com.google.gson.JsonElement; + +import java.util.Objects; +import java.util.function.Consumer; +import java.util.function.Supplier; + +/** + * A named, typed configuration value owned by a module. + * + *

Values keep their default so a module can be reset to a known state. + * Subclasses add type-specific helpers and decide how the value is encoded for + * configuration storage via {@link #toJson()} and {@link #fromJson(JsonElement)}.

+ * + * @param stored value type. + */ +public abstract class Value { + + private final String name; + private final T defaultValue; + + private T value; + + private Supplier visibility = () -> true; + private Consumer changeListener; + + protected Value(String name, T defaultValue) { + if (name == null || name.trim().isEmpty()) { + throw new IllegalArgumentException("Value name must not be empty"); + } + this.name = name; + this.defaultValue = defaultValue; + this.value = defaultValue; + } + + public String getName() { + return name; + } + + public T getValue() { + return value; + } + + public void setValue(T newValue) { + if (Objects.equals(value, newValue)) { + return; + } + this.value = newValue; + if (changeListener != null) { + changeListener.accept(newValue); + } + } + + public T getDefaultValue() { + return defaultValue; + } + + public boolean isDefault() { + return Objects.equals(value, defaultValue); + } + + public void reset() { + setValue(defaultValue); + } + + /** + * Hides the value from configuration UIs while the condition is false. + */ + public Value visibleWhen(Supplier condition) { + this.visibility = condition == null ? () -> true : condition; + return this; + } + + public boolean isVisible() { + return Boolean.TRUE.equals(visibility.get()); + } + + /** + * Registers a callback invoked after the value actually changes. + */ + public Value onChange(Consumer listener) { + this.changeListener = listener; + return this; + } + + /** + * Encodes the current value for configuration storage. + */ + public abstract JsonElement toJson(); + + /** + * Restores a previously encoded value. Invalid input is ignored so a + * malformed config never leaves a module in an unusable state. + */ + public abstract void fromJson(JsonElement json); + + protected static boolean isBoolean(JsonElement json) { + return json != null && json.isJsonPrimitive() && json.getAsJsonPrimitive().isBoolean(); + } + + protected static boolean isNumber(JsonElement json) { + return json != null && json.isJsonPrimitive() && json.getAsJsonPrimitive().isNumber(); + } + + protected static boolean isString(JsonElement json) { + return json != null && json.isJsonPrimitive() && json.getAsJsonPrimitive().isString(); + } + + @Override + public String toString() { + return name + "=" + value; + } +}