package net.knarcraft.stargate; 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 class EconomyHandler { public static boolean economyEnabled = false; public static Economy economy = null; public static Plugin vault = null; public static int useCost = 0; public static int createCost = 0; public static int destroyCost = 0; public static boolean toOwner = false; public static boolean chargeFreeDestination = true; public static boolean freeGatesGreen = false; /** * Gets the balance (money) of the given player * @param player

The player to get balance for

* @return

The current balance of the player. Returns 0 if economy is disabled

*/ public static double getBalance(Player player) { if (economyEnabled) { return economy.getBalance(player); } else { return 0; } } /** * Charges a player, giving the charge to a target * @param player

The player to charge

* @param target

The UUID of the player to pay

* @param amount

The amount to charge

* @return

True if the payment succeeded, or if no payment was necessary

*/ public 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; } economy.withdrawPlayer(player, amount); economy.depositPlayer(Bukkit.getOfflinePlayer(target), amount); } return true; } /** * Charges a player * @param player

The player to charge

* @param amount

The amount to charge

* @return

True if the payment succeeded, or if no payment was necessary

*/ public static boolean chargePlayer(Player player, double amount) { if (economyEnabled && economy != null) { if (!economy.has(player, amount)) { return false; } economy.withdrawPlayer(player, amount); } return true; } /** * Gets a formatted string for an amount, adding the name of the currency * @param amount

The amount to display

* @return

A 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 pluginManager

The plugin manager to get plugins from

* @return

True 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()) { RegisteredServiceProvider economyProvider = Stargate.server.getServicesManager().getRegistration(net.milkbowl.vault.economy.Economy.class); if (economyProvider != null) { economy = economyProvider.getProvider(); EconomyHandler.vault = vault; return true; } else { Stargate.log.info(Stargate.getString("prefix") + Stargate.getString("ecoLoadError")); } } else { Stargate.log.info(Stargate.getString("prefix") + Stargate.getString("vaultLoadError")); } economyEnabled = false; return false; } /** * Gets whether to use economy * @return

True if the user has turned on economy and economy is available

*/ public static boolean useEconomy() { return economyEnabled && economy != null; } }