Tidies up and comments the economy handler and removes the depreciated method of getting an offline player by username
This commit is contained in:
parent
f97cb32466
commit
e702a0d734
@ -9,25 +9,28 @@ import org.bukkit.plugin.RegisteredServiceProvider;
|
|||||||
|
|
||||||
import java.util.UUID;
|
import java.util.UUID;
|
||||||
|
|
||||||
/**
|
/*
|
||||||
* stargate - A portal plugin for Bukkit
|
stargate - A portal plugin for Bukkit
|
||||||
* Copyright (C) 2011, 2012 Steven "Drakia" Scott <Contact@TheDgtl.net>
|
Copyright (C) 2011, 2012 Steven "Drakia" Scott <Contact@TheDgtl.net>
|
||||||
* Copyright (C) 2021 Kristian Knarvik
|
Copyright (C) 2021 Kristian Knarvik
|
||||||
* <p>
|
<p>
|
||||||
* This program is free software: you can redistribute it and/or modify
|
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
|
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
|
the Free Software Foundation, either version 3 of the License, or
|
||||||
* (at your option) any later version.
|
(at your option) any later version.
|
||||||
* <p>
|
<p>
|
||||||
* This program is distributed in the hope that it will be useful,
|
This program is distributed in the hope that it will be useful,
|
||||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||||
* GNU Lesser General Public License for more details.
|
GNU Lesser General Public License for more details.
|
||||||
* <p>
|
<p>
|
||||||
* You should have received a copy of the GNU Lesser General Public License
|
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/>.
|
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 class EconomyHandler {
|
||||||
public static boolean economyEnabled = false;
|
public static boolean economyEnabled = false;
|
||||||
public static Economy economy = null;
|
public static Economy economy = null;
|
||||||
@ -40,65 +43,94 @@ public class EconomyHandler {
|
|||||||
public static boolean chargeFreeDestination = true;
|
public static boolean chargeFreeDestination = true;
|
||||||
public static boolean freeGatesGreen = false;
|
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) {
|
public static double getBalance(Player player) {
|
||||||
if (!economyEnabled) return 0;
|
if (economyEnabled) {
|
||||||
return economy.getBalance(player);
|
return economy.getBalance(player);
|
||||||
}
|
} else {
|
||||||
|
return 0;
|
||||||
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);
|
|
||||||
}
|
}
|
||||||
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) {
|
public static boolean chargePlayer(Player player, UUID target, double amount) {
|
||||||
if (!economyEnabled) return true;
|
if (economyEnabled && player.getUniqueId().compareTo(target) != 0 && economy != null) {
|
||||||
if (player.getUniqueId().compareTo(target) == 0) return true;
|
if (!economy.has(player, amount)) {
|
||||||
if (economy != null) {
|
return false;
|
||||||
if (!economy.has(player, amount)) return false;
|
}
|
||||||
economy.withdrawPlayer(player, amount);
|
economy.withdrawPlayer(player, amount);
|
||||||
economy.depositPlayer(Bukkit.getOfflinePlayer(target), amount);
|
economy.depositPlayer(Bukkit.getOfflinePlayer(target), amount);
|
||||||
}
|
}
|
||||||
return true;
|
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) {
|
public static boolean chargePlayer(Player player, double amount) {
|
||||||
if (!economyEnabled) return true;
|
if (economyEnabled && economy != null) {
|
||||||
if (economy != null) {
|
if (!economy.has(player, amount)) {
|
||||||
if (!economy.has(player, amount)) return false;
|
return false;
|
||||||
|
}
|
||||||
economy.withdrawPlayer(player, amount);
|
economy.withdrawPlayer(player, amount);
|
||||||
}
|
}
|
||||||
return true;
|
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) {
|
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
|
// Check for Vault
|
||||||
Plugin p = pm.getPlugin("Vault");
|
Plugin vault = pluginManager.getPlugin("Vault");
|
||||||
if (p != null && p.isEnabled()) {
|
if (vault != null && vault.isEnabled()) {
|
||||||
RegisteredServiceProvider<Economy> economyProvider = Stargate.server.getServicesManager().getRegistration(net.milkbowl.vault.economy.Economy.class);
|
RegisteredServiceProvider<Economy> economyProvider = Stargate.server.getServicesManager().getRegistration(net.milkbowl.vault.economy.Economy.class);
|
||||||
if (economyProvider != null) {
|
if (economyProvider != null) {
|
||||||
economy = economyProvider.getProvider();
|
economy = economyProvider.getProvider();
|
||||||
vault = p;
|
EconomyHandler.vault = vault;
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
Stargate.log.info("[stargate] Economy is enabled but vault could not be loaded. Economy disabled");
|
||||||
economyEnabled = false;
|
economyEnabled = false;
|
||||||
return 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() {
|
public static boolean useEconomy() {
|
||||||
return economyEnabled && economy != null;
|
return economyEnabled && economy != null;
|
||||||
}
|
}
|
||||||
|
@ -542,18 +542,6 @@ public class Stargate extends JavaPlugin {
|
|||||||
return portal.isOwner(player) && hasPerm(player, "stargate.destroy.personal");
|
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
|
* 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) {
|
if (cost > 0) {
|
||||||
boolean success;
|
boolean success;
|
||||||
if (portal.getGate().getToOwner()) {
|
if (portal.getGate().getToOwner()) {
|
||||||
if (portal.getOwnerUUID() == null) {
|
success = portal.getOwnerUUID() != null && Stargate.chargePlayer(player, portal.getOwnerUUID(), cost);
|
||||||
success = Stargate.chargePlayer(player, portal.getOwnerUUID(), cost);
|
|
||||||
} else {
|
|
||||||
success = Stargate.chargePlayer(player, portal.getOwnerName(), cost);
|
|
||||||
}
|
|
||||||
} else {
|
} else {
|
||||||
success = Stargate.chargePlayer(player, cost);
|
success = Stargate.chargePlayer(player, cost);
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user