From 5e0ec6d4aa35a5563f1f4f3e925fe84e4460bf6e Mon Sep 17 00:00:00 2001 From: alemidev Date: Mon, 30 Jan 2023 23:35:32 +0100 Subject: feat: added QuickModule with a toggle keybind awful name but eeehh --- .../java/co/fantabos/bscv/module/QuickModule.java | 61 ++++++++++++++++++++++ 1 file changed, 61 insertions(+) create mode 100644 src/main/java/co/fantabos/bscv/module/QuickModule.java 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 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); + } + +} -- cgit v1.2.3-56-ga3b1