diff --git a/src/main/java/net/knarcraft/stargate/config/EconomyConfig.java b/src/main/java/net/knarcraft/stargate/config/EconomyConfig.java index 10cf01b..22ae241 100644 --- a/src/main/java/net/knarcraft/stargate/config/EconomyConfig.java +++ b/src/main/java/net/knarcraft/stargate/config/EconomyConfig.java @@ -6,13 +6,13 @@ 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.configuration.file.FileConfiguration; import org.bukkit.entity.Player; import org.bukkit.plugin.Plugin; import org.bukkit.plugin.PluginManager; import org.bukkit.plugin.RegisteredServiceProvider; import org.bukkit.plugin.ServicesManager; +import java.util.Map; import java.util.UUID; /** @@ -33,10 +33,10 @@ public final class EconomyConfig { /** * Instantiates a new economy config * - * @param newConfig

The file configuration to read values from

+ * @param configOptions

The loaded config options to read

*/ - public EconomyConfig(FileConfiguration newConfig) { - loadEconomyConfig(newConfig); + public EconomyConfig(Map configOptions) { + loadEconomyConfig(configOptions); } /** @@ -332,16 +332,16 @@ public final class EconomyConfig { /** * Loads all config values related to economy * - * @param newConfig

The configuration containing the values to read

+ * @param configOptions

The loaded config options to get values from

*/ - private void loadEconomyConfig(FileConfiguration newConfig) { - economyEnabled = newConfig.getBoolean("economy.useEconomy"); - setDefaultCreateCost(newConfig.getInt("economy.createCost")); - setDefaultDestroyCost(newConfig.getInt("economy.destroyCost")); - setDefaultUseCost(newConfig.getInt("economy.useCost")); - toOwner = newConfig.getBoolean("economy.toOwner"); - chargeFreeDestination = newConfig.getBoolean("economy.chargeFreeDestination"); - freeGatesGreen = newConfig.getBoolean("economy.freeGatesGreen"); + 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); + freeGatesGreen = (boolean) configOptions.get(ConfigOption.FREE_GATES_GREEN); } /** diff --git a/src/main/java/net/knarcraft/stargate/config/StargateConfig.java b/src/main/java/net/knarcraft/stargate/config/StargateConfig.java index 5b13fe3..656ca23 100644 --- a/src/main/java/net/knarcraft/stargate/config/StargateConfig.java +++ b/src/main/java/net/knarcraft/stargate/config/StargateConfig.java @@ -18,6 +18,7 @@ import org.bukkit.plugin.messaging.Messenger; import java.io.File; import java.io.IOException; +import java.util.HashMap; import java.util.HashSet; import java.util.Map; import java.util.Queue; @@ -47,6 +48,7 @@ public final class StargateConfig { private boolean debuggingEnabled = false; private boolean permissionDebuggingEnabled = false; + private final Map configOptions; /** * Instantiates a new stargate config @@ -55,6 +57,7 @@ public final class StargateConfig { */ public StargateConfig(Logger logger) { this.logger = logger; + configOptions = new HashMap<>(); dataFolderPath = Stargate.getInstance().getDataFolder().getPath().replaceAll("\\\\", "/"); portalFolder = dataFolderPath + "/portals/"; @@ -307,16 +310,34 @@ public final class StargateConfig { //Copy missing default values if any values are missing newConfig.options().copyDefaults(true); + //Load all options + for (ConfigOption option : ConfigOption.values()) { + Object optionValue; + String configNode = option.getConfigNode(); + + //Load the option using its correct data type + switch (option.getDataType()) { + case STRING -> { + String value = newConfig.getString(configNode); + optionValue = value != null ? value.trim() : ""; + } + case BOOLEAN -> optionValue = newConfig.getBoolean(configNode); + case INTEGER -> optionValue = newConfig.getInt(configNode); + default -> throw new IllegalArgumentException("Invalid config data type encountered"); + } + configOptions.put(option, optionValue); + } + //Get the language name from the config - languageName = newConfig.getString("language"); + languageName = (String) configOptions.get(ConfigOption.LANGUAGE); //Get important folders from the config - portalFolder = newConfig.getString("folders.portalFolder"); - gateFolder = newConfig.getString("folders.gateFolder"); + portalFolder = (String) configOptions.get(ConfigOption.PORTAL_FOLDER); + gateFolder = (String) configOptions.get(ConfigOption.GATE_FOLDER); //Get enabled debug settings from the config - debuggingEnabled = newConfig.getBoolean("debugging.debug"); - permissionDebuggingEnabled = newConfig.getBoolean("debugging.permissionDebug"); + debuggingEnabled = (boolean) configOptions.get(ConfigOption.DEBUG); + permissionDebuggingEnabled = (boolean) configOptions.get(ConfigOption.PERMISSION_DEBUG); //If users have an outdated config, assume they also need to update their default gates if (isMigrating) { @@ -324,10 +345,10 @@ public final class StargateConfig { } //Load all gate config values - stargateGateConfig = new StargateGateConfig(newConfig); + stargateGateConfig = new StargateGateConfig(configOptions); //Load all economy config values - economyConfig = new EconomyConfig(newConfig); + economyConfig = new EconomyConfig(configOptions); Stargate.getInstance().saveConfig(); } diff --git a/src/main/java/net/knarcraft/stargate/config/StargateGateConfig.java b/src/main/java/net/knarcraft/stargate/config/StargateGateConfig.java index be13cdc..f14dd6b 100644 --- a/src/main/java/net/knarcraft/stargate/config/StargateGateConfig.java +++ b/src/main/java/net/knarcraft/stargate/config/StargateGateConfig.java @@ -3,36 +3,25 @@ package net.knarcraft.stargate.config; import net.knarcraft.stargate.Stargate; import net.knarcraft.stargate.portal.PortalSignDrawer; import org.bukkit.ChatColor; -import org.bukkit.configuration.file.FileConfiguration; + +import java.util.Map; /** * The Stargate gate config keeps track of all global config values related to gates */ public final class StargateGateConfig { - - private int maxGatesEachNetwork = 0; - private boolean rememberDestination = false; - private boolean handleVehicles = true; - private boolean handleEmptyVehicles = true; - private boolean handleCreatureTransportation = true; - private boolean handleNonPlayerVehicles = true; - private boolean handleLeashedCreatures = true; - private boolean sortNetworkDestinations = false; - private boolean protectEntrance = false; - private boolean enableBungee = true; - private boolean verifyPortals = true; - private boolean destroyExplosion = false; - private String defaultGateNetwork = "central"; private static final int activeTime = 10; private static final int openTime = 10; + private final Map configOptions; /** * Instantiates a new stargate config * - * @param newConfig

The file configuration to read values from

+ * @param configOptions

The loaded config options to use

*/ - public StargateGateConfig(FileConfiguration newConfig) { - loadGateConfig(newConfig); + public StargateGateConfig(Map configOptions) { + this.configOptions = configOptions; + loadGateConfig(); } /** @@ -59,7 +48,7 @@ public final class StargateGateConfig { * @return

Maximum number of gates for each network

*/ public int maxGatesEachNetwork() { - return maxGatesEachNetwork; + return (int) configOptions.get(ConfigOption.MAX_GATES_EACH_NETWORK); } /** @@ -68,7 +57,7 @@ public final class StargateGateConfig { * @return

Whether a portal's lastly used destination should be remembered

*/ public boolean rememberDestination() { - return rememberDestination; + return (boolean) configOptions.get(ConfigOption.REMEMBER_DESTINATION); } /** @@ -77,7 +66,7 @@ public final class StargateGateConfig { * @return

Whether vehicle teleportation should be handled

*/ public boolean handleVehicles() { - return handleVehicles; + return (boolean) configOptions.get(ConfigOption.HANDLE_VEHICLES); } /** @@ -89,7 +78,7 @@ public final class StargateGateConfig { * @return

Whether vehicles without passengers should be handled

*/ public boolean handleEmptyVehicles() { - return handleEmptyVehicles; + return (boolean) configOptions.get(ConfigOption.HANDLE_EMPTY_VEHICLES); } /** @@ -101,7 +90,7 @@ public final class StargateGateConfig { * @return

Whether vehicles with creatures should be handled

*/ public boolean handleCreatureTransportation() { - return handleCreatureTransportation; + return (boolean) configOptions.get(ConfigOption.HANDLE_CREATURE_TRANSPORTATION); } /** @@ -118,7 +107,7 @@ public final class StargateGateConfig { * @return

Whether non-empty vehicles without a player should be handled

*/ public boolean handleNonPlayerVehicles() { - return handleNonPlayerVehicles; + return (boolean) configOptions.get(ConfigOption.HANDLE_NON_PLAYER_VEHICLES); } /** @@ -127,7 +116,7 @@ public final class StargateGateConfig { * @return

Whether leashed creatures should be handled

*/ public boolean handleLeashedCreatures() { - return handleLeashedCreatures; + return (boolean) configOptions.get(ConfigOption.HANDLE_LEASHED_CREATURES); } /** @@ -136,7 +125,7 @@ public final class StargateGateConfig { * @return

Whether network destinations should be sorted

*/ public boolean sortNetworkDestinations() { - return sortNetworkDestinations; + return (boolean) configOptions.get(ConfigOption.SORT_NETWORK_DESTINATIONS); } /** @@ -145,7 +134,7 @@ public final class StargateGateConfig { * @return

Whether portal entrances should be protected

*/ public boolean protectEntrance() { - return protectEntrance; + return (boolean) configOptions.get(ConfigOption.PROTECT_ENTRANCE); } /** @@ -154,7 +143,7 @@ public final class StargateGateConfig { * @return

Whether bungee support is enabled

*/ public boolean enableBungee() { - return enableBungee; + return (boolean) configOptions.get(ConfigOption.ENABLE_BUNGEE); } /** @@ -163,7 +152,7 @@ public final class StargateGateConfig { * @return

Whether portals need to be verified

*/ public boolean verifyPortals() { - return verifyPortals; + return (boolean) configOptions.get(ConfigOption.VERIFY_PORTALS); } /** @@ -172,7 +161,7 @@ public final class StargateGateConfig { * @return

Whether portals should be destroyed by explosions

*/ public boolean destroyedByExplosion() { - return destroyExplosion; + return (boolean) configOptions.get(ConfigOption.DESTROYED_BY_EXPLOSION); } /** @@ -181,37 +170,16 @@ public final class StargateGateConfig { * @return

The default portal network

*/ public String getDefaultPortalNetwork() { - return defaultGateNetwork; + return (String) configOptions.get(ConfigOption.DEFAULT_GATE_NETWORK); } /** * Loads all config values related to gates - * - * @param newConfig

The configuration containing the values to read

*/ - private void loadGateConfig(FileConfiguration newConfig) { - String defaultNetwork = newConfig.getString("gates.defaultGateNetwork"); - defaultGateNetwork = defaultNetwork != null ? defaultNetwork.trim() : null; - maxGatesEachNetwork = newConfig.getInt("gates.maxGatesEachNetwork"); - - //Functionality - handleVehicles = newConfig.getBoolean("gates.functionality.handleVehicles"); - handleEmptyVehicles = newConfig.getBoolean("gates.functionality.handleEmptyVehicles"); - handleCreatureTransportation = newConfig.getBoolean("gates.functionality.handleCreatureTransportation"); - handleNonPlayerVehicles = newConfig.getBoolean("gates.functionality.handleNonPlayerVehicles"); - handleLeashedCreatures = newConfig.getBoolean("gates.functionality.handleLeashedCreatures"); - enableBungee = newConfig.getBoolean("gates.functionality.enableBungee"); - - //Integrity - protectEntrance = newConfig.getBoolean("gates.integrity.protectEntrance"); - verifyPortals = newConfig.getBoolean("gates.integrity.verifyPortals"); - destroyExplosion = newConfig.getBoolean("gates.integrity.destroyedByExplosion"); - - //Cosmetic - sortNetworkDestinations = newConfig.getBoolean("gates.cosmetic.sortNetworkDestinations"); - rememberDestination = newConfig.getBoolean("gates.cosmetic.rememberDestination"); - loadSignColor(newConfig.getString("gates.cosmetic.mainSignColor"), - newConfig.getString("gates.cosmetic.highlightSignColor")); + private void loadGateConfig() { + //Load the sign colors + loadSignColor((String) configOptions.get(ConfigOption.MAIN_SIGN_COLOR), + (String) configOptions.get(ConfigOption.HIGHLIGHT_SIGN_COLOR)); } /**