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

This commit is contained in:
Kristian Knarvik 2021-11-23 07:20:48 +01:00
parent ab9a118d49
commit 22bb75d4df
8 changed files with 165 additions and 182 deletions

View File

@ -144,6 +144,7 @@ public class Stargate extends JavaPlugin {
* *
* @return <p>The max portal name/network length</p> * @return <p>The max portal name/network length</p>
*/ */
@SuppressWarnings("SameReturnValue")
public static int getMaxNameNetworkLength() { public static int getMaxNameNetworkLength() {
return 13; return 13;
} }

View File

@ -68,11 +68,11 @@ public class CommandConfig implements CommandExecutor {
switch (selectedOption.getDataType()) { switch (selectedOption.getDataType()) {
case BOOLEAN -> configuration.set(selectedOption.getConfigNode(), Boolean.parseBoolean(value)); case BOOLEAN -> configuration.set(selectedOption.getConfigNode(), Boolean.parseBoolean(value));
case INTEGER -> { case INTEGER -> {
try { Integer intValue = getInteger(commandSender, selectedOption, value);
configuration.set(selectedOption.getConfigNode(), Integer.parseInt(value)); if (intValue == null) {
} catch (NumberFormatException exception) {
commandSender.sendMessage(ChatColor.RED + "Invalid number given");
return; return;
} else {
configuration.set(selectedOption.getConfigNode(), intValue);
} }
} }
case STRING -> { case STRING -> {
@ -93,6 +93,30 @@ public class CommandConfig implements CommandExecutor {
reloadIfNecessary(commandSender); 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 * Reloads the config if necessary
* *

View File

@ -1,12 +1,10 @@
package net.knarcraft.stargate.config; package net.knarcraft.stargate.config;
import net.knarcraft.stargate.Stargate; import net.knarcraft.stargate.Stargate;
import net.knarcraft.stargate.portal.Portal;
import net.knarcraft.stargate.portal.PortalSignDrawer; import net.knarcraft.stargate.portal.PortalSignDrawer;
import net.knarcraft.stargate.portal.property.gate.Gate; import net.knarcraft.stargate.portal.property.gate.Gate;
import net.knarcraft.stargate.utility.PermissionHelper; import net.knarcraft.stargate.utility.PermissionHelper;
import net.milkbowl.vault.economy.Economy; import net.milkbowl.vault.economy.Economy;
import org.bukkit.Bukkit;
import org.bukkit.ChatColor; import org.bukkit.ChatColor;
import org.bukkit.entity.Player; import org.bukkit.entity.Player;
import org.bukkit.plugin.Plugin; import org.bukkit.plugin.Plugin;
@ -15,22 +13,16 @@ import org.bukkit.plugin.RegisteredServiceProvider;
import org.bukkit.plugin.ServicesManager; import org.bukkit.plugin.ServicesManager;
import java.util.Map; 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 * The economy config keeps track of economy config values and performs economy actions such as payment for using a gate
*/ */
public final class EconomyConfig { public final class EconomyConfig {
private boolean economyEnabled = false;
private Economy economy = null; private Economy economy = null;
private Plugin vault = null; private Plugin vault = null;
private int useCost = 0;
private int createCost = 0; private final Map<ConfigOption, Object> configOptions;
private int destroyCost = 0;
private boolean toOwner = false;
private boolean chargeFreeDestination = true;
private boolean freeGatesColored = false;
/** /**
* Instantiates a new economy config * Instantiates a new economy config
@ -38,6 +30,7 @@ public final class EconomyConfig {
* @param configOptions <p>The loaded config options to read</p> * @param configOptions <p>The loaded config options to read</p>
*/ */
public EconomyConfig(Map<ConfigOption, Object> configOptions) { public EconomyConfig(Map<ConfigOption, Object> configOptions) {
this.configOptions = configOptions;
loadEconomyConfig(configOptions); loadEconomyConfig(configOptions);
} }
@ -47,7 +40,7 @@ public final class EconomyConfig {
* @return <p>The gate use cost</p> * @return <p>The gate use cost</p>
*/ */
public int getDefaultUseCost() { 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> * @return <p>Whether economy is enabled</p>
*/ */
public boolean isEconomyEnabled() { 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> * @return <p>Whether free portals should be colored</p>
*/ */
public boolean drawFreePortalsColored() { 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> * @return <p>Whether to charge for free destinations</p>
*/ */
public boolean chargeFreeDestination() { public boolean freeIfFreeDestination() {
return chargeFreeDestination; 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> * @return <p>Whether to send payments to the portal owner</p>
*/ */
public boolean sendPaymentToOwner() { public boolean sendPaymentToOwner() {
return toOwner; return (boolean) configOptions.get(ConfigOption.TO_OWNER);
}
/**
* 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;
} }
/** /**
@ -136,18 +115,7 @@ public final class EconomyConfig {
* @return <p>The gate creation cost</p> * @return <p>The gate creation cost</p>
*/ */
public int getDefaultCreateCost() { public int getDefaultCreateCost() {
return createCost; return (Integer) configOptions.get(ConfigOption.CREATE_COST);
}
/**
* 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;
} }
/** /**
@ -156,31 +124,7 @@ public final class EconomyConfig {
* @return <p>The gate destruction cost</p> * @return <p>The gate destruction cost</p>
*/ */
public int getDefaultDestroyCost() { public int getDefaultDestroyCost() {
return destroyCost; return (Integer) configOptions.get(ConfigOption.DESTROY_COST);
}
/**
* 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);
} }
/** /**
@ -194,22 +138,6 @@ public final class EconomyConfig {
return economy.getBalance(player) > cost; 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 * 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> * @return <p>A formatted text string describing the amount</p>
*/ */
public String format(int amount) { public String format(int amount) {
if (economyEnabled) { if (isEconomyEnabled()) {
return economy.format(amount); return economy.format(amount);
} else { } else {
return ""; return "";
@ -231,7 +159,7 @@ public final class EconomyConfig {
* @return <p>True if economy was enabled</p> * @return <p>True if economy was enabled</p>
*/ */
public boolean setupEconomy(PluginManager pluginManager) { public boolean setupEconomy(PluginManager pluginManager) {
if (!economyEnabled) { if (!isEconomyEnabled()) {
return false; return false;
} }
//Check if vault is loaded //Check if vault is loaded
@ -249,7 +177,7 @@ public final class EconomyConfig {
} else { } else {
Stargate.logInfo(Stargate.getString("vaultLoadError")); Stargate.logInfo(Stargate.getString("vaultLoadError"));
} }
economyEnabled = false; configOptions.put(ConfigOption.USE_ECONOMY, false);
return 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> * @return <p>True if the user has turned on economy and economy is available</p>
*/ */
public boolean useEconomy() { public boolean useEconomy() {
return economyEnabled && economy != null; return isEconomyEnabled() && 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();
} }
/** /**
@ -337,13 +226,6 @@ public final class EconomyConfig {
* @param configOptions <p>The loaded config options to get values from</p> * @param configOptions <p>The loaded config options to get values from</p>
*/ */
private void loadEconomyConfig(Map<ConfigOption, Object> configOptions) { 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 { try {
String freeColor = (String) configOptions.get(ConfigOption.FREE_GATES_COLOR); 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); 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;
}
} }

View File

@ -171,7 +171,7 @@ public class BlockEventListener implements Listener {
if (cost != 0) { if (cost != 0) {
String portalName = portal.getName(); String portalName = portal.getName();
//Cannot pay //Cannot pay
if (!Stargate.getEconomyConfig().chargePlayerIfNecessary(player, cost)) { if (!EconomyHelper.chargePlayerIfNecessary(player, cost)) {
Stargate.debug("onBlockBreak", "Insufficient Funds"); Stargate.debug("onBlockBreak", "Insufficient Funds");
EconomyHelper.sendInsufficientFundsMessage(portalName, player, cost); EconomyHelper.sendInsufficientFundsMessage(portalName, player, cost);
event.setCancelled(true); event.setCancelled(true);

View File

@ -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 //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 // 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 (cost > 0) {
if (!takePlayerPayment(passengers, entrancePortal, cost)) { if (!takePlayerPayment(passengers, entrancePortal, cost)) {
return; return;
@ -164,7 +164,7 @@ public class VehicleEventListener implements Listener {
} }
//Check if the player is able to afford the teleport fee //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); boolean canAffordFee = cost <= 0 || Stargate.getEconomyConfig().canAffordFee(player, cost);
if (!canAffordFee && !entrancePortal.getOptions().isSilent()) { if (!canAffordFee && !entrancePortal.getOptions().isSilent()) {
Stargate.getMessageSender().sendErrorMessage(player, Stargate.getString("ecoInFunds")); Stargate.getMessageSender().sendErrorMessage(player, Stargate.getString("ecoInFunds"));

View File

@ -266,7 +266,7 @@ public class PortalCreator {
if (cost > 0) { if (cost > 0) {
//Deduct the required fee from the player //Deduct the required fee from the player
if (!Stargate.getEconomyConfig().chargePlayerIfNecessary(player, cost)) { if (!EconomyHelper.chargePlayerIfNecessary(player, cost)) {
EconomyHelper.sendInsufficientFundsMessage(portalName, player, cost); EconomyHelper.sendInsufficientFundsMessage(portalName, player, cost);
Stargate.debug("createPortal", "Insufficient Funds"); Stargate.debug("createPortal", "Insufficient Funds");
return false; return false;

View File

@ -1,8 +1,11 @@
package net.knarcraft.stargate.utility; package net.knarcraft.stargate.utility;
import net.knarcraft.stargate.Stargate; import net.knarcraft.stargate.Stargate;
import net.knarcraft.stargate.config.EconomyConfig;
import net.knarcraft.stargate.portal.Portal; import net.knarcraft.stargate.portal.Portal;
import net.knarcraft.stargate.portal.property.PortalOwner; import net.knarcraft.stargate.portal.property.PortalOwner;
import net.milkbowl.vault.economy.Economy;
import org.bukkit.Bukkit;
import org.bukkit.entity.Player; import org.bukkit.entity.Player;
import java.util.UUID; 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)); "was therefore not possible. Make the owner re-create the portal to fix this.", entrancePortal));
} }
if (entrancePortal.getGate().getToOwner() && ownerUUID != null) { if (entrancePortal.getGate().getToOwner() && ownerUUID != null) {
success = Stargate.getEconomyConfig().chargePlayerIfNecessary(player, ownerUUID, cost); success = chargePlayerIfNecessary(player, ownerUUID, cost);
} else { } else {
success = Stargate.getEconomyConfig().chargePlayerIfNecessary(player, cost); success = chargePlayerIfNecessary(player, cost);
} }
//Send the insufficient funds message //Send the insufficient funds message
@ -118,6 +121,116 @@ public final class EconomyHelper {
Stargate.getMessageSender().sendSuccessMessage(player, refundMsg); 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 * Replaces the cost and portal variables in a string
* *

View File

@ -243,7 +243,7 @@ public final class PermissionHelper {
return true; return true;
} }
//Don't charge for free destinations unless specified in the config //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 //Player cannot pay for teleportation
int cost = Stargate.getEconomyConfig().getUseCost(player, entrancePortal, destination); int cost = EconomyHelper.getUseCost(player, entrancePortal, destination);
if (cost > 0) { if (cost > 0) {
return EconomyHelper.cannotPayTeleportFee(entrancePortal, player, cost); return EconomyHelper.cannotPayTeleportFee(entrancePortal, player, cost);
} }