package net.knarcraft.stargate.utility; import net.knarcraft.knarlib.formatting.StringFormatter; import net.knarcraft.stargate.Stargate; import net.knarcraft.stargate.config.EconomyConfig; import net.knarcraft.stargate.portal.Portal; import net.knarcraft.stargate.portal.property.PortalOwner; import net.milkbowl.vault.economy.Economy; import org.bukkit.Bukkit; 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 UUID ownerUUID = entrancePortal.getOwner().getUUID(); if (ownerUUID == null) { Stargate.logWarning(String.format("The owner of the portal %s does not have a UUID and payment to owner " + "was therefore not possible. Make the owner re-create the portal to fix this.", entrancePortal)); } if (entrancePortal.getGate().getToOwner() && ownerUUID != null) { success = chargePlayerIfNecessary(player, ownerUUID, cost); } else { success = 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.getInstance().getServer().getPlayer(owner.getUUID()); } else { portalOwner = Stargate.getInstance().getServer().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 = replacePlaceholders(obtainedMsg, portalName, earnings); Stargate.getMessageSender().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 = replacePlaceholders(deductMsg, portalName, cost); Stargate.getMessageSender().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 = replacePlaceholders(inFundMsg, portalName, cost); Stargate.getMessageSender().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 = replacePlaceholders(refundMsg, portalName, -cost); Stargate.getMessageSender().sendSuccessMessage(player, refundMsg); } /** * Determines the cost of using a gate * * @param player

The player trying to use the gate

* @param source

The source/entry portal

* @param destination

The destination portal

* @return

The cost of using the portal

*/ public static int getUseCost(Player player, Portal source, Portal destination) { EconomyConfig config = Stargate.getEconomyConfig(); //No payment required if (!config.useEconomy() || source.getOptions().isFree()) { return 0; } //Not charging for free destinations if (destination != null && config.freeIfFreeDestination() && destination.getOptions().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 (PermissionHelper.hasPermission(player, "stargate.free.use")) { return 0; } return source.getGate().getUseCost(); } /** * Charges the player for an action, if required * * @param player

The player to take money from

* @param target

The target to pay

* @param cost

The cost of the transaction

* @return

True if the player was charged successfully

*/ public static boolean chargePlayerIfNecessary(Player player, UUID target, int cost) { if (skipPayment(cost)) { return true; } //Charge player return chargePlayer(player, target, cost); } /** * Charges a player * * @param player

The player to charge

* @param amount

The amount to charge

* @return

True if the payment succeeded, or if no payment was necessary

*/ private static boolean chargePlayer(Player player, double amount) { Economy economy = Stargate.getEconomyConfig().getEconomy(); if (Stargate.getEconomyConfig().isEconomyEnabled() && economy != null) { if (!economy.has(player, amount)) { return false; } economy.withdrawPlayer(player, amount); } return true; } /** * Charges the player for an action, if required * * @param player

The player to take money from

* @param cost

The cost of the transaction

* @return

True if the player was charged successfully

*/ public static boolean chargePlayerIfNecessary(Player player, int cost) { if (skipPayment(cost)) { return true; } //Charge player return chargePlayer(player, cost); } /** * Checks whether a payment transaction should be skipped * * @param cost

The cost of the transaction

* @return

True if the transaction should be skipped

*/ private static boolean skipPayment(int cost) { return cost == 0 || !Stargate.getEconomyConfig().useEconomy(); } /** * Charges a player, giving the charge to a target * * @param player

The player to charge

* @param target

The UUID of the player to pay

* @param amount

The amount to charge

* @return

True if the payment succeeded, or if no payment was necessary

*/ private static boolean chargePlayer(Player player, UUID target, double amount) { Economy economy = Stargate.getEconomyConfig().getEconomy(); if (Stargate.getEconomyConfig().isEconomyEnabled() && 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; } /** * 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 replacePlaceholders(String message, String portalName, int cost) { return StringFormatter.replacePlaceholders(message, new String[]{"%cost%", "%portal%"}, new String[]{Stargate.getEconomyConfig().format(cost), portalName}); } }