From 6201f66c85550b56cd462617a1d46e1bdba99e08 Mon Sep 17 00:00:00 2001 From: alemi Date: Thu, 2 Nov 2023 16:52:21 +0100 Subject: feat: added ruler --- src/main/java/ftbsc/bscv/Boscovicino.java | 6 ++ src/main/java/ftbsc/bscv/modules/QuickModule.java | 11 +-- src/main/java/ftbsc/bscv/system/Ruler.java | 85 +++++++++++++++++++++++ src/main/java/ftbsc/bscv/tools/Keybind.java | 13 ++++ 4 files changed, 106 insertions(+), 9 deletions(-) create mode 100644 src/main/java/ftbsc/bscv/system/Ruler.java create mode 100644 src/main/java/ftbsc/bscv/tools/Keybind.java (limited to 'src/main') diff --git a/src/main/java/ftbsc/bscv/Boscovicino.java b/src/main/java/ftbsc/bscv/Boscovicino.java index 65c5830..8d6ab67 100644 --- a/src/main/java/ftbsc/bscv/Boscovicino.java +++ b/src/main/java/ftbsc/bscv/Boscovicino.java @@ -8,6 +8,7 @@ import ftbsc.bscv.api.IModule; import ftbsc.bscv.patches.CommandsPatch.CommandsBuiltEvent; import ftbsc.bscv.system.Friends; import ftbsc.bscv.system.ModManager; +import ftbsc.bscv.system.Ruler; import net.minecraft.client.gui.screen.IngameMenuScreen; import net.minecraft.client.gui.widget.button.Button; import net.minecraft.command.CommandSource; @@ -42,6 +43,9 @@ public class Boscovicino implements ICommons { private static Friends friends; public static Friends friends() { return Boscovicino.friends; } + @SuppressWarnings("unused") // it just needs to exist to be used by player + private static Ruler ruler; + public Boscovicino() { FMLJavaModLoadingContext.get().getModEventBus().addListener(this::onSetupComplete); @@ -53,6 +57,8 @@ public class Boscovicino implements ICommons { Boscovicino.modManager.finish(); + Boscovicino.ruler = new Ruler(); + Boscovicino.spec = cfg.build(); ForgeConfigSpec.Builder friendSpec = new ForgeConfigSpec.Builder(); diff --git a/src/main/java/ftbsc/bscv/modules/QuickModule.java b/src/main/java/ftbsc/bscv/modules/QuickModule.java index 009a057..648b020 100644 --- a/src/main/java/ftbsc/bscv/modules/QuickModule.java +++ b/src/main/java/ftbsc/bscv/modules/QuickModule.java @@ -1,6 +1,7 @@ package ftbsc.bscv.modules; import ftbsc.bscv.Boscovicino; +import ftbsc.bscv.tools.Keybind; import net.minecraft.client.settings.KeyBinding; import net.minecraft.client.util.InputMappings; import net.minecraftforge.client.event.InputEvent; @@ -51,7 +52,7 @@ public abstract class QuickModule extends AbstractModule { public QuickModule() { super(); - this.keybind = new KeyBinding(key_name(this.getName()), this.getDefaultKey(), key_category()); + this.keybind = new KeyBinding(Keybind.name(this.getName()), this.getDefaultKey(), Keybind.category()); ClientRegistry.registerKeyBinding(this.keybind); // register a separate subclass on the hook, so that it's always listening @@ -76,12 +77,4 @@ public abstract class QuickModule extends AbstractModule { // ); } - 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); - } - } diff --git a/src/main/java/ftbsc/bscv/system/Ruler.java b/src/main/java/ftbsc/bscv/system/Ruler.java new file mode 100644 index 0000000..bb4c012 --- /dev/null +++ b/src/main/java/ftbsc/bscv/system/Ruler.java @@ -0,0 +1,85 @@ +package ftbsc.bscv.system; + +import ftbsc.bscv.ICommons; +import ftbsc.bscv.tools.Keybind; +import net.minecraft.client.settings.KeyBinding; +import net.minecraft.client.util.InputMappings; +import net.minecraft.util.math.RayTraceResult; +import net.minecraft.util.math.RayTraceResult.Type; +import net.minecraftforge.client.event.InputEvent; +import net.minecraftforge.common.MinecraftForge; +import net.minecraftforge.eventbus.api.SubscribeEvent; +import net.minecraftforge.fml.client.registry.ClientRegistry; + +import static ftbsc.bscv.Boscovicino.log; + +public class Ruler implements ICommons { + + public final KeyBinding keybind; + + public Ruler() { + super(); + + this.keybind = new KeyBinding(Keybind.name("Ruler"), InputMappings.UNKNOWN.getValue(), Keybind.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)); + + // dispatcher.register( + // Commands.literal(this.name.toLowerCase()) + // .then( + // Commands.literal("bind") + // .then( + // Commands.argument("key", StringArgumentType.word()) + // .executes( ctx -> { + // this.keybind.setKey( + // InputMappings.getKey( // TODO it's not this easy! + // StringArgumentType.getString(ctx, "key") + // ) + // ); + // return 1; + // }) + // ) + // ) + // ); + } + + public static void measure() { + RayTraceResult aim = MC.player.pick(1024, 0, false); // will 1024 be enough? + double distance = Math.sqrt(aim.distanceTo(MC.player)); + if (aim.getType() == Type.BLOCK) { + log("distance: %.1fm", distance); + } + } + + // TODO can this be made an util or a global event listener? + private class ToggleHook { + private final KeyBinding key; + private boolean debounce; + // TODO all examples show isPressed() to get a debounced value + // but it seems to be missing? making my own debounce for now + protected ToggleHook(KeyBinding key) { + this.key = key; + this.debounce = false; + } + + private void onInput() { + if (this.debounce) { + if (!this.key.isDown()) { + this.debounce = false; + } + } else { + if (this.key.isDown()) { + Ruler.measure(); + this.debounce = true; + } + } + } + + @SubscribeEvent + public void onKeyPress(InputEvent.KeyInputEvent event) { this.onInput(); } + @SubscribeEvent + public void onKeyPress(InputEvent.MouseInputEvent event) { this.onInput(); } + } +} diff --git a/src/main/java/ftbsc/bscv/tools/Keybind.java b/src/main/java/ftbsc/bscv/tools/Keybind.java new file mode 100644 index 0000000..64fe1ad --- /dev/null +++ b/src/main/java/ftbsc/bscv/tools/Keybind.java @@ -0,0 +1,13 @@ +package ftbsc.bscv.tools; + +import ftbsc.bscv.Boscovicino; + +public class Keybind { + public static String name(String name) { + return String.format("key.%s.%s", Boscovicino.MOD_ID, name); + } + + public static String category() { + return String.format("key.category.%s", Boscovicino.MOD_ID); + } +} -- cgit v1.2.3-56-ga3b1