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
This commit is contained in:
xiaozhou233 committed 2026-11-07 18:58:04 +08:00
1 parent 7e27752ffc
commit f6d8c7da2f
6 files changed
+415 -3

No files matched your search

@@ -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<String, Value<?>> 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 extends Value<?>> 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<Value<?>> getValues() {
return Collections.unmodifiableList(new ArrayList<>(valueMap.values()));
}
public List<Value<?>> getVisibleValues() {
List<Value<?>> 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() {
@@ -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<Module> 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());
}
}
}
}
@@ -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<Boolean> {
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());
}
}
}
@@ -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<Integer> {
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());
}
}
}
@@ -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<String> {
private final List<String> modes;
public ModeValue(String name, String defaultMode, String... modes) {
this(name, defaultMode, Arrays.asList(modes));
}
public ModeValue(String name, String defaultMode, Collection<String> 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<String> 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);
}
}
}
}
@@ -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.
*
* <p>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)}.</p>
*
* @param <T> stored value type.
*/
public abstract class Value<T> {
private final String name;
private final T defaultValue;
private T value;
private Supplier<Boolean> visibility = () -> true;
private Consumer<T> 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<T> visibleWhen(Supplier<Boolean> 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<T> onChange(Consumer<T> 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;
}
}