Added hotkey utils methods (#599)

Co-authored-by: slprime <[email protected]>
This commit is contained in:
slprimeandslprime authored and GitHub committed 2025-01-26 12:59:16 -06:00
1 parent 48934d621f
commit ac50d1b27e
7 files changed
+205 -68

No files matched your search

@@ -61,6 +61,7 @@ import codechicken.nei.recipe.GuiRecipeTab;
import codechicken.nei.recipe.IRecipeHandler;
import codechicken.nei.recipe.RecipeCatalysts;
import codechicken.nei.recipe.RecipeInfo;
import codechicken.nei.util.NEIKeyboardUtils;
import codechicken.obfuscator.ObfuscationRun;
public class NEIClientConfig {
@@ -789,7 +790,7 @@ public class NEIClientConfig {
private static final Map<String, String> keySettings = new HashMap<>();
public static int getKeyBinding(String string) {
final String key = keySettings.computeIfAbsent(string, (s) -> "keys." + s);
final String key = keySettings.computeIfAbsent(string, s -> "keys." + s);
return getSetting(key).getIntValue(Keyboard.KEY_NONE);
}
@@ -799,17 +800,37 @@ public class NEIClientConfig {
public static boolean isKeyHashDown(String string) {
final int hash = getKeyBinding(string);
return hash != Keyboard.CHAR_NONE && hash == NEIClientUtils.getKeyHash();
if (hash != Keyboard.CHAR_NONE && Keyboard.getEventKeyState()) {
return KeyManager.keyStates.containsKey(string) ? hash == Keyboard.getEventKey()
: hash == NEIClientUtils.getKeyHash();
}
return false;
}
public static String getKeyName(String keyBind) {
return getKeyName(keyBind, 0);
}
public static String getKeyName(String keyBind, int meta, int mouseBind) {
final int hash = getKeyBinding(keyBind);
if (hash == Keyboard.CHAR_NONE) {
return null;
}
return NEIClientUtils.getKeyName(hash);
return NEIClientUtils.getKeyName(hash + meta, mouseBind);
}
public static String getKeyName(String keyBind, int meta) {
final int hash = getKeyBinding(keyBind);
if (hash == Keyboard.CHAR_NONE) {
return null;
}
return NEIKeyboardUtils.getKeyName(hash + meta);
}
public static void bootNEI(World world) {
@@ -50,6 +50,8 @@ import codechicken.lib.util.LangProxy;
import codechicken.nei.api.GuiInfo;
import codechicken.nei.api.IInfiniteItemHandler;
import codechicken.nei.api.ItemInfo;
import codechicken.nei.util.NEIKeyboardUtils;
import codechicken.nei.util.NEIMouseUtils;
public class NEIClientUtils extends NEIServerUtils {
@@ -57,25 +59,10 @@ public class NEIClientUtils extends NEIServerUtils {
/** Formats a number with group separator and at most 2 fraction digits. */
private static final Map<Locale, DecimalFormat> decimalFormatters = new HashMap<>();
private static final Map<Integer, Integer> keyHashMap = new HashMap<>();
public static final int ALT_HASH = 1 << 27;
public static final int SHIFT_HASH = 1 << 26;
public static final int CTRL_HASH = 1 << 25;
static {
keyHashMap.put(Keyboard.KEY_LSHIFT, SHIFT_HASH);
keyHashMap.put(Keyboard.KEY_RSHIFT, SHIFT_HASH);
keyHashMap.put(Keyboard.KEY_LCONTROL, CTRL_HASH);
keyHashMap.put(Keyboard.KEY_LCONTROL, CTRL_HASH);
keyHashMap.put(Keyboard.KEY_LMETA, CTRL_HASH);
keyHashMap.put(Keyboard.KEY_RMETA, CTRL_HASH);
keyHashMap.put(Keyboard.KEY_LMENU, ALT_HASH);
keyHashMap.put(Keyboard.KEY_LMENU, ALT_HASH);
}
public static final int ALT_HASH = NEIKeyboardUtils.ALT_HASH;
public static final int SHIFT_HASH = NEIKeyboardUtils.SHIFT_HASH;
public static final int CTRL_HASH = NEIKeyboardUtils.CTRL_HASH;
public static Minecraft mc() {
return Minecraft.getMinecraft();
@@ -452,7 +439,7 @@ public class NEIClientUtils extends NEIServerUtils {
if (Keyboard.getEventKeyState()) {
final int keycode = Keyboard.getEventKey();
if (!keyHashMap.containsKey(keycode)) {
if (!NEIKeyboardUtils.isHashKey(keycode)) {
return getMetaHash() + keycode;
}
}
@@ -460,45 +447,22 @@ public class NEIClientUtils extends NEIServerUtils {
return Keyboard.CHAR_NONE;
}
public static String getKeyHashName(int keyBind) {
public static String getKeyName(int keyBind, int mouseBind) {
StringJoiner keyText = new StringJoiner(" + ");
String keyHash = keyBind == Keyboard.KEY_NONE ? "" : NEIKeyboardUtils.getKeyName(keyBind);
String mouseHash = mouseBind == NEIMouseUtils.MOUSE_BTN_NONE ? "" : NEIMouseUtils.getKeyName(mouseBind);
if ((keyBind & CTRL_HASH) != 0) {
keyText.add(translate(Minecraft.isRunningOnMac ? "key.ctrl.mac" : "key.ctrl"));
if (!keyHash.isEmpty()) {
keyText.add(keyHash);
}
if ((keyBind & SHIFT_HASH) != 0) {
keyText.add(translate("key.shift"));
}
if ((keyBind & ALT_HASH) != 0) {
keyText.add(translate("key.alt"));
if (!mouseHash.isEmpty()) {
keyText.add(mouseHash);
}
return keyText.toString();
}
public static String getKeyName(int keyBind) {
keyBind = keyHashMap.getOrDefault(keyBind, keyBind);
StringJoiner keyText = new StringJoiner(" + ");
String hashText = getKeyHashName(keyBind);
int keyID = unHashKey(keyBind);
if (!hashText.isEmpty()) {
keyText.add(hashText);
}
if (keyID != Keyboard.CHAR_NONE || hashText.isEmpty()) {
keyText.add(Keyboard.getKeyName(keyID));
}
return keyText.toString();
}
public static int unHashKey(int keyBind) {
return keyBind & ~(CTRL_HASH | SHIFT_HASH | ALT_HASH);
}
public static void playClickSound() {
mc().getSoundHandler()
.playSound(PositionedSoundRecord.func_147674_a(new ResourceLocation("gui.button.press"), 1.0F));
@@ -33,6 +33,7 @@ import codechicken.nei.api.ItemFilter;
import codechicken.nei.api.ItemFilter.ItemFilterProvider;
import codechicken.nei.guihook.GuiContainerManager;
import codechicken.nei.guihook.IContainerTooltipHandler;
import codechicken.nei.util.NEIMouseUtils;
public class SubsetWidget extends Button implements ItemFilterProvider, IContainerTooltipHandler {
@@ -841,21 +842,21 @@ public class SubsetWidget extends Button implements ItemFilterProvider, IContain
if (NEIClientConfig.canCheatItem(SubsetWidget.hoverStack)) {
hotkeys.put(
NEIClientUtils.translate("subsets.item.cheat.key"),
NEIClientUtils.getKeyName(NEIClientUtils.CTRL_HASH, NEIMouseUtils.MOUSE_BTN_LMB),
NEIClientUtils.translate("subsets.item.cheat"));
}
if (SubsetWidget.enableSearchBySubsets) {
hotkeys.put(
NEIClientUtils.translate("subsets.item.search.key"),
NEIClientUtils.getKeyName(NEIClientUtils.SHIFT_HASH, NEIMouseUtils.MOUSE_BTN_LMB),
NEIClientUtils.translate("subsets.item.search"));
}
hotkeys.put(
NEIClientUtils.translate("subsets.item.show.key"),
NEIMouseUtils.getKeyName(NEIMouseUtils.MOUSE_BTN_LMB),
NEIClientUtils.translate("subsets.item.show"));
hotkeys.put(
NEIClientUtils.translate("subsets.item.hide.key"),
NEIMouseUtils.getKeyName(NEIMouseUtils.MOUSE_BTN_RMB),
NEIClientUtils.translate("subsets.item.hide"));
} else if (SubsetWidget.hoverTag != null) {
hotkeys.put(
@@ -864,12 +865,16 @@ public class SubsetWidget extends Button implements ItemFilterProvider, IContain
if (SubsetWidget.enableSearchBySubsets) {
hotkeys.put(
NEIClientUtils.translate("subsets.tag.search.key"),
NEIClientUtils.getKeyName(NEIClientUtils.SHIFT_HASH, NEIMouseUtils.MOUSE_BTN_LMB),
NEIClientUtils.translate("subsets.tag.search"));
}
hotkeys.put(NEIClientUtils.translate("subsets.tag.show.key"), NEIClientUtils.translate("subsets.tag.show"));
hotkeys.put(NEIClientUtils.translate("subsets.tag.hide.key"), NEIClientUtils.translate("subsets.tag.hide"));
hotkeys.put(
NEIMouseUtils.getKeyName(NEIMouseUtils.MOUSE_BTN_LMB),
NEIClientUtils.translate("subsets.tag.show"));
hotkeys.put(
NEIMouseUtils.getKeyName(NEIMouseUtils.MOUSE_BTN_RMB),
NEIClientUtils.translate("subsets.tag.hide"));
}
return hotkeys;
@@ -8,6 +8,7 @@ import java.awt.Rectangle;
import org.lwjgl.input.Keyboard;
import codechicken.nei.NEIClientUtils;
import codechicken.nei.util.NEIKeyboardUtils;
public class OptionKeyBind extends OptionButton {
@@ -123,10 +124,10 @@ public class OptionKeyBind extends OptionButton {
public String getButtonText() {
if (editing) {
return NEIClientUtils.getKeyHashName(NEIClientUtils.getMetaHash());
return NEIKeyboardUtils.getHashName(NEIClientUtils.getMetaHash());
}
return NEIClientUtils.getKeyName(Math.max(getValue(), 0));
return NEIKeyboardUtils.getKeyName(Math.max(getValue(), 0));
}
@Override
@@ -0,0 +1,80 @@
package codechicken.nei.util;
import java.util.HashMap;
import java.util.Map;
import java.util.StringJoiner;
import net.minecraft.client.Minecraft;
import org.lwjgl.input.Keyboard;
import codechicken.nei.NEIClientUtils;
public class NEIKeyboardUtils {
public static final int ALT_HASH = 1 << 27;
public static final int SHIFT_HASH = 1 << 26;
public static final int CTRL_HASH = 1 << 25;
private static final Map<Integer, Integer> keyHashMap = new HashMap<>();
static {
keyHashMap.put(Keyboard.KEY_LSHIFT, SHIFT_HASH);
keyHashMap.put(Keyboard.KEY_RSHIFT, SHIFT_HASH);
keyHashMap.put(Keyboard.KEY_LCONTROL, CTRL_HASH);
keyHashMap.put(Keyboard.KEY_LCONTROL, CTRL_HASH);
keyHashMap.put(Keyboard.KEY_LMETA, CTRL_HASH);
keyHashMap.put(Keyboard.KEY_RMETA, CTRL_HASH);
keyHashMap.put(Keyboard.KEY_LMENU, ALT_HASH);
keyHashMap.put(Keyboard.KEY_LMENU, ALT_HASH);
}
private NEIKeyboardUtils() {}
public static boolean isHashKey(int keycode) {
return keyHashMap.containsKey(keycode);
}
public static String getHashName(int keyBind) {
StringJoiner keyText = new StringJoiner(" + ");
if ((keyBind & CTRL_HASH) != 0) {
keyText.add(NEIClientUtils.translate(Minecraft.isRunningOnMac ? "key.ctrl.mac" : "key.ctrl"));
}
if ((keyBind & SHIFT_HASH) != 0) {
keyText.add(NEIClientUtils.translate("key.shift"));
}
if ((keyBind & ALT_HASH) != 0) {
keyText.add(NEIClientUtils.translate("key.alt"));
}
return keyText.toString();
}
public static String getKeyName(int keyBind) {
keyBind = keyHashMap.getOrDefault(keyBind, keyBind);
StringJoiner keyText = new StringJoiner(" + ");
String hashText = getHashName(keyBind);
int keyID = unhash(keyBind);
if (!hashText.isEmpty()) {
keyText.add(hashText);
}
if (keyID != Keyboard.CHAR_NONE || hashText.isEmpty()) {
keyText.add(Keyboard.getKeyName(keyID));
}
return keyText.toString();
}
public static int unhash(int keyBind) {
return keyBind & ~(CTRL_HASH | SHIFT_HASH | ALT_HASH);
}
}
@@ -0,0 +1,67 @@
package codechicken.nei.util;
import java.util.StringJoiner;
import codechicken.nei.NEIClientUtils;
public class NEIMouseUtils {
public static final int MOUSE_BTN_NONE = Integer.MIN_VALUE;
public static final int MOUSE_BTN_LMB = 0;
public static final int MOUSE_BTN_RMB = 1;
public static final int MOUSE_BTN_MMB = 2;
public static final int MOUSE_SCROLL = 1 << 27;
public static final int MOUSE_DRAG = 1 << 26;
private NEIMouseUtils() {}
public static String getHashName(int mouseBind) {
StringJoiner mouseText = new StringJoiner(" + ");
if ((mouseBind & MOUSE_SCROLL) != 0) {
mouseText.add(NEIClientUtils.translate("mouse.scroll"));
}
if ((mouseBind & MOUSE_DRAG) != 0) {
mouseText.add(NEIClientUtils.translate("mouse.drag"));
}
return mouseText.toString();
}
public static String getKeyName(int mouseBind) {
StringJoiner mouseText = new StringJoiner(" + ");
String hashText = getHashName(mouseBind < 0 ? mouseBind - MOUSE_BTN_NONE : mouseBind);
if (mouseBind >= 0) {
int button = unhash(mouseBind);
switch (button) {
case 0:
mouseText.add(NEIClientUtils.translate("mouse.left"));
break;
case 1:
mouseText.add(NEIClientUtils.translate("mouse.right"));
break;
case 2:
mouseText.add(NEIClientUtils.translate("mouse.middle"));
break;
default:
mouseText.add(NEIClientUtils.translate("mouse.other", button));
break;
}
}
if (!hashText.isEmpty()) {
mouseText.add(hashText);
}
return mouseText.toString();
}
public static int unhash(int mouseBind) {
return mouseBind & ~(MOUSE_SCROLL | MOUSE_DRAG);
}
}