DoubleFieldBuilder.java 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  1. package me.shedaniel.clothconfig2.impl.builders;
  2. import me.shedaniel.clothconfig2.gui.entries.DoubleListEntry;
  3. import org.jetbrains.annotations.NotNull;
  4. import java.util.Optional;
  5. import java.util.function.Consumer;
  6. import java.util.function.Function;
  7. import java.util.function.Supplier;
  8. public class DoubleFieldBuilder extends FieldBuilder<Double, DoubleListEntry> {
  9. private Consumer<Double> saveConsumer = null;
  10. private Function<Double, Optional<String[]>> tooltipSupplier = d -> Optional.empty();
  11. private double value;
  12. private Double min = null, max = null;
  13. public DoubleFieldBuilder(String resetButtonKey, String fieldNameKey, double value) {
  14. super(resetButtonKey, fieldNameKey);
  15. this.value = value;
  16. }
  17. public DoubleFieldBuilder setErrorSupplier(Function<Double, Optional<String>> errorSupplier) {
  18. this.errorSupplier = errorSupplier;
  19. return this;
  20. }
  21. public DoubleFieldBuilder requireRestart() {
  22. requireRestart(true);
  23. return this;
  24. }
  25. public DoubleFieldBuilder setSaveConsumer(Consumer<Double> saveConsumer) {
  26. this.saveConsumer = saveConsumer;
  27. return this;
  28. }
  29. public DoubleFieldBuilder setDefaultValue(Supplier<Double> defaultValue) {
  30. this.defaultValue = defaultValue;
  31. return this;
  32. }
  33. public DoubleFieldBuilder setDefaultValue(double defaultValue) {
  34. this.defaultValue = () -> defaultValue;
  35. return this;
  36. }
  37. public DoubleFieldBuilder setMin(double min) {
  38. this.min = min;
  39. return this;
  40. }
  41. public DoubleFieldBuilder setMax(double max) {
  42. this.max = max;
  43. return this;
  44. }
  45. public DoubleFieldBuilder removeMin() {
  46. this.min = null;
  47. return this;
  48. }
  49. public DoubleFieldBuilder removeMax() {
  50. this.max = null;
  51. return this;
  52. }
  53. public DoubleFieldBuilder setTooltipSupplier(Function<Double, Optional<String[]>> tooltipSupplier) {
  54. this.tooltipSupplier = tooltipSupplier;
  55. return this;
  56. }
  57. public DoubleFieldBuilder setTooltipSupplier(Supplier<Optional<String[]>> tooltipSupplier) {
  58. this.tooltipSupplier = d -> tooltipSupplier.get();
  59. return this;
  60. }
  61. public DoubleFieldBuilder setTooltip(Optional<String[]> tooltip) {
  62. this.tooltipSupplier = d -> tooltip;
  63. return this;
  64. }
  65. public DoubleFieldBuilder setTooltip(String... tooltip) {
  66. this.tooltipSupplier = d -> Optional.ofNullable(tooltip);
  67. return this;
  68. }
  69. @NotNull
  70. @Override
  71. public DoubleListEntry build() {
  72. DoubleListEntry entry = new DoubleListEntry(getFieldNameKey(), value, getResetButtonKey(), defaultValue, saveConsumer, null, isRequireRestart());
  73. if (min != null)
  74. entry.setMinimum(min);
  75. if (max != null)
  76. entry.setMaximum(max);
  77. entry.setTooltipSupplier(() -> tooltipSupplier.apply(entry.getValue()));
  78. if (errorSupplier != null)
  79. entry.setErrorSupplier(() -> errorSupplier.apply(entry.getValue()));
  80. return entry;
  81. }
  82. }