2016-06-14 05:06:58 +02:00
|
|
|
package org.appledash.saneeconomy.economy;
|
|
|
|
|
2016-12-15 23:47:26 +01:00
|
|
|
import com.google.common.base.Strings;
|
2017-07-03 06:17:18 +02:00
|
|
|
import org.appledash.sanelib.messages.MessageUtils;
|
2016-09-19 07:44:55 +02:00
|
|
|
import org.bukkit.configuration.ConfigurationSection;
|
2016-06-14 05:06:58 +02:00
|
|
|
|
|
|
|
import java.text.DecimalFormat;
|
2016-10-02 16:18:57 +02:00
|
|
|
import java.text.DecimalFormatSymbols;
|
2016-06-14 05:06:58 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Created by AppleDash on 6/13/2016.
|
|
|
|
* Blackjack is still best pony.
|
|
|
|
*
|
|
|
|
* Represents an in-game currency.
|
|
|
|
*/
|
|
|
|
public class Currency {
|
2016-06-14 10:59:16 +02:00
|
|
|
private final String nameSingular;
|
|
|
|
private final String namePlural;
|
|
|
|
private final DecimalFormat format;
|
2017-06-14 16:39:43 +02:00
|
|
|
private final String balanceFormat;
|
2016-06-14 05:06:58 +02:00
|
|
|
|
2016-07-29 13:16:11 +02:00
|
|
|
public Currency(String nameSingular, String namePlural, DecimalFormat format) {
|
2017-06-14 16:39:43 +02:00
|
|
|
this(nameSingular, namePlural, format, "{1} {2}");
|
2017-06-08 00:38:48 +02:00
|
|
|
}
|
|
|
|
|
2017-06-14 16:39:43 +02:00
|
|
|
public Currency(String nameSingular, String namePlural, DecimalFormat format, String balanceFormat) {
|
2017-06-08 00:38:48 +02:00
|
|
|
this.nameSingular = nameSingular;
|
|
|
|
this.namePlural = namePlural;
|
|
|
|
this.format = format;
|
2017-06-14 16:39:43 +02:00
|
|
|
this.balanceFormat = balanceFormat;
|
2016-06-14 05:06:58 +02:00
|
|
|
}
|
|
|
|
|
2016-09-19 07:44:55 +02:00
|
|
|
public static Currency fromConfig(ConfigurationSection config) {
|
2016-10-02 16:18:57 +02:00
|
|
|
DecimalFormat format = new DecimalFormat(config.getString("format", "0.00"));
|
|
|
|
|
|
|
|
if (config.getInt("grouping", 0) > 0) {
|
|
|
|
DecimalFormatSymbols symbols = format.getDecimalFormatSymbols();
|
|
|
|
if (symbols.getDecimalSeparator() == ',') { // French
|
|
|
|
symbols.setGroupingSeparator(' ');
|
|
|
|
} else {
|
|
|
|
symbols.setGroupingSeparator(',');
|
|
|
|
}
|
2016-12-15 23:47:26 +01:00
|
|
|
|
|
|
|
String groupingSeparator = config.getString("grouping-separator", null);
|
|
|
|
|
|
|
|
if (!Strings.isNullOrEmpty(groupingSeparator)) {
|
|
|
|
symbols.setGroupingSeparator(groupingSeparator.charAt(0));
|
|
|
|
}
|
|
|
|
|
2016-10-02 16:18:57 +02:00
|
|
|
format.setDecimalFormatSymbols(symbols);
|
|
|
|
format.setGroupingUsed(true);
|
|
|
|
format.setGroupingSize(3);
|
|
|
|
}
|
|
|
|
|
2016-06-14 05:06:58 +02:00
|
|
|
return new Currency(
|
2016-09-19 07:44:55 +02:00
|
|
|
config.getString("name.singular", "dollar"),
|
|
|
|
config.getString("name.plural", "dollars"),
|
2017-06-08 00:38:48 +02:00
|
|
|
format,
|
2017-06-14 16:39:43 +02:00
|
|
|
config.getString("balance-format", "{1} {2}")
|
2016-06-14 05:06:58 +02:00
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Format a money amount with this currency's format.
|
|
|
|
* @param amount Money amount.
|
|
|
|
* @return Formatted amount string.
|
|
|
|
*/
|
|
|
|
public String formatAmount(double amount) {
|
|
|
|
if (amount == 1) {
|
2017-06-14 16:39:43 +02:00
|
|
|
return MessageUtils.indexedFormat(balanceFormat, format.format(amount), nameSingular);
|
2016-06-14 05:06:58 +02:00
|
|
|
}
|
2017-06-14 16:39:43 +02:00
|
|
|
|
|
|
|
return MessageUtils.indexedFormat(balanceFormat, format.format(amount), namePlural);
|
2016-06-14 05:06:58 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Get this currency's singular name.
|
|
|
|
* Example: "Dollar"
|
|
|
|
* @return Singular name.
|
|
|
|
*/
|
|
|
|
public String getSingularName() {
|
|
|
|
return nameSingular;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Get this currency's plural name.
|
|
|
|
* Example: "Dollars"
|
|
|
|
* @return Plural name.
|
|
|
|
*/
|
|
|
|
public String getPluralName() {
|
|
|
|
return namePlural;
|
|
|
|
}
|
2016-06-14 12:20:14 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Get this currency's number format.
|
|
|
|
* @return DecimalFormat instance
|
|
|
|
*/
|
|
|
|
public DecimalFormat getFormat() {
|
|
|
|
return format;
|
|
|
|
}
|
2016-06-14 05:06:58 +02:00
|
|
|
}
|