aboutsummaryrefslogtreecommitdiff
path: root/src/main/java/ftbsc
diff options
context:
space:
mode:
author alemi <me@alemi.dev>2023-11-02 16:52:21 +0100
committer alemi <me@alemi.dev>2023-11-02 16:52:21 +0100
commit6201f66c85550b56cd462617a1d46e1bdba99e08 (patch)
tree76aaca5355cf3abe39839daf76d0aab2aaf92df7 /src/main/java/ftbsc
parent78b9ffa352c846ec06729a288f7b69de837a9f92 (diff)
feat: added ruler
Diffstat (limited to 'src/main/java/ftbsc')
-rw-r--r--src/main/java/ftbsc/bscv/Boscovicino.java6
-rw-r--r--src/main/java/ftbsc/bscv/modules/QuickModule.java11
-rw-r--r--src/main/java/ftbsc/bscv/system/Ruler.java85
-rw-r--r--src/main/java/ftbsc/bscv/tools/Keybind.java13
4 files changed, 106 insertions, 9 deletions
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);
+ }
+}