Files
2019-02-24 22:29:41 +08:00

196 lines
8.3 KiB
Diff

--- ../src-base/minecraft/net/minecraft/client/settings/KeyBinding.java
+++ ../src-work/minecraft/net/minecraft/client/settings/KeyBinding.java
@@ -15,7 +15,7 @@
public class KeyBinding implements Comparable<KeyBinding>
{
private static final Map<String, KeyBinding> KEYBIND_ARRAY = Maps.<String, KeyBinding>newHashMap();
- private static final IntHashMap<KeyBinding> HASH = new IntHashMap<KeyBinding>();
+ private static final net.minecraftforge.client.settings.KeyBindingMap HASH = new net.minecraftforge.client.settings.KeyBindingMap();
private static final Set<String> KEYBIND_SET = Sets.<String>newHashSet();
private static final Map<String, Integer> CATEGORY_ORDER = Maps.<String, Integer>newHashMap();
private final String keyDescription;
@@ -29,7 +29,7 @@
{
if (keyCode != 0)
{
- KeyBinding keybinding = HASH.lookup(keyCode);
+ KeyBinding keybinding = HASH.lookupActive(keyCode);
if (keybinding != null)
{
@@ -42,7 +42,7 @@
{
if (keyCode != 0)
{
- KeyBinding keybinding = HASH.lookup(keyCode);
+ for (KeyBinding keybinding : HASH.lookupAll(keyCode))
if (keybinding != null)
{
@@ -102,7 +102,7 @@
public boolean isKeyDown()
{
- return this.pressed;
+ return this.pressed && getKeyConflictContext().isActive() && getKeyModifier().isActive(getKeyConflictContext());
}
public String getKeyCategory()
@@ -151,9 +151,146 @@
public int compareTo(KeyBinding p_compareTo_1_)
{
- return this.keyCategory.equals(p_compareTo_1_.keyCategory) ? I18n.format(this.keyDescription).compareTo(I18n.format(p_compareTo_1_.keyDescription)) : ((Integer)CATEGORY_ORDER.get(this.keyCategory)).compareTo(CATEGORY_ORDER.get(p_compareTo_1_.keyCategory));
+ if (this.keyCategory.equals(p_compareTo_1_.keyCategory)) return I18n.format(this.keyDescription).compareTo(I18n.format(p_compareTo_1_.keyDescription));
+ Integer tCat = CATEGORY_ORDER.get(this.keyCategory);
+ Integer oCat = CATEGORY_ORDER.get(p_compareTo_1_.keyCategory);
+ if (tCat == null && oCat != null) return 1;
+ if (tCat != null && oCat == null) return -1;
+ if (tCat == null && oCat == null) return I18n.format(this.keyCategory).compareTo(I18n.format(p_compareTo_1_.keyCategory));
+ return tCat.compareTo(oCat);
}
+ /****************** Forge Start *****************************/
+ private net.minecraftforge.client.settings.KeyModifier keyModifierDefault = net.minecraftforge.client.settings.KeyModifier.NONE;
+ private net.minecraftforge.client.settings.KeyModifier keyModifier = net.minecraftforge.client.settings.KeyModifier.NONE;
+ private net.minecraftforge.client.settings.IKeyConflictContext keyConflictContext = net.minecraftforge.client.settings.KeyConflictContext.UNIVERSAL;
+
+ /**
+ * Convenience constructor for creating KeyBindings with keyConflictContext set.
+ */
+ public KeyBinding(String description, net.minecraftforge.client.settings.IKeyConflictContext keyConflictContext, int keyCode, String category)
+ {
+ this(description, keyConflictContext, net.minecraftforge.client.settings.KeyModifier.NONE, keyCode, category);
+ }
+
+ /**
+ * Convenience constructor for creating KeyBindings with keyConflictContext and keyModifier set.
+ */
+ public KeyBinding(String description, net.minecraftforge.client.settings.IKeyConflictContext keyConflictContext, net.minecraftforge.client.settings.KeyModifier keyModifier, int keyCode, String category)
+ {
+ this.keyDescription = description;
+ this.keyCode = keyCode;
+ this.keyCodeDefault = keyCode;
+ this.keyCategory = category;
+ this.keyConflictContext = keyConflictContext;
+ this.keyModifier = keyModifier;
+ this.keyModifierDefault = keyModifier;
+ if (this.keyModifier.matches(keyCode))
+ {
+ this.keyModifier = net.minecraftforge.client.settings.KeyModifier.NONE;
+ }
+ KEYBIND_ARRAY.put(description, this);
+ HASH.addKey(keyCode, this);
+ KEYBIND_SET.add(category);
+ }
+
+ /**
+ * Checks that the key conflict context and modifier are active, and that the keyCode matches this binding.
+ */
+ public boolean isActiveAndMatches(int keyCode)
+ {
+ return keyCode != 0 && keyCode == this.getKeyCode() && getKeyConflictContext().isActive() && getKeyModifier().isActive(getKeyConflictContext());
+ }
+
+ public void setKeyConflictContext(net.minecraftforge.client.settings.IKeyConflictContext keyConflictContext)
+ {
+ this.keyConflictContext = keyConflictContext;
+ }
+
+ public net.minecraftforge.client.settings.IKeyConflictContext getKeyConflictContext()
+ {
+ return keyConflictContext;
+ }
+
+ public net.minecraftforge.client.settings.KeyModifier getKeyModifierDefault()
+ {
+ return keyModifierDefault;
+ }
+
+ public net.minecraftforge.client.settings.KeyModifier getKeyModifier()
+ {
+ return keyModifier;
+ }
+
+ public void setKeyModifierAndCode(net.minecraftforge.client.settings.KeyModifier keyModifier, int keyCode)
+ {
+ this.keyCode = keyCode;
+ if (keyModifier.matches(keyCode))
+ {
+ keyModifier = net.minecraftforge.client.settings.KeyModifier.NONE;
+ }
+ HASH.removeKey(this);
+ this.keyModifier = keyModifier;
+ HASH.addKey(keyCode, this);
+ }
+
+ public void setToDefault()
+ {
+ setKeyModifierAndCode(getKeyModifierDefault(), getKeyCodeDefault());
+ }
+
+ public boolean isSetToDefaultValue()
+ {
+ return getKeyCode() == getKeyCodeDefault() && getKeyModifier() == getKeyModifierDefault();
+ }
+
+ /**
+ * Returns true when the other keyBinding conflicts with this one
+ */
+ public boolean conflicts(KeyBinding other)
+ {
+ if (getKeyConflictContext().conflicts(other.getKeyConflictContext()) || other.getKeyConflictContext().conflicts(getKeyConflictContext()))
+ {
+ net.minecraftforge.client.settings.KeyModifier keyModifier = getKeyModifier();
+ net.minecraftforge.client.settings.KeyModifier otherKeyModifier = other.getKeyModifier();
+ if (keyModifier.matches(other.getKeyCode()) || otherKeyModifier.matches(getKeyCode()))
+ {
+ return true;
+ }
+ else if (getKeyCode() == other.getKeyCode())
+ {
+ return keyModifier == otherKeyModifier ||
+ // IN_GAME key contexts have a conflict when at least one modifier is NONE.
+ // For example: If you hold shift to crouch, you can still press E to open your inventory. This means that a Shift+E hotkey is in conflict with E.
+ // GUI and other key contexts do not have this limitation.
+ (getKeyConflictContext().conflicts(net.minecraftforge.client.settings.KeyConflictContext.IN_GAME) &&
+ (keyModifier == net.minecraftforge.client.settings.KeyModifier.NONE || otherKeyModifier == net.minecraftforge.client.settings.KeyModifier.NONE));
+ }
+ }
+ return false;
+ }
+
+ /**
+ * Returns true when one of the bindings' key codes conflicts with the other's modifier.
+ */
+ public boolean hasKeyCodeModifierConflict(KeyBinding other)
+ {
+ if (getKeyConflictContext().conflicts(other.getKeyConflictContext()) || other.getKeyConflictContext().conflicts(getKeyConflictContext()))
+ {
+ if (getKeyModifier().matches(other.getKeyCode()) || other.getKeyModifier().matches(getKeyCode()))
+ {
+ return true;
+ }
+ }
+ return false;
+ }
+
+ public String getDisplayName()
+ {
+ return getKeyModifier().getLocalizedComboName(getKeyCode());
+ }
+ /****************** Forge End *****************************/
+
public static Supplier<String> getDisplayString(String key)
{
KeyBinding keybinding = KEYBIND_ARRAY.get(key);
@@ -162,7 +299,7 @@
return key;
} : () ->
{
- return GameSettings.getKeyDisplayString(keybinding.getKeyCode());
+ return keybinding.getDisplayName();
};
}