LongFieldBuilder.java 2.7 KB

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