Skip to content

Commit

Permalink
Added %value_commas% to ConfiguredPrice
Browse files Browse the repository at this point in the history
  • Loading branch information
WillFP committed Jan 6, 2024
1 parent 476faee commit 89e58be
Show file tree
Hide file tree
Showing 4 changed files with 24 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -120,8 +120,12 @@ public String getDisplay(@NotNull final Player player) {
*/
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
);
Expand Down
14 changes: 14 additions & 0 deletions eco-api/src/main/java/com/willfp/eco/util/NumberUtils.java
Original file line number Diff line number Diff line change
Expand Up @@ -203,6 +203,20 @@ public static String format(final double toFormat) {
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.
*
Expand Down
4 changes: 4 additions & 0 deletions eco-api/src/main/kotlin/com/willfp/eco/util/NumberUtils.kt
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
1 change: 1 addition & 0 deletions eco-core/core-plugin/src/main/resources/lang.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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"

0 comments on commit 89e58be

Please sign in to comment.