add formatting function to economy hook

This commit is contained in:
jascotty2 2019-08-27 08:28:05 -05:00
parent f0a2d47b58
commit 98ce075114
5 changed files with 37 additions and 7 deletions

View File

@ -52,6 +52,15 @@ public class EconomyManager {
return manager.getName();
}
/**
* 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 static String formatEconomy(double amt) {
return manager.isEnabled() ? manager.getCurrentHook().formatEconomy(amt) : String.valueOf(amt);
}
/**
* Check to see if a player has at least some balance available. <br />
* NOTE: using a default economy assumes that this library is shaded

View File

@ -1,9 +1,10 @@
package com.songoda.core.hooks.economies;
import com.songoda.core.hooks.Hook;
import java.text.DecimalFormat;
import org.bukkit.OfflinePlayer;
public interface Economy extends Hook {
public abstract class Economy implements Hook {
/**
* Check to see if a player has at least some balance available
@ -12,7 +13,7 @@ public interface Economy extends Hook {
* @param cost minimum amount this player should have
* @return true if this player can have this amount withdrawn
*/
boolean hasBalance(OfflinePlayer player, double cost);
public abstract boolean hasBalance(OfflinePlayer player, double cost);
/**
* Try to withdraw an amount from a player's balance
@ -21,7 +22,7 @@ public interface Economy extends Hook {
* @param cost amount to remove from this player
* @return true if the total amount was withdrawn successfully
*/
boolean withdrawBalance(OfflinePlayer player, double cost);
public abstract boolean withdrawBalance(OfflinePlayer player, double cost);
/**
* Try to add an amount to a player's balance
@ -30,5 +31,15 @@ public interface Economy extends Hook {
* @param amount amount to add to this player
* @return true if the total amount was added successfully
*/
boolean deposit(OfflinePlayer player, double amount);
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

@ -4,7 +4,7 @@ import org.black_ixx.playerpoints.PlayerPoints;
import org.bukkit.Bukkit;
import org.bukkit.OfflinePlayer;
public class PlayerPointsEconomy implements Economy {
public class PlayerPointsEconomy extends Economy {
private final PlayerPoints playerPoints;

View File

@ -6,7 +6,7 @@ import org.bukkit.OfflinePlayer;
import java.math.BigDecimal;
public class ReserveEconomy implements Economy {
public class ReserveEconomy extends Economy {
EconomyAPI economyAPI;
@ -25,6 +25,11 @@ public class ReserveEconomy implements Economy {
return "Reserve";
}
@Override
public String formatEconomy(double amt) {
return economyAPI.format(BigDecimal.valueOf(amt));
}
@Override
public boolean hasBalance(OfflinePlayer player, double cost) {
return economyAPI.hasHoldings(player.getUniqueId(), new BigDecimal(cost));

View File

@ -4,7 +4,7 @@ import org.bukkit.Bukkit;
import org.bukkit.OfflinePlayer;
import org.bukkit.plugin.RegisteredServiceProvider;
public class VaultEconomy implements Economy {
public class VaultEconomy extends Economy {
private final net.milkbowl.vault.economy.Economy vault;
@ -29,6 +29,11 @@ public class VaultEconomy implements Economy {
return "Vault";
}
@Override
public String formatEconomy(double amt) {
return vault != null ? vault.format(amt) : super.formatEconomy(amt);
}
@Override
public boolean hasBalance(OfflinePlayer player, double cost) {
return vault != null && vault.has(player, cost);