From 136e0155a455dd0da8c455c932e87695aea721a1 Mon Sep 17 00:00:00 2001 From: zeroeightsix Date: Tue, 9 Oct 2018 20:22:14 +0200 Subject: [PATCH] Implemented binds with key modifiers --- .../kami/command/commands/BindCommand.java | 2 +- .../kami/gui/kami/component/BindButton.java | 55 ++++++++++--- .../me/zeroeightsix/kami/module/Module.java | 13 +-- .../kami/module/ModuleManager.java | 5 +- .../kami/module/modules/ClickGUI.java | 6 +- .../kami/module/modules/movement/Sprint.java | 3 +- .../java/me/zeroeightsix/kami/util/Bind.java | 82 +++++++++++++++++++ 7 files changed, 139 insertions(+), 27 deletions(-) create mode 100644 src/main/java/me/zeroeightsix/kami/util/Bind.java diff --git a/src/main/java/me/zeroeightsix/kami/command/commands/BindCommand.java b/src/main/java/me/zeroeightsix/kami/command/commands/BindCommand.java index 45dbceee..bc13ae49 100644 --- a/src/main/java/me/zeroeightsix/kami/command/commands/BindCommand.java +++ b/src/main/java/me/zeroeightsix/kami/command/commands/BindCommand.java @@ -53,7 +53,7 @@ public class BindCommand extends Command { return; } - m.setKey(key); + m.getBind().setKey(key); sendChatMessage("Bind for &b" + m.getName() + "&r set to &b" + rkey.toUpperCase()); } } diff --git a/src/main/java/me/zeroeightsix/kami/gui/kami/component/BindButton.java b/src/main/java/me/zeroeightsix/kami/gui/kami/component/BindButton.java index 1c2a9fa9..5124be03 100644 --- a/src/main/java/me/zeroeightsix/kami/gui/kami/component/BindButton.java +++ b/src/main/java/me/zeroeightsix/kami/gui/kami/component/BindButton.java @@ -3,6 +3,7 @@ package me.zeroeightsix.kami.gui.kami.component; import me.zeroeightsix.kami.gui.rgui.component.listen.KeyListener; import me.zeroeightsix.kami.gui.rgui.component.listen.MouseListener; import me.zeroeightsix.kami.module.Module; +import me.zeroeightsix.kami.util.Bind; import org.lwjgl.input.Keyboard; /** @@ -15,15 +16,14 @@ public class BindButton extends EnumButton { boolean waiting = false; Module m; + boolean ctrl = false, shift = false, alt = false; + public BindButton(String name, Module m) { super(name, none); this.m = m; - int key = m.getBind(); - if (key == -1) - modes = none; - else - modes = new String[]{Keyboard.getKeyName(key)}; + Bind bind = m.getBind(); + modes = new String[]{bind.toString()}; addKeyListener(new KeyListener() { @Override @@ -31,20 +31,35 @@ public class BindButton extends EnumButton { if (!waiting) return; int key = event.getKey(); - if (key == Keyboard.KEY_BACK){ - m.setKey(-1); - modes = none; + if (isShift(key)) { + shift = true; + modes = new String[]{(ctrl ? "Ctrl + " : "") + (alt ? "Alt + " : "") + "Shift + "}; + } else if (isCtrl(key)) { + ctrl = true; + modes = new String[]{"Ctrl + " + (alt ? "Alt + " : "") + (shift ? "Shift + " : "")}; + } else if (isAlt(key)) { + alt = true; + modes = new String[]{(ctrl ? "Ctrl + " : "") + "Alt + " + (shift ? "Shift + " : "")}; + } else if (key == Keyboard.KEY_BACK) { + m.getBind().setCtrl(false); + m.getBind().setShift(false); + m.getBind().setAlt(false); + m.getBind().setKey(-1); + modes = new String[]{m.getBind().toString()}; + waiting = false; + } else { + m.getBind().setCtrl(ctrl); + m.getBind().setShift(shift); + m.getBind().setAlt(alt); + m.getBind().setKey(key); + modes = new String[]{m.getBind().toString()}; + ctrl = alt = shift = false; waiting = false; - return; } - m.setKey(key); - modes = new String[]{Keyboard.getKeyName(key)}; - waiting = false; } @Override public void onKeyUp(KeyEvent event) { - } }); @@ -76,4 +91,18 @@ public class BindButton extends EnumButton { } }); } + + private boolean isAlt(int key) { + return key == Keyboard.KEY_LMENU || key == Keyboard.KEY_RMENU; + } + + private boolean isCtrl(int key) { + return key == Keyboard.KEY_LCONTROL || key == Keyboard.KEY_RCONTROL; + } + + private boolean isShift(int key) { + return key == Keyboard.KEY_LSHIFT || key == Keyboard.KEY_RSHIFT; + } + + } diff --git a/src/main/java/me/zeroeightsix/kami/module/Module.java b/src/main/java/me/zeroeightsix/kami/module/Module.java index 0217db21..456fe032 100644 --- a/src/main/java/me/zeroeightsix/kami/module/Module.java +++ b/src/main/java/me/zeroeightsix/kami/module/Module.java @@ -5,8 +5,8 @@ import me.zeroeightsix.kami.event.events.RenderEvent; import me.zeroeightsix.kami.module.modules.movement.Sprint; import me.zeroeightsix.kami.setting.Setting; import me.zeroeightsix.kami.setting.SettingsClass; +import me.zeroeightsix.kami.util.Bind; import net.minecraft.client.Minecraft; -import org.lwjgl.input.Keyboard; import java.lang.annotation.Retention; import java.lang.annotation.RetentionPolicy; @@ -20,7 +20,7 @@ public class Module extends SettingsClass { private final String description = getAnnotation().description(); private final Category category = getAnnotation().category(); @Setting(name = "Bind", hidden = true) - private int bind = getAnnotation().bind(); + private Bind bind = Bind.none(); @Setting(name = "Enabled", hidden = true) private boolean enabled; public boolean alwaysListening = false; @@ -42,16 +42,12 @@ public class Module extends SettingsClass { public void onRender() {} public void onWorldRender(RenderEvent event) {} - public int getBind() { + public Bind getBind() { return bind; } public String getBindName() { - return bind == -1 ? "NONE" : Keyboard.getKeyName(bind); - } - - public void setKey(int key) { - this.bind = key; + return bind.toString(); } public enum Category @@ -87,7 +83,6 @@ public class Module extends SettingsClass { { String name(); String description() default "Descriptionless"; - int bind() default 0; Module.Category category(); boolean alwaysListening() default false; } diff --git a/src/main/java/me/zeroeightsix/kami/module/ModuleManager.java b/src/main/java/me/zeroeightsix/kami/module/ModuleManager.java index f51bafcc..ca86b13d 100644 --- a/src/main/java/me/zeroeightsix/kami/module/ModuleManager.java +++ b/src/main/java/me/zeroeightsix/kami/module/ModuleManager.java @@ -98,8 +98,11 @@ public class ModuleManager { public static void onBind(int eventKey) { if (eventKey == 0) return; // if key is the 'none' key (stuff like mod key in i3 might return 0) modules.forEach(module -> { - if (module.getBind() == eventKey) + if (module.getBind().isDown()) { + System.out.println(module.getBind() + " is down!"); + module.toggle(); + } }); } diff --git a/src/main/java/me/zeroeightsix/kami/module/modules/ClickGUI.java b/src/main/java/me/zeroeightsix/kami/module/modules/ClickGUI.java index ec7c34f5..5e3f9fc1 100644 --- a/src/main/java/me/zeroeightsix/kami/module/modules/ClickGUI.java +++ b/src/main/java/me/zeroeightsix/kami/module/modules/ClickGUI.java @@ -7,9 +7,13 @@ import org.lwjgl.input.Keyboard; /** * Created by 086 on 23/08/2017. */ -@Module.Info(name = "clickGUI", description = "Opens the Click GUI", bind = Keyboard.KEY_Y, category = Module.Category.HIDDEN) +@Module.Info(name = "clickGUI", description = "Opens the Click GUI", category = Module.Category.HIDDEN) public class ClickGUI extends Module { + public ClickGUI() { + getBind().setKey(Keyboard.KEY_Y); + } + @Override protected void onEnable() { if (!(mc.currentScreen instanceof DisplayGuiScreen)) { diff --git a/src/main/java/me/zeroeightsix/kami/module/modules/movement/Sprint.java b/src/main/java/me/zeroeightsix/kami/module/modules/movement/Sprint.java index 2f318dbf..cf006715 100644 --- a/src/main/java/me/zeroeightsix/kami/module/modules/movement/Sprint.java +++ b/src/main/java/me/zeroeightsix/kami/module/modules/movement/Sprint.java @@ -1,12 +1,11 @@ package me.zeroeightsix.kami.module.modules.movement; import me.zeroeightsix.kami.module.Module; -import org.lwjgl.input.Keyboard; /** * Created by 086 on 23/08/2017. */ -@Module.Info(name = "Sprint", bind = Keyboard.KEY_A, description = "Automatically makes the player sprint", category = Module.Category.MOVEMENT) +@Module.Info(name = "Sprint", description = "Automatically makes the player sprint", category = Module.Category.MOVEMENT) public class Sprint extends Module { @Override diff --git a/src/main/java/me/zeroeightsix/kami/util/Bind.java b/src/main/java/me/zeroeightsix/kami/util/Bind.java new file mode 100644 index 00000000..647ed926 --- /dev/null +++ b/src/main/java/me/zeroeightsix/kami/util/Bind.java @@ -0,0 +1,82 @@ +package me.zeroeightsix.kami.util; + +import org.lwjgl.input.Keyboard; + +/** + * Created by 086 on 9/10/2018. + */ +public class Bind { + + boolean ctrl; + boolean alt; + boolean shift; + int key; + + public Bind(boolean ctrl, boolean alt, boolean shift, int key) { + this.ctrl = ctrl; + this.alt = alt; + this.shift = shift; + this.key = key; + } + + public int getKey() { + return key; + } + + public boolean isCtrl() { + return ctrl; + } + + public boolean isAlt() { + return alt; + } + + public boolean isShift() { + return shift; + } + + public boolean isEmpty() { + return !ctrl && !shift && !alt && key < 0; + } + + public void setAlt(boolean alt) { + this.alt = alt; + } + + public void setCtrl(boolean ctrl) { + this.ctrl = ctrl; + } + + public void setKey(int key) { + this.key = key; + } + + public void setShift(boolean shift) { + this.shift = shift; + } + + @Override + public String toString() { + return isEmpty() ? "None" : (isCtrl() ? "Ctrl + " : "") + (isAlt() ? "Alt + " : "") + (isShift() ? "Shift + " : "") + (key < 0 ? "None" : Keyboard.getKeyName(key)); + } + + public boolean isDown() { + return !isEmpty() && (isShift() == isShiftDown()) && (isCtrl() == isCtrlDown()) && (isAlt() == isAltDown()) && Keyboard.isKeyDown(getKey()); + } + + private boolean isShiftDown() { + return Keyboard.isKeyDown(Keyboard.KEY_LSHIFT) || Keyboard.isKeyDown(Keyboard.KEY_RSHIFT); + } + private boolean isCtrlDown() { + return Keyboard.isKeyDown(Keyboard.KEY_LCONTROL) || Keyboard.isKeyDown(Keyboard.KEY_RCONTROL); + } + + private boolean isAltDown() { + return Keyboard.isKeyDown(Keyboard.KEY_LMENU) || Keyboard.isKeyDown(Keyboard.KEY_RMENU); + } + + public static Bind none() { + return new Bind(false, false, false, -1); + } + +}