Skip to content
This repository was archived by the owner on Mar 5, 2026. It is now read-only.

Commit 58ffa67

Browse files
author
Kiriyaga W
committed
refactor: keybinds
1 parent f50deaa commit 58ffa67

16 files changed

Lines changed: 283 additions & 106 deletions

File tree

‎nami-api/gradle.properties‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,4 +4,4 @@ loader_version=0.18.2
44
maven_group = namidevelopment.kiriyaga
55
archives_base_name = nami-api
66
fabric_version=0.140.0+1.21.11
7-
mod_version=1.4.56
7+
mod_version=1.4.58

‎nami-api/src/main/java/namidevelopment/kiriyaga/api/NamiApi.java‎

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,7 @@ public class NamiApi implements ClientModInitializer {
5151
public static final PluginService PLUGIN_SERVICE = new PluginService();
5252
public static final BreakPredictionService BREAKPREDICT_SERVICE = new BreakPredictionService();
5353
public static final TotemCounterService TOTEMCOUNTER_SERVICE = new TotemCounterService();
54+
public static final KeyBindService KEYBIND_SERVICE = new KeyBindService();
5455

5556
public static NamiFormatService CAT_FORMAT = new NamiFormatService();
5657

@@ -66,9 +67,11 @@ public void onInitializeClient() {
6667
SERVER_SERVICE.init();
6768
CHAT_SERVICE.init();
6869
INPUT_SERVICE.init();
70+
KEYBIND_SERVICE.init();
6971
BREAKPREDICT_SERVICE.init();
7072
TOTEMCOUNTER_SERVICE.init();
7173

74+
7275
CAT_FORMAT.add(new CatStyles());
7376

7477
SOCIALS_SERVICE.load();
Lines changed: 128 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,128 @@
1+
package namidevelopment.kiriyaga.api.core;
2+
3+
import namidevelopment.kiriyaga.api.annotation.SubscribeEvent;
4+
import namidevelopment.kiriyaga.api.core.macro.model.Macro;
5+
import namidevelopment.kiriyaga.api.event.impl.KeyboardHandlerEvent;
6+
import namidevelopment.kiriyaga.api.event.impl.MouseHandlerEvent;
7+
import namidevelopment.kiriyaga.api.model.feature.Feature;
8+
import namidevelopment.kiriyaga.api.model.setting.KeyBindSetting;
9+
10+
import java.util.HashSet;
11+
import java.util.Set;
12+
13+
import static namidevelopment.kiriyaga.api.NamiApi.*;
14+
15+
public class KeyBindService {
16+
private final Set<Integer> pressedKeys = new HashSet<>();
17+
18+
public void init() {
19+
EVENT_SERVICE.register(this);
20+
}
21+
22+
@SubscribeEvent
23+
public void onKeyboardHandler(KeyboardHandlerEvent event) {
24+
if (MC.screen != null)
25+
return;
26+
int key = event.getKey();
27+
boolean handled = false;
28+
29+
if (event.isPress()) {
30+
pressedKeys.add(key);
31+
handled = handlePress(key);
32+
}
33+
34+
if (event.isRelease()) {
35+
pressedKeys.remove(key);
36+
handled = handleRelease(key);
37+
}
38+
39+
if (handled)
40+
event.cancel();
41+
}
42+
43+
@SubscribeEvent
44+
public void onMouseHandler(MouseHandlerEvent event) {
45+
if (MC.screen != null)
46+
return;
47+
int key = event.getButton();
48+
boolean handled = false;
49+
50+
if (event.isPress()) {
51+
pressedKeys.add(key);
52+
handled = handlePress(key);
53+
}
54+
55+
if (event.isRelease()) {
56+
pressedKeys.remove(key);
57+
handled = handleRelease(key);
58+
}
59+
60+
if (handled)
61+
event.cancel();
62+
}
63+
64+
private boolean handlePress(int key) {
65+
boolean handled = false;
66+
for (Feature feature : FEATURE_SERVICE.getStorage().getAll()) {
67+
KeyBindSetting bind = feature.getKeyBind();
68+
if (bind == null)
69+
continue;
70+
if (bind.get() != key)
71+
continue;
72+
73+
handled = true;
74+
75+
if (bind.isHoldMode()) {
76+
feature.setEnabled(true);
77+
} else {
78+
feature.toggle();
79+
}
80+
}
81+
82+
for (Macro macro : MACRO_SERVICE.getAll()) {
83+
if (macro.getKeyCode() == key && MC.player != null) {
84+
MC.player.connection.sendChat(macro.getMessage());
85+
handled = true;
86+
}
87+
}
88+
89+
return handled;
90+
}
91+
92+
private boolean handleRelease(int key) {
93+
boolean handled = false;
94+
for (Feature feature : FEATURE_SERVICE.getStorage().getAll()) {
95+
KeyBindSetting bind = feature.getKeyBind();
96+
if (bind == null)
97+
continue;
98+
99+
if (bind.get() != key)
100+
continue;
101+
102+
if (bind.isHoldMode()) {
103+
feature.setEnabled(false);
104+
handled = true;
105+
}
106+
}
107+
return handled;
108+
}
109+
110+
public boolean isKeyDown(int key) {
111+
return pressedKeys.contains(key);
112+
}
113+
114+
public boolean isPressed(KeyBindSetting bind) {
115+
return bind != null && bind.isBound() && pressedKeys.contains(bind.get());
116+
}
117+
118+
public boolean isPressedToggle(KeyBindSetting bind) {
119+
if (bind == null || !bind.isBound())
120+
return false;
121+
if (pressedKeys.contains(bind.get())) {
122+
pressedKeys.remove(bind.get());
123+
return true;
124+
}
125+
126+
return false;
127+
}
128+
}
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
package namidevelopment.kiriyaga.api.event.impl;
2+
3+
import namidevelopment.kiriyaga.api.event.Event;
4+
5+
public class KeyboardHandlerEvent extends Event {
6+
7+
private final int key;
8+
private final int scancode;
9+
private final int action;
10+
private final int modifiers;
11+
12+
public KeyboardHandlerEvent(int key, int scancode, int action, int modifiers) {
13+
this.key = key;
14+
this.scancode = scancode;
15+
this.action = action;
16+
this.modifiers = modifiers;
17+
}
18+
19+
public int getKey() { return key; }
20+
public int getScancode() { return scancode; }
21+
public int getAction() { return action; }
22+
public int getModifiers() { return modifiers; }
23+
24+
public boolean isPress() {
25+
return action == 1;
26+
}
27+
28+
public boolean isRelease() {
29+
return action == 0;
30+
}
31+
32+
public boolean isRepeat() {
33+
return action == 2;
34+
}
35+
}
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
package namidevelopment.kiriyaga.api.event.impl;
2+
3+
import namidevelopment.kiriyaga.api.event.Event;
4+
5+
public class MouseHandlerEvent extends Event {
6+
7+
private final int button;
8+
private final int action;
9+
private final int modifiers;
10+
11+
public MouseHandlerEvent(int button, int action, int modifiers) {
12+
this.button = button;
13+
this.action = action;
14+
this.modifiers = modifiers;
15+
}
16+
17+
public int getButton() { return button; }
18+
public int getAction() { return action; }
19+
public int getModifiers() { return modifiers; }
20+
21+
public boolean isPress() {
22+
return action == 1;
23+
}
24+
25+
public boolean isRelease() {
26+
return action == 0;
27+
}
28+
}
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
package namidevelopment.kiriyaga.api.mixin;
2+
3+
import namidevelopment.kiriyaga.api.contract.FeatureContractService;
4+
import namidevelopment.kiriyaga.api.contract.feature.RotationsFeatureConfig;
5+
import namidevelopment.kiriyaga.api.event.impl.KeyInputEvent;
6+
import namidevelopment.kiriyaga.api.event.impl.KeyboardHandlerEvent;
7+
import net.minecraft.client.KeyboardHandler;
8+
import net.minecraft.client.input.KeyEvent;
9+
import net.minecraft.client.player.LocalPlayer;
10+
import net.minecraft.util.Mth;
11+
import org.spongepowered.asm.mixin.Mixin;
12+
import org.spongepowered.asm.mixin.Shadow;
13+
import org.spongepowered.asm.mixin.injection.At;
14+
import org.spongepowered.asm.mixin.injection.Inject;
15+
import org.spongepowered.asm.mixin.injection.callback.CallbackInfo;
16+
17+
import static namidevelopment.kiriyaga.api.NamiApi.*;
18+
19+
@Mixin(value = KeyboardHandler.class)
20+
public abstract class MixinKeyboardHandler {
21+
22+
@Inject(method = "keyPress", at = @At("HEAD"), cancellable = true)
23+
private void keyPress(long window, int action, KeyEvent keyEvent, CallbackInfo ci) {
24+
if (window != MC.getWindow().handle()) return;
25+
26+
KeyboardHandlerEvent ev = new KeyboardHandlerEvent(keyEvent.key(), keyEvent.scancode(), action, keyEvent.modifiers());
27+
28+
EVENT_SERVICE.post(ev);
29+
30+
if (ev.isCancelled()) {
31+
ci.cancel();
32+
}
33+
}
34+
}

‎nami-api/src/main/java/namidevelopment/kiriyaga/api/mixin/MixinMinecraft.java‎

Lines changed: 0 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -23,52 +23,6 @@
2323
import static namidevelopment.kiriyaga.api.NamiApi.*;
2424
@Mixin(Minecraft.class)
2525
public abstract class MixinMinecraft {
26-
@Shadow @Nullable public LocalPlayer player;
27-
@Shadow public ClientLevel level;
28-
29-
@Inject(method = "handleKeybinds", at = @At("TAIL"))
30-
private void onHandleInputEvents_TAIL(CallbackInfo ci) {
31-
if (MC == null || MC.mouseHandler == null || MC.screen != null) return;
32-
33-
for (Feature Feature : FEATURE_SERVICE.getStorage().getAll()) {
34-
if (Feature == null) continue;
35-
KeyBindSetting bind = Feature.getKeyBind();
36-
if (bind == null) continue;
37-
38-
if (bind.get() != KeyBindSetting.KEY_NONE) {
39-
boolean currentlyPressed = bind.isPressed();
40-
41-
if (bind.isHoldMode()) {
42-
if (currentlyPressed && !Feature.isEnabled()) {
43-
Feature.setEnabled(true);
44-
} else if (!currentlyPressed && Feature.isEnabled()) {
45-
Feature.setEnabled(false);
46-
}
47-
} else {
48-
if (currentlyPressed && !bind.wasPressedLastTick()) {
49-
Feature.toggle();
50-
}
51-
}
52-
53-
bind.setWasPressedLastTick(currentlyPressed);
54-
}
55-
}
56-
57-
for (Macro macro : MACRO_SERVICE.getAll()) {
58-
int keyCode = macro.getKeyCode();
59-
boolean currentlyPressed = MACRO_SERVICE.isKeyPressed(keyCode);
60-
boolean wasPressed = MACRO_SERVICE.wasKeyPressedLastTick(keyCode);
61-
62-
if (currentlyPressed && !wasPressed) {
63-
if (MC.player != null) {
64-
MC.player.connection.sendChat(macro.getMessage());
65-
}
66-
}
67-
68-
MACRO_SERVICE.setKeyPressedLastTick(keyCode, currentlyPressed);
69-
}
70-
}
71-
7226
@ModifyArg(method = "runTick", at = @At(value = "INVOKE", target = "Lnet/minecraft/client/gui/components/DebugScreenOverlay;logFrameDuration(J)V"),index = 0)
7327
private long runTick(long frameDurationNs) {
7428
SERVER_SERVICE.setLastFrameDurationNs(frameDurationNs);
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
package namidevelopment.kiriyaga.api.mixin;
2+
3+
import namidevelopment.kiriyaga.api.event.impl.KeyInputEvent;
4+
import namidevelopment.kiriyaga.api.event.impl.MouseHandlerEvent;
5+
import net.minecraft.client.KeyboardHandler;
6+
import net.minecraft.client.MouseHandler;
7+
import net.minecraft.client.input.KeyEvent;
8+
import net.minecraft.client.input.MouseButtonInfo;
9+
import org.spongepowered.asm.mixin.Mixin;
10+
import org.spongepowered.asm.mixin.injection.At;
11+
import org.spongepowered.asm.mixin.injection.Inject;
12+
import org.spongepowered.asm.mixin.injection.callback.CallbackInfo;
13+
14+
import static namidevelopment.kiriyaga.api.NamiApi.EVENT_SERVICE;
15+
import static namidevelopment.kiriyaga.api.NamiApi.MC;
16+
17+
@Mixin(value = MouseHandler.class)
18+
public abstract class MixinMouseHandler {
19+
20+
@Inject(method = "onButton", at = @At("HEAD"), cancellable = true)
21+
private void onButton(long window, MouseButtonInfo info, int action, CallbackInfo ci) {
22+
if (window != MC.getWindow().handle()) return;
23+
24+
MouseHandlerEvent ev =new MouseHandlerEvent(info.button(), action, info.modifiers());
25+
26+
EVENT_SERVICE.post(ev);
27+
28+
if (ev.isCancelled()) {
29+
ci.cancel();
30+
}
31+
}
32+
}

0 commit comments

Comments
 (0)