diff options
author | xander <xander@isxander.dev> | 2022-09-01 11:58:49 +0100 |
---|---|---|
committer | xander <xander@isxander.dev> | 2022-09-01 11:58:49 +0100 |
commit | 4d977cc9764ecf0073650f126700f6ff638fa06b (patch) | |
tree | 883e68bbd80874c048b3e34db59bf0aa926b489b /src/main/java/dev/isxander/yacl/impl | |
parent | e63a3c989e3a899bdc81558dd2e4c5cc2c659bde (diff) | |
download | YetAnotherConfigLib-4d977cc9764ecf0073650f126700f6ff638fa06b.tar.gz YetAnotherConfigLib-4d977cc9764ecf0073650f126700f6ff638fa06b.tar.bz2 YetAnotherConfigLib-4d977cc9764ecf0073650f126700f6ff638fa06b.zip |
javadoc!
added LongSliderController
renamed Control -> Controller
add minecraft simple option binding constructor
Diffstat (limited to 'src/main/java/dev/isxander/yacl/impl')
3 files changed, 54 insertions, 16 deletions
diff --git a/src/main/java/dev/isxander/yacl/impl/ButtonOptionImpl.java b/src/main/java/dev/isxander/yacl/impl/ButtonOptionImpl.java index 516a5ee..4104ff0 100644 --- a/src/main/java/dev/isxander/yacl/impl/ButtonOptionImpl.java +++ b/src/main/java/dev/isxander/yacl/impl/ButtonOptionImpl.java @@ -2,7 +2,7 @@ package dev.isxander.yacl.impl; import dev.isxander.yacl.api.Binding; import dev.isxander.yacl.api.ButtonOption; -import dev.isxander.yacl.api.Control; +import dev.isxander.yacl.api.Controller; import net.minecraft.text.Text; import org.jetbrains.annotations.ApiStatus; import org.jetbrains.annotations.NotNull; @@ -15,19 +15,19 @@ public class ButtonOptionImpl implements ButtonOption { private final Text name; private final Text tooltip; private final Runnable action; - private final Control<Runnable> control; + private final Controller<Runnable> controller; private final Binding<Runnable> binding; public ButtonOptionImpl( @NotNull Text name, @Nullable Text tooltip, @NotNull Runnable action, - @NotNull Function<ButtonOption, Control<Runnable>> controlGetter + @NotNull Function<ButtonOption, Controller<Runnable>> controlGetter ) { this.name = name; this.tooltip = tooltip; this.action = action; - this.control = controlGetter.apply(this); + this.controller = controlGetter.apply(this); this.binding = new EmptyBinderImpl(); } @@ -37,7 +37,7 @@ public class ButtonOptionImpl implements ButtonOption { } @Override - public @Nullable Text tooltip() { + public @NotNull Text tooltip() { return tooltip; } @@ -47,8 +47,8 @@ public class ButtonOptionImpl implements ButtonOption { } @Override - public @NotNull Control<Runnable> control() { - return control; + public @NotNull Controller<Runnable> controller() { + return controller; } @Override @@ -62,7 +62,7 @@ public class ButtonOptionImpl implements ButtonOption { } @Override - public Runnable pendingValue() { + public @NotNull Runnable pendingValue() { throw new UnsupportedOperationException(); } diff --git a/src/main/java/dev/isxander/yacl/impl/OptionImpl.java b/src/main/java/dev/isxander/yacl/impl/OptionImpl.java index a62726f..c61eaa1 100644 --- a/src/main/java/dev/isxander/yacl/impl/OptionImpl.java +++ b/src/main/java/dev/isxander/yacl/impl/OptionImpl.java @@ -1,7 +1,7 @@ package dev.isxander.yacl.impl; import dev.isxander.yacl.api.Binding; -import dev.isxander.yacl.api.Control; +import dev.isxander.yacl.api.Controller; import dev.isxander.yacl.api.Option; import net.minecraft.text.Text; import org.jetbrains.annotations.ApiStatus; @@ -14,7 +14,7 @@ import java.util.function.Function; public class OptionImpl<T> implements Option<T> { private final Text name; private final Text tooltip; - private final Control<T> control; + private final Controller<T> controller; private final Binding<T> binding; private T pendingValue; @@ -22,12 +22,12 @@ public class OptionImpl<T> implements Option<T> { public OptionImpl( @NotNull Text name, @Nullable Text tooltip, - @NotNull Function<Option<T>, Control<T>> controlGetter, + @NotNull Function<Option<T>, Controller<T>> controlGetter, @NotNull Binding<T> binding ) { this.name = name; this.tooltip = tooltip; - this.control = controlGetter.apply(this); + this.controller = controlGetter.apply(this); this.binding = binding; this.pendingValue = binding().getValue(); } @@ -38,13 +38,13 @@ public class OptionImpl<T> implements Option<T> { } @Override - public @Nullable Text tooltip() { + public @NotNull Text tooltip() { return tooltip; } @Override - public @NotNull Control<T> control() { - return control; + public @NotNull Controller<T> controller() { + return controller; } @Override @@ -58,7 +58,7 @@ public class OptionImpl<T> implements Option<T> { } @Override - public T pendingValue() { + public @NotNull T pendingValue() { return pendingValue; } diff --git a/src/main/java/dev/isxander/yacl/impl/utils/NumberFormatHelper.java b/src/main/java/dev/isxander/yacl/impl/utils/NumberFormatHelper.java new file mode 100644 index 0000000..bbf44a1 --- /dev/null +++ b/src/main/java/dev/isxander/yacl/impl/utils/NumberFormatHelper.java @@ -0,0 +1,38 @@ +package dev.isxander.yacl.impl.utils; + +import org.jetbrains.annotations.ApiStatus; + +import java.util.Map; +import java.util.NavigableMap; +import java.util.TreeMap; + +@ApiStatus.Internal +public class NumberFormatHelper { + private static final NavigableMap<Long, String> suffixes = new TreeMap<>(); + static { + suffixes.put(1_000L, "K"); + suffixes.put(1_000_000L, "M"); + suffixes.put(1_000_000_000L, "B"); + suffixes.put(1_000_000_000_000L, "T"); + suffixes.put(1_000_000_000_000_000L, "P"); + suffixes.put(1_000_000_000_000_000_000L, "E"); + } + + /** + * @author <a href="https://stackoverflow.com/a/30661479">assylias</a> + */ + public static String formatSmaller(long value) { + // Long.MIN_VALUE == -Long.MIN_VALUE, so we need an adjustment here + if (value == Long.MIN_VALUE) return formatSmaller(Long.MIN_VALUE + 1); + if (value < 0) return "-" + formatSmaller(-value); + if (value < 1000) return Long.toString(value); //deal with easy case + + Map.Entry<Long, String> e = suffixes.floorEntry(value); + Long divideBy = e.getKey(); + String suffix = e.getValue(); + + long truncated = value / (divideBy / 10); //the number part of the output times 10 + boolean hasDecimal = truncated < 100 && (truncated / 10d) != (truncated / 10); + return hasDecimal ? (truncated / 10d) + suffix : (truncated / 10) + suffix; + } +} |