Skip to content
This repository was archived by the owner on Feb 19, 2019. It is now read-only.

Commit 3a1f1fc

Browse files
LeafHackerZeroMemes
authored andcommitted
Hook KeyEvent into Minecraft's runKeyboardTick()
Instead of creating our own loop and keeping track of which keys were pressed, we should instead hook into Minecraft's existing Keyboard.next() loop (runKeyboardTick()). This both simplifies our code and allows KeyEvents to take advantage of Keyboard's event methods (e.g. getEventKeyCharacter()).
1 parent 515989e commit 3a1f1fc

File tree

2 files changed

+29
-38
lines changed

2 files changed

+29
-38
lines changed

src/main/java/me/zero/client/api/event/handle/ClientHandler.java

Lines changed: 12 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,6 @@
2525
import me.zero.client.api.event.defaults.game.network.PacketEvent;
2626
import me.zero.client.api.event.defaults.game.core.KeyEvent;
2727
import me.zero.client.api.event.defaults.game.core.ProfilerEvent;
28-
import me.zero.client.api.event.defaults.game.core.TickEvent;
2928
import me.zero.client.api.event.defaults.game.render.Render3DEvent;
3029
import me.zero.client.api.event.defaults.game.render.RenderHudEvent;
3130
import me.zero.client.api.util.interfaces.Helper;
@@ -34,12 +33,9 @@
3433
import me.zero.client.api.util.render.camera.CameraManager;
3534
import net.minecraft.network.play.client.CPacketChatMessage;
3635
import net.minecraft.network.play.server.SPacketChat;
37-
import org.lwjgl.input.Keyboard;
3836

3937
import java.util.stream.Stream;
4038

41-
import static org.lwjgl.input.Keyboard.KEYBOARD_SIZE;
42-
4339
/**
4440
* Some basic events that the client uses
4541
*
@@ -48,11 +44,6 @@
4844
*/
4945
public final class ClientHandler implements Helper {
5046

51-
/**
52-
* A map of all key states
53-
*/
54-
private final boolean[] keyMap = new boolean[KEYBOARD_SIZE];
55-
5647
/**
5748
* Handles camera updates
5849
*/
@@ -64,8 +55,18 @@ public final class ClientHandler implements Helper {
6455
* Handles keybinds
6556
*/
6657
@EventHandler
67-
private final Listener<KeyEvent> keyListener = new Listener<>(event ->
68-
Keybind.getKeybinds().stream().filter(keybind -> keybind.getType() == Keybind.Type.TOGGLE && keybind.getKey() == event.getKey()).forEach(Keybind::onClick));
58+
private final Listener<KeyEvent> keyListener = new Listener<>(event -> {
59+
// Get all matching keybinds
60+
Stream<Keybind> keybinds = Keybind.getKeybinds().stream()
61+
.filter(bind -> bind.getKey() == event.getKey());
62+
63+
// Run onClick for the toggle keybinds
64+
keybinds.filter(bind -> bind.getType() == Keybind.Type.TOGGLE)
65+
.forEach(Keybind::onClick);
66+
67+
// Run onPres for all matching keybinds
68+
keybinds.forEach(Keybind::onPress);
69+
});
6970

7071
/**
7172
* Handles profiling events
@@ -78,29 +79,6 @@ public final class ClientHandler implements Helper {
7879
ClientAPI.EVENT_BUS.post(new Render3DEvent());
7980
});
8081

81-
/**
82-
* Handles key states
83-
*/
84-
@EventHandler
85-
private final Listener<TickEvent> tickListener = new Listener<>(event -> {
86-
if (mc.currentScreen != null) return;
87-
88-
for (int i = 1; i < KEYBOARD_SIZE; i++) {
89-
final int key = i;
90-
boolean currentState = Keyboard.isKeyDown(i);
91-
if (currentState != keyMap[i]) {
92-
if (keyMap[i] = !keyMap[i])
93-
ClientAPI.EVENT_BUS.post(new KeyEvent(i));
94-
95-
Stream<Keybind> keybinds = Keybind.getKeybinds().stream().filter(keybind -> keybind.getKey() == key);
96-
if (currentState)
97-
keybinds.forEach(Keybind::onPress);
98-
else
99-
keybinds.forEach(Keybind::onRelease);
100-
}
101-
}
102-
});
103-
10482
/**
10583
* Handles packet out-flow
10684
*/

src/main/java/me/zero/client/load/mixin/MixinMinecraft.java

Lines changed: 17 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -20,13 +20,11 @@
2020
import me.zero.client.api.Client;
2121
import me.zero.client.api.ClientInfo;
2222
import me.zero.client.api.ClientAPI;
23-
import me.zero.client.api.event.defaults.game.core.ClickEvent;
24-
import me.zero.client.api.event.defaults.game.core.GameShutdownEvent;
25-
import me.zero.client.api.event.defaults.game.core.LoopEvent;
26-
import me.zero.client.api.event.defaults.game.core.TickEvent;
23+
import me.zero.client.api.event.defaults.game.core.*;
2724
import me.zero.client.api.event.defaults.game.render.GuiEvent;
2825
import me.zero.client.api.event.defaults.game.world.WorldEvent;
2926
import me.zero.client.api.event.handle.ClientHandler;
27+
import me.zero.client.api.util.keybind.Keybind;
3028
import me.zero.client.api.util.render.gl.GlUtils;
3129
import me.zero.client.load.ClientInitException;
3230
import me.zero.client.load.mixin.wrapper.IMinecraft;
@@ -35,6 +33,7 @@
3533
import net.minecraft.client.multiplayer.WorldClient;
3634
import net.minecraft.util.Session;
3735
import net.minecraft.util.Timer;
36+
import org.lwjgl.input.Keyboard;
3837
import org.spongepowered.asm.mixin.Mixin;
3938
import org.spongepowered.asm.mixin.Shadow;
4039
import org.spongepowered.asm.mixin.gen.Accessor;
@@ -86,6 +85,20 @@ public void onLoop(CallbackInfo ci) {
8685
ClientAPI.EVENT_BUS.post(new LoopEvent());
8786
}
8887

88+
@Inject(method = "runTickKeyboard", at = @At(value = "INVOKE_ASSIGN", target = "org/lwjgl/input/Keyboard.getEventKeyState()Z", remap = false))
89+
public void onKeyEvent(CallbackInfo ci) {
90+
boolean down = Keyboard.getEventKeyState();
91+
int key = Keyboard.getEventKey();
92+
93+
if (down)
94+
ClientAPI.EVENT_BUS.post(new KeyEvent(key));
95+
else
96+
// TODO: split into new KeyUp event
97+
Keybind.getKeybinds().stream()
98+
.filter(bind -> bind.getKey() == key)
99+
.forEach(Keybind::onRelease);
100+
}
101+
89102
@Inject(method = "init", at = @At("RETURN"))
90103
public void init(CallbackInfo ci) {
91104
// Try and find the "client.json" config

0 commit comments

Comments
 (0)