ModifierKeyCodeImpl.java 2.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
  1. package me.shedaniel.clothconfig2.impl;
  2. import me.shedaniel.clothconfig2.api.Modifier;
  3. import me.shedaniel.clothconfig2.api.ModifierKeyCode;
  4. import net.minecraft.client.resource.language.I18n;
  5. import net.minecraft.client.util.InputUtil;
  6. import java.util.Objects;
  7. public class ModifierKeyCodeImpl implements ModifierKeyCode {
  8. private InputUtil.KeyCode keyCode;
  9. private Modifier modifier;
  10. public ModifierKeyCodeImpl() {
  11. }
  12. @Override
  13. public InputUtil.KeyCode getKeyCode() {
  14. return keyCode;
  15. }
  16. @Override
  17. public Modifier getModifier() {
  18. return modifier;
  19. }
  20. @Override
  21. public ModifierKeyCode setKeyCode(InputUtil.KeyCode keyCode) {
  22. this.keyCode = keyCode.getCategory().createFromCode(keyCode.getKeyCode());
  23. if (keyCode.equals(InputUtil.UNKNOWN_KEYCODE))
  24. setModifier(Modifier.none());
  25. return this;
  26. }
  27. @Override
  28. public ModifierKeyCode setModifier(Modifier modifier) {
  29. this.modifier = Modifier.of(modifier.getValue());
  30. return this;
  31. }
  32. @Override
  33. public String toString() {
  34. String string_1 = this.keyCode.getName();
  35. int int_1 = this.keyCode.getKeyCode();
  36. String string_2 = null;
  37. switch (this.keyCode.getCategory()) {
  38. case KEYSYM:
  39. string_2 = InputUtil.getKeycodeName(int_1);
  40. break;
  41. case SCANCODE:
  42. string_2 = InputUtil.getScancodeName(int_1);
  43. break;
  44. case MOUSE:
  45. String string_3 = I18n.translate(string_1);
  46. string_2 = Objects.equals(string_3, string_1) ? I18n.translate(InputUtil.Type.MOUSE.getName(), int_1 + 1) : string_3;
  47. }
  48. String base = string_2 == null ? I18n.translate(string_1) : string_2;
  49. if (modifier.hasShift())
  50. base = I18n.translate("modifier.cloth-config.shift", base);
  51. if (modifier.hasControl())
  52. base = I18n.translate("modifier.cloth-config.ctrl", base);
  53. if (modifier.hasAlt())
  54. base = I18n.translate("modifier.cloth-config.alt", base);
  55. return base;
  56. }
  57. @Override
  58. public boolean equals(Object o) {
  59. if (this == o)
  60. return true;
  61. if (!(o instanceof ModifierKeyCode))
  62. return false;
  63. ModifierKeyCode that = (ModifierKeyCode) o;
  64. return keyCode.equals(that.getKeyCode()) && modifier.equals(that.getModifier());
  65. }
  66. @Override
  67. public int hashCode() {
  68. int result = keyCode != null ? keyCode.hashCode() : 0;
  69. result = 31 * result + (modifier != null ? modifier.hashCode() : 0);
  70. return result;
  71. }
  72. }