Skip to content

Commit

Permalink
Fixed the problem that all key bindings cannot be triggered because …
Browse files Browse the repository at this point in the history
…other mods cancel the key release event

    The solution comes from malilib maruohon/malilib#59
  • Loading branch information
xiaocihua committed Dec 11, 2022
1 parent ee69195 commit 09223c9
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 3 deletions.
2 changes: 1 addition & 1 deletion gradle.properties
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,6 @@ loader_version=0.14.10
fabric_version=0.64.0+1.19.2

# Mod Properties
mod_version=0.2.1
mod_version=0.2.2
maven_group=io.github.xiaocihua.stacktonearbychests
archives_base_name=stack-to-nearby-chests
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
import net.minecraft.client.util.InputUtil;
import net.minecraft.text.Text;
import net.minecraft.util.ActionResult;
import org.lwjgl.glfw.GLFW;

import java.util.ArrayList;
import java.util.List;
Expand Down Expand Up @@ -44,6 +45,27 @@ public static void init() {
});
}

// Fix the problem that all key bindings cannot be triggered because other mods cancel the key release event
// The solution comes from malilib https://github.com/maruohon/malilib/issues/59
public static void reCheckPressedKeys() {
for (int key : PRESSING_KEYS) {
if (!isKeyPressed(key)) {
PRESSING_KEYS.rem(key);
}
}
}

public static boolean isKeyPressed(int key) {
long window = MinecraftClient.getInstance().getWindow().getHandle();
return isMouseButton(key)
? GLFW.glfwGetMouseButton(window, key + MOUSE_BUTTON_CODE_OFFSET) == GLFW.GLFW_PRESS
: InputUtil.isKeyPressed(window, key);
}

private static boolean isMouseButton(int key) {
return key < -1;
}

public void addKey(int key) {
if (keys.size() >= 3) {
keys.clear();
Expand Down Expand Up @@ -72,8 +94,8 @@ public Text getLocalizedText() {

String localized = keys.stream()
.map(key -> {
if (key < -1) {
return InputUtil.Type.MOUSE.createFromCode(key + KeySequence.MOUSE_BUTTON_CODE_OFFSET);
if (isMouseButton(key)) {
return InputUtil.Type.MOUSE.createFromCode(key + MOUSE_BUTTON_CODE_OFFSET);
} else {
return InputUtil.Type.KEYSYM.createFromCode(key);
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package io.github.xiaocihua.stacktonearbychests.mixin;

import io.github.xiaocihua.stacktonearbychests.KeySequence;
import io.github.xiaocihua.stacktonearbychests.LockedSlots;
import io.github.xiaocihua.stacktonearbychests.event.SetScreenCallback;
import net.minecraft.client.MinecraftClient;
Expand Down Expand Up @@ -27,4 +28,9 @@ private void onSetScreen(Screen screen, CallbackInfo ci) {
ci.cancel();
}
}

@Inject(method = "tick", at = @At("RETURN"))
private void onTick(CallbackInfo ci) {
KeySequence.reCheckPressedKeys();
}
}

0 comments on commit 09223c9

Please sign in to comment.