StringFieldBuilder.java 2.6 KB

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