mirror of
https://github.com/xiaozhou233/OrangeX-Client.git
synced 2026-12-05 21:15:53 +08:00
feat: module system - option system, config persistence, key bindings
- Add generic Option base class + 6 implementations (Boolean/Double/Int/Mode/Color/String) - Add OptionSerializer for batch serialization - Add ModuleManager for module registration/querying/config persistence - Add KeyBindHandler as dedicated key event listener - Add ModuleConfig for config file I/O - Fix setEnabled: return early if EventBus register fails, preventing orphaned onEnable - Module now supports addOption for option integration - OrangeX integration with ModuleManager startup flow - TestModule demo with all option types
This commit is contained in:
1 parent
767ed60b1f
commit
8a806a02e1
14 files changed
+508
-10
No files matched your search
@@ -42,3 +42,4 @@ bin/
|
||||
.DS_Store
|
||||
|
||||
Minecraft-1.8.8-runtime/*
|
||||
AGENTS.md
|
||||
@@ -3,6 +3,7 @@ package cn.xiaozhou233.orangex;
|
||||
import cn.xiaozhou233.orangex.font.FontManager;
|
||||
import cn.xiaozhou233.orangex.font.StbFontManager;
|
||||
import cn.xiaozhou233.orangex.mixin.MixinManager;
|
||||
import cn.xiaozhou233.orangex.module.ModuleManager;
|
||||
import lombok.Getter;
|
||||
import org.greenrobot.eventbus.EventBus;
|
||||
|
||||
@@ -12,6 +13,7 @@ public class OrangeX {
|
||||
public MixinManager mixinManager = new MixinManager();
|
||||
FontManager fontManager = new FontManager();
|
||||
StbFontManager stbFontManager = new StbFontManager();
|
||||
ModuleManager moduleManager = new ModuleManager();
|
||||
private final EventBus eventBus = EventBus.builder()
|
||||
.logNoSubscriberMessages(false)
|
||||
.logSubscriberExceptions(false)
|
||||
@@ -25,12 +27,15 @@ public class OrangeX {
|
||||
|
||||
stbFontManager.loadFonts();
|
||||
mixinManager.start();
|
||||
moduleManager.registerModules();
|
||||
moduleManager.loadConfig();
|
||||
|
||||
System.out.println("OrangeX started.");
|
||||
}
|
||||
|
||||
public void stop() {
|
||||
System.out.println("OrangeX stopping...");
|
||||
moduleManager.saveConfig();
|
||||
System.out.println("OrangeX stopped.");
|
||||
}
|
||||
|
||||
|
||||
@@ -1,20 +1,25 @@
|
||||
package cn.xiaozhou233.orangex.module;
|
||||
|
||||
import cn.xiaozhou233.orangex.OrangeX;
|
||||
import cn.xiaozhou233.orangex.module.option.Option;
|
||||
import lombok.Getter;
|
||||
import lombok.Setter;
|
||||
import net.minecraft.client.Minecraft;
|
||||
import org.lwjgl.input.Keyboard;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
@Getter
|
||||
public class Module {
|
||||
protected static final Minecraft mc = Minecraft.getMinecraft();
|
||||
protected final String name;
|
||||
protected final String description;
|
||||
protected final ModuleCategory category;
|
||||
@Setter
|
||||
protected int key = Keyboard.KEY_NONE;
|
||||
protected boolean enabled = false;
|
||||
|
||||
|
||||
protected final List<Option<?>> options = new ArrayList<>();
|
||||
|
||||
public Module(String name, String description, ModuleCategory category) {
|
||||
this.name = name;
|
||||
@@ -27,6 +32,10 @@ public class Module {
|
||||
this.key = key;
|
||||
}
|
||||
|
||||
public void addOption(Option<?> option) {
|
||||
options.add(option);
|
||||
}
|
||||
|
||||
protected void onEnable() {
|
||||
System.out.println("[OrangeX] Enable " + name);
|
||||
}
|
||||
@@ -38,17 +47,19 @@ public class Module {
|
||||
public void setEnabled(boolean setEnabled) {
|
||||
if (this.enabled == setEnabled) return;
|
||||
|
||||
this.enabled = setEnabled;
|
||||
|
||||
if (this.enabled) {
|
||||
if (OrangeX.getInstance().getEventBus().isRegistered(this))
|
||||
if (setEnabled) {
|
||||
try {
|
||||
OrangeX.getInstance().getEventBus().register(this);
|
||||
} catch (Exception e) {
|
||||
return;
|
||||
try { OrangeX.getInstance().getEventBus().register(this); } catch (Exception ignored) {}
|
||||
}
|
||||
this.enabled = true;
|
||||
onEnable();
|
||||
} else {
|
||||
if (!OrangeX.getInstance().getEventBus().isRegistered(this))
|
||||
return;
|
||||
try { OrangeX.getInstance().getEventBus().unregister(this); } catch (Exception ignored) {}
|
||||
try {
|
||||
OrangeX.getInstance().getEventBus().unregister(this);
|
||||
} catch (Exception ignored) {}
|
||||
this.enabled = false;
|
||||
onDisable();
|
||||
}
|
||||
}
|
||||
|
||||
@@ -0,0 +1,78 @@
|
||||
package cn.xiaozhou233.orangex.module;
|
||||
|
||||
import cn.xiaozhou233.orangex.OrangeX;
|
||||
import cn.xiaozhou233.orangex.event.impl.EventKey;
|
||||
import cn.xiaozhou233.orangex.module.config.ModuleConfig;
|
||||
import cn.xiaozhou233.orangex.module.impl.TestModule;
|
||||
import lombok.Getter;
|
||||
import org.greenrobot.eventbus.Subscribe;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
import java.util.concurrent.CopyOnWriteArrayList;
|
||||
|
||||
@Getter
|
||||
public class ModuleManager {
|
||||
public static String userHome = System.getProperty("user.home");
|
||||
private final List<Module> modules = new CopyOnWriteArrayList<>();
|
||||
private final ModuleConfig config;
|
||||
|
||||
public ModuleManager() {
|
||||
config = new ModuleConfig(userHome + "/.orangex/profile.json");
|
||||
}
|
||||
|
||||
public void addModule(Module module) {
|
||||
modules.add(module);
|
||||
}
|
||||
|
||||
public void registerModules() {
|
||||
OrangeX.getInstance().getEventBus().register(this);
|
||||
addModule(new TestModule());
|
||||
}
|
||||
|
||||
public List<Module> getModulesByCategory(ModuleCategory category) {
|
||||
List<Module> result = new ArrayList<>();
|
||||
for (Module module : modules) {
|
||||
if (module.getCategory() == category) {
|
||||
result.add(module);
|
||||
}
|
||||
}
|
||||
return result;
|
||||
}
|
||||
|
||||
@SuppressWarnings("unchecked")
|
||||
public <T extends Module> T getModule(Class<T> clazz) {
|
||||
for (Module module : modules) {
|
||||
if (module.getClass() == clazz) {
|
||||
return (T) module;
|
||||
}
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
public Module getModule(String name) {
|
||||
for (Module module : modules) {
|
||||
if (module.getName().equalsIgnoreCase(name)) {
|
||||
return module;
|
||||
}
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
public void saveConfig() {
|
||||
config.save(modules);
|
||||
}
|
||||
|
||||
public void loadConfig() {
|
||||
config.load(modules);
|
||||
}
|
||||
|
||||
@Subscribe
|
||||
public void onKey(EventKey event) {
|
||||
for (Module module : OrangeX.getInstance().getModuleManager().getModules()) {
|
||||
if (module.getKey() == event.getKey()) {
|
||||
module.toggle();
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,78 @@
|
||||
package cn.xiaozhou233.orangex.module.config;
|
||||
|
||||
import cn.xiaozhou233.orangex.module.Module;
|
||||
import cn.xiaozhou233.orangex.module.option.OptionSerializer;
|
||||
import com.google.gson.*;
|
||||
|
||||
import java.io.*;
|
||||
import java.nio.charset.StandardCharsets;
|
||||
import java.nio.file.Files;
|
||||
import java.util.List;
|
||||
|
||||
public class ModuleConfig {
|
||||
private final File configFile;
|
||||
private final Gson gson;
|
||||
|
||||
public ModuleConfig(String path) {
|
||||
this.configFile = new File(path);
|
||||
this.gson = new GsonBuilder().setPrettyPrinting().create();
|
||||
}
|
||||
|
||||
public void save(List<Module> modules) {
|
||||
JsonObject config = new JsonObject();
|
||||
JsonArray modulesArray = new JsonArray();
|
||||
for (Module module : modules) {
|
||||
JsonObject moduleObj = new JsonObject();
|
||||
moduleObj.addProperty("name", module.getName());
|
||||
moduleObj.addProperty("enabled", module.isEnabled());
|
||||
moduleObj.addProperty("key", module.getKey());
|
||||
if (!module.getOptions().isEmpty()) {
|
||||
moduleObj.add("options", OptionSerializer.toJson(module.getOptions()));
|
||||
}
|
||||
modulesArray.add(moduleObj);
|
||||
}
|
||||
config.add("modules", modulesArray);
|
||||
configFile.getParentFile().mkdirs();
|
||||
try (Writer writer = new OutputStreamWriter(Files.newOutputStream(configFile.toPath()), StandardCharsets.UTF_8)) {
|
||||
gson.toJson(config, writer);
|
||||
} catch (IOException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
|
||||
public void load(List<Module> modules) {
|
||||
if (!configFile.exists()) return;
|
||||
try (Reader reader = new InputStreamReader(Files.newInputStream(configFile.toPath()), StandardCharsets.UTF_8)) {
|
||||
JsonObject config = gson.fromJson(reader, JsonObject.class);
|
||||
if (config == null) return;
|
||||
JsonArray modulesArray = config.getAsJsonArray("modules");
|
||||
if (modulesArray == null) return;
|
||||
for (JsonElement element : modulesArray) {
|
||||
JsonObject moduleObj = element.getAsJsonObject();
|
||||
String name = moduleObj.get("name").getAsString();
|
||||
Module module = findModule(modules, name);
|
||||
if (module == null) continue;
|
||||
if (moduleObj.has("enabled") && moduleObj.get("enabled").getAsBoolean()) {
|
||||
module.setEnabled(true);
|
||||
}
|
||||
if (moduleObj.has("key")) {
|
||||
module.setKey(moduleObj.get("key").getAsInt());
|
||||
}
|
||||
if (moduleObj.has("options")) {
|
||||
OptionSerializer.fromJson(moduleObj.getAsJsonObject("options"), module.getOptions());
|
||||
}
|
||||
}
|
||||
} catch (IOException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
|
||||
private Module findModule(List<Module> modules, String name) {
|
||||
for (Module module : modules) {
|
||||
if (module.getName().equalsIgnoreCase(name)) {
|
||||
return module;
|
||||
}
|
||||
}
|
||||
return null;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,37 @@
|
||||
package cn.xiaozhou233.orangex.module.impl;
|
||||
|
||||
import cn.xiaozhou233.orangex.module.Module;
|
||||
import cn.xiaozhou233.orangex.module.ModuleCategory;
|
||||
import cn.xiaozhou233.orangex.module.option.impl.*;
|
||||
import org.greenrobot.eventbus.Subscribe;
|
||||
import org.lwjgl.input.Keyboard;
|
||||
|
||||
import java.awt.event.KeyEvent;
|
||||
|
||||
public class TestModule extends Module {
|
||||
public TestModule() {
|
||||
super("Test", "A test module", ModuleCategory.MISC, Keyboard.KEY_SPACE);
|
||||
|
||||
addOption(new BooleanOption("boolean", "Boolean option", this, true));
|
||||
addOption(new DoubleOption("double", "Double slider", this, 0.5, 0.0, 1.0, 0.05, 2));
|
||||
addOption(new IntOption("int", "Int slider", this, 10, 1, 100, 5));
|
||||
addOption(new ModeOption("mode", "Mode selector", this, "A", "A", "B", "C"));
|
||||
addOption(new ColorOption("color", "ARGB color", this, 0xFFFF0000));
|
||||
addOption(new StringOption("string", "Text input", this, "hello", 32));
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void onEnable() {
|
||||
System.out.println("[OrangeX] TestModule enabled");
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void onDisable() {
|
||||
System.out.println("[OrangeX] TestModule disabled");
|
||||
}
|
||||
|
||||
@Subscribe
|
||||
public void onKey(KeyEvent event) {
|
||||
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,33 @@
|
||||
package cn.xiaozhou233.orangex.module.option;
|
||||
|
||||
import cn.xiaozhou233.orangex.module.Module;
|
||||
import com.google.gson.JsonElement;
|
||||
import lombok.Getter;
|
||||
|
||||
@Getter
|
||||
public abstract class Option<T> {
|
||||
protected final String name;
|
||||
protected final String description;
|
||||
protected final Module parent;
|
||||
protected T value;
|
||||
protected final T defaultValue;
|
||||
|
||||
public Option(String name, String description, Module parent, T defaultValue) {
|
||||
this.name = name;
|
||||
this.description = description;
|
||||
this.parent = parent;
|
||||
this.defaultValue = defaultValue;
|
||||
this.value = defaultValue;
|
||||
}
|
||||
|
||||
public void setValue(T value) {
|
||||
this.value = value;
|
||||
}
|
||||
|
||||
public void reset() {
|
||||
this.value = defaultValue;
|
||||
}
|
||||
|
||||
public abstract JsonElement serialize();
|
||||
public abstract void deserialize(JsonElement element);
|
||||
}
|
||||
@@ -0,0 +1,26 @@
|
||||
package cn.xiaozhou233.orangex.module.option;
|
||||
|
||||
import com.google.gson.JsonElement;
|
||||
import com.google.gson.JsonObject;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
public class OptionSerializer {
|
||||
public static JsonObject toJson(List<Option<?>> options) {
|
||||
JsonObject json = new JsonObject();
|
||||
for (Option<?> option : options) {
|
||||
json.add(option.getName(), option.serialize());
|
||||
}
|
||||
return json;
|
||||
}
|
||||
|
||||
public static void fromJson(JsonObject json, List<Option<?>> options) {
|
||||
if (json == null) return;
|
||||
for (Option<?> option : options) {
|
||||
JsonElement element = json.get(option.getName());
|
||||
if (element != null) {
|
||||
option.deserialize(element);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,22 @@
|
||||
package cn.xiaozhou233.orangex.module.option.impl;
|
||||
|
||||
import cn.xiaozhou233.orangex.module.Module;
|
||||
import cn.xiaozhou233.orangex.module.option.Option;
|
||||
import com.google.gson.JsonElement;
|
||||
import com.google.gson.JsonPrimitive;
|
||||
|
||||
public class BooleanOption extends Option<Boolean> {
|
||||
public BooleanOption(String name, String description, Module parent, Boolean defaultValue) {
|
||||
super(name, description, parent, defaultValue);
|
||||
}
|
||||
|
||||
@Override
|
||||
public JsonElement serialize() {
|
||||
return new JsonPrimitive(value);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void deserialize(JsonElement element) {
|
||||
this.value = element.getAsBoolean();
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,27 @@
|
||||
package cn.xiaozhou233.orangex.module.option.impl;
|
||||
|
||||
import cn.xiaozhou233.orangex.module.Module;
|
||||
import cn.xiaozhou233.orangex.module.option.Option;
|
||||
import com.google.gson.JsonElement;
|
||||
import com.google.gson.JsonPrimitive;
|
||||
|
||||
public class ColorOption extends Option<Integer> {
|
||||
public ColorOption(String name, String description, Module parent, Integer defaultValue) {
|
||||
super(name, description, parent, defaultValue);
|
||||
}
|
||||
|
||||
@Override
|
||||
public JsonElement serialize() {
|
||||
return new JsonPrimitive(String.format("0x%08X", value));
|
||||
}
|
||||
|
||||
@Override
|
||||
public void deserialize(JsonElement element) {
|
||||
String raw = element.getAsString();
|
||||
if (raw.startsWith("0x") || raw.startsWith("0X")) {
|
||||
this.value = (int) Long.parseLong(raw.substring(2), 16);
|
||||
} else {
|
||||
this.value = Integer.parseUnsignedInt(raw);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,46 @@
|
||||
package cn.xiaozhou233.orangex.module.option.impl;
|
||||
|
||||
import cn.xiaozhou233.orangex.module.Module;
|
||||
import cn.xiaozhou233.orangex.module.option.Option;
|
||||
import com.google.gson.JsonElement;
|
||||
import com.google.gson.JsonPrimitive;
|
||||
import lombok.Getter;
|
||||
|
||||
@Getter
|
||||
public class DoubleOption extends Option<Double> {
|
||||
private final double min;
|
||||
private final double max;
|
||||
private final double step;
|
||||
private final int precision;
|
||||
|
||||
public DoubleOption(String name, String description, Module parent,
|
||||
Double defaultValue, double min, double max, double step, int precision) {
|
||||
super(name, description, parent, clamp(defaultValue, min, max));
|
||||
this.min = min;
|
||||
this.max = max;
|
||||
this.step = step;
|
||||
this.precision = precision;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void setValue(Double value) {
|
||||
double clamped = clamp(value, min, max);
|
||||
double stepped = Math.round(clamped / step) * step;
|
||||
double factor = Math.pow(10, precision);
|
||||
this.value = Math.round(stepped * factor) / factor;
|
||||
}
|
||||
|
||||
@Override
|
||||
public JsonElement serialize() {
|
||||
return new JsonPrimitive(value);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void deserialize(JsonElement element) {
|
||||
setValue(element.getAsDouble());
|
||||
}
|
||||
|
||||
private static double clamp(double v, double min, double max) {
|
||||
return Math.min(max, Math.max(min, v));
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,41 @@
|
||||
package cn.xiaozhou233.orangex.module.option.impl;
|
||||
|
||||
import cn.xiaozhou233.orangex.module.Module;
|
||||
import cn.xiaozhou233.orangex.module.option.Option;
|
||||
import com.google.gson.JsonElement;
|
||||
import com.google.gson.JsonPrimitive;
|
||||
import lombok.Getter;
|
||||
|
||||
@Getter
|
||||
public class IntOption extends Option<Integer> {
|
||||
private final int min;
|
||||
private final int max;
|
||||
private final int step;
|
||||
|
||||
public IntOption(String name, String description, Module parent,
|
||||
Integer defaultValue, int min, int max, int step) {
|
||||
super(name, description, parent, clamp(defaultValue, min, max));
|
||||
this.min = min;
|
||||
this.max = max;
|
||||
this.step = step;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void setValue(Integer value) {
|
||||
this.value = clamp(value, min, max);
|
||||
}
|
||||
|
||||
@Override
|
||||
public JsonElement serialize() {
|
||||
return new JsonPrimitive(value);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void deserialize(JsonElement element) {
|
||||
setValue(element.getAsInt());
|
||||
}
|
||||
|
||||
private static int clamp(int v, int min, int max) {
|
||||
return Math.min(max, Math.max(min, v));
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,58 @@
|
||||
package cn.xiaozhou233.orangex.module.option.impl;
|
||||
|
||||
import cn.xiaozhou233.orangex.module.Module;
|
||||
import cn.xiaozhou233.orangex.module.option.Option;
|
||||
import com.google.gson.JsonElement;
|
||||
import com.google.gson.JsonPrimitive;
|
||||
import lombok.Getter;
|
||||
|
||||
import java.util.Arrays;
|
||||
|
||||
@Getter
|
||||
public class ModeOption extends Option<String> {
|
||||
private final String[] modes;
|
||||
|
||||
public ModeOption(String name, String description, Module parent,
|
||||
String defaultValue, String... modes) {
|
||||
super(name, description, parent, defaultValue);
|
||||
this.modes = modes;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void setValue(String value) {
|
||||
for (String mode : modes) {
|
||||
if (mode.equalsIgnoreCase(value)) {
|
||||
this.value = mode;
|
||||
return;
|
||||
}
|
||||
}
|
||||
this.value = modes.length > 0 ? modes[0] : value;
|
||||
}
|
||||
|
||||
public int getIndex() {
|
||||
for (int i = 0; i < modes.length; i++) {
|
||||
if (modes[i].equals(value)) return i;
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
|
||||
public void cycle() {
|
||||
int next = (getIndex() + 1) % modes.length;
|
||||
this.value = modes[next];
|
||||
}
|
||||
|
||||
public void cycleReverse() {
|
||||
int prev = (getIndex() - 1 + modes.length) % modes.length;
|
||||
this.value = modes[prev];
|
||||
}
|
||||
|
||||
@Override
|
||||
public JsonElement serialize() {
|
||||
return new JsonPrimitive(value);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void deserialize(JsonElement element) {
|
||||
setValue(element.getAsString());
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,35 @@
|
||||
package cn.xiaozhou233.orangex.module.option.impl;
|
||||
|
||||
import cn.xiaozhou233.orangex.module.Module;
|
||||
import cn.xiaozhou233.orangex.module.option.Option;
|
||||
import com.google.gson.JsonElement;
|
||||
import com.google.gson.JsonPrimitive;
|
||||
|
||||
public class StringOption extends Option<String> {
|
||||
private final int maxLength;
|
||||
|
||||
public StringOption(String name, String description, Module parent,
|
||||
String defaultValue, int maxLength) {
|
||||
super(name, description, parent, defaultValue);
|
||||
this.maxLength = maxLength;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void setValue(String value) {
|
||||
if (maxLength > 0 && value.length() > maxLength) {
|
||||
this.value = value.substring(0, maxLength);
|
||||
} else {
|
||||
this.value = value;
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public JsonElement serialize() {
|
||||
return new JsonPrimitive(value);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void deserialize(JsonElement element) {
|
||||
setValue(element.getAsString());
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user