KeyCodeBuilder.java 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  1. package me.shedaniel.clothconfig2.impl.builders;
  2. import me.shedaniel.clothconfig2.api.Modifier;
  3. import me.shedaniel.clothconfig2.api.ModifierKeyCode;
  4. import me.shedaniel.clothconfig2.gui.entries.KeyCodeEntry;
  5. import net.minecraft.client.util.InputUtil;
  6. import org.jetbrains.annotations.NotNull;
  7. import org.jetbrains.annotations.Nullable;
  8. import java.util.Optional;
  9. import java.util.function.Consumer;
  10. import java.util.function.Function;
  11. import java.util.function.Supplier;
  12. public class KeyCodeBuilder extends FieldBuilder<ModifierKeyCode, KeyCodeEntry> {
  13. @Nullable private Consumer<ModifierKeyCode> saveConsumer = null;
  14. @NotNull private Function<ModifierKeyCode, Optional<String[]>> tooltipSupplier = bool -> Optional.empty();
  15. private ModifierKeyCode value;
  16. private boolean allowKey = true, allowMouse = true, allowModifiers = true;
  17. public KeyCodeBuilder(String resetButtonKey, String fieldNameKey, ModifierKeyCode value) {
  18. super(resetButtonKey, fieldNameKey);
  19. this.value = ModifierKeyCode.copyOf(value);
  20. }
  21. public KeyCodeBuilder setAllowModifiers(boolean allowModifiers) {
  22. this.allowModifiers = allowModifiers;
  23. if (!allowModifiers)
  24. value.setModifier(Modifier.none());
  25. return this;
  26. }
  27. public KeyCodeBuilder setAllowKey(boolean allowKey) {
  28. if (!allowMouse && !allowKey)
  29. throw new IllegalArgumentException();
  30. this.allowKey = allowKey;
  31. return this;
  32. }
  33. public KeyCodeBuilder setAllowMouse(boolean allowMouse) {
  34. if (!allowKey && !allowMouse)
  35. throw new IllegalArgumentException();
  36. this.allowMouse = allowMouse;
  37. return this;
  38. }
  39. public KeyCodeBuilder setErrorSupplier(@Nullable Function<InputUtil.KeyCode, Optional<String>> errorSupplier) {
  40. return setModifierErrorSupplier(keyCode -> errorSupplier.apply(keyCode.getKeyCode()));
  41. }
  42. public KeyCodeBuilder setModifierErrorSupplier(@Nullable Function<ModifierKeyCode, Optional<String>> errorSupplier) {
  43. this.errorSupplier = errorSupplier;
  44. return this;
  45. }
  46. public KeyCodeBuilder requireRestart() {
  47. requireRestart(true);
  48. return this;
  49. }
  50. public KeyCodeBuilder setSaveConsumer(Consumer<InputUtil.KeyCode> saveConsumer) {
  51. return setModifierSaveConsumer(keyCode -> saveConsumer.accept(keyCode.getKeyCode()));
  52. }
  53. public KeyCodeBuilder setDefaultValue(Supplier<InputUtil.KeyCode> defaultValue) {
  54. return setModifierDefaultValue(() -> ModifierKeyCode.of(defaultValue.get(), Modifier.none()));
  55. }
  56. public KeyCodeBuilder setModifierSaveConsumer(Consumer<ModifierKeyCode> saveConsumer) {
  57. this.saveConsumer = saveConsumer;
  58. return this;
  59. }
  60. public KeyCodeBuilder setModifierDefaultValue(Supplier<ModifierKeyCode> defaultValue) {
  61. this.defaultValue = defaultValue;
  62. return this;
  63. }
  64. public KeyCodeBuilder setDefaultValue(InputUtil.KeyCode defaultValue) {
  65. return setDefaultValue(ModifierKeyCode.of(defaultValue, Modifier.none()));
  66. }
  67. public KeyCodeBuilder setDefaultValue(ModifierKeyCode defaultValue) {
  68. this.defaultValue = () -> defaultValue;
  69. return this;
  70. }
  71. public KeyCodeBuilder setTooltipSupplier(@NotNull Function<InputUtil.KeyCode, Optional<String[]>> tooltipSupplier) {
  72. return setModifierTooltipSupplier(keyCode -> tooltipSupplier.apply(keyCode.getKeyCode()));
  73. }
  74. public KeyCodeBuilder setModifierTooltipSupplier(@NotNull Function<ModifierKeyCode, Optional<String[]>> tooltipSupplier) {
  75. this.tooltipSupplier = tooltipSupplier;
  76. return this;
  77. }
  78. public KeyCodeBuilder setTooltipSupplier(@NotNull Supplier<Optional<String[]>> tooltipSupplier) {
  79. this.tooltipSupplier = bool -> tooltipSupplier.get();
  80. return this;
  81. }
  82. public KeyCodeBuilder setTooltip(Optional<String[]> tooltip) {
  83. this.tooltipSupplier = bool -> tooltip;
  84. return this;
  85. }
  86. public KeyCodeBuilder setTooltip(@Nullable String... tooltip) {
  87. this.tooltipSupplier = bool -> Optional.ofNullable(tooltip);
  88. return this;
  89. }
  90. @NotNull
  91. @Override
  92. public KeyCodeEntry build() {
  93. KeyCodeEntry entry = new KeyCodeEntry(getFieldNameKey(), value, getResetButtonKey(), defaultValue, saveConsumer, null, isRequireRestart());
  94. entry.setTooltipSupplier(() -> tooltipSupplier.apply(entry.getValue()));
  95. if (errorSupplier != null)
  96. entry.setErrorSupplier(() -> errorSupplier.apply(entry.getValue()));
  97. entry.setAllowKey(allowKey);
  98. entry.setAllowMouse(allowMouse);
  99. entry.setAllowModifiers(allowModifiers);
  100. return entry;
  101. }
  102. }