package net.knarcraft.stargate.config; import net.knarcraft.stargate.Stargate; import net.knarcraft.stargate.portal.Gate; import net.knarcraft.stargate.portal.Portal; import net.knarcraft.stargate.utility.PermissionHelper; import net.milkbowl.vault.economy.Economy; import org.bukkit.Bukkit; import org.bukkit.configuration.file.FileConfiguration; import org.bukkit.entity.Player; import org.bukkit.plugin.Plugin; import org.bukkit.plugin.PluginManager; import org.bukkit.plugin.RegisteredServiceProvider; import org.bukkit.plugin.ServicesManager; import java.util.UUID; /** * The economy config keeps track of economy config values and performs economy actions such as payment for using a gate */ public final class EconomyConfig { private boolean economyEnabled = false; private Economy economy = null; private Plugin vault = null; private int useCost = 0; private int createCost = 0; private int destroyCost = 0; private boolean toOwner = false; private boolean chargeFreeDestination = true; private boolean freeGatesGreen = false; /** * Instantiates a new economy config * * @param newConfig

The file configuration to read values from

*/ public EconomyConfig(FileConfiguration newConfig) { loadEconomyConfig(newConfig); } /** * Gets the cost of using a gate without a specified cost * * @return

The gate use cost

*/ public int getDefaultUseCost() { return useCost; } /** * Gets whether economy is enabled * * @return

Whether economy is enabled

*/ public boolean isEconomyEnabled() { return economyEnabled; } /** * Gets the economy object to use for transactions * * @return

An economy object, or null if economy is disabled or not initialized

*/ public Economy getEconomy() { return economy; } /** * Gets an instance of the Vault plugin * * @return

An instance of the Vault plugin, or null if Vault is not loaded

*/ public Plugin getVault() { return vault; } /** * Disables economy support by clearing relevant values */ public void disableEconomy() { this.economy = null; this.vault = null; } /** * Gets whether free portals should be marked with green coloring * * @return

Whether free portals should be green

*/ public boolean drawFreePortalsGreen() { return freeGatesGreen; } /** * Whether a gate whose destination is a free gate is still charged * *

If teleporting from a free portal, it's free regardless of destination. If chargeFreeDestination is disabled, * it's also free to teleport back to the free portal. If chargeFreeDestination is enabled, it's only free to * teleport back if teleporting from another free portal.

* * @return

Whether to charge for free destinations

*/ public boolean chargeFreeDestination() { return chargeFreeDestination; } /** * Gets whether payments should be sent to the owner of the used portal * * @return

Whether to send payments to the portal owner

*/ public boolean sendPaymentToOwner() { return toOwner; } /** * Sets the cost of using a gate without a specified cost * *

The use cost cannot be negative.

* * @param useCost

The gate use cost

*/ public void setDefaultUseCost(int useCost) { if (useCost < 0) { throw new IllegalArgumentException("Using a gate cannot cost a negative amount"); } this.useCost = useCost; } /** * Gets the cost of creating a gate without a specified cost * * @return

The gate creation cost

*/ public int getDefaultCreateCost() { return createCost; } /** * Sets the cost of creating a gate without a specified cost * *

The gate create cost cannot be negative

* * @param createCost

The gate creation cost

*/ public void setDefaultCreateCost(int createCost) { this.createCost = createCost; } /** * Gets the cost of destroying a gate without a specified cost * * @return

The gate destruction cost

*/ public int getDefaultDestroyCost() { return destroyCost; } /** * Sets the cost of destroying a gate without a specified cost * * @param destroyCost

The gate destruction cost

*/ public void setDefaultDestroyCost(int destroyCost) { this.destroyCost = destroyCost; } /** * Charges the player for an action, if required * * @param player

The player to take money from

* @param cost

The cost of the transaction

* @return

True if the player was charged successfully

*/ public boolean chargePlayerIfNecessary(Player player, int cost) { if (skipPayment(cost)) { return true; } //Charge player return chargePlayer(player, cost); } /** * Checks whether the given player can afford the given fee * * @param player

The player to check

* @param cost

The fee to pay

* @return

True if the player can afford to pay the fee

*/ public boolean canAffordFee(Player player, int cost) { return economy.getBalance(player) > cost; } /** * Charges the player for an action, if required * * @param player

The player to take money from

* @param target

The target to pay

* @param cost

The cost of the transaction

* @return

True if the player was charged successfully

*/ public boolean chargePlayerIfNecessary(Player player, UUID target, int cost) { if (skipPayment(cost)) { return true; } //Charge player return chargePlayer(player, target, cost); } /** * 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 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 boolean setupEconomy(PluginManager pluginManager) { if (!economyEnabled) { return false; } //Check if vault is loaded Plugin vault = pluginManager.getPlugin("Vault"); if (vault != null && vault.isEnabled()) { ServicesManager servicesManager = Stargate.server.getServicesManager(); RegisteredServiceProvider economyProvider = servicesManager.getRegistration(Economy.class); if (economyProvider != null) { economy = economyProvider.getProvider(); this.vault = vault; return true; } else { Stargate.getConsoleLogger().info(Stargate.getString("prefix") + Stargate.getString("ecoLoadError")); } } else { Stargate.getConsoleLogger().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 boolean useEconomy() { return economyEnabled && economy != null; } /** * Checks whether a payment transaction should be skipped * * @param cost

The cost of the transaction

* @return

True if the transaction should be skipped

*/ private boolean skipPayment(int cost) { return cost == 0 || !useEconomy(); } /** * Determines the cost of using a gate * * @param player

The player trying to use the gate

* @param source

The source/entry portal

* @param destination

The destination portal

* @return

The cost of using the portal

*/ public int getUseCost(Player player, Portal source, Portal destination) { //No payment required if (!useEconomy() || source.getOptions().isFree()) { return 0; } //Not charging for free destinations if (destination != null && !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 player

The player creating the gate

* @param gate

The gate type used

* @return

The cost of creating the gate

*/ public 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 player

The player creating the gate

* @param gate

The gate type used

* @return

The cost of destroying the gate

*/ public int getDestroyCost(Player player, Gate gate) { if (isFree(player, "destroy")) { return 0; } else { return gate.getDestroyCost(); } } /** * Loads all config values related to economy * * @param newConfig

The configuration containing the values to read

*/ private void loadEconomyConfig(FileConfiguration newConfig) { economyEnabled = newConfig.getBoolean("economy.useEconomy"); setDefaultCreateCost(newConfig.getInt("economy.createCost")); setDefaultDestroyCost(newConfig.getInt("economy.destroyCost")); setDefaultUseCost(newConfig.getInt("economy.useCost")); toOwner = newConfig.getBoolean("economy.toOwner"); chargeFreeDestination = newConfig.getBoolean("economy.chargeFreeDestination"); freeGatesGreen = newConfig.getBoolean("economy.freeGatesGreen"); } /** * Determines if a player can do a gate action for free * * @param player

The player to check

* @param permissionNode

The free.permissionNode necessary to allow free gate {action}

* @return

*/ private boolean isFree(Player player, String permissionNode) { return !useEconomy() || PermissionHelper.hasPermission(player, "stargate.free") || PermissionHelper.hasPermission(player, "stargate.free." + permissionNode); } /** * 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

*/ private 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 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

*/ private 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; } }