DoubleFieldBuilder.java 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081
  1. package me.shedaniel.clothconfig2.impl.builders;
  2. import me.shedaniel.clothconfig2.gui.entries.DoubleListEntry;
  3. import java.util.Optional;
  4. import java.util.function.Consumer;
  5. import java.util.function.Supplier;
  6. public class DoubleFieldBuilder extends FieldBuilder<Double, DoubleListEntry> {
  7. private Consumer<Double> saveConsumer = null;
  8. private Supplier<Optional<String[]>> tooltipSupplier = null;
  9. private double value;
  10. private Double min = null, max = null;
  11. public DoubleFieldBuilder(String resetButtonKey, String fieldNameKey, double value) {
  12. super(resetButtonKey, fieldNameKey);
  13. this.value = value;
  14. }
  15. public DoubleFieldBuilder setSaveConsumer(Consumer<Double> saveConsumer) {
  16. this.saveConsumer = saveConsumer;
  17. return this;
  18. }
  19. public DoubleFieldBuilder setDefaultValue(Supplier<Double> defaultValue) {
  20. this.defaultValue = defaultValue;
  21. return this;
  22. }
  23. public DoubleFieldBuilder setDefaultValue(double defaultValue) {
  24. this.defaultValue = () -> defaultValue;
  25. return this;
  26. }
  27. public DoubleFieldBuilder setMin(double min) {
  28. this.min = min;
  29. return this;
  30. }
  31. public DoubleFieldBuilder setMax(double max) {
  32. this.max = max;
  33. return this;
  34. }
  35. public DoubleFieldBuilder removeMin() {
  36. this.min = null;
  37. return this;
  38. }
  39. public DoubleFieldBuilder removeMax() {
  40. this.max = null;
  41. return this;
  42. }
  43. public DoubleFieldBuilder setTooltipSupplier(Supplier<Optional<String[]>> tooltipSupplier) {
  44. this.tooltipSupplier = tooltipSupplier;
  45. return this;
  46. }
  47. public DoubleFieldBuilder setTooltip(Optional<String[]> tooltip) {
  48. this.tooltipSupplier = () -> tooltip;
  49. return this;
  50. }
  51. public DoubleFieldBuilder setTooltip(String... tooltip) {
  52. this.tooltipSupplier = () -> Optional.ofNullable(tooltip);
  53. return this;
  54. }
  55. @Override
  56. public DoubleListEntry build() {
  57. DoubleListEntry entry = new DoubleListEntry(getFieldNameKey(), value, getResetButtonKey(), defaultValue, saveConsumer, tooltipSupplier);
  58. if (min != null)
  59. entry.setMinimum(min);
  60. if (max != null)
  61. entry.setMaximum(max);
  62. return entry;
  63. }
  64. }