Moves some code from Stargate to EconomyHandler

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

View File

@ -264,8 +264,11 @@ public class Stargate extends JavaPlugin {
return !hasPermission(player, "stargate.world." + world);
}
/*
* Check whether player can use network
/**
* Checks whether a player can access the given network
* @param player <p>The player to check</p>
* @param network <p>The network to check</p>
* @return <p>True if the player is denied from accessing the network</p>
*/
public static boolean cannotAccessNetwork(Player player, String network) {
// Can user all stargate player features, or access all networks
@ -273,8 +276,10 @@ public class Stargate extends JavaPlugin {
// Do a deep check to see if the player lacks this specific network node
return !hasPermDeep(player, "stargate.network." + network);
}
// Can access this network
if (hasPermission(player, "stargate.network." + network)) return false;
//Check if the player can access this network
if (hasPermission(player, "stargate.network." + network)) {
return false;
}
// Is able to create personal gates (Assumption is made they can also access them)
String playerName = player.getName();
if (playerName.length() > 11) playerName = playerName.substring(0, 11);
@ -457,72 +462,6 @@ public class Stargate extends JavaPlugin {
return portal.isOwner(player) && hasPermission(player, "stargate.destroy.personal");
}
/*
* Charge player for {action} if required, true on success, false if can't afford
*/
public static boolean chargePlayer(Player player, UUID 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
*/
public static boolean chargePlayer(Player player, 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, cost);
}
/*
* Determine the cost of a gate
*/
public static int getUseCost(Player player, Portal src, Portal dest) {
// Not using Economy
if (!EconomyHandler.useEconomy()) return 0;
// Portal is free
if (src.isFree()) return 0;
// Not charging for free destinations
if (dest != null && !EconomyHandler.chargeFreeDestination && dest.isFree()) return 0;
// Cost is 0 if the player owns this gate and funds go to the owner
if (src.getGate().getToOwner() && src.isOwner(player)) return 0;
// Player gets free gate use
if (hasPermission(player, "stargate.free") || hasPermission(player, "stargate.free.use")) return 0;
return src.getGate().getUseCost();
}
/*
* Determine the cost to create the gate
*/
public static int getCreateCost(Player player, Gate gate) {
// Not using Economy
if (!EconomyHandler.useEconomy()) return 0;
// Player gets free gate destruction
if (hasPermission(player, "stargate.free") || hasPermission(player, "stargate.free.create")) return 0;
return gate.getCreateCost();
}
/*
* Determine the cost to destroy the gate
*/
public static int getDestroyCost(Player player, Gate gate) {
// Not using Economy
if (!EconomyHandler.useEconomy()) return 0;
// Player gets free gate destruction
if (hasPermission(player, "stargate.free") || hasPermission(player, "stargate.free.destroy")) return 0;
return gate.getDestroyCost();
}
/**
* Replaces a list of variables in a string in the order they are given
*

View File

@ -4,6 +4,7 @@ import net.knarcraft.stargate.portal.Portal;
import net.knarcraft.stargate.portal.PortalHandler;
import net.knarcraft.stargate.Stargate;
import net.knarcraft.stargate.event.StargateDestroyEvent;
import net.knarcraft.stargate.utility.EconomyHandler;
import net.knarcraft.stargate.utility.EconomyHelper;
import net.knarcraft.stargate.utility.MaterialHelper;
import org.bukkit.Material;
@ -89,7 +90,7 @@ public class BlockEventListener implements Listener {
Stargate.log.info(Stargate.getString("prefix") + player.getName() + " tried to destroy gate");
}
int cost = Stargate.getDestroyCost(player, portal.getGate());
int cost = EconomyHandler.getDestroyCost(player, portal.getGate());
//Create and call a StarGateDestroyEvent
StargateDestroyEvent destroyEvent = new StargateDestroyEvent(portal, player, deny, denyMsg, cost);
@ -128,16 +129,19 @@ public class BlockEventListener implements Listener {
BlockBreakEvent event) {
int cost = destroyEvent.getCost();
if (cost != 0) {
if (!Stargate.chargePlayer(player, cost)) {
String portalName = portal.getName();
//Cannot pay
if (!EconomyHandler.chargePlayerIfNecessary(player, cost)) {
Stargate.debug("onBlockBreak", "Insufficient Funds");
EconomyHelper.sendInsufficientFundsMessage(portal.getName(), player, cost);
EconomyHelper.sendInsufficientFundsMessage(portalName, player, cost);
event.setCancelled(true);
return false;
}
//Tell the player they've paid or deceived money
if (cost > 0) {
EconomyHelper.sendDeductMessage(portal.getName(), player, cost);
EconomyHelper.sendDeductMessage(portalName, player, cost);
} else {
EconomyHelper.sendRefundMessage(portal.getName(), player, cost);
EconomyHelper.sendRefundMessage(portalName, player, cost);
}
}
return true;

View File

@ -5,6 +5,7 @@ import net.knarcraft.stargate.portal.Portal;
import net.knarcraft.stargate.portal.PortalHandler;
import net.knarcraft.stargate.Stargate;
import net.knarcraft.stargate.utility.BungeeHelper;
import net.knarcraft.stargate.utility.EconomyHandler;
import net.knarcraft.stargate.utility.EconomyHelper;
import net.knarcraft.stargate.utility.MaterialHelper;
import org.bukkit.GameMode;
@ -378,7 +379,7 @@ public class PlayerEventListener implements Listener {
}
//Player cannot pay for teleportation
int cost = Stargate.getUseCost(player, entrancePortal, destination);
int cost = EconomyHandler.getUseCost(player, entrancePortal, destination);
if (cost > 0) {
return EconomyHelper.payTeleportFee(entrancePortal, player, cost);
}

View File

@ -3,6 +3,7 @@ package net.knarcraft.stargate.listener;
import net.knarcraft.stargate.portal.Portal;
import net.knarcraft.stargate.portal.PortalHandler;
import net.knarcraft.stargate.Stargate;
import net.knarcraft.stargate.utility.EconomyHandler;
import net.knarcraft.stargate.utility.EconomyHelper;
import net.knarcraft.stargate.utility.EntityHelper;
import org.bukkit.entity.Entity;
@ -99,7 +100,7 @@ public class VehicleEventListener implements Listener {
}
//Transfer payment if necessary
int cost = Stargate.getUseCost(player, entrancePortal, destinationPortal);
int cost = EconomyHandler.getUseCost(player, entrancePortal, destinationPortal);
if (cost > 0) {
if (!EconomyHelper.payTeleportFee(entrancePortal, player, cost)) {
return;

View File

@ -161,7 +161,7 @@ public class Gate {
* @return <p>The cost of creating a portal with this gate</p>
*/
public Integer getCreateCost() {
return createCost < 0 ? EconomyHandler.getCreateCost() : createCost;
return createCost < 0 ? EconomyHandler.getDefaultCreateCost() : createCost;
}
/**
@ -170,8 +170,7 @@ public class Gate {
* @return <p>The cost of destroying a portal with this gate</p>
*/
public Integer getDestroyCost() {
if (destroyCost < 0) return EconomyHandler.getDestroyCost();
return destroyCost;
return destroyCost < 0 ? EconomyHandler.getDefaultDestroyCost() : destroyCost;
}
/**

View File

@ -6,6 +6,7 @@ import net.knarcraft.stargate.Stargate;
import net.knarcraft.stargate.container.TwoTuple;
import net.knarcraft.stargate.event.StargateCreateEvent;
import net.knarcraft.stargate.utility.DirectionHelper;
import net.knarcraft.stargate.utility.EconomyHandler;
import net.knarcraft.stargate.utility.EconomyHelper;
import org.bukkit.Bukkit;
import org.bukkit.Location;
@ -395,7 +396,7 @@ public class PortalHandler {
portal = new Portal(topLeft, modX, modZ, yaw, id, button, destinationName, name, false, network,
gate, player.getUniqueId(), player.getName(), portalOptions);
int cost = Stargate.getCreateCost(player, gate);
int cost = EconomyHandler.getCreateCost(player, gate);
// Call StargateCreateEvent
StargateCreateEvent cEvent = new StargateCreateEvent(player, portal, event.getLines(), deny, denyMsg, cost);
@ -440,7 +441,7 @@ public class PortalHandler {
}
if (cost > 0) {
if (!Stargate.chargePlayer(player, cost)) {
if (!EconomyHandler.chargePlayerIfNecessary(player, cost)) {
EconomyHelper.sendInsufficientFundsMessage(name, player, cost);
Stargate.debug("createPortal", "Insufficient Funds");
return null;

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,41 +103,33 @@ 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;
}
// 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;
}
// Charge player
return EconomyHandler.chargePlayer(player, target, cost);
}
/**
* Gets a formatted string for an amount, adding the name of the currency
@ -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