From e5ec40b8c28d1eba9019e648900c8f4c4225956e Mon Sep 17 00:00:00 2001 From: PlexSheep Date: Sun, 12 Nov 2023 16:59:25 +0100 Subject: [PATCH] utils --- src/client/java/de/cscherr/mcpht/Fly.java | 44 +++++-------------- src/client/java/de/cscherr/mcpht/XRay.java | 37 ++++++---------- .../java/de/cscherr/mcpht/util/Hack.java | 39 ++++++++++++++++ .../java/de/cscherr/mcpht/util/MaybeKey.java | 27 ++++++++++++ .../de/cscherr/mcpht/util/event/Event.java | 9 ++++ .../de/cscherr/mcpht/util/event/Listener.java | 6 +++ .../mcpht/util/event/UpdateListener.java | 25 +++++++++++ 7 files changed, 132 insertions(+), 55 deletions(-) create mode 100644 src/client/java/de/cscherr/mcpht/util/Hack.java create mode 100644 src/client/java/de/cscherr/mcpht/util/MaybeKey.java create mode 100644 src/client/java/de/cscherr/mcpht/util/event/Event.java create mode 100644 src/client/java/de/cscherr/mcpht/util/event/Listener.java create mode 100644 src/client/java/de/cscherr/mcpht/util/event/UpdateListener.java diff --git a/src/client/java/de/cscherr/mcpht/Fly.java b/src/client/java/de/cscherr/mcpht/Fly.java index fab92a5..1319d1a 100644 --- a/src/client/java/de/cscherr/mcpht/Fly.java +++ b/src/client/java/de/cscherr/mcpht/Fly.java @@ -1,41 +1,21 @@ package de.cscherr.mcpht; -import net.fabricmc.fabric.api.client.event.lifecycle.v1.ClientTickEvents; -import net.fabricmc.fabric.api.client.keybinding.v1.KeyBindingHelper; -import net.minecraft.block.Block; -import net.minecraft.block.BlockState; -import net.minecraft.block.Blocks; + +import de.cscherr.mcpht.util.Hack; +import de.cscherr.mcpht.util.MaybeKey; import net.minecraft.client.option.KeyBinding; -import net.minecraft.client.util.InputUtil; -import org.lwjgl.glfw.GLFW; -import java.util.ArrayList; -import java.util.List; - -public class Fly { - - public boolean enable = true; - private static KeyBinding toggleKey; - - public void init() { - toggleKey = KeyBindingHelper.registerKeyBinding(new KeyBinding( - "key.mcpht.toggleXray", // Translation of name - InputUtil.Type.KEYSYM, // Type: MOUSE or KESYM (Keyboard) - GLFW.GLFW_KEY_Y, // The keycode of the key (default?) - "category.mcpht.rendering" // Translation key for the keybinding category - )); - ClientTickEvents.END_CLIENT_TICK.register(client -> { - // NOTE: IDK why we need a while here - while (toggleKey.wasPressed()) { - if (MCPHTClient.XRAY.getEnable()) { - MCPHTClient.LOGGER.info(String.format("toggle XRay: %s", MCPHTClient.XRAY.enable)); - MCPHTClient.XRAY.enable = !MCPHTClient.XRAY.enable; - MCPHTClient.MC.worldRenderer.reload(); - } - } - }); +public class Fly extends Hack { + public Fly() { + super(new MaybeKey()); } + @Override + public void onKeyPress() { + + } + + @Override public boolean getEnable() { return enable && MCPHTClient.CONFIG.fly.enable; } diff --git a/src/client/java/de/cscherr/mcpht/XRay.java b/src/client/java/de/cscherr/mcpht/XRay.java index f9a77a9..bb0412d 100644 --- a/src/client/java/de/cscherr/mcpht/XRay.java +++ b/src/client/java/de/cscherr/mcpht/XRay.java @@ -1,19 +1,17 @@ package de.cscherr.mcpht; -import net.fabricmc.fabric.api.client.event.lifecycle.v1.ClientTickEvents; -import net.fabricmc.fabric.api.client.keybinding.v1.KeyBindingHelper; +import de.cscherr.mcpht.util.Hack; +import de.cscherr.mcpht.util.MaybeKey; import net.minecraft.block.Block; import net.minecraft.block.BlockState; import net.minecraft.block.Blocks; import net.minecraft.client.option.KeyBinding; -import net.minecraft.client.util.InputUtil; import org.lwjgl.glfw.GLFW; import java.util.ArrayList; import java.util.List; -public class XRay { - public boolean enable = true; +public class XRay extends Hack { public ArrayList whitelistBlocks = new ArrayList<>( List.of( // Ores @@ -80,31 +78,24 @@ public class XRay { Blocks.RED_SHULKER_BOX ) ); - private static KeyBinding toggleKey; - public void init() { - toggleKey = KeyBindingHelper.registerKeyBinding(new KeyBinding( - "key.mcpht.toggleXray", // Translation of name - InputUtil.Type.KEYSYM, // Type: MOUSE or KESYM (Keyboard) - GLFW.GLFW_KEY_X, // The keycode of the key (default?) - "category.mcpht.rendering" // Translation key for the keybinding category - )); - ClientTickEvents.END_CLIENT_TICK.register(client -> { - // NOTE: IDK why we need a while here - while (toggleKey.wasPressed()) { - if (MCPHTClient.XRAY.getEnable()) { - MCPHTClient.LOGGER.info(String.format("toggle XRay: %s", MCPHTClient.XRAY.enable)); - MCPHTClient.XRAY.enable = !MCPHTClient.XRAY.enable; - MCPHTClient.MC.worldRenderer.reload(); - } - } - }); + public XRay() { + super(new MaybeKey("key.mcpht.toggleXray", + "category.mcpht.rendering", + GLFW.GLFW_KEY_X)); + } + + @Override + public void onKeyPress() { + MCPHTClient.XRAY.enable = !MCPHTClient.XRAY.enable; + MCPHTClient.MC.worldRenderer.reload(); } public boolean showRenderBlock(BlockState state) { return whitelistBlocks.contains(state.getBlock()); } + @Override public boolean getEnable() { return enable && MCPHTClient.CONFIG.xRay.enable; } diff --git a/src/client/java/de/cscherr/mcpht/util/Hack.java b/src/client/java/de/cscherr/mcpht/util/Hack.java new file mode 100644 index 0000000..cf1a463 --- /dev/null +++ b/src/client/java/de/cscherr/mcpht/util/Hack.java @@ -0,0 +1,39 @@ +package de.cscherr.mcpht.util; + +import de.cscherr.mcpht.MCPHTClient; +import net.fabricmc.fabric.api.client.event.lifecycle.v1.ClientTickEvents; +import net.fabricmc.fabric.api.client.keybinding.v1.KeyBindingHelper; +import net.minecraft.client.option.KeyBinding; +import net.minecraft.client.util.InputUtil; +import org.lwjgl.glfw.GLFW; + +import java.util.Optional; + +public abstract class Hack { + public boolean enable = false; + public MaybeKey toggleKey; + + public Hack(MaybeKey toggleKey) { + toggleKey = toggleKey; + } + + public void init() { + if (toggleKey.hasKey) { + ClientTickEvents.END_CLIENT_TICK.register(client -> { + // NOTE: IDK why we need a while here + while (toggleKey.key.wasPressed()) { + if (this.getEnable()) { + MCPHTClient.LOGGER.info(String.format("toggle %s: %s", + this.getClass().getSimpleName(), + this.getEnable())); + onKeyPress(); + } + } + }); + } + } + + public abstract void onKeyPress(); + + public abstract boolean getEnable(); +} \ No newline at end of file diff --git a/src/client/java/de/cscherr/mcpht/util/MaybeKey.java b/src/client/java/de/cscherr/mcpht/util/MaybeKey.java new file mode 100644 index 0000000..f5790a5 --- /dev/null +++ b/src/client/java/de/cscherr/mcpht/util/MaybeKey.java @@ -0,0 +1,27 @@ +package de.cscherr.mcpht.util; + +import net.fabricmc.fabric.api.client.keybinding.v1.KeyBindingHelper; +import net.minecraft.client.option.KeyBinding; +import net.minecraft.client.util.InputUtil; + +public class MaybeKey { + public KeyBinding key; + public String translationKey; + public String translationCategory; + public int keyCode; + public boolean hasKey; + + public MaybeKey() { + hasKey = false; + } + + public MaybeKey(String translationKey, String translationCategory, int keyCode) { + key = KeyBindingHelper.registerKeyBinding(new KeyBinding( + translationKey, // Translation of name + InputUtil.Type.KEYSYM, // Type: MOUSE or KESYM (Keyboard) + keyCode, // The keycode of the key (default?) + translationCategory // Translation key for the keybinding category + )); + hasKey = true; + } +} diff --git a/src/client/java/de/cscherr/mcpht/util/event/Event.java b/src/client/java/de/cscherr/mcpht/util/event/Event.java new file mode 100644 index 0000000..f6379c0 --- /dev/null +++ b/src/client/java/de/cscherr/mcpht/util/event/Event.java @@ -0,0 +1,9 @@ +package de.cscherr.mcpht.util.event; + +import java.util.ArrayList; + +public abstract class Event { + public abstract void fire(ArrayList listeners); + + public abstract Class getListenerType(); +} \ No newline at end of file diff --git a/src/client/java/de/cscherr/mcpht/util/event/Listener.java b/src/client/java/de/cscherr/mcpht/util/event/Listener.java new file mode 100644 index 0000000..9dbf0b9 --- /dev/null +++ b/src/client/java/de/cscherr/mcpht/util/event/Listener.java @@ -0,0 +1,6 @@ +package de.cscherr.mcpht.util.event; + +import java.util.EventListener; + +public interface Listener extends EventListener { +} diff --git a/src/client/java/de/cscherr/mcpht/util/event/UpdateListener.java b/src/client/java/de/cscherr/mcpht/util/event/UpdateListener.java new file mode 100644 index 0000000..a017a07 --- /dev/null +++ b/src/client/java/de/cscherr/mcpht/util/event/UpdateListener.java @@ -0,0 +1,25 @@ +package de.cscherr.mcpht.util.event; + +import java.util.ArrayList; +import java.util.EventListener; + +public interface UpdateListener extends Listener { + public void onUpdate(); + public static class UpdateEvent extends Event + { + public static final UpdateEvent INSTANCE = new UpdateEvent(); + + @Override + public void fire(ArrayList listeners) + { + for(UpdateListener listener : listeners) + listener.onUpdate(); + } + + @Override + public Class getListenerType() + { + return UpdateListener.class; + } + } +}