package net.knarcraft.stargate.config; import net.knarcraft.stargate.Stargate; import net.knarcraft.stargate.portal.PortalSignDrawer; import net.knarcraft.stargate.portal.property.gate.Gate; import net.knarcraft.stargate.utility.PermissionHelper; import net.md_5.bungee.api.ChatColor; import net.milkbowl.vault.economy.Economy; 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.Map; /** * 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 Economy economy = null; private Plugin vault = null; private final Map configOptions; /** * Instantiates a new economy config * * @param configOptions

The loaded config options to read

*/ public EconomyConfig(Map configOptions) { this.configOptions = configOptions; try { String freeColor = (String) configOptions.get(ConfigOption.FREE_GATES_COLOR); PortalSignDrawer.setFreeColor(ChatColor.of(freeColor.toUpperCase())); } catch (IllegalArgumentException | NullPointerException ignored) { PortalSignDrawer.setFreeColor(ChatColor.DARK_GREEN); } } /** * Gets the cost of using a gate without a specified cost * * @return

The gate use cost

*/ public int getDefaultUseCost() { return (Integer) configOptions.get(ConfigOption.USE_COST); } /** * Gets whether economy is enabled * * @return

Whether economy is enabled

*/ public boolean isEconomyEnabled() { return (boolean) configOptions.get(ConfigOption.USE_ECONOMY); } /** * 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 a different coloring * * @return

Whether free portals should be colored

*/ public boolean drawFreePortalsColored() { return (boolean) configOptions.get(ConfigOption.FREE_GATES_COLORED); } /** * 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 freeIfFreeDestination() { return !((boolean) configOptions.get(ConfigOption.CHARGE_FREE_DESTINATION)); } /** * 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 (boolean) configOptions.get(ConfigOption.TO_OWNER); } /** * Gets the cost of creating a gate without a specified cost * * @return

The gate creation cost

*/ public int getDefaultCreateCost() { return (Integer) configOptions.get(ConfigOption.CREATE_COST); } /** * Gets the cost of destroying a gate without a specified cost * * @return

The gate destruction cost

*/ public int getDefaultDestroyCost() { return (Integer) configOptions.get(ConfigOption.DESTROY_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; } /** * 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 (isEconomyEnabled()) { 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 (!isEconomyEnabled()) { return false; } //Check if vault is loaded Plugin vault = pluginManager.getPlugin("Vault"); if (vault != null && vault.isEnabled()) { ServicesManager servicesManager = Stargate.getInstance().getServer().getServicesManager(); RegisteredServiceProvider economyProvider = servicesManager.getRegistration(Economy.class); if (economyProvider != null) { economy = economyProvider.getProvider(); this.vault = vault; return true; } else { Stargate.logInfo(Stargate.getString("ecoLoadError")); } } else { Stargate.logInfo(Stargate.getString("vaultLoadError")); } configOptions.put(ConfigOption.USE_ECONOMY, 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 isEconomyEnabled() && economy != null; } /** * 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(); } } /** * 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." + permissionNode); } }