diff options
-rw-r--r-- | src/main/java/co/fantabos/bscv/module/QuickModule.java | 61 |
1 files changed, 61 insertions, 0 deletions
diff --git a/src/main/java/co/fantabos/bscv/module/QuickModule.java b/src/main/java/co/fantabos/bscv/module/QuickModule.java new file mode 100644 index 0000000..4942476 --- /dev/null +++ b/src/main/java/co/fantabos/bscv/module/QuickModule.java @@ -0,0 +1,61 @@ +package co.fantabos.bscv.module; + +import com.mojang.brigadier.CommandDispatcher; + +import net.minecraft.client.settings.KeyBinding; +import net.minecraft.command.CommandSource; +import net.minecraftforge.client.event.InputEvent; +import net.minecraftforge.common.ForgeConfigSpec; +import net.minecraftforge.common.MinecraftForge; +import net.minecraftforge.eventbus.api.SubscribeEvent; +import net.minecraftforge.fml.client.registry.ClientRegistry; +import co.fantabos.bscv.BoSCoVicino; + +// TODO rename +public class QuickModule extends Module { + + private class ToggleHook { + private final KeyBinding key; + private final Module mod; + protected ToggleHook(KeyBinding key, Module mod) { + this.key = key; + this.mod = mod; + } + + @SubscribeEvent + public void onKeyPress(InputEvent.KeyInputEvent event) { + if (this.key.isDown()) { + this.mod.toggle(); + } + } + + @SubscribeEvent + public void onKeyPress(InputEvent.MouseInputEvent event) { + if (this.key.isDown()) { + this.mod.toggle(); + + } + } + } + + public final KeyBinding keybind; + + public QuickModule(String name, Group group, int default_key, ForgeConfigSpec.Builder builder, CommandDispatcher<CommandSource> dispatcher) { + super(name, group, builder, dispatcher); + + this.keybind = new KeyBinding(key_name(name), default_key, key_category()); + ClientRegistry.registerKeyBinding(this.keybind); + + // register a separate subclass on the hook, so that it's always listening + MinecraftForge.EVENT_BUS.register(new ToggleHook(this.keybind, this)); + } + + private static String key_name(String name) { + return String.format("key.%s.%s", BoSCoVicino.MOD_ID, name); + } + + private static String key_category() { + return String.format("key.category.%s", BoSCoVicino.MOD_ID); + } + +} |