Tidies up and comments the economy handler and removes the depreciated method of getting an offline player by username

This commit is contained in:
Kristian Knarvik 2021-02-08 14:30:14 +01:00
parent f97cb32466
commit e702a0d734
2 changed files with 77 additions and 61 deletions

View File

@ -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 <Contact@TheDgtl.net>
* Copyright (C) 2021 Kristian Knarvik
* <p>
* 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.
* <p>
* 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.
* <p>
* You should have received a copy of the GNU Lesser General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
/*
stargate - A portal plugin for Bukkit
Copyright (C) 2011, 2012 Steven "Drakia" Scott <Contact@TheDgtl.net>
Copyright (C) 2021 Kristian Knarvik
<p>
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.
<p>
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.
<p>
You should have received a copy of the GNU Lesser General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
/**
* 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 <p>The player to get balance for</p>
* @return <p>The current balance of the player. Returns 0 if economy is disabled</p>
*/
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 <p>The player to charge</p>
* @param target <p>The UUID of the player to pay</p>
* @param amount <p>The amount to charge</p>
* @return <p>True if the payment succeeded, or if no payment was necessary</p>
*/
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 <p>The player to charge</p>
* @param amount <p>The amount to charge</p>
* @return <p>True if the payment succeeded, or if no payment was necessary</p>
*/
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 <p>The amount to display</p>
* @return <p>A formatted text string describing the amount</p>
*/
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 <p>The plugin manager to get plugins from</p>
* @return <p>True if economy was enabled</p>
*/
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<Economy> 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 <p>True if the user has turned on economy and economy is available</p>
*/
public static boolean useEconomy() {
return economyEnabled && economy != null;
}

View File

@ -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);
}