TextFieldBuilder.java 2.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
  1. package me.shedaniel.clothconfig2.impl.builders;
  2. import me.shedaniel.clothconfig2.gui.entries.StringListEntry;
  3. import net.fabricmc.api.EnvType;
  4. import net.fabricmc.api.Environment;
  5. import net.minecraft.network.chat.Component;
  6. import org.jetbrains.annotations.NotNull;
  7. import java.util.Objects;
  8. import java.util.Optional;
  9. import java.util.function.Consumer;
  10. import java.util.function.Function;
  11. import java.util.function.Supplier;
  12. @Environment(EnvType.CLIENT)
  13. public class TextFieldBuilder extends FieldBuilder<String, StringListEntry> {
  14. private Consumer<String> saveConsumer = null;
  15. private Function<String, Optional<Component[]>> tooltipSupplier = str -> Optional.empty();
  16. private final String value;
  17. public TextFieldBuilder(Component resetButtonKey, Component fieldNameKey, String value) {
  18. super(resetButtonKey, fieldNameKey);
  19. Objects.requireNonNull(value);
  20. this.value = value;
  21. }
  22. public TextFieldBuilder setErrorSupplier(Function<String, Optional<Component>> errorSupplier) {
  23. this.errorSupplier = errorSupplier;
  24. return this;
  25. }
  26. public TextFieldBuilder requireRestart() {
  27. requireRestart(true);
  28. return this;
  29. }
  30. public TextFieldBuilder setSaveConsumer(Consumer<String> saveConsumer) {
  31. this.saveConsumer = saveConsumer;
  32. return this;
  33. }
  34. public TextFieldBuilder setDefaultValue(Supplier<String> defaultValue) {
  35. this.defaultValue = defaultValue;
  36. return this;
  37. }
  38. public TextFieldBuilder setDefaultValue(String defaultValue) {
  39. this.defaultValue = () -> Objects.requireNonNull(defaultValue);
  40. return this;
  41. }
  42. public TextFieldBuilder setTooltipSupplier(Supplier<Optional<Component[]>> tooltipSupplier) {
  43. this.tooltipSupplier = str -> tooltipSupplier.get();
  44. return this;
  45. }
  46. public TextFieldBuilder setTooltipSupplier(Function<String, Optional<Component[]>> tooltipSupplier) {
  47. this.tooltipSupplier = tooltipSupplier;
  48. return this;
  49. }
  50. public TextFieldBuilder setTooltip(Optional<Component[]> tooltip) {
  51. this.tooltipSupplier = str -> tooltip;
  52. return this;
  53. }
  54. public TextFieldBuilder setTooltip(Component... tooltip) {
  55. this.tooltipSupplier = str -> Optional.ofNullable(tooltip);
  56. return this;
  57. }
  58. @NotNull
  59. @Override
  60. public StringListEntry build() {
  61. StringListEntry entry = new StringListEntry(getFieldNameKey(), value, getResetButtonKey(), defaultValue, saveConsumer, null, isRequireRestart());
  62. entry.setTooltipSupplier(() -> tooltipSupplier.apply(entry.getValue()));
  63. if (errorSupplier != null)
  64. entry.setErrorSupplier(() -> errorSupplier.apply(entry.getValue()));
  65. return entry;
  66. }
  67. }