From 89e58be1a39aeb73bdcbf07d2eccd29aae0c15a4 Mon Sep 17 00:00:00 2001 From: Will FP Date: Sat, 6 Jan 2024 13:28:04 +0000 Subject: [PATCH] Added %value_commas% to ConfiguredPrice --- .../com/willfp/eco/core/price/ConfiguredPrice.java | 6 +++++- .../main/java/com/willfp/eco/util/NumberUtils.java | 14 ++++++++++++++ .../main/kotlin/com/willfp/eco/util/NumberUtils.kt | 4 ++++ eco-core/core-plugin/src/main/resources/lang.yml | 1 + 4 files changed, 24 insertions(+), 1 deletion(-) diff --git a/eco-api/src/main/java/com/willfp/eco/core/price/ConfiguredPrice.java b/eco-api/src/main/java/com/willfp/eco/core/price/ConfiguredPrice.java index d2a2f563..d6f97d60 100644 --- a/eco-api/src/main/java/com/willfp/eco/core/price/ConfiguredPrice.java +++ b/eco-api/src/main/java/com/willfp/eco/core/price/ConfiguredPrice.java @@ -120,8 +120,12 @@ public final class ConfiguredPrice implements Price { */ public String getDisplay(@NotNull final Player player, final double multiplier) { + double value = this.getPrice().getValue(player, multiplier); + return StringUtils.format( - formatString.replace("%value%", NumberUtils.format(this.getPrice().getValue(player, multiplier))), + formatString + .replace("%value%", NumberUtils.format(value)) + .replace("%value_commas%", NumberUtils.formatWithCommas(value)), player, StringUtils.FormatOption.WITH_PLACEHOLDERS ); diff --git a/eco-api/src/main/java/com/willfp/eco/util/NumberUtils.java b/eco-api/src/main/java/com/willfp/eco/util/NumberUtils.java index 61192def..05cc91a9 100644 --- a/eco-api/src/main/java/com/willfp/eco/util/NumberUtils.java +++ b/eco-api/src/main/java/com/willfp/eco/util/NumberUtils.java @@ -203,6 +203,20 @@ public final class NumberUtils { return formatted.endsWith("00") ? String.valueOf((int) toFormat) : formatted; } + /** + * Format double to string with commas. + * + * @param toFormat The number to format. + * @return Formatted. + */ + @NotNull + public static String formatWithCommas(final double toFormat) { + DecimalFormat df = new DecimalFormat("#,##0.00"); + String formatted = df.format(toFormat); + + return formatted.endsWith(".00") ? formatted.substring(0, formatted.length() - 3) : formatted; + } + /** * Evaluate an expression. * diff --git a/eco-api/src/main/kotlin/com/willfp/eco/util/NumberUtils.kt b/eco-api/src/main/kotlin/com/willfp/eco/util/NumberUtils.kt index 281b80a5..29c3a3ec 100644 --- a/eco-api/src/main/kotlin/com/willfp/eco/util/NumberUtils.kt +++ b/eco-api/src/main/kotlin/com/willfp/eco/util/NumberUtils.kt @@ -8,6 +8,10 @@ import com.willfp.eco.core.placeholder.context.PlaceholderContext fun Number.toNumeral(): String = NumberUtils.toNumeral(this.toInt()) +/** @see NumberUtils.formatWithCommas */ +fun Number.formatWithCommas(): String = + NumberUtils.formatWithCommas(this.toDouble()) + /** @see NumberUtils.fromNumeral */ fun String.parseNumeral(): Int = NumberUtils.fromNumeral(this) diff --git a/eco-core/core-plugin/src/main/resources/lang.yml b/eco-core/core-plugin/src/main/resources/lang.yml index 54bd9317..393e0681 100644 --- a/eco-core/core-plugin/src/main/resources/lang.yml +++ b/eco-core/core-plugin/src/main/resources/lang.yml @@ -2,6 +2,7 @@ multiple-in-craft: '&l&c! &fThis recipe requires &a%amount%&f of this item.' # Specify default display names for prices made through ConfiguredPrice#create # These will override any custom configured price display names. +# You can use %value% and %value_commas% as placeholders. price-display: - type: example_type display: "&e%value% Price"