package net.knarcraft.stargate.utility; import net.knarcraft.stargate.Stargate; import net.knarcraft.stargate.portal.Gate; import net.knarcraft.stargate.portal.Portal; import net.milkbowl.vault.economy.Economy; import org.bukkit.Bukkit; import org.bukkit.entity.Player; import org.bukkit.plugin.Plugin; import org.bukkit.plugin.PluginManager; import org.bukkit.plugin.RegisteredServiceProvider; import java.util.UUID; /** * This handler handles economy actions such as payment for using a gate */ public final class EconomyHandler { public static boolean economyEnabled = false; public static Economy economy = null; public static Plugin vault = null; private static int useCost = 0; private static int createCost = 0; private static int destroyCost = 0; public static boolean toOwner = false; public static boolean chargeFreeDestination = true; public static boolean freeGatesGreen = false; /** * Gets the cost of using a gate without a specified cost * * @return
The gate use cost
*/ public static int getUseCost() { return useCost; } /** * Sets the cost of using a gate without a specified cost * *The use cost cannot be negative.
* * @param useCostThe gate use cost
*/ public static void setUseCost(int useCost) { if (useCost < 0) { throw new IllegalArgumentException("Using a gate cannot cost a negative amount"); } EconomyHandler.useCost = useCost; } /** * Gets the cost of creating a gate without a specified cost * * @returnThe gate creation cost
*/ public static int getDefaultCreateCost() { return createCost; } /** * Sets the cost of creating a gate without a specified cost * *The gate create cost cannot be negative
* * @param createCostThe gate creation cost
*/ public static void setCreateCost(int createCost) { EconomyHandler.createCost = createCost; } /** * Gets the cost of destroying a gate without a specified cost * * @returnThe gate destruction cost
*/ public static int getDefaultDestroyCost() { return destroyCost; } /** * Sets the cost of destroying a gate without a specified cost * * @param destroyCostThe gate destruction cost
*/ public static void setDestroyCost(int destroyCost) { EconomyHandler.destroyCost = destroyCost; } /** * Charges the player for an action, if required * * @param playerThe player to take money from
* @param costThe cost of the transaction
* @returnTrue if the player was charged successfully
*/ public static boolean chargePlayerIfNecessary(Player player, int cost) { if (skipPayment(cost)) { return true; } // Charge player return EconomyHandler.chargePlayer(player, cost); } /** * Charges the player for an action, if required * * @param playerThe player to take money from
* @param targetThe target to pay
* @param costThe cost of the transaction
* @returnTrue if the player was charged successfully
*/ public static boolean chargePlayerIfNecessary(Player player, UUID target, int cost) { if (skipPayment(cost)) { return true; } // Charge player return EconomyHandler.chargePlayer(player, target, cost); } /** * Gets a formatted string for an amount, adding the name of the currency * * @param amountThe amount to display
* @returnA formatted text string describing the amount
*/ public static String format(int amount) { if (economyEnabled) { return economy.format(amount); } else { return ""; } } /** * Sets up economy by initializing vault and the vault economy provider * * @param pluginManagerThe plugin manager to get plugins from
* @returnTrue if economy was enabled
*/ public static boolean setupEconomy(PluginManager pluginManager) { if (!economyEnabled) { return false; } // Check for Vault Plugin vault = pluginManager.getPlugin("Vault"); if (vault != null && vault.isEnabled()) { RegisteredServiceProviderTrue if the user has turned on economy and economy is available
*/ public static boolean useEconomy() { return economyEnabled && economy != null; } /** * Checks whether a payment transaction should be skipped * * @param costThe cost of the transaction
* @returnTrue if the transaction should be skipped
*/ private static boolean skipPayment(int cost) { return cost == 0 || !EconomyHandler.useEconomy(); } /** * Determines the cost of using a gate * * @param playerThe player trying to use the gate
* @param sourceThe source/entry portal
* @param destinationThe destination portal
* @returnThe cost of using the portal
*/ public static int getUseCost(Player player, Portal source, Portal destination) { //No payment required if (!EconomyHandler.useEconomy() || source.getOptions().isFree()) { return 0; } //Not charging for free destinations if (destination != null && !EconomyHandler.chargeFreeDestination && destination.getOptions().isFree()) { return 0; } //Cost is 0 if the player owns this gate and funds go to the owner if (source.getGate().getToOwner() && source.isOwner(player)) { return 0; } //Player gets free gate use if (PermissionHelper.hasPermission(player, "stargate.free") || PermissionHelper.hasPermission(player, "stargate.free.use")) { return 0; } return source.getGate().getUseCost(); } /** * Gets the cost of creating the given gate * * @param playerThe player creating the gate
* @param gateThe gate type used
* @returnThe cost of creating the gate
*/ public static int getCreateCost(Player player, Gate gate) { if (isFree(player, "create")) { return 0; } else { return gate.getCreateCost(); } } /** * Gets the cost of destroying the given gate * * @param playerThe player creating the gate
* @param gateThe gate type used
* @returnThe cost of destroying the gate
*/ public static int getDestroyCost(Player player, Gate gate) { if (isFree(player, "destroy")) { return 0; } else { return gate.getDestroyCost(); } } /** * Determines if a player can do a gate action for free * * @param playerThe player to check
* @param permissionNodeThe free.permissionNode necessary to allow free gate {action}
* @return */ private static boolean isFree(Player player, String permissionNode) { return !EconomyHandler.useEconomy() || PermissionHelper.hasPermission(player, "stargate.free") || PermissionHelper.hasPermission(player, "stargate.free." + permissionNode); } /** * Charges a player * * @param playerThe player to charge
* @param amountThe amount to charge
* @returnTrue if the payment succeeded, or if no payment was necessary
*/ private static boolean chargePlayer(Player player, double amount) { if (economyEnabled && economy != null) { if (!economy.has(player, amount)) { return false; } economy.withdrawPlayer(player, amount); } return true; } /** * Charges a player, giving the charge to a target * * @param playerThe player to charge
* @param targetThe UUID of the player to pay
* @param amountThe amount to charge
* @returnTrue if the payment succeeded, or if no payment was necessary
*/ private static boolean chargePlayer(Player player, UUID target, double amount) { if (economyEnabled && player.getUniqueId().compareTo(target) != 0 && economy != null) { if (!economy.has(player, amount)) { return false; } //Take money from the user and give to the owner economy.withdrawPlayer(player, amount); economy.depositPlayer(Bukkit.getOfflinePlayer(target), amount); } return true; } }