diff --git a/src/main/java/net/knarcraft/stargate/EconomyHandler.java b/src/main/java/net/knarcraft/stargate/EconomyHandler.java index 0d4c94f..b8bf120 100644 --- a/src/main/java/net/knarcraft/stargate/EconomyHandler.java +++ b/src/main/java/net/knarcraft/stargate/EconomyHandler.java @@ -9,25 +9,28 @@ import org.bukkit.plugin.RegisteredServiceProvider; import java.util.UUID; -/** - * stargate - A portal plugin for Bukkit - * Copyright (C) 2011, 2012 Steven "Drakia" Scott - * Copyright (C) 2021 Kristian Knarvik - *

- * This program is free software: you can redistribute it and/or modify - * it under the terms of the GNU Lesser General Public License as published by - * the Free Software Foundation, either version 3 of the License, or - * (at your option) any later version. - *

- * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU Lesser General Public License for more details. - *

- * You should have received a copy of the GNU Lesser General Public License - * along with this program. If not, see . +/* + stargate - A portal plugin for Bukkit + Copyright (C) 2011, 2012 Steven "Drakia" Scott + Copyright (C) 2021 Kristian Knarvik +

+ This program is free software: you can redistribute it and/or modify + it under the terms of the GNU Lesser General Public License as published by + the Free Software Foundation, either version 3 of the License, or + (at your option) any later version. +

+ This program is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU Lesser General Public License for more details. +

+ You should have received a copy of the GNU Lesser General Public License + along with this program. If not, see . */ +/** + * 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; @@ -40,65 +43,94 @@ public class EconomyHandler { 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 0; - return economy.getBalance(player); - } - - public static boolean chargePlayer(Player player, String target, double amount) { - if (!economyEnabled) return true; - if (player.getName().equals(target)) return true; - if (economy != null) { - if (!economy.has(player, amount)) return false; - economy.withdrawPlayer(player, amount); - economy.depositPlayer(target, amount); + if (economyEnabled) { + return economy.getBalance(player); + } else { + return 0; } - 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

+ */ public static boolean chargePlayer(Player player, UUID target, double amount) { - if (!economyEnabled) return true; - if (player.getUniqueId().compareTo(target) == 0) return true; - if (economy != null) { - if (!economy.has(player, amount)) return false; + 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) return true; - if (economy != null) { - if (!economy.has(player, amount)) return false; + if (economyEnabled && economy != null) { + if (!economy.has(player, amount)) { + return false; + } economy.withdrawPlayer(player, amount); } return true; } - public static String format(int amt) { + /** + * 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(amt); + return economy.format(amount); + } else { + return ""; } - return ""; } - public static boolean setupEconomy(PluginManager pm) { - if (!economyEnabled) return false; + /** + * 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 p = pm.getPlugin("Vault"); - if (p != null && p.isEnabled()) { + 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(); - vault = p; + EconomyHandler.vault = vault; return true; } } + Stargate.log.info("[stargate] Economy is enabled but vault could not be loaded. Economy disabled"); 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; } diff --git a/src/main/java/net/knarcraft/stargate/Stargate.java b/src/main/java/net/knarcraft/stargate/Stargate.java index 9079b37..a533912 100644 --- a/src/main/java/net/knarcraft/stargate/Stargate.java +++ b/src/main/java/net/knarcraft/stargate/Stargate.java @@ -542,18 +542,6 @@ public class Stargate extends JavaPlugin { return portal.isOwner(player) && hasPerm(player, "stargate.destroy.personal"); } - /* - * Charge player for {action} if required, true on success, false if can't afford - */ - public static boolean chargePlayer(Player player, String target, int cost) { - // If cost is 0 - if (cost == 0) return true; - // Economy is disabled - if (!EconomyHandler.useEconomy()) return true; - // Charge player - return EconomyHandler.chargePlayer(player, target, cost); - } - /* * Charge player for {action} if required, true on success, false if can't afford */ @@ -817,11 +805,7 @@ public class Stargate extends JavaPlugin { if (cost > 0) { boolean success; if (portal.getGate().getToOwner()) { - if (portal.getOwnerUUID() == null) { - success = Stargate.chargePlayer(player, portal.getOwnerUUID(), cost); - } else { - success = Stargate.chargePlayer(player, portal.getOwnerName(), cost); - } + success = portal.getOwnerUUID() != null && Stargate.chargePlayer(player, portal.getOwnerUUID(), cost); } else { success = Stargate.chargePlayer(player, cost); }