diff --git a/src/main/java/net/knarcraft/stargate/Stargate.java b/src/main/java/net/knarcraft/stargate/Stargate.java index 1e0f3da..e3055d6 100644 --- a/src/main/java/net/knarcraft/stargate/Stargate.java +++ b/src/main/java/net/knarcraft/stargate/Stargate.java @@ -144,6 +144,7 @@ public class Stargate extends JavaPlugin { * * @return

The max portal name/network length

*/ + @SuppressWarnings("SameReturnValue") public static int getMaxNameNetworkLength() { return 13; } diff --git a/src/main/java/net/knarcraft/stargate/command/CommandConfig.java b/src/main/java/net/knarcraft/stargate/command/CommandConfig.java index e494e20..54aa1a5 100644 --- a/src/main/java/net/knarcraft/stargate/command/CommandConfig.java +++ b/src/main/java/net/knarcraft/stargate/command/CommandConfig.java @@ -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

The command sender that sent the config command

+ * @param selectedOption

The option the command sender is trying to change

+ * @param value

The value given

+ * @return

An integer, or null if it was invalid

+ */ + 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 * diff --git a/src/main/java/net/knarcraft/stargate/config/EconomyConfig.java b/src/main/java/net/knarcraft/stargate/config/EconomyConfig.java index 6c645d8..ba842d3 100644 --- a/src/main/java/net/knarcraft/stargate/config/EconomyConfig.java +++ b/src/main/java/net/knarcraft/stargate/config/EconomyConfig.java @@ -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 configOptions; /** * Instantiates a new economy config @@ -38,6 +30,7 @@ public final class EconomyConfig { * @param configOptions

The loaded config options to read

*/ public EconomyConfig(Map configOptions) { + this.configOptions = configOptions; loadEconomyConfig(configOptions); } @@ -47,7 +40,7 @@ public final class EconomyConfig { * @return

The gate use cost

*/ public int getDefaultUseCost() { - return useCost; + return (Integer) configOptions.get(ConfigOption.USE_COST); } /** @@ -56,7 +49,7 @@ public final class EconomyConfig { * @return

Whether economy is enabled

*/ public boolean isEconomyEnabled() { - return economyEnabled; + return (boolean) configOptions.get(ConfigOption.USE_ECONOMY); } /** @@ -91,7 +84,7 @@ public final class EconomyConfig { * @return

Whether free portals should be colored

*/ public boolean drawFreePortalsColored() { - return freeGatesColored; + return (boolean) configOptions.get(ConfigOption.FREE_GATES_COLORED); } /** @@ -103,8 +96,8 @@ public final class EconomyConfig { * * @return

Whether to charge for free destinations

*/ - 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

Whether to send payments to the portal owner

*/ public boolean sendPaymentToOwner() { - return toOwner; - } - - /** - * Sets the cost of using a gate without a specified cost - * - *

The use cost cannot be negative.

- * - * @param useCost

The gate use cost

- */ - 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

The gate creation cost

*/ public int getDefaultCreateCost() { - return createCost; - } - - /** - * Sets the cost of creating a gate without a specified cost - * - *

The gate create cost cannot be negative

- * - * @param createCost

The gate creation cost

- */ - public void setDefaultCreateCost(int createCost) { - this.createCost = createCost; + return (Integer) configOptions.get(ConfigOption.CREATE_COST); } /** @@ -156,31 +124,7 @@ public final class EconomyConfig { * @return

The gate destruction cost

*/ public int getDefaultDestroyCost() { - return destroyCost; - } - - /** - * Sets the cost of destroying a gate without a specified cost - * - * @param destroyCost

The gate destruction cost

- */ - public void setDefaultDestroyCost(int destroyCost) { - this.destroyCost = destroyCost; - } - - /** - * 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 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

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 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

A formatted text string describing the amount

*/ public String format(int amount) { - if (economyEnabled) { + if (isEconomyEnabled()) { return economy.format(amount); } else { return ""; @@ -231,7 +159,7 @@ public final class EconomyConfig { * @return

True if economy was enabled

*/ 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

True if the user has turned on economy and economy is available

*/ public boolean useEconomy() { - return economyEnabled && economy != null; - } - - /** - * Checks whether a payment transaction should be skipped - * - * @param cost

The cost of the transaction

- * @return

True if the transaction should be skipped

- */ - private boolean skipPayment(int cost) { - return cost == 0 || !useEconomy(); - } - - /** - * 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 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

The loaded config options to get values from

*/ private void loadEconomyConfig(Map 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

The player to charge

- * @param amount

The amount to charge

- * @return

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

- */ - 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

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 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; - } - } diff --git a/src/main/java/net/knarcraft/stargate/listener/BlockEventListener.java b/src/main/java/net/knarcraft/stargate/listener/BlockEventListener.java index b0c0576..b15d70d 100644 --- a/src/main/java/net/knarcraft/stargate/listener/BlockEventListener.java +++ b/src/main/java/net/knarcraft/stargate/listener/BlockEventListener.java @@ -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); diff --git a/src/main/java/net/knarcraft/stargate/listener/VehicleEventListener.java b/src/main/java/net/knarcraft/stargate/listener/VehicleEventListener.java index 79947b0..1cbd0b3 100644 --- a/src/main/java/net/knarcraft/stargate/listener/VehicleEventListener.java +++ b/src/main/java/net/knarcraft/stargate/listener/VehicleEventListener.java @@ -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")); diff --git a/src/main/java/net/knarcraft/stargate/portal/PortalCreator.java b/src/main/java/net/knarcraft/stargate/portal/PortalCreator.java index 306503f..4ec865c 100644 --- a/src/main/java/net/knarcraft/stargate/portal/PortalCreator.java +++ b/src/main/java/net/knarcraft/stargate/portal/PortalCreator.java @@ -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; diff --git a/src/main/java/net/knarcraft/stargate/utility/EconomyHelper.java b/src/main/java/net/knarcraft/stargate/utility/EconomyHelper.java index d1dc0e4..e3ab32c 100644 --- a/src/main/java/net/knarcraft/stargate/utility/EconomyHelper.java +++ b/src/main/java/net/knarcraft/stargate/utility/EconomyHelper.java @@ -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

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 * diff --git a/src/main/java/net/knarcraft/stargate/utility/PermissionHelper.java b/src/main/java/net/knarcraft/stargate/utility/PermissionHelper.java index 9454c43..2a8c1ce 100644 --- a/src/main/java/net/knarcraft/stargate/utility/PermissionHelper.java +++ b/src/main/java/net/knarcraft/stargate/utility/PermissionHelper.java @@ -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); }