Moves some methods from EconomyConfig to EconomyHelper and tries to simplify EconomyConfig
	
		
			
	
		
	
	
		
	
		
			Some checks failed
		
		
	
	
		
			
				
	
				EpicKnarvik97/Stargate/pipeline/head There was a failure building this commit
				
			
		
		
	
	
				
					
				
			
		
			Some checks failed
		
		
	
	EpicKnarvik97/Stargate/pipeline/head There was a failure building this commit
				
			This commit is contained in:
		@@ -144,6 +144,7 @@ public class Stargate extends JavaPlugin {
 | 
			
		||||
     *
 | 
			
		||||
     * @return <p>The max portal name/network length</p>
 | 
			
		||||
     */
 | 
			
		||||
    @SuppressWarnings("SameReturnValue")
 | 
			
		||||
    public static int getMaxNameNetworkLength() {
 | 
			
		||||
        return 13;
 | 
			
		||||
    }
 | 
			
		||||
 
 | 
			
		||||
@@ -68,11 +68,11 @@ public class CommandConfig implements CommandExecutor {
 | 
			
		||||
        switch (selectedOption.getDataType()) {
 | 
			
		||||
            case BOOLEAN -> configuration.set(selectedOption.getConfigNode(), Boolean.parseBoolean(value));
 | 
			
		||||
            case INTEGER -> {
 | 
			
		||||
                try {
 | 
			
		||||
                    configuration.set(selectedOption.getConfigNode(), Integer.parseInt(value));
 | 
			
		||||
                } catch (NumberFormatException exception) {
 | 
			
		||||
                    commandSender.sendMessage(ChatColor.RED + "Invalid number given");
 | 
			
		||||
                Integer intValue = getInteger(commandSender, selectedOption, value);
 | 
			
		||||
                if (intValue == null) {
 | 
			
		||||
                    return;
 | 
			
		||||
                } else {
 | 
			
		||||
                    configuration.set(selectedOption.getConfigNode(), intValue);
 | 
			
		||||
                }
 | 
			
		||||
            }
 | 
			
		||||
            case STRING -> {
 | 
			
		||||
@@ -93,6 +93,30 @@ public class CommandConfig implements CommandExecutor {
 | 
			
		||||
        reloadIfNecessary(commandSender);
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    /**
 | 
			
		||||
     * Gets an integer from a string
 | 
			
		||||
     *
 | 
			
		||||
     * @param commandSender  <p>The command sender that sent the config command</p>
 | 
			
		||||
     * @param selectedOption <p>The option the command sender is trying to change</p>
 | 
			
		||||
     * @param value          <p>The value given</p>
 | 
			
		||||
     * @return <p>An integer, or null if it was invalid</p>
 | 
			
		||||
     */
 | 
			
		||||
    private Integer getInteger(CommandSender commandSender, ConfigOption selectedOption, String value) {
 | 
			
		||||
        try {
 | 
			
		||||
            int intValue = Integer.parseInt(value);
 | 
			
		||||
 | 
			
		||||
            if ((selectedOption == ConfigOption.USE_COST || selectedOption == ConfigOption.CREATE_COST) && intValue < 0) {
 | 
			
		||||
                commandSender.sendMessage(ChatColor.RED + "This config option cannot be negative.");
 | 
			
		||||
                return null;
 | 
			
		||||
            }
 | 
			
		||||
 | 
			
		||||
            return intValue;
 | 
			
		||||
        } catch (NumberFormatException exception) {
 | 
			
		||||
            commandSender.sendMessage(ChatColor.RED + "Invalid number given");
 | 
			
		||||
            return null;
 | 
			
		||||
        }
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    /**
 | 
			
		||||
     * Reloads the config if necessary
 | 
			
		||||
     *
 | 
			
		||||
 
 | 
			
		||||
@@ -1,12 +1,10 @@
 | 
			
		||||
package net.knarcraft.stargate.config;
 | 
			
		||||
 | 
			
		||||
import net.knarcraft.stargate.Stargate;
 | 
			
		||||
import net.knarcraft.stargate.portal.Portal;
 | 
			
		||||
import net.knarcraft.stargate.portal.PortalSignDrawer;
 | 
			
		||||
import net.knarcraft.stargate.portal.property.gate.Gate;
 | 
			
		||||
import net.knarcraft.stargate.utility.PermissionHelper;
 | 
			
		||||
import net.milkbowl.vault.economy.Economy;
 | 
			
		||||
import org.bukkit.Bukkit;
 | 
			
		||||
import org.bukkit.ChatColor;
 | 
			
		||||
import org.bukkit.entity.Player;
 | 
			
		||||
import org.bukkit.plugin.Plugin;
 | 
			
		||||
@@ -15,22 +13,16 @@ import org.bukkit.plugin.RegisteredServiceProvider;
 | 
			
		||||
import org.bukkit.plugin.ServicesManager;
 | 
			
		||||
 | 
			
		||||
import java.util.Map;
 | 
			
		||||
import java.util.UUID;
 | 
			
		||||
 | 
			
		||||
/**
 | 
			
		||||
 * The economy config keeps track of economy config values and performs economy actions such as payment for using a gate
 | 
			
		||||
 */
 | 
			
		||||
public final class EconomyConfig {
 | 
			
		||||
 | 
			
		||||
    private boolean economyEnabled = false;
 | 
			
		||||
    private Economy economy = null;
 | 
			
		||||
    private Plugin vault = null;
 | 
			
		||||
    private int useCost = 0;
 | 
			
		||||
    private int createCost = 0;
 | 
			
		||||
    private int destroyCost = 0;
 | 
			
		||||
    private boolean toOwner = false;
 | 
			
		||||
    private boolean chargeFreeDestination = true;
 | 
			
		||||
    private boolean freeGatesColored = false;
 | 
			
		||||
 | 
			
		||||
    private final Map<ConfigOption, Object> configOptions;
 | 
			
		||||
 | 
			
		||||
    /**
 | 
			
		||||
     * Instantiates a new economy config
 | 
			
		||||
@@ -38,6 +30,7 @@ public final class EconomyConfig {
 | 
			
		||||
     * @param configOptions <p>The loaded config options to read</p>
 | 
			
		||||
     */
 | 
			
		||||
    public EconomyConfig(Map<ConfigOption, Object> configOptions) {
 | 
			
		||||
        this.configOptions = configOptions;
 | 
			
		||||
        loadEconomyConfig(configOptions);
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
@@ -47,7 +40,7 @@ public final class EconomyConfig {
 | 
			
		||||
     * @return <p>The gate use cost</p>
 | 
			
		||||
     */
 | 
			
		||||
    public int getDefaultUseCost() {
 | 
			
		||||
        return useCost;
 | 
			
		||||
        return (Integer) configOptions.get(ConfigOption.USE_COST);
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    /**
 | 
			
		||||
@@ -56,7 +49,7 @@ public final class EconomyConfig {
 | 
			
		||||
     * @return <p>Whether economy is enabled</p>
 | 
			
		||||
     */
 | 
			
		||||
    public boolean isEconomyEnabled() {
 | 
			
		||||
        return economyEnabled;
 | 
			
		||||
        return (boolean) configOptions.get(ConfigOption.USE_ECONOMY);
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    /**
 | 
			
		||||
@@ -91,7 +84,7 @@ public final class EconomyConfig {
 | 
			
		||||
     * @return <p>Whether free portals should be colored</p>
 | 
			
		||||
     */
 | 
			
		||||
    public boolean drawFreePortalsColored() {
 | 
			
		||||
        return freeGatesColored;
 | 
			
		||||
        return (boolean) configOptions.get(ConfigOption.FREE_GATES_COLORED);
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    /**
 | 
			
		||||
@@ -103,8 +96,8 @@ public final class EconomyConfig {
 | 
			
		||||
     *
 | 
			
		||||
     * @return <p>Whether to charge for free destinations</p>
 | 
			
		||||
     */
 | 
			
		||||
    public boolean chargeFreeDestination() {
 | 
			
		||||
        return chargeFreeDestination;
 | 
			
		||||
    public boolean freeIfFreeDestination() {
 | 
			
		||||
        return !((boolean) configOptions.get(ConfigOption.CHARGE_FREE_DESTINATION));
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    /**
 | 
			
		||||
@@ -113,21 +106,7 @@ public final class EconomyConfig {
 | 
			
		||||
     * @return <p>Whether to send payments to the portal owner</p>
 | 
			
		||||
     */
 | 
			
		||||
    public boolean sendPaymentToOwner() {
 | 
			
		||||
        return toOwner;
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    /**
 | 
			
		||||
     * Sets the cost of using a gate without a specified cost
 | 
			
		||||
     *
 | 
			
		||||
     * <p>The use cost cannot be negative.</p>
 | 
			
		||||
     *
 | 
			
		||||
     * @param useCost <p>The gate use cost</p>
 | 
			
		||||
     */
 | 
			
		||||
    public void setDefaultUseCost(int useCost) {
 | 
			
		||||
        if (useCost < 0) {
 | 
			
		||||
            throw new IllegalArgumentException("Using a gate cannot cost a negative amount");
 | 
			
		||||
        }
 | 
			
		||||
        this.useCost = useCost;
 | 
			
		||||
        return (boolean) configOptions.get(ConfigOption.TO_OWNER);
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    /**
 | 
			
		||||
@@ -136,18 +115,7 @@ public final class EconomyConfig {
 | 
			
		||||
     * @return <p>The gate creation cost</p>
 | 
			
		||||
     */
 | 
			
		||||
    public int getDefaultCreateCost() {
 | 
			
		||||
        return createCost;
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    /**
 | 
			
		||||
     * Sets the cost of creating a gate without a specified cost
 | 
			
		||||
     *
 | 
			
		||||
     * <p>The gate create cost cannot be negative</p>
 | 
			
		||||
     *
 | 
			
		||||
     * @param createCost <p>The gate creation cost</p>
 | 
			
		||||
     */
 | 
			
		||||
    public void setDefaultCreateCost(int createCost) {
 | 
			
		||||
        this.createCost = createCost;
 | 
			
		||||
        return (Integer) configOptions.get(ConfigOption.CREATE_COST);
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    /**
 | 
			
		||||
@@ -156,31 +124,7 @@ public final class EconomyConfig {
 | 
			
		||||
     * @return <p>The gate destruction cost</p>
 | 
			
		||||
     */
 | 
			
		||||
    public int getDefaultDestroyCost() {
 | 
			
		||||
        return destroyCost;
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    /**
 | 
			
		||||
     * Sets the cost of destroying a gate without a specified cost
 | 
			
		||||
     *
 | 
			
		||||
     * @param destroyCost <p>The gate destruction cost</p>
 | 
			
		||||
     */
 | 
			
		||||
    public void setDefaultDestroyCost(int destroyCost) {
 | 
			
		||||
        this.destroyCost = destroyCost;
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    /**
 | 
			
		||||
     * 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 boolean chargePlayerIfNecessary(Player player, int cost) {
 | 
			
		||||
        if (skipPayment(cost)) {
 | 
			
		||||
            return true;
 | 
			
		||||
        }
 | 
			
		||||
        //Charge player
 | 
			
		||||
        return chargePlayer(player, cost);
 | 
			
		||||
        return (Integer) configOptions.get(ConfigOption.DESTROY_COST);
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    /**
 | 
			
		||||
@@ -194,22 +138,6 @@ public final class EconomyConfig {
 | 
			
		||||
        return economy.getBalance(player) > cost;
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    /**
 | 
			
		||||
     * 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 boolean chargePlayerIfNecessary(Player player, UUID target, int cost) {
 | 
			
		||||
        if (skipPayment(cost)) {
 | 
			
		||||
            return true;
 | 
			
		||||
        }
 | 
			
		||||
        //Charge player
 | 
			
		||||
        return chargePlayer(player, target, cost);
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    /**
 | 
			
		||||
     * Gets a formatted string for an amount, adding the name of the currency
 | 
			
		||||
     *
 | 
			
		||||
@@ -217,7 +145,7 @@ public final class EconomyConfig {
 | 
			
		||||
     * @return <p>A formatted text string describing the amount</p>
 | 
			
		||||
     */
 | 
			
		||||
    public String format(int amount) {
 | 
			
		||||
        if (economyEnabled) {
 | 
			
		||||
        if (isEconomyEnabled()) {
 | 
			
		||||
            return economy.format(amount);
 | 
			
		||||
        } else {
 | 
			
		||||
            return "";
 | 
			
		||||
@@ -231,7 +159,7 @@ public final class EconomyConfig {
 | 
			
		||||
     * @return <p>True if economy was enabled</p>
 | 
			
		||||
     */
 | 
			
		||||
    public boolean setupEconomy(PluginManager pluginManager) {
 | 
			
		||||
        if (!economyEnabled) {
 | 
			
		||||
        if (!isEconomyEnabled()) {
 | 
			
		||||
            return false;
 | 
			
		||||
        }
 | 
			
		||||
        //Check if vault is loaded
 | 
			
		||||
@@ -249,7 +177,7 @@ public final class EconomyConfig {
 | 
			
		||||
        } else {
 | 
			
		||||
            Stargate.logInfo(Stargate.getString("vaultLoadError"));
 | 
			
		||||
        }
 | 
			
		||||
        economyEnabled = false;
 | 
			
		||||
        configOptions.put(ConfigOption.USE_ECONOMY, false);
 | 
			
		||||
        return false;
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
@@ -259,46 +187,7 @@ public final class EconomyConfig {
 | 
			
		||||
     * @return <p>True if the user has turned on economy and economy is available</p>
 | 
			
		||||
     */
 | 
			
		||||
    public boolean useEconomy() {
 | 
			
		||||
        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 boolean skipPayment(int cost) {
 | 
			
		||||
        return cost == 0 || !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 int getUseCost(Player player, Portal source, Portal destination) {
 | 
			
		||||
        //No payment required
 | 
			
		||||
        if (!useEconomy() || source.getOptions().isFree()) {
 | 
			
		||||
            return 0;
 | 
			
		||||
        }
 | 
			
		||||
        //Not charging for free destinations
 | 
			
		||||
        if (destination != null && !chargeFreeDestination && 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();
 | 
			
		||||
        return isEconomyEnabled() && economy != null;
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    /**
 | 
			
		||||
@@ -337,13 +226,6 @@ public final class EconomyConfig {
 | 
			
		||||
     * @param configOptions <p>The loaded config options to get values from</p>
 | 
			
		||||
     */
 | 
			
		||||
    private void loadEconomyConfig(Map<ConfigOption, Object> configOptions) {
 | 
			
		||||
        economyEnabled = (boolean) configOptions.get(ConfigOption.USE_ECONOMY);
 | 
			
		||||
        setDefaultCreateCost((Integer) configOptions.get(ConfigOption.CREATE_COST));
 | 
			
		||||
        setDefaultDestroyCost((Integer) configOptions.get(ConfigOption.DESTROY_COST));
 | 
			
		||||
        setDefaultUseCost((Integer) configOptions.get(ConfigOption.USE_COST));
 | 
			
		||||
        toOwner = (boolean) configOptions.get(ConfigOption.TO_OWNER);
 | 
			
		||||
        chargeFreeDestination = (boolean) configOptions.get(ConfigOption.CHARGE_FREE_DESTINATION);
 | 
			
		||||
        freeGatesColored = (boolean) configOptions.get(ConfigOption.FREE_GATES_COLORED);
 | 
			
		||||
 | 
			
		||||
        try {
 | 
			
		||||
            String freeColor = (String) configOptions.get(ConfigOption.FREE_GATES_COLOR);
 | 
			
		||||
@@ -364,41 +246,4 @@ public final class EconomyConfig {
 | 
			
		||||
        return !useEconomy() || PermissionHelper.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 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 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;
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
}
 | 
			
		||||
 
 | 
			
		||||
@@ -171,7 +171,7 @@ public class BlockEventListener implements Listener {
 | 
			
		||||
        if (cost != 0) {
 | 
			
		||||
            String portalName = portal.getName();
 | 
			
		||||
            //Cannot pay
 | 
			
		||||
            if (!Stargate.getEconomyConfig().chargePlayerIfNecessary(player, cost)) {
 | 
			
		||||
            if (!EconomyHelper.chargePlayerIfNecessary(player, cost)) {
 | 
			
		||||
                Stargate.debug("onBlockBreak", "Insufficient Funds");
 | 
			
		||||
                EconomyHelper.sendInsufficientFundsMessage(portalName, player, cost);
 | 
			
		||||
                event.setCancelled(true);
 | 
			
		||||
 
 | 
			
		||||
@@ -110,7 +110,7 @@ public class VehicleEventListener implements Listener {
 | 
			
		||||
 | 
			
		||||
        //To prevent the case where the first passenger pays and then the second passenger is denied, this has to be
 | 
			
		||||
        // run after it has been confirmed that all passengers are able to pay
 | 
			
		||||
        int cost = Stargate.getEconomyConfig().getUseCost(player, entrancePortal, destinationPortal);
 | 
			
		||||
        int cost = EconomyHelper.getUseCost(player, entrancePortal, destinationPortal);
 | 
			
		||||
        if (cost > 0) {
 | 
			
		||||
            if (!takePlayerPayment(passengers, entrancePortal, cost)) {
 | 
			
		||||
                return;
 | 
			
		||||
@@ -164,7 +164,7 @@ public class VehicleEventListener implements Listener {
 | 
			
		||||
        }
 | 
			
		||||
 | 
			
		||||
        //Check if the player is able to afford the teleport fee
 | 
			
		||||
        int cost = Stargate.getEconomyConfig().getUseCost(player, entrancePortal, destinationPortal);
 | 
			
		||||
        int cost = EconomyHelper.getUseCost(player, entrancePortal, destinationPortal);
 | 
			
		||||
        boolean canAffordFee = cost <= 0 || Stargate.getEconomyConfig().canAffordFee(player, cost);
 | 
			
		||||
        if (!canAffordFee && !entrancePortal.getOptions().isSilent()) {
 | 
			
		||||
            Stargate.getMessageSender().sendErrorMessage(player, Stargate.getString("ecoInFunds"));
 | 
			
		||||
 
 | 
			
		||||
@@ -266,7 +266,7 @@ public class PortalCreator {
 | 
			
		||||
 | 
			
		||||
        if (cost > 0) {
 | 
			
		||||
            //Deduct the required fee from the player
 | 
			
		||||
            if (!Stargate.getEconomyConfig().chargePlayerIfNecessary(player, cost)) {
 | 
			
		||||
            if (!EconomyHelper.chargePlayerIfNecessary(player, cost)) {
 | 
			
		||||
                EconomyHelper.sendInsufficientFundsMessage(portalName, player, cost);
 | 
			
		||||
                Stargate.debug("createPortal", "Insufficient Funds");
 | 
			
		||||
                return false;
 | 
			
		||||
 
 | 
			
		||||
@@ -1,8 +1,11 @@
 | 
			
		||||
package net.knarcraft.stargate.utility;
 | 
			
		||||
 | 
			
		||||
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;
 | 
			
		||||
@@ -34,9 +37,9 @@ public final class EconomyHelper {
 | 
			
		||||
                    "was therefore not possible. Make the owner re-create the portal to fix this.", entrancePortal));
 | 
			
		||||
        }
 | 
			
		||||
        if (entrancePortal.getGate().getToOwner() && ownerUUID != null) {
 | 
			
		||||
            success = Stargate.getEconomyConfig().chargePlayerIfNecessary(player, ownerUUID, cost);
 | 
			
		||||
            success = chargePlayerIfNecessary(player, ownerUUID, cost);
 | 
			
		||||
        } else {
 | 
			
		||||
            success = Stargate.getEconomyConfig().chargePlayerIfNecessary(player, cost);
 | 
			
		||||
            success = chargePlayerIfNecessary(player, cost);
 | 
			
		||||
        }
 | 
			
		||||
 | 
			
		||||
        //Send the insufficient funds message
 | 
			
		||||
@@ -118,6 +121,116 @@ public final class EconomyHelper {
 | 
			
		||||
        Stargate.getMessageSender().sendSuccessMessage(player, refundMsg);
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    /**
 | 
			
		||||
     * 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) {
 | 
			
		||||
        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 <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 chargePlayerIfNecessary(Player player, UUID target, int cost) {
 | 
			
		||||
        if (skipPayment(cost)) {
 | 
			
		||||
            return true;
 | 
			
		||||
        }
 | 
			
		||||
        //Charge player
 | 
			
		||||
        return chargePlayer(player, target, 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>
 | 
			
		||||
     */
 | 
			
		||||
    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 <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 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 <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 || !Stargate.getEconomyConfig().useEconomy();
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    /**
 | 
			
		||||
     * 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) {
 | 
			
		||||
        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
 | 
			
		||||
     *
 | 
			
		||||
 
 | 
			
		||||
@@ -243,7 +243,7 @@ public final class PermissionHelper {
 | 
			
		||||
            return true;
 | 
			
		||||
        }
 | 
			
		||||
        //Don't charge for free destinations unless specified in the config
 | 
			
		||||
        return dest != null && !Stargate.getEconomyConfig().chargeFreeDestination() && dest.getOptions().isFree();
 | 
			
		||||
        return dest != null && Stargate.getEconomyConfig().freeIfFreeDestination() && dest.getOptions().isFree();
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    /**
 | 
			
		||||
@@ -407,7 +407,7 @@ public final class PermissionHelper {
 | 
			
		||||
        }
 | 
			
		||||
 | 
			
		||||
        //Player cannot pay for teleportation
 | 
			
		||||
        int cost = Stargate.getEconomyConfig().getUseCost(player, entrancePortal, destination);
 | 
			
		||||
        int cost = EconomyHelper.getUseCost(player, entrancePortal, destination);
 | 
			
		||||
        if (cost > 0) {
 | 
			
		||||
            return EconomyHelper.cannotPayTeleportFee(entrancePortal, player, cost);
 | 
			
		||||
        }
 | 
			
		||||
 
 | 
			
		||||
		Reference in New Issue
	
	Block a user