mirror of
https://github.com/darksaid98/nightcore-spigot.git
synced 2026-12-02 06:28:53 +00:00
Merge pull request #1 from NicoNekoDev/master
Add better number formatting/compacting/rounding
This commit is contained in:
5 files changed
+109
-14
No files matched your search
@@ -4,6 +4,7 @@ import org.bukkit.inventory.ItemStack;
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
import org.jetbrains.annotations.Nullable;
|
||||
import su.nightexpress.nightcore.util.TriFunction;
|
||||
import su.nightexpress.nightcore.util.wrapper.UniFormatter;
|
||||
import su.nightexpress.nightcore.util.wrapper.UniParticle;
|
||||
import su.nightexpress.nightcore.util.wrapper.UniSound;
|
||||
|
||||
@@ -129,6 +130,14 @@ public class ConfigValue<T> {
|
||||
return create(path, reader, writer, defaultValue, description);
|
||||
}
|
||||
|
||||
@NotNull
|
||||
public static ConfigValue<UniFormatter> create(@NotNull String path, @NotNull UniFormatter defaultValue, @Nullable String... description) {
|
||||
Reader<UniFormatter> reader = (cfg, path1, def) -> UniFormatter.read(cfg, path1);
|
||||
Writer<UniFormatter> writer = (cfg, path1, obj) -> obj.write(cfg, path1);
|
||||
|
||||
return create(path, reader, writer, defaultValue, description);
|
||||
}
|
||||
|
||||
@NotNull
|
||||
public static <E extends Enum<E>> ConfigValue<E> create(@NotNull String path, @NotNull Class<E> clazz, @NotNull E defaultValue, @Nullable String... description) {
|
||||
Reader<E> reader = (cfg, path1, def) -> cfg.getEnum(path1, clazz, def);
|
||||
|
||||
@@ -3,10 +3,14 @@ package su.nightexpress.nightcore.core;
|
||||
import su.nightexpress.nightcore.config.ConfigValue;
|
||||
import su.nightexpress.nightcore.util.text.NightMessage;
|
||||
import su.nightexpress.nightcore.util.text.tag.impl.ColorTag;
|
||||
import su.nightexpress.nightcore.util.wrapper.UniFormatter;
|
||||
|
||||
import java.awt.*;
|
||||
import java.math.RoundingMode;
|
||||
import java.util.Arrays;
|
||||
import java.util.HashMap;
|
||||
import java.util.Map;
|
||||
import java.util.stream.Collectors;
|
||||
|
||||
public class CoreConfig {
|
||||
|
||||
@@ -68,4 +72,10 @@ public class CoreConfig {
|
||||
"This is useful if you want to use custom player nicknames in commands.",
|
||||
"(Works only for nightcore based plugins.)",
|
||||
"[Default is false]");
|
||||
|
||||
public static final ConfigValue<UniFormatter> NUMBER_FORMAT = ConfigValue.create("Engine.Number_Format",
|
||||
UniFormatter.of("#,###.##", RoundingMode.HALF_EVEN),
|
||||
"Control over how numerical data is formatted and rounded.",
|
||||
"Allowed modes: " + Arrays.stream(RoundingMode.values()).map(RoundingMode::name).map(String::toLowerCase).collect(Collectors.joining(", ")),
|
||||
"A tutorial can be found here: https://docs.oracle.com/javase/tutorial/i18n/format/decimalFormat.html");
|
||||
}
|
||||
@@ -87,6 +87,12 @@ public class CoreLang {
|
||||
public static final LangItem EDITOR_ITEM_NEXT_PAGE = LangItem.of("Editor.Generic.NextPage", WHITE.enclose("Next Page") + GRAY.enclose(" (→)"));
|
||||
public static final LangItem EDITOR_ITEM_PREVIOUS_PAGE = LangItem.of("Editor.Generic.PreviousPage", GRAY.enclose("(←) ") + WHITE.enclose("Previous Page"));
|
||||
|
||||
public static final LangString NUMBER_SHORT_THOUSAND = LangString.of("Number.Thousand", "k");
|
||||
public static final LangString NUMBER_SHORT_MILLION = LangString.of("Number.Million", "m");
|
||||
public static final LangString NUMBER_SHORT_BILLION = LangString.of("Number.Billion", "b");
|
||||
public static final LangString NUMBER_SHORT_TRILLION = LangString.of("Number.Trillion", "t");
|
||||
public static final LangString NUMBER_SHORT_QUADRILLION = LangString.of("Number.Quadrillion", "q");
|
||||
|
||||
@NotNull
|
||||
public static String getYesOrNo(boolean value) {
|
||||
return (value ? OTHER_YES : OTHER_NO).getString();
|
||||
|
||||
@@ -1,21 +1,25 @@
|
||||
package su.nightexpress.nightcore.util;
|
||||
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
import su.nightexpress.nightcore.core.CoreConfig;
|
||||
import su.nightexpress.nightcore.core.CoreLang;
|
||||
|
||||
import java.math.BigDecimal;
|
||||
import java.math.RoundingMode;
|
||||
import java.text.DecimalFormat;
|
||||
import java.text.DecimalFormatSymbols;
|
||||
import java.util.Locale;
|
||||
import java.util.TreeMap;
|
||||
import java.util.function.Supplier;
|
||||
|
||||
public class NumberUtil {
|
||||
|
||||
private static final DecimalFormat FORMAT_ROUND_HUMAN;
|
||||
private final static TreeMap<Integer, String> ROMAN_MAP = new TreeMap<>();
|
||||
private final static TreeMap<Integer, Supplier<String>> NUMERIC_MAP = new TreeMap<>();
|
||||
|
||||
static {
|
||||
FORMAT_ROUND_HUMAN = new DecimalFormat("#,###.##", new DecimalFormatSymbols(Locale.ENGLISH));
|
||||
NUMERIC_MAP.put(0, () -> "");
|
||||
NUMERIC_MAP.put(1, CoreLang.NUMBER_SHORT_THOUSAND::getString);
|
||||
NUMERIC_MAP.put(2, CoreLang.NUMBER_SHORT_MILLION::getString);
|
||||
NUMERIC_MAP.put(3, CoreLang.NUMBER_SHORT_BILLION::getString);
|
||||
NUMERIC_MAP.put(4, CoreLang.NUMBER_SHORT_TRILLION::getString);
|
||||
NUMERIC_MAP.put(5, CoreLang.NUMBER_SHORT_QUADRILLION::getString);
|
||||
|
||||
ROMAN_MAP.put(1000, "M");
|
||||
ROMAN_MAP.put(900, "CM");
|
||||
@@ -34,11 +38,27 @@ public class NumberUtil {
|
||||
|
||||
@NotNull
|
||||
public static String format(double value) {
|
||||
return FORMAT_ROUND_HUMAN.format(value);
|
||||
return CoreConfig.NUMBER_FORMAT.get().format(value);
|
||||
}
|
||||
|
||||
@NotNull
|
||||
public static Pair<String, String> formatCompact(double value) {
|
||||
boolean negative = false;
|
||||
if (value < 0) {
|
||||
value = Math.abs(value);
|
||||
negative = true;
|
||||
}
|
||||
int index = 0;
|
||||
while ((value / 1000) >= 1 && index < NUMERIC_MAP.size() - 1) {
|
||||
value = value / 1000;
|
||||
index++;
|
||||
}
|
||||
|
||||
return Pair.of(CoreConfig.NUMBER_FORMAT.get().format(negative ? -value : value), NUMERIC_MAP.get(NUMERIC_MAP.floorKey(index)).get());
|
||||
}
|
||||
|
||||
public static double round(double value) {
|
||||
return new BigDecimal(value).setScale(2, RoundingMode.HALF_UP).doubleValue();
|
||||
return new BigDecimal(value).setScale(2, CoreConfig.NUMBER_FORMAT.get().getRounding()).doubleValue();
|
||||
}
|
||||
|
||||
@NotNull
|
||||
@@ -79,8 +99,7 @@ public class NumberUtil {
|
||||
if (!Double.isNaN(amount) && !Double.isInfinite(amount)) {
|
||||
return amount;
|
||||
}
|
||||
}
|
||||
catch (NumberFormatException ignored) {}
|
||||
} catch (NumberFormatException ignored) {}
|
||||
|
||||
return defaultValue;
|
||||
}
|
||||
@@ -96,8 +115,7 @@ public class NumberUtil {
|
||||
public static int getAnyInteger(@NotNull String input, int defaultValue) {
|
||||
try {
|
||||
return Integer.parseInt(input);
|
||||
}
|
||||
catch (NumberFormatException ignored) {}
|
||||
} catch (NumberFormatException ignored) {}
|
||||
|
||||
return defaultValue;
|
||||
}
|
||||
@@ -108,8 +126,7 @@ public class NumberUtil {
|
||||
for (int index = 0; index < split.length; index++) {
|
||||
try {
|
||||
array[index] = Integer.parseInt(split[index].trim());
|
||||
}
|
||||
catch (NumberFormatException e) {
|
||||
} catch (NumberFormatException e) {
|
||||
array[index] = 0;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -0,0 +1,53 @@
|
||||
package su.nightexpress.nightcore.util.wrapper;
|
||||
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
import su.nightexpress.nightcore.config.FileConfig;
|
||||
|
||||
import java.math.RoundingMode;
|
||||
import java.text.DecimalFormat;
|
||||
import java.text.DecimalFormatSymbols;
|
||||
import java.util.Locale;
|
||||
|
||||
public class UniFormatter {
|
||||
|
||||
private final DecimalFormat formatter;
|
||||
|
||||
private final String format;
|
||||
private final RoundingMode rounding;
|
||||
|
||||
private UniFormatter(String format, RoundingMode rounding) {
|
||||
this.format = format;
|
||||
this.rounding = rounding;
|
||||
this.formatter = new DecimalFormat(format, new DecimalFormatSymbols(Locale.US));
|
||||
this.formatter.setRoundingMode(rounding);
|
||||
}
|
||||
|
||||
@NotNull
|
||||
public static UniFormatter of(String format, RoundingMode rounding) {
|
||||
return new UniFormatter(format, rounding);
|
||||
}
|
||||
|
||||
@NotNull
|
||||
public static UniFormatter read(@NotNull FileConfig cfg, @NotNull String path) {
|
||||
String format = cfg.getString(path + ".Format_Pattern", "#,###.##");
|
||||
RoundingMode rounding = RoundingMode.valueOf(cfg.getString(path + ".Rounding_Mode", "half_even").toUpperCase());
|
||||
return of(format, rounding);
|
||||
}
|
||||
|
||||
public void write(@NotNull FileConfig cfg, @NotNull String path) {
|
||||
cfg.set(path + ".Format", this.getFormat());
|
||||
cfg.set(path + ".Rounding", this.getRounding().name().toLowerCase());
|
||||
}
|
||||
|
||||
public String getFormat() {
|
||||
return this.format;
|
||||
}
|
||||
|
||||
public RoundingMode getRounding() {
|
||||
return this.rounding;
|
||||
}
|
||||
|
||||
public String format(double value) {
|
||||
return this.formatter.format(value);
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user