Changes how config values are loaded by using the ConfigOption class

This commit is contained in:
Kristian Knarvik 2021-11-09 15:40:10 +01:00
parent 85edaa4657
commit d5f4b87e9b
3 changed files with 65 additions and 76 deletions

View File

@ -6,13 +6,13 @@ 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.Bukkit;
import org.bukkit.configuration.file.FileConfiguration;
import org.bukkit.entity.Player; import org.bukkit.entity.Player;
import org.bukkit.plugin.Plugin; import org.bukkit.plugin.Plugin;
import org.bukkit.plugin.PluginManager; import org.bukkit.plugin.PluginManager;
import org.bukkit.plugin.RegisteredServiceProvider; import org.bukkit.plugin.RegisteredServiceProvider;
import org.bukkit.plugin.ServicesManager; import org.bukkit.plugin.ServicesManager;
import java.util.Map;
import java.util.UUID; import java.util.UUID;
/** /**
@ -33,10 +33,10 @@ public final class EconomyConfig {
/** /**
* Instantiates a new economy config * Instantiates a new economy config
* *
* @param newConfig <p>The file configuration to read values from</p> * @param configOptions <p>The loaded config options to read</p>
*/ */
public EconomyConfig(FileConfiguration newConfig) { public EconomyConfig(Map<ConfigOption, Object> configOptions) {
loadEconomyConfig(newConfig); loadEconomyConfig(configOptions);
} }
/** /**
@ -332,16 +332,16 @@ public final class EconomyConfig {
/** /**
* Loads all config values related to economy * Loads all config values related to economy
* *
* @param newConfig <p>The configuration containing the values to read</p> * @param configOptions <p>The loaded config options to get values from</p>
*/ */
private void loadEconomyConfig(FileConfiguration newConfig) { private void loadEconomyConfig(Map<ConfigOption, Object> configOptions) {
economyEnabled = newConfig.getBoolean("economy.useEconomy"); economyEnabled = (boolean) configOptions.get(ConfigOption.USE_ECONOMY);
setDefaultCreateCost(newConfig.getInt("economy.createCost")); setDefaultCreateCost((Integer) configOptions.get(ConfigOption.CREATE_COST));
setDefaultDestroyCost(newConfig.getInt("economy.destroyCost")); setDefaultDestroyCost((Integer) configOptions.get(ConfigOption.DESTROY_COST));
setDefaultUseCost(newConfig.getInt("economy.useCost")); setDefaultUseCost((Integer) configOptions.get(ConfigOption.USE_COST));
toOwner = newConfig.getBoolean("economy.toOwner"); toOwner = (boolean) configOptions.get(ConfigOption.TO_OWNER);
chargeFreeDestination = newConfig.getBoolean("economy.chargeFreeDestination"); chargeFreeDestination = (boolean) configOptions.get(ConfigOption.CHARGE_FREE_DESTINATION);
freeGatesGreen = newConfig.getBoolean("economy.freeGatesGreen"); freeGatesGreen = (boolean) configOptions.get(ConfigOption.FREE_GATES_GREEN);
} }
/** /**

View File

@ -18,6 +18,7 @@ import org.bukkit.plugin.messaging.Messenger;
import java.io.File; import java.io.File;
import java.io.IOException; import java.io.IOException;
import java.util.HashMap;
import java.util.HashSet; import java.util.HashSet;
import java.util.Map; import java.util.Map;
import java.util.Queue; import java.util.Queue;
@ -47,6 +48,7 @@ public final class StargateConfig {
private boolean debuggingEnabled = false; private boolean debuggingEnabled = false;
private boolean permissionDebuggingEnabled = false; private boolean permissionDebuggingEnabled = false;
private final Map<ConfigOption, Object> configOptions;
/** /**
* Instantiates a new stargate config * Instantiates a new stargate config
@ -55,6 +57,7 @@ public final class StargateConfig {
*/ */
public StargateConfig(Logger logger) { public StargateConfig(Logger logger) {
this.logger = logger; this.logger = logger;
configOptions = new HashMap<>();
dataFolderPath = Stargate.getInstance().getDataFolder().getPath().replaceAll("\\\\", "/"); dataFolderPath = Stargate.getInstance().getDataFolder().getPath().replaceAll("\\\\", "/");
portalFolder = dataFolderPath + "/portals/"; portalFolder = dataFolderPath + "/portals/";
@ -307,16 +310,34 @@ public final class StargateConfig {
//Copy missing default values if any values are missing //Copy missing default values if any values are missing
newConfig.options().copyDefaults(true); 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 //Get the language name from the config
languageName = newConfig.getString("language"); languageName = (String) configOptions.get(ConfigOption.LANGUAGE);
//Get important folders from the config //Get important folders from the config
portalFolder = newConfig.getString("folders.portalFolder"); portalFolder = (String) configOptions.get(ConfigOption.PORTAL_FOLDER);
gateFolder = newConfig.getString("folders.gateFolder"); gateFolder = (String) configOptions.get(ConfigOption.GATE_FOLDER);
//Get enabled debug settings from the config //Get enabled debug settings from the config
debuggingEnabled = newConfig.getBoolean("debugging.debug"); debuggingEnabled = (boolean) configOptions.get(ConfigOption.DEBUG);
permissionDebuggingEnabled = newConfig.getBoolean("debugging.permissionDebug"); permissionDebuggingEnabled = (boolean) configOptions.get(ConfigOption.PERMISSION_DEBUG);
//If users have an outdated config, assume they also need to update their default gates //If users have an outdated config, assume they also need to update their default gates
if (isMigrating) { if (isMigrating) {
@ -324,10 +345,10 @@ public final class StargateConfig {
} }
//Load all gate config values //Load all gate config values
stargateGateConfig = new StargateGateConfig(newConfig); stargateGateConfig = new StargateGateConfig(configOptions);
//Load all economy config values //Load all economy config values
economyConfig = new EconomyConfig(newConfig); economyConfig = new EconomyConfig(configOptions);
Stargate.getInstance().saveConfig(); Stargate.getInstance().saveConfig();
} }

View File

@ -3,36 +3,25 @@ package net.knarcraft.stargate.config;
import net.knarcraft.stargate.Stargate; import net.knarcraft.stargate.Stargate;
import net.knarcraft.stargate.portal.PortalSignDrawer; import net.knarcraft.stargate.portal.PortalSignDrawer;
import org.bukkit.ChatColor; 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 * The Stargate gate config keeps track of all global config values related to gates
*/ */
public final class StargateGateConfig { 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 activeTime = 10;
private static final int openTime = 10; private static final int openTime = 10;
private final Map<ConfigOption, Object> configOptions;
/** /**
* Instantiates a new stargate config * Instantiates a new stargate config
* *
* @param newConfig <p>The file configuration to read values from</p> * @param configOptions <p>The loaded config options to use</p>
*/ */
public StargateGateConfig(FileConfiguration newConfig) { public StargateGateConfig(Map<ConfigOption, Object> configOptions) {
loadGateConfig(newConfig); this.configOptions = configOptions;
loadGateConfig();
} }
/** /**
@ -59,7 +48,7 @@ public final class StargateGateConfig {
* @return <p>Maximum number of gates for each network</p> * @return <p>Maximum number of gates for each network</p>
*/ */
public int maxGatesEachNetwork() { public int maxGatesEachNetwork() {
return maxGatesEachNetwork; return (int) configOptions.get(ConfigOption.MAX_GATES_EACH_NETWORK);
} }
/** /**
@ -68,7 +57,7 @@ public final class StargateGateConfig {
* @return <p>Whether a portal's lastly used destination should be remembered</p> * @return <p>Whether a portal's lastly used destination should be remembered</p>
*/ */
public boolean rememberDestination() { public boolean rememberDestination() {
return rememberDestination; return (boolean) configOptions.get(ConfigOption.REMEMBER_DESTINATION);
} }
/** /**
@ -77,7 +66,7 @@ public final class StargateGateConfig {
* @return <p>Whether vehicle teleportation should be handled</p> * @return <p>Whether vehicle teleportation should be handled</p>
*/ */
public boolean handleVehicles() { public boolean handleVehicles() {
return handleVehicles; return (boolean) configOptions.get(ConfigOption.HANDLE_VEHICLES);
} }
/** /**
@ -89,7 +78,7 @@ public final class StargateGateConfig {
* @return <p>Whether vehicles without passengers should be handled</p> * @return <p>Whether vehicles without passengers should be handled</p>
*/ */
public boolean handleEmptyVehicles() { public boolean handleEmptyVehicles() {
return handleEmptyVehicles; return (boolean) configOptions.get(ConfigOption.HANDLE_EMPTY_VEHICLES);
} }
/** /**
@ -101,7 +90,7 @@ public final class StargateGateConfig {
* @return <p>Whether vehicles with creatures should be handled</p> * @return <p>Whether vehicles with creatures should be handled</p>
*/ */
public boolean handleCreatureTransportation() { public boolean handleCreatureTransportation() {
return handleCreatureTransportation; return (boolean) configOptions.get(ConfigOption.HANDLE_CREATURE_TRANSPORTATION);
} }
/** /**
@ -118,7 +107,7 @@ public final class StargateGateConfig {
* @return <p>Whether non-empty vehicles without a player should be handled</p> * @return <p>Whether non-empty vehicles without a player should be handled</p>
*/ */
public boolean handleNonPlayerVehicles() { public boolean handleNonPlayerVehicles() {
return handleNonPlayerVehicles; return (boolean) configOptions.get(ConfigOption.HANDLE_NON_PLAYER_VEHICLES);
} }
/** /**
@ -127,7 +116,7 @@ public final class StargateGateConfig {
* @return <p>Whether leashed creatures should be handled</p> * @return <p>Whether leashed creatures should be handled</p>
*/ */
public boolean handleLeashedCreatures() { public boolean handleLeashedCreatures() {
return handleLeashedCreatures; return (boolean) configOptions.get(ConfigOption.HANDLE_LEASHED_CREATURES);
} }
/** /**
@ -136,7 +125,7 @@ public final class StargateGateConfig {
* @return <p>Whether network destinations should be sorted</p> * @return <p>Whether network destinations should be sorted</p>
*/ */
public boolean sortNetworkDestinations() { public boolean sortNetworkDestinations() {
return sortNetworkDestinations; return (boolean) configOptions.get(ConfigOption.SORT_NETWORK_DESTINATIONS);
} }
/** /**
@ -145,7 +134,7 @@ public final class StargateGateConfig {
* @return <p>Whether portal entrances should be protected</p> * @return <p>Whether portal entrances should be protected</p>
*/ */
public boolean protectEntrance() { public boolean protectEntrance() {
return protectEntrance; return (boolean) configOptions.get(ConfigOption.PROTECT_ENTRANCE);
} }
/** /**
@ -154,7 +143,7 @@ public final class StargateGateConfig {
* @return <p>Whether bungee support is enabled</p> * @return <p>Whether bungee support is enabled</p>
*/ */
public boolean enableBungee() { public boolean enableBungee() {
return enableBungee; return (boolean) configOptions.get(ConfigOption.ENABLE_BUNGEE);
} }
/** /**
@ -163,7 +152,7 @@ public final class StargateGateConfig {
* @return <p>Whether portals need to be verified</p> * @return <p>Whether portals need to be verified</p>
*/ */
public boolean verifyPortals() { public boolean verifyPortals() {
return verifyPortals; return (boolean) configOptions.get(ConfigOption.VERIFY_PORTALS);
} }
/** /**
@ -172,7 +161,7 @@ public final class StargateGateConfig {
* @return <p>Whether portals should be destroyed by explosions</p> * @return <p>Whether portals should be destroyed by explosions</p>
*/ */
public boolean destroyedByExplosion() { public boolean destroyedByExplosion() {
return destroyExplosion; return (boolean) configOptions.get(ConfigOption.DESTROYED_BY_EXPLOSION);
} }
/** /**
@ -181,37 +170,16 @@ public final class StargateGateConfig {
* @return <p>The default portal network</p> * @return <p>The default portal network</p>
*/ */
public String getDefaultPortalNetwork() { public String getDefaultPortalNetwork() {
return defaultGateNetwork; return (String) configOptions.get(ConfigOption.DEFAULT_GATE_NETWORK);
} }
/** /**
* Loads all config values related to gates * Loads all config values related to gates
*
* @param newConfig <p>The configuration containing the values to read</p>
*/ */
private void loadGateConfig(FileConfiguration newConfig) { private void loadGateConfig() {
String defaultNetwork = newConfig.getString("gates.defaultGateNetwork"); //Load the sign colors
defaultGateNetwork = defaultNetwork != null ? defaultNetwork.trim() : null; loadSignColor((String) configOptions.get(ConfigOption.MAIN_SIGN_COLOR),
maxGatesEachNetwork = newConfig.getInt("gates.maxGatesEachNetwork"); (String) configOptions.get(ConfigOption.HIGHLIGHT_SIGN_COLOR));
//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"));
} }
/** /**