From 3be09e93843986e9ccf056d712f0cd1bedb0fd6e Mon Sep 17 00:00:00 2001 From: ftbsc Date: Tue, 28 Feb 2023 18:18:06 +0100 Subject: chore: AbstractModule --- .../java/ftbsc/bscv/modules/AbstractModule.java | 106 +++++++++++++++++++++ src/main/java/ftbsc/bscv/modules/Module.java | 106 --------------------- 2 files changed, 106 insertions(+), 106 deletions(-) create mode 100644 src/main/java/ftbsc/bscv/modules/AbstractModule.java delete mode 100644 src/main/java/ftbsc/bscv/modules/Module.java (limited to 'src') diff --git a/src/main/java/ftbsc/bscv/modules/AbstractModule.java b/src/main/java/ftbsc/bscv/modules/AbstractModule.java new file mode 100644 index 0000000..b17f3b3 --- /dev/null +++ b/src/main/java/ftbsc/bscv/modules/AbstractModule.java @@ -0,0 +1,106 @@ +package ftbsc.bscv.modules; + +import com.mojang.brigadier.CommandDispatcher; +import com.mojang.brigadier.arguments.BoolArgumentType; + +import net.minecraft.command.CommandSource; +import net.minecraft.command.Commands; +import net.minecraftforge.common.ForgeConfigSpec; +import net.minecraftforge.common.MinecraftForge; + +import static ftbsc.bscv.BoSCoVicino.log; + +public abstract class AbstractModule { + public enum Group { + SELF, + HUD, + BUILD, + DEFENSE, + VISION, + MOTION, + } + + public final String name; + public final Group group; + + protected ForgeConfigSpec.ConfigValue enabled; + + public void initialize(ForgeConfigSpec.Builder builder, CommandDispatcher dispatcher) { + builder.push(this.name.toLowerCase()); + this.enabled = builder + .comment(String.format("Enables %s", this.name)) + .define("enabled", false); + + // TODO: can this be made an util or moved somewhere else? + dispatcher.register( + Commands.literal(this.name.toLowerCase()) + .then( + Commands.literal("toggle") + .then( + Commands.argument("state", BoolArgumentType.bool()) + .executes( ctx -> { + boolean value = ctx.getArgument("state", Boolean.class); + if (this.enabled.get() ^ value) { // must change + if (value) { this.enable(); } + else { this.disable(); } + } + return 1; + }) + ) + .executes(ctx -> { + this.toggle(); + return 1; + }) + ) + .executes(ctx -> { + log(String.format("%s [%s]", this.name, this.enabled.get() ? "ON" : "OFF")); + // TODO: print all mod options! + return 1; + }) + ); + + this.register(builder, dispatcher); + + builder.pop(); + } + + public abstract void register(ForgeConfigSpec.Builder builder, CommandDispatcher dispatcher); + + protected AbstractModule(ForgeConfigSpec.Builder builder, CommandDispatcher dispatcher) { + // TODO can this be done in a less magic way? + String[] pkg = this.getClass().getPackage().getName().split("."); + this.group = Group.valueOf(pkg[pkg.length-1].toUpperCase()); + this.name = this.getClass().getName(); + + this.initialize(builder, dispatcher); + } + + public AbstractModule done(ForgeConfigSpec.Builder builder) { + builder.pop(); + return this; + } + + protected void onEnabled() {} + protected void onDisabled() {} + + public final void toggle() { + if (this.enabled.get()) this.disable(); + else this.enable(); + } + + public final void enable() { + MinecraftForge.EVENT_BUS.register(this); + this.enabled.set(true); + // this.enabled.save(); + this.onEnabled(); + log(String.format("%s ON", this.name)); + } + + public final void disable() { + MinecraftForge.EVENT_BUS.unregister(this); + this.enabled.set(false); + // this.enabled.save(); + this.onDisabled(); + log(String.format("%s OFF", this.name)); + } +} diff --git a/src/main/java/ftbsc/bscv/modules/Module.java b/src/main/java/ftbsc/bscv/modules/Module.java deleted file mode 100644 index 0f73a7a..0000000 --- a/src/main/java/ftbsc/bscv/modules/Module.java +++ /dev/null @@ -1,106 +0,0 @@ -package ftbsc.bscv.modules; - -import com.mojang.brigadier.CommandDispatcher; -import com.mojang.brigadier.arguments.BoolArgumentType; - -import net.minecraft.command.CommandSource; -import net.minecraft.command.Commands; -import net.minecraftforge.common.ForgeConfigSpec; -import net.minecraftforge.common.MinecraftForge; - -import static ftbsc.bscv.BoSCoVicino.log; - -public abstract class Module { - public enum Group { - SELF, - HUD, - BUILD, - DEFENSE, - VISION, - MOTION, - } - - public final String name; - public final Group group; - - protected ForgeConfigSpec.ConfigValue enabled; - - public void initialize(ForgeConfigSpec.Builder builder, CommandDispatcher dispatcher) { - builder.push(this.name.toLowerCase()); - this.enabled = builder - .comment(String.format("Enables %s", this.name)) - .define("enabled", false); - - // TODO: can this be made an util or moved somewhere else? - dispatcher.register( - Commands.literal(this.name.toLowerCase()) - .then( - Commands.literal("toggle") - .then( - Commands.argument("state", BoolArgumentType.bool()) - .executes( ctx -> { - boolean value = ctx.getArgument("state", Boolean.class); - if (this.enabled.get() ^ value) { // must change - if (value) { this.enable(); } - else { this.disable(); } - } - return 1; - }) - ) - .executes(ctx -> { - this.toggle(); - return 1; - }) - ) - .executes(ctx -> { - log(String.format("%s [%s]", this.name, this.enabled.get() ? "ON" : "OFF")); - // TODO: print all mod options! - return 1; - }) - ); - - this.register(builder, dispatcher); - - builder.pop(); - } - - public abstract void register(ForgeConfigSpec.Builder builder, CommandDispatcher dispatcher); - - protected Module(ForgeConfigSpec.Builder builder, CommandDispatcher dispatcher) { - // TODO can this be done in a less magic way? - String[] pkg = this.getClass().getPackage().getName().split("."); - this.group = Group.valueOf(pkg[pkg.length-1].toUpperCase()); - this.name = this.getClass().getName(); - - this.initialize(builder, dispatcher); - } - - public Module done(ForgeConfigSpec.Builder builder) { - builder.pop(); - return this; - } - - protected void onEnabled() {} - protected void onDisabled() {} - - public final void toggle() { - if (this.enabled.get()) this.disable(); - else this.enable(); - } - - public final void enable() { - MinecraftForge.EVENT_BUS.register(this); - this.enabled.set(true); - // this.enabled.save(); - this.onEnabled(); - log(String.format("%s ON", this.name)); - } - - public final void disable() { - MinecraftForge.EVENT_BUS.unregister(this); - this.enabled.set(false); - // this.enabled.save(); - this.onDisabled(); - log(String.format("%s OFF", this.name)); - } -} -- cgit v1.2.3-56-ga3b1