2021-02-07 03:37:25 +01:00
|
|
|
package net.knarcraft.stargate;
|
|
|
|
|
|
|
|
import net.milkbowl.vault.economy.Economy;
|
|
|
|
import org.bukkit.Bukkit;
|
|
|
|
import org.bukkit.entity.Player;
|
|
|
|
import org.bukkit.plugin.Plugin;
|
|
|
|
import org.bukkit.plugin.PluginManager;
|
|
|
|
import org.bukkit.plugin.RegisteredServiceProvider;
|
|
|
|
|
|
|
|
import java.util.UUID;
|
|
|
|
|
2021-02-08 14:30:14 +01:00
|
|
|
/**
|
|
|
|
* This handler handles economy actions such as payment for using a gate
|
|
|
|
*/
|
2021-02-07 03:37:25 +01:00
|
|
|
public class EconomyHandler {
|
|
|
|
public static boolean economyEnabled = false;
|
|
|
|
public static Economy economy = null;
|
|
|
|
public static Plugin vault = null;
|
|
|
|
|
|
|
|
public static int useCost = 0;
|
|
|
|
public static int createCost = 0;
|
|
|
|
public static int destroyCost = 0;
|
|
|
|
public static boolean toOwner = false;
|
|
|
|
public static boolean chargeFreeDestination = true;
|
|
|
|
public static boolean freeGatesGreen = false;
|
|
|
|
|
2021-02-08 14:30:14 +01:00
|
|
|
/**
|
|
|
|
* Gets the balance (money) of the given player
|
2021-02-20 13:57:04 +01:00
|
|
|
*
|
2021-02-08 14:30:14 +01:00
|
|
|
* @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>
|
|
|
|
*/
|
2021-02-07 03:37:25 +01:00
|
|
|
public static double getBalance(Player player) {
|
2021-02-08 14:30:14 +01:00
|
|
|
if (economyEnabled) {
|
|
|
|
return economy.getBalance(player);
|
|
|
|
} else {
|
|
|
|
return 0;
|
2021-02-07 03:37:25 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-02-08 14:30:14 +01:00
|
|
|
/**
|
|
|
|
* Charges a player, giving the charge to a target
|
2021-02-20 13:57:04 +01:00
|
|
|
*
|
2021-02-08 14:30:14 +01:00
|
|
|
* @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>
|
|
|
|
*/
|
2021-02-07 03:37:25 +01:00
|
|
|
public static boolean chargePlayer(Player player, UUID target, double amount) {
|
2021-02-08 14:30:14 +01:00
|
|
|
if (economyEnabled && player.getUniqueId().compareTo(target) != 0 && economy != null) {
|
|
|
|
if (!economy.has(player, amount)) {
|
|
|
|
return false;
|
|
|
|
}
|
2021-02-07 03:37:25 +01:00
|
|
|
economy.withdrawPlayer(player, amount);
|
|
|
|
economy.depositPlayer(Bukkit.getOfflinePlayer(target), amount);
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2021-02-08 14:30:14 +01:00
|
|
|
/**
|
|
|
|
* Charges a player
|
2021-02-20 13:57:04 +01:00
|
|
|
*
|
2021-02-08 14:30:14 +01:00
|
|
|
* @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>
|
|
|
|
*/
|
2021-02-07 03:37:25 +01:00
|
|
|
public static boolean chargePlayer(Player player, double amount) {
|
2021-02-08 14:30:14 +01:00
|
|
|
if (economyEnabled && economy != null) {
|
|
|
|
if (!economy.has(player, amount)) {
|
|
|
|
return false;
|
|
|
|
}
|
2021-02-07 03:37:25 +01:00
|
|
|
economy.withdrawPlayer(player, amount);
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2021-02-08 14:30:14 +01:00
|
|
|
/**
|
|
|
|
* Gets a formatted string for an amount, adding the name of the currency
|
2021-02-20 13:57:04 +01:00
|
|
|
*
|
2021-02-08 14:30:14 +01:00
|
|
|
* @param amount <p>The amount to display</p>
|
|
|
|
* @return <p>A formatted text string describing the amount</p>
|
|
|
|
*/
|
|
|
|
public static String format(int amount) {
|
2021-02-07 03:37:25 +01:00
|
|
|
if (economyEnabled) {
|
2021-02-08 14:30:14 +01:00
|
|
|
return economy.format(amount);
|
|
|
|
} else {
|
|
|
|
return "";
|
2021-02-07 03:37:25 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-02-08 14:30:14 +01:00
|
|
|
/**
|
|
|
|
* Sets up economy by initializing vault and the vault economy provider
|
2021-02-20 13:57:04 +01:00
|
|
|
*
|
2021-02-08 14:30:14 +01:00
|
|
|
* @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;
|
|
|
|
}
|
2021-02-07 03:37:25 +01:00
|
|
|
// Check for Vault
|
2021-02-08 14:30:14 +01:00
|
|
|
Plugin vault = pluginManager.getPlugin("Vault");
|
|
|
|
if (vault != null && vault.isEnabled()) {
|
2021-02-07 03:37:25 +01:00
|
|
|
RegisteredServiceProvider<Economy> economyProvider = Stargate.server.getServicesManager().getRegistration(net.milkbowl.vault.economy.Economy.class);
|
|
|
|
if (economyProvider != null) {
|
|
|
|
economy = economyProvider.getProvider();
|
2021-02-08 14:30:14 +01:00
|
|
|
EconomyHandler.vault = vault;
|
2021-02-07 03:37:25 +01:00
|
|
|
return true;
|
2021-02-09 20:09:49 +01:00
|
|
|
} else {
|
|
|
|
Stargate.log.info(Stargate.getString("prefix") + Stargate.getString("ecoLoadError"));
|
2021-02-07 03:37:25 +01:00
|
|
|
}
|
2021-02-09 20:09:49 +01:00
|
|
|
} else {
|
|
|
|
Stargate.log.info(Stargate.getString("prefix") + Stargate.getString("vaultLoadError"));
|
2021-02-07 03:37:25 +01:00
|
|
|
}
|
|
|
|
economyEnabled = false;
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2021-02-08 14:30:14 +01:00
|
|
|
/**
|
|
|
|
* Gets whether to use economy
|
2021-02-20 13:57:04 +01:00
|
|
|
*
|
2021-02-08 14:30:14 +01:00
|
|
|
* @return <p>True if the user has turned on economy and economy is available</p>
|
|
|
|
*/
|
2021-02-07 03:37:25 +01:00
|
|
|
public static boolean useEconomy() {
|
|
|
|
return economyEnabled && economy != null;
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|