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

Commit 4cc4242

Browse files
committed
Fix ClickEvent firing at wrong times
1 parent 5dc05e8 commit 4cc4242

File tree

2 files changed

+37
-1
lines changed

2 files changed

+37
-1
lines changed

src/main/java/clientapi/load/mixin/MixinMinecraft.java

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@
2525
import clientapi.event.handle.ClientHandler;
2626
import clientapi.load.ClientInitException;
2727
import clientapi.load.mixin.extension.IMinecraft;
28+
import clientapi.util.io.MouseKeyTracker;
2829
import clientapi.util.io.StreamReader;
2930
import clientapi.util.render.gl.GLUtils;
3031
import com.google.gson.GsonBuilder;
@@ -116,7 +117,10 @@ private void onMouseEvent(CallbackInfo info) {
116117
if (currentScreen != null)
117118
return;
118119

119-
ClientAPI.EVENT_BUS.post(new ClickEvent(Mouse.getEventButton()));
120+
int button = Mouse.getEventButton();
121+
if (button > 0 && MouseKeyTracker.INSTANCE.wasButtonPressed(button, Mouse.getEventButtonState())) {
122+
ClientAPI.EVENT_BUS.post(new ClickEvent(button));
123+
}
120124
}
121125

122126
@Inject(method = "init", at = @At("RETURN"))
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
package clientapi.util.io;
2+
3+
/**
4+
* @author Brady
5+
* @since 4/13/2018 4:59 PM
6+
*/
7+
public enum MouseKeyTracker {
8+
9+
INSTANCE;
10+
11+
/**
12+
* Array that maps the mouse button id (the index in the array) to
13+
* the state of that button. Used to track when the state of the
14+
* button changes so that a ClickEvent is correctly dispatched.
15+
*/
16+
private boolean[] mouseKeyStates = new boolean[0];
17+
18+
public final boolean wasButtonPressed(int button, boolean newState) {
19+
// Check if the state array needs to be expanded
20+
if (mouseKeyStates.length <= button) {
21+
// Create a new array with the required size
22+
boolean[] expandedArray = new boolean[button + 1];
23+
// Copy the elements from the old array to the new one
24+
System.arraycopy(mouseKeyStates, 0, expandedArray, 0, mouseKeyStates.length);
25+
// Set the state array to the expanded one
26+
mouseKeyStates = expandedArray;
27+
}
28+
29+
// Return true if the mouse is pressed in the new state and wasn't in the old state
30+
return newState && !mouseKeyStates[button];
31+
}
32+
}

0 commit comments

Comments
 (0)