From 6c0bb42b73abbb2a51627db5c1542a56eac8e302 Mon Sep 17 00:00:00 2001 From: TudbuT Date: Fri, 22 Mar 2024 00:04:56 +0100 Subject: [PATCH] able to runClient, config saving works --- .../java/com/baseband/client/BaseBand.java | 31 +++++++++++++++++++ .../main/java/com/baseband/client/Setup.java | 3 +- .../client/configuration/ConfigHandle.java | 5 +-- .../client/configuration/Configuration.java | 2 -- .../client/configuration/Updater.java | 13 ++++++-- .../client/gui/lib/component/EnumButton.java | 2 +- .../client/gui/lib/component/IntSlider.java | 3 +- .../client/gui/lib/component/Slider.java | 3 +- .../gui/lib/component/ToggleButton.java | 4 +-- .../com/baseband/client/module/Category.java | 15 +++++++-- .../com/baseband/client/module/Module.java | 8 ++++- .../client/module/category/Command.java | 11 +++++++ .../client/module/category/Render.java | 11 +++++++ .../baseband/client/module/command/Test.java | 4 ++- .../client/module/render/ClickGUI.java | 2 ++ 15 files changed, 97 insertions(+), 20 deletions(-) create mode 100644 Client/src/main/java/com/baseband/client/module/category/Command.java create mode 100644 Client/src/main/java/com/baseband/client/module/category/Render.java diff --git a/Client/src/main/java/com/baseband/client/BaseBand.java b/Client/src/main/java/com/baseband/client/BaseBand.java index 42b15be..f6bc9d7 100644 --- a/Client/src/main/java/com/baseband/client/BaseBand.java +++ b/Client/src/main/java/com/baseband/client/BaseBand.java @@ -3,10 +3,14 @@ package com.baseband.client; import com.baseband.client.configuration.Updater; import com.baseband.client.module.Module; import net.minecraft.client.Minecraft; +import net.minecraftforge.fml.common.Mod; +import net.minecraftforge.fml.common.event.FMLPostInitializationEvent; import javax.annotation.Nonnull; +import javax.swing.*; import java.util.ArrayList; +@Mod(modid = "baseband_testing") public class BaseBand { public static BaseBand INSTANCE; { INSTANCE = this; } @@ -20,6 +24,12 @@ public class BaseBand { updater.populate(); } + + @Mod.EventHandler + public void onInit(FMLPostInitializationEvent event) { + onInit(); + } + public void onInit() { mc = Minecraft.getMinecraft(); @@ -28,6 +38,27 @@ public class BaseBand { for (int i = 0; i < modules.length; i++) { modules[i].register(this, mc); } + + for (Updater updater : updaters) { + System.out.println("populating updater " + updater); + updater.populate(); + } + + new Thread(() -> { + while (true) { + for (Updater updater : updaters) { + System.out.println("polling updater " + updater); + updater.poll(); + } + try { + Thread.sleep(1000); + } catch (InterruptedException e) { + throw new RuntimeException(e); + } + } + }).start(); + + new Thread(() -> JOptionPane.showMessageDialog(null, "Gaming")).start(); } @Nonnull diff --git a/Client/src/main/java/com/baseband/client/Setup.java b/Client/src/main/java/com/baseband/client/Setup.java index 5388ec6..5cb8e79 100644 --- a/Client/src/main/java/com/baseband/client/Setup.java +++ b/Client/src/main/java/com/baseband/client/Setup.java @@ -2,7 +2,7 @@ package com.baseband.client; import com.baseband.client.module.Module; import com.baseband.client.module.command.Test; -import de.tudbut.parsing.TCN; +import com.baseband.client.module.render.ClickGUI; /** * @author TudbuT @@ -14,6 +14,7 @@ public class Setup { public final String REGISTRY_FILENAME = "baseband.db"; public final Module[] MODULES = new Module[] { new Test(), + new ClickGUI() }; diff --git a/Client/src/main/java/com/baseband/client/configuration/ConfigHandle.java b/Client/src/main/java/com/baseband/client/configuration/ConfigHandle.java index 6061bea..fc4f414 100644 --- a/Client/src/main/java/com/baseband/client/configuration/ConfigHandle.java +++ b/Client/src/main/java/com/baseband/client/configuration/ConfigHandle.java @@ -25,9 +25,10 @@ public class ConfigHandle { return tcn; } - public void updated() { + public void updated(String id) { for (Updater updater : onUpdate) { - updater.onUpdateConfig(); + if(updater.id.equals(id)) + updater.onUpdateConfig(); } } diff --git a/Client/src/main/java/com/baseband/client/configuration/Configuration.java b/Client/src/main/java/com/baseband/client/configuration/Configuration.java index 0f2d8bb..ee11cd1 100644 --- a/Client/src/main/java/com/baseband/client/configuration/Configuration.java +++ b/Client/src/main/java/com/baseband/client/configuration/Configuration.java @@ -1,11 +1,9 @@ package com.baseband.client.configuration; import com.baseband.client.Setup; -import de.tudbut.parsing.TCN; import de.tudbut.tools.Registry; import java.io.IOException; -import java.util.HashMap; public class Configuration { diff --git a/Client/src/main/java/com/baseband/client/configuration/Updater.java b/Client/src/main/java/com/baseband/client/configuration/Updater.java index b36f3df..deea9fa 100644 --- a/Client/src/main/java/com/baseband/client/configuration/Updater.java +++ b/Client/src/main/java/com/baseband/client/configuration/Updater.java @@ -1,5 +1,7 @@ package com.baseband.client.configuration; +import de.tudbut.tools.ReflectUtil; + import java.lang.reflect.Field; public class Updater { @@ -13,6 +15,7 @@ public class Updater { this.handle = handle; this.o = o; this.field = field; + ReflectUtil.forceAccessible(field); } void onUpdateConfig() { @@ -45,7 +48,7 @@ public class Updater { field.getType() == double.class || field.getType() == boolean.class) { handle.getContent().set(id, field.get(o)); - handle.updated(); + handle.updated(id); } else { throw new IllegalStateException("Unhandled type of field: " + field.getType()); } @@ -63,11 +66,17 @@ public class Updater { } public void populate() { - onUpdateConfig(); + if(handle.getContent().get(id) != null) + onUpdateConfig(); } public Updater name(String id) { this.id = id; return this; } + + @Override + public String toString() { + return this.handle.name + "::" + this.id; + } } diff --git a/Client/src/main/java/com/baseband/client/gui/lib/component/EnumButton.java b/Client/src/main/java/com/baseband/client/gui/lib/component/EnumButton.java index cd0d78a..f50894b 100644 --- a/Client/src/main/java/com/baseband/client/gui/lib/component/EnumButton.java +++ b/Client/src/main/java/com/baseband/client/gui/lib/component/EnumButton.java @@ -21,7 +21,7 @@ public class EnumButton extends Component { int finalI = i; subComponents.add(button = new Button(enums[i].toString(), it -> { handle.getContent().set(field, finalI); - handle.updated(); + handle.updated(field); for (Component component : subComponents) { component.green = false; } diff --git a/Client/src/main/java/com/baseband/client/gui/lib/component/IntSlider.java b/Client/src/main/java/com/baseband/client/gui/lib/component/IntSlider.java index 9981759..808f2f2 100644 --- a/Client/src/main/java/com/baseband/client/gui/lib/component/IntSlider.java +++ b/Client/src/main/java/com/baseband/client/gui/lib/component/IntSlider.java @@ -26,7 +26,6 @@ public class IntSlider extends Component { this.mapper = mapper; this.adder = adder; this.updateMethod = updateMethod; - update(); } public IntSlider(String s, ConfigHandle handle, String field, Function text, int mapper, int adder) { @@ -51,7 +50,7 @@ public class IntSlider extends Component { f = Math.max(Math.min(x, 100), 0) / 100f; handle.getContent().set(field, Math.round(f * mapper + adder)); - handle.updated(); + handle.updated(field); if(!updateMethod.apply(Math.round(f * mapper + adder))) { System.out.println("Something went wrong handling the sliders!"); throw new RuntimeException(); diff --git a/Client/src/main/java/com/baseband/client/gui/lib/component/Slider.java b/Client/src/main/java/com/baseband/client/gui/lib/component/Slider.java index c8c415c..1902cd4 100644 --- a/Client/src/main/java/com/baseband/client/gui/lib/component/Slider.java +++ b/Client/src/main/java/com/baseband/client/gui/lib/component/Slider.java @@ -26,7 +26,6 @@ public class Slider extends Component { this.mapper = mapper; this.adder = adder; this.updateMethod = updateMethod; - update(); } public Slider(String s, ConfigHandle handle, String field, Function text, float mapper, float adder) { @@ -51,7 +50,7 @@ public class Slider extends Component { f = Math.max(Math.min(x, 100), 0) / 100f; handle.getContent().set(field, f * mapper + adder); - handle.updated(); + handle.updated(field); if(!updateMethod.apply(f * mapper + adder)) { System.out.println("Something went wrong handling the sliders!"); throw new RuntimeException(); diff --git a/Client/src/main/java/com/baseband/client/gui/lib/component/ToggleButton.java b/Client/src/main/java/com/baseband/client/gui/lib/component/ToggleButton.java index 4695c76..d96a658 100644 --- a/Client/src/main/java/com/baseband/client/gui/lib/component/ToggleButton.java +++ b/Client/src/main/java/com/baseband/client/gui/lib/component/ToggleButton.java @@ -12,14 +12,12 @@ public class ToggleButton extends Component { this.text = s; this.handle = handle; this.field = field; - update(); } public ToggleButton(String s, ConfigHandle handle, String field, Runnable lambda) { this.lambda = lambda; this.text = s; this.handle = handle; this.field = field; - update(); } @Override @@ -31,7 +29,7 @@ public class ToggleButton extends Component { public synchronized void click(int x, int y, int mouseButton) { super.click(x, y, mouseButton); handle.getContent().set(field, green); - handle.updated(); + handle.updated(field); if(lambda != null) lambda.run(); } diff --git a/Client/src/main/java/com/baseband/client/module/Category.java b/Client/src/main/java/com/baseband/client/module/Category.java index 2917d12..3bdf3d2 100644 --- a/Client/src/main/java/com/baseband/client/module/Category.java +++ b/Client/src/main/java/com/baseband/client/module/Category.java @@ -3,20 +3,24 @@ package com.baseband.client.module; import com.baseband.client.BaseBand; import com.baseband.client.configuration.ConfigHandle; import com.baseband.client.configuration.Configuration; +import com.baseband.client.module.category.Command; +import com.baseband.client.module.category.Render; import com.baseband.client.util.FieldUtil; import com.baseband.client.util.Marker; import javax.annotation.Nonnull; +import java.lang.annotation.Annotation; public enum Category { - COMMAND("Commands"), - RENDER("Render") + COMMAND("Commands", Command.class), + RENDER("Render", Render.class) ; - Category(String name) { + Category(String name, Class annotationClass) { this.name = name; + this.annotationClass = annotationClass; try { handle = Configuration.register("Category/" + name.toLowerCase().replace(' ', '_')); BaseBand.registerUpdater(handle.linkWith(this, FieldUtil.findMarked(getClass(), 1)).name("x")); @@ -28,6 +32,7 @@ public enum Category { } } + private Class annotationClass; private ConfigHandle handle; @Marker(1) public int x; @@ -50,4 +55,8 @@ public enum Category { } return null; } + + public Class getAnnotationClass() { + return annotationClass; + } } diff --git a/Client/src/main/java/com/baseband/client/module/Module.java b/Client/src/main/java/com/baseband/client/module/Module.java index 866d08f..963973e 100644 --- a/Client/src/main/java/com/baseband/client/module/Module.java +++ b/Client/src/main/java/com/baseband/client/module/Module.java @@ -62,6 +62,11 @@ public abstract class Module extends ToggleButton { this.bb = bbInst; this.mc = mc; + for (Category c : Category.values()) { + if(getClass().getDeclaredAnnotation(c.getAnnotationClass()) != null) + category = c; + } + subComponents.clear(); ConfigHandle settings = config("Settings"); @@ -99,7 +104,7 @@ public abstract class Module extends ToggleButton { } handle = settings; - BaseBand.registerUpdater(settings.linkWith(this, FieldUtil.findMarked(getClass(), 1)).name("Enabled")); + BaseBand.registerUpdater(settings.linkWith(this, FieldUtil.findMarked(Module.class, 1)).name("Enabled")); setup(); } @@ -134,6 +139,7 @@ public abstract class Module extends ToggleButton { try { ConfigHandle h = Configuration.register(getID() + "/" + handle); ownedHandles.put(handle, h); + System.out.println("Module " + toString() + " registered config: " + handle); return h; } catch (IllegalAccessException e) { throw new RuntimeException(e); diff --git a/Client/src/main/java/com/baseband/client/module/category/Command.java b/Client/src/main/java/com/baseband/client/module/category/Command.java new file mode 100644 index 0000000..24bdf9f --- /dev/null +++ b/Client/src/main/java/com/baseband/client/module/category/Command.java @@ -0,0 +1,11 @@ +package com.baseband.client.module.category; + +import java.lang.annotation.ElementType; +import java.lang.annotation.Retention; +import java.lang.annotation.RetentionPolicy; +import java.lang.annotation.Target; + +@Target(ElementType.TYPE) +@Retention(RetentionPolicy.RUNTIME) +public @interface Command { +} diff --git a/Client/src/main/java/com/baseband/client/module/category/Render.java b/Client/src/main/java/com/baseband/client/module/category/Render.java new file mode 100644 index 0000000..7fc2c7a --- /dev/null +++ b/Client/src/main/java/com/baseband/client/module/category/Render.java @@ -0,0 +1,11 @@ +package com.baseband.client.module.category; + +import java.lang.annotation.ElementType; +import java.lang.annotation.Retention; +import java.lang.annotation.RetentionPolicy; +import java.lang.annotation.Target; + +@Target(ElementType.TYPE) +@Retention(RetentionPolicy.RUNTIME) +public @interface Render { +} diff --git a/Client/src/main/java/com/baseband/client/module/command/Test.java b/Client/src/main/java/com/baseband/client/module/command/Test.java index 89fd46c..a38e68f 100644 --- a/Client/src/main/java/com/baseband/client/module/command/Test.java +++ b/Client/src/main/java/com/baseband/client/module/command/Test.java @@ -3,17 +3,19 @@ package com.baseband.client.module.command; import com.baseband.client.configuration.annotation.Config; import com.baseband.client.configuration.annotation.Range; import com.baseband.client.module.Module; +import com.baseband.client.module.category.Command; /** * @author TudbuT * * Self-Test command, checks if everything is working */ +@Command public class Test extends Module { @Config("Int Setting") @Range("0..10") - private int intSetting = 5; + public int intSetting = 5; @Override diff --git a/Client/src/main/java/com/baseband/client/module/render/ClickGUI.java b/Client/src/main/java/com/baseband/client/module/render/ClickGUI.java index 48a7333..3ec5a70 100644 --- a/Client/src/main/java/com/baseband/client/module/render/ClickGUI.java +++ b/Client/src/main/java/com/baseband/client/module/render/ClickGUI.java @@ -3,7 +3,9 @@ package com.baseband.client.module.render; import com.baseband.client.configuration.annotation.Config; import com.baseband.client.gui.GuiTTC; import com.baseband.client.module.Module; +import com.baseband.client.module.category.Render; +@Render public class ClickGUI extends Module { @Config("Mouse Fix") public boolean mouseFix;