diff options
Diffstat (limited to 'src/main/java/bscv/module/Module.java')
-rw-r--r-- | src/main/java/bscv/module/Module.java | 154 |
1 files changed, 154 insertions, 0 deletions
diff --git a/src/main/java/bscv/module/Module.java b/src/main/java/bscv/module/Module.java new file mode 100644 index 0000000..c840ebf --- /dev/null +++ b/src/main/java/bscv/module/Module.java @@ -0,0 +1,154 @@ +package bscv.module; + +import com.mojang.brigadier.CommandDispatcher; +import com.mojang.brigadier.arguments.ArgumentType; + +import bscv.BoSCoVicino; +import net.minecraft.command.CommandSource; +import net.minecraft.command.Commands; +import net.minecraftforge.common.ForgeConfigSpec; +import net.minecraftforge.common.MinecraftForge; + +import static bscv.BoSCoVicino.log; + +public abstract class Module { + public enum Group { + SELF, + HUD, + BUILD, + DEFENSE, + VISION, + MOTION, + } + + public final String name; + public final Group group; + public final ForgeConfigSpec.ConfigValue<Boolean> enabled; + + protected Module(String name, Group group, ForgeConfigSpec.Builder builder, CommandDispatcher<CommandSource> dispatcher) { + this.name = name; + this.group = group; + + builder.push(this.name.toLowerCase()); + this.enabled = builder + .comment(String.format("Enables %s", this.name)) + .define("enabled", false); + + dispatcher.register( + Commands.literal(this.name.toLowerCase()) + .then( + Commands.literal("toggle") + .executes(ctx -> { + this.toggle(); + return 1; + }) + ) + .executes(ctx -> { + log(String.format("=[ %s ]%s", this.name, this.enabled.get() ? "+" : "")); + // TODO: print all mod options! + return 1; + }) + ); + } + + + // TODO can I merge these two option into one? Maybe redo with builder pattern? + + public <T extends Enum<T>> ForgeConfigSpec.EnumValue<T> optionEnum( + String name, + String comment, + T fallback, + ArgumentType<T> argument, + Class<T> clazz, + ForgeConfigSpec.Builder builder, + CommandDispatcher<CommandSource> dispatcher + ) { + ForgeConfigSpec.EnumValue<T> conf = builder + .comment(comment) + .defineEnum(name, fallback); + + dispatcher.register( + Commands.literal(this.name.toLowerCase()) + .then( + Commands.literal(name) + .then( + Commands.argument(name, argument) + .executes( ctx -> { + T value = ctx.getArgument(name, clazz); + conf.set(value); + conf.save(); + log(String.format("> %s -> %s", String.join(".", conf.getPath()), conf.get().toString())); + return 1; + })) + .executes(ctx -> { + log(String.format("> %s: %s", String.join(".", conf.getPath()), conf.get().toString())); + return 1; + }) + ) + ); + + return conf; + } + + public <T> ForgeConfigSpec.ConfigValue<T> option( + String name, + String comment, + T fallback, + ArgumentType<T> argument, + Class<T> clazz, + ForgeConfigSpec.Builder builder, + CommandDispatcher<CommandSource> dispatcher + ) { + ForgeConfigSpec.ConfigValue<T> conf = builder + .comment(comment) + .define(name, fallback); + + dispatcher.register( + Commands.literal(this.name.toLowerCase()) + .then( + Commands.literal(name) + .then( + Commands.argument(name, argument) + .executes( ctx -> { + T value = ctx.getArgument(name, clazz); + conf.set(value); + conf.save(); + log(String.format("> %s -> %s", String.join(".", conf.getPath()), conf.get().toString())); + return 1; + })) + .executes(ctx -> { + log(String.format("> %s: %s", name, conf.get().toString())); + return 1; + }) + ) + ); + + return conf; + } + + 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 enabled", this.name)); + BoSCoVicino.LOGGER.info(String.format("%s enabled", this.name)); + } + + public final void disable() { + MinecraftForge.EVENT_BUS.unregister(this); + this.enabled.set(false); + // this.enabled.save(); + this.onDisabled(); + log(String.format("%s disabled", this.name)); + BoSCoVicino.LOGGER.info(String.format("%s disabled", this.name)); + } +} |