KeyCodeBuilder.java 4.8 KB

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