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

Commit f848425

Browse files
LeafHackerZeroMemes
authored andcommitted
Add KeyUpEvent
Add an event for when a key is released, allowing us to move keybind onRelease triggering back to ClientHandler.
1 parent 3a1f1fc commit f848425

File tree

3 files changed

+54
-10
lines changed

3 files changed

+54
-10
lines changed
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
/*
2+
* Copyright 2017 ZeroMemes
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package me.zero.client.api.event.defaults.game.core;
18+
19+
/**
20+
* Event called when a Key is released outside of a GUI while in-game
21+
*
22+
* @see ClickEvent
23+
*
24+
* @author Leaf
25+
* @since 2.1
26+
*/
27+
public final class KeyUpEvent {
28+
29+
private final int key;
30+
31+
/**
32+
* Creates a new instance of KeyUpEvent.
33+
*
34+
* @param key - The key code for the key that was released
35+
*/
36+
public KeyUpEvent(int key) {
37+
this.key = key;
38+
}
39+
40+
/**
41+
* @return The key code that corresponds to the released key
42+
*/
43+
public final int getKey() {
44+
return this.key;
45+
}
46+
}

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

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,10 +21,9 @@
2121
import me.zero.alpine.listener.Listener;
2222
import me.zero.client.api.event.defaults.filters.PacketFilter;
2323
import me.zero.alpine.type.EventPriority;
24+
import me.zero.client.api.event.defaults.game.core.*;
2425
import me.zero.client.api.event.defaults.game.misc.ChatEvent;
2526
import me.zero.client.api.event.defaults.game.network.PacketEvent;
26-
import me.zero.client.api.event.defaults.game.core.KeyEvent;
27-
import me.zero.client.api.event.defaults.game.core.ProfilerEvent;
2827
import me.zero.client.api.event.defaults.game.render.Render3DEvent;
2928
import me.zero.client.api.event.defaults.game.render.RenderHudEvent;
3029
import me.zero.client.api.util.interfaces.Helper;
@@ -68,6 +67,12 @@ public final class ClientHandler implements Helper {
6867
keybinds.forEach(Keybind::onPress);
6968
});
7069

70+
@EventHandler
71+
private final Listener<KeyUpEvent> keyUpListener = new Listener<>(event ->
72+
Keybind.getKeybinds().stream()
73+
.filter(bind -> bind.getKey() == event.getKey())
74+
.forEach(Keybind::onRelease));
75+
7176
/**
7277
* Handles profiling events
7378
*/

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

Lines changed: 1 addition & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,6 @@
2424
import me.zero.client.api.event.defaults.game.render.GuiEvent;
2525
import me.zero.client.api.event.defaults.game.world.WorldEvent;
2626
import me.zero.client.api.event.handle.ClientHandler;
27-
import me.zero.client.api.util.keybind.Keybind;
2827
import me.zero.client.api.util.render.gl.GlUtils;
2928
import me.zero.client.load.ClientInitException;
3029
import me.zero.client.load.mixin.wrapper.IMinecraft;
@@ -90,13 +89,7 @@ public void onKeyEvent(CallbackInfo ci) {
9089
boolean down = Keyboard.getEventKeyState();
9190
int key = Keyboard.getEventKey();
9291

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);
92+
ClientAPI.EVENT_BUS.post(down ? new KeyEvent(key) : new KeyUpEvent(key));
10093
}
10194

10295
@Inject(method = "init", at = @At("RETURN"))

0 commit comments

Comments
 (0)