Moves some code from Stargate to EconomyHandler

This commit is contained in:
2021-09-19 15:05:19 +02:00
parent b191ac1de5
commit 8835e69e3c
8 changed files with 166 additions and 113 deletions

View File

@ -1,6 +1,8 @@
package net.knarcraft.stargate.utility;
import net.knarcraft.stargate.Stargate;
import net.knarcraft.stargate.portal.Gate;
import net.knarcraft.stargate.portal.Portal;
import net.milkbowl.vault.economy.Economy;
import org.bukkit.Bukkit;
import org.bukkit.entity.Player;
@ -53,7 +55,7 @@ public final class EconomyHandler {
*
* @return <p>The gate creation cost</p>
*/
public static int getCreateCost() {
public static int getDefaultCreateCost() {
return createCost;
}
@ -73,7 +75,7 @@ public final class EconomyHandler {
*
* @return <p>The gate destruction cost</p>
*/
public static int getDestroyCost() {
public static int getDefaultDestroyCost() {
return destroyCost;
}
@ -101,40 +103,32 @@ public final class EconomyHandler {
}
/**
* 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>
* Charges the player for an action, if required
* @param player <p>The player to take money from</p>
* @param cost <p>The cost of the transaction</p>
* @return <p>True if the player was charged successfully</p>
*/
public static boolean chargePlayer(Player player, UUID target, double amount) {
if (economyEnabled && player.getUniqueId().compareTo(target) != 0 && economy != null) {
if (!economy.has(player, amount)) {
return false;
}
//Take money from the user and give to the owner
economy.withdrawPlayer(player, amount);
economy.depositPlayer(Bukkit.getOfflinePlayer(target), amount);
public static boolean chargePlayerIfNecessary(Player player, int cost) {
if (skipPayment(cost)) {
return true;
}
return true;
// Charge player
return EconomyHandler.chargePlayer(player, cost);
}
/**
* 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>
* Charges the player for an action, if required
* @param player <p>The player to take money from</p>
* @param target <p>The target to pay</p>
* @param cost <p>The cost of the transaction</p>
* @return <p>True if the player was charged successfully</p>
*/
public static boolean chargePlayer(Player player, double amount) {
if (economyEnabled && economy != null) {
if (!economy.has(player, amount)) {
return false;
}
economy.withdrawPlayer(player, amount);
public static boolean chargePlayerIfNecessary(Player player, UUID target, int cost) {
if (skipPayment(cost)) {
return true;
}
return true;
// Charge player
return EconomyHandler.chargePlayer(player, target, cost);
}
/**
@ -188,4 +182,118 @@ public final class EconomyHandler {
return economyEnabled && economy != null;
}
/**
* Checks whether a payment transaction should be skipped
* @param cost <p>The cost of the transaction</p>
* @return <p>True if the transaction should be skipped</p>
*/
private static boolean skipPayment(int cost) {
return cost == 0 || !EconomyHandler.useEconomy();
}
/**
* Determines the cost of using a gate
*
* @param player <p>The player trying to use the gate</p>
* @param source <p>The source/entry portal</p>
* @param destination <p>The destination portal</p>
* @return <p>The cost of using the portal</p>
*/
public static int getUseCost(Player player, Portal source, Portal destination) {
//No payment required
if (!EconomyHandler.useEconomy() || source.isFree()) {
return 0;
}
//Not charging for free destinations
if (destination != null && !EconomyHandler.chargeFreeDestination && destination.isFree()) {
return 0;
}
//Cost is 0 if the player owns this gate and funds go to the owner
if (source.getGate().getToOwner() && source.isOwner(player)) {
return 0;
}
//Player gets free gate use
if (Stargate.hasPermission(player, "stargate.free") || Stargate.hasPermission(player, "stargate.free.use")) {
return 0;
}
return source.getGate().getUseCost();
}
/**
* Gets the cost of creating the given gate
* @param player <p>The player creating the gate</p>
* @param gate <p>The gate type used</p>
* @return <p>The cost of creating the gate</p>
*/
public static 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 <p>The player creating the gate</p>
* @param gate <p>The gate type used</p>
* @return <p>The cost of destroying the gate</p>
*/
public static 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 <p>The player to check</p>
* @param permissionNode <p>The free.permissionNode necessary to allow free gate {action}</p>
* @return <p></p>
*/
private static boolean isFree(Player player, String permissionNode) {
return !EconomyHandler.useEconomy() || Stargate.hasPermission(player, "stargate.free") ||
Stargate.hasPermission(player, "stargate.free." + permissionNode);
}
/**
* 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>
*/
private static boolean chargePlayer(Player player, double amount) {
if (economyEnabled && economy != null) {
if (!economy.has(player, amount)) {
return false;
}
economy.withdrawPlayer(player, 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>
*/
private static boolean chargePlayer(Player player, UUID target, double amount) {
if (economyEnabled && player.getUniqueId().compareTo(target) != 0 && economy != null) {
if (!economy.has(player, amount)) {
return false;
}
//Take money from the user and give to the owner
economy.withdrawPlayer(player, amount);
economy.depositPlayer(Bukkit.getOfflinePlayer(target), amount);
}
return true;
}
}

View File

@ -26,10 +26,10 @@ public final class EconomyHelper {
//Try to charge the player
if (entrancePortal.getGate().getToOwner()) {
success = entrancePortal.getOwnerUUID() != null && Stargate.chargePlayer(player,
success = entrancePortal.getOwnerUUID() != null && EconomyHandler.chargePlayerIfNecessary(player,
entrancePortal.getOwnerUUID(), cost);
} else {
success = Stargate.chargePlayer(player, cost);
success = EconomyHandler.chargePlayerIfNecessary(player, cost);
}
// Insufficient Funds