Allow users to change the currency symbol.

This commit is contained in:
Brianna 2020-09-09 16:01:47 -05:00
parent 6051052da2
commit d6ec13f820
4 changed files with 16 additions and 23 deletions

View File

@ -3,11 +3,15 @@ package com.songoda.core.hooks;
import com.songoda.core.hooks.economies.Economy;
import org.bukkit.OfflinePlayer;
import java.text.DecimalFormat;
/**
* A convenience class for static access to an Economy HookManager
*/
public class EconomyManager {
private static String currencySymbol = "$";
private static final HookManager<Economy> manager = new HookManager(Economy.class);
/**
@ -58,8 +62,9 @@ public class EconomyManager {
* @param amt amount to display
* @return a currency string as formatted by the economy plugin
*/
public static String formatEconomy(double amt) {
return manager.isEnabled() ? manager.getCurrentHook().formatEconomy(amt) : String.valueOf(amt);
public String formatEconomy(double amt) {
DecimalFormat formatter = new DecimalFormat(amt == Math.ceil(amt) ? "#,###" : "#,###.00");
return currencySymbol + formatter.format(amt);
}
/**
@ -110,4 +115,13 @@ public class EconomyManager {
public static boolean deposit(OfflinePlayer player, double amount) {
return manager.isEnabled() && manager.getCurrentHook().deposit(player, amount);
}
/**
* Change the curency symbl used in the #formatEconomy method.
*
* @param currencySymbol the new symbol
*/
public static void setCurrencySymbol(String currencySymbol) {
EconomyManager.currencySymbol = currencySymbol;
}
}

View File

@ -41,15 +41,4 @@ public abstract class Economy implements Hook {
* @return true if the total amount was added successfully
*/
public abstract boolean deposit(OfflinePlayer player, double amount);
/**
* Format the given amount to a human-readable string in this currency
*
* @param amt amount to display
* @return a currency string as formatted by the economy plugin
*/
public String formatEconomy(double amt) {
DecimalFormat formatter = new DecimalFormat(amt == Math.ceil(amt) ? "#,###" : "#,###.00");
return "$" + formatter.format(amt);
}
}

View File

@ -25,11 +25,6 @@ public class ReserveEconomy extends Economy {
return "Reserve";
}
@Override
public String formatEconomy(double amt) {
return economyAPI.format(BigDecimal.valueOf(amt));
}
@Override
public double getBalance(OfflinePlayer player) {
return economyAPI.getBankHoldings(player.getUniqueId()).doubleValue();

View File

@ -29,11 +29,6 @@ public class VaultEconomy extends Economy {
return "Vault";
}
@Override
public String formatEconomy(double amt) {
return vault != null ? vault.format(amt) : super.formatEconomy(amt);
}
@Override
public double getBalance(OfflinePlayer player) {
if (vault == null)