package net.knarcraft.stargate.utility; import net.knarcraft.stargate.Stargate; import net.knarcraft.stargate.portal.Portal; import net.knarcraft.stargate.portal.PortalOwner; import org.bukkit.entity.Player; import java.util.UUID; /** * The economy helper class has helper functions for player payment */ public final class EconomyHelper { private EconomyHelper() { } /** * Tries to make the given user pay the teleport fee * * @param entrancePortal

The portal the player is entering

* @param player

The player wishing to teleport

* @param cost

The cost of teleportation

* @return

False if payment was successful. True if the payment was unsuccessful

*/ public static boolean cannotPayTeleportFee(Portal entrancePortal, Player player, int cost) { boolean success; //Try to charge the player. Paying the portal owner is only possible if a UUID is available if (entrancePortal.getGate().getToOwner()) { UUID ownerUUID = entrancePortal.getOwner().getUUID(); success = ownerUUID != null && EconomyHandler.chargePlayerIfNecessary(player, ownerUUID, cost); } else { success = EconomyHandler.chargePlayerIfNecessary(player, cost); } //Send the insufficient funds message if (!success) { sendInsufficientFundsMessage(entrancePortal.getName(), player, cost); entrancePortal.getPortalOpener().closePortal(false); return true; } //Send the deduct-message to the player sendDeductMessage(entrancePortal.getName(), player, cost); if (entrancePortal.getGate().getToOwner()) { PortalOwner owner = entrancePortal.getOwner(); Player portalOwner; if (owner.getUUID() != null) { portalOwner = Stargate.server.getPlayer(owner.getUUID()); } else { portalOwner = Stargate.server.getPlayer(owner.getName()); } //Notify the gate owner of received payment if (portalOwner != null) { sendObtainMessage(entrancePortal.getName(), portalOwner, cost); } } return false; } /** * Sends a message to the gate owner telling him/her how much he/she earned from a player using his/her gate * * @param portalName

The name of the used portal

* @param portalOwner

The owner of the portal

* @param earnings

The amount the owner earned

*/ public static void sendObtainMessage(String portalName, Player portalOwner, int earnings) { String obtainedMsg = Stargate.getString("ecoObtain"); obtainedMsg = replaceVars(obtainedMsg, portalName, earnings); Stargate.sendSuccessMessage(portalOwner, obtainedMsg); } /** * Sends a message telling the user how much they paid for interacting with a portal * * @param portalName

The name of the portal interacted with

* @param player

The interacting player

* @param cost

The cost of the interaction

*/ public static void sendDeductMessage(String portalName, Player player, int cost) { String deductMsg = Stargate.getString("ecoDeduct"); deductMsg = replaceVars(deductMsg, portalName, cost); Stargate.sendSuccessMessage(player, deductMsg); } /** * Sends a message telling the user they don't have enough funds to do a portal interaction * * @param portalName

The name of the portal interacted with

* @param player

The interacting player

* @param cost

The cost of the interaction

*/ public static void sendInsufficientFundsMessage(String portalName, Player player, int cost) { String inFundMsg = Stargate.getString("ecoInFunds"); inFundMsg = replaceVars(inFundMsg, portalName, cost); Stargate.sendErrorMessage(player, inFundMsg); } /** * Sends a message telling the user how much they are refunded for breaking their portal * * @param portalName

The name of the broken portal

* @param player

The player breaking the portal

* @param cost

The amount the user has to pay for destroying the portal. (expects a negative value)

*/ public static void sendRefundMessage(String portalName, Player player, int cost) { String refundMsg = Stargate.getString("ecoRefund"); refundMsg = replaceVars(refundMsg, portalName, -cost); Stargate.sendSuccessMessage(player, refundMsg); } /** * Replaces the cost and portal variables in a string * * @param message

The message to replace variables in

* @param portalName

The name of the relevant portal

* @param cost

The cost for a given interaction

* @return

The same string with cost and portal variables replaced

*/ private static String replaceVars(String message, String portalName, int cost) { return Stargate.replaceVars(message, new String[]{"%cost%", "%portal%"}, new String[]{EconomyHandler.format(cost), portalName}); } }