Rewrites a lot of the config command to only do the minimum necessary steps to load the changes
Adds a ConfigTag class for helping to decide the action necessary for updating a given config option Splits the color setting in PortalSignDrawer as only one color is set at a time when the /config command is used Updates the configOptions map when a config option is changed
This commit is contained in:
parent
6e7ac5dbb9
commit
32975ca35d
@ -2,6 +2,10 @@ package net.knarcraft.stargate.command;
|
||||
|
||||
import net.knarcraft.stargate.Stargate;
|
||||
import net.knarcraft.stargate.config.ConfigOption;
|
||||
import net.knarcraft.stargate.config.ConfigTag;
|
||||
import net.knarcraft.stargate.portal.Portal;
|
||||
import net.knarcraft.stargate.portal.PortalRegistry;
|
||||
import net.knarcraft.stargate.portal.PortalSignDrawer;
|
||||
import org.bukkit.ChatColor;
|
||||
import org.bukkit.command.Command;
|
||||
import org.bukkit.command.CommandExecutor;
|
||||
@ -55,7 +59,7 @@ public class CommandConfig implements CommandExecutor {
|
||||
FileConfiguration configuration = Stargate.getInstance().getConfig();
|
||||
|
||||
//Validate any sign colors
|
||||
if (selectedOption == ConfigOption.MAIN_SIGN_COLOR || selectedOption == ConfigOption.HIGHLIGHT_SIGN_COLOR) {
|
||||
if (ConfigTag.COLOR.isTagged(selectedOption)) {
|
||||
try {
|
||||
ChatColor.valueOf(value.toUpperCase());
|
||||
} catch (IllegalArgumentException | NullPointerException ignored) {
|
||||
@ -66,12 +70,20 @@ public class CommandConfig implements CommandExecutor {
|
||||
|
||||
//Store the config values, accounting for the data type
|
||||
switch (selectedOption.getDataType()) {
|
||||
case BOOLEAN -> configuration.set(selectedOption.getConfigNode(), Boolean.parseBoolean(value));
|
||||
case BOOLEAN -> {
|
||||
boolean newValue = Boolean.parseBoolean(value);
|
||||
if (selectedOption == ConfigOption.ENABLE_BUNGEE && newValue != Stargate.getGateConfig().enableBungee()) {
|
||||
Stargate.getStargateConfig().startStopBungeeListener(newValue);
|
||||
}
|
||||
Stargate.getStargateConfig().getConfigOptionsReference().put(selectedOption, newValue);
|
||||
configuration.set(selectedOption.getConfigNode(), newValue);
|
||||
}
|
||||
case INTEGER -> {
|
||||
Integer intValue = getInteger(commandSender, selectedOption, value);
|
||||
if (intValue == null) {
|
||||
return;
|
||||
} else {
|
||||
Stargate.getStargateConfig().getConfigOptionsReference().put(selectedOption, intValue);
|
||||
configuration.set(selectedOption.getConfigNode(), intValue);
|
||||
}
|
||||
}
|
||||
@ -83,14 +95,68 @@ public class CommandConfig implements CommandExecutor {
|
||||
return;
|
||||
}
|
||||
}
|
||||
if (ConfigTag.COLOR.isTagged(selectedOption)) {
|
||||
if (!registerColor(selectedOption, value, commandSender)) {
|
||||
return;
|
||||
}
|
||||
}
|
||||
if (selectedOption == ConfigOption.LANGUAGE) {
|
||||
Stargate.getStargateConfig().getLanguageLoader().setChosenLanguage(value);
|
||||
}
|
||||
Stargate.getStargateConfig().getConfigOptionsReference().put(selectedOption, value);
|
||||
configuration.set(selectedOption.getConfigNode(), value);
|
||||
}
|
||||
default -> {
|
||||
Stargate.getStargateConfig().getConfigOptionsReference().put(selectedOption, value);
|
||||
configuration.set(selectedOption.getConfigNode(), value);
|
||||
}
|
||||
default -> configuration.set(selectedOption.getConfigNode(), value);
|
||||
}
|
||||
|
||||
//Save the config file and reload if necessary
|
||||
Stargate.getInstance().saveConfig();
|
||||
reloadIfNecessary(commandSender);
|
||||
|
||||
Stargate.getMessageSender().sendSuccessMessage(commandSender, "Config updated");
|
||||
|
||||
//Reload whatever is necessary
|
||||
reloadIfNecessary(commandSender, selectedOption);
|
||||
}
|
||||
|
||||
/**
|
||||
* Registers the chat color if
|
||||
*
|
||||
* @param selectedOption <p>The option to change</p>
|
||||
* @param commandSender <p>The command sender to alert if the color is invalid</p>
|
||||
* @param value <p>The new option value</p>
|
||||
*/
|
||||
private boolean registerColor(ConfigOption selectedOption, String value, CommandSender commandSender) {
|
||||
ChatColor parsedColor = parseColor(value);
|
||||
if (parsedColor == null) {
|
||||
commandSender.sendMessage(ChatColor.RED + "Invalid color given");
|
||||
return false;
|
||||
}
|
||||
|
||||
if (selectedOption == ConfigOption.FREE_GATES_COLOR) {
|
||||
PortalSignDrawer.setFreeColor(parsedColor);
|
||||
} else if (selectedOption == ConfigOption.MAIN_SIGN_COLOR) {
|
||||
PortalSignDrawer.setMainColor(parsedColor);
|
||||
} else if (selectedOption == ConfigOption.HIGHLIGHT_SIGN_COLOR) {
|
||||
PortalSignDrawer.setHighlightColor(parsedColor);
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
/**
|
||||
* Parses a chat color
|
||||
*
|
||||
* @param value <p>The value to parse</p>
|
||||
* @return <p>The parsed color or null</p>
|
||||
*/
|
||||
private ChatColor parseColor(String value) {
|
||||
try {
|
||||
return ChatColor.valueOf(value.toUpperCase());
|
||||
} catch (IllegalArgumentException | NullPointerException ignored) {
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
@ -121,12 +187,27 @@ public class CommandConfig implements CommandExecutor {
|
||||
* Reloads the config if necessary
|
||||
*
|
||||
* @param commandSender <p>The command sender initiating the reload</p>
|
||||
* @param configOption <p>The changed config option</p>
|
||||
*/
|
||||
private void reloadIfNecessary(CommandSender commandSender) {
|
||||
//TODO: Only update the config values which have changed and do the least amount of work necessary to load the
|
||||
// changes. Only do a full reload if absolutely necessary, or when the partial reloading would be as
|
||||
// inefficient as a full reload.
|
||||
private void reloadIfNecessary(CommandSender commandSender, ConfigOption configOption) {
|
||||
if (ConfigTag.requiresFullReload(configOption)) {
|
||||
//Reload everything
|
||||
Stargate.getStargateConfig().reload(commandSender);
|
||||
} else if (ConfigTag.requiresPortalReload(configOption)) {
|
||||
//Just unload and reload the portals
|
||||
Stargate.getStargateConfig().unloadAllPortals();
|
||||
Stargate.getStargateConfig().loadAllPortals();
|
||||
} else if (ConfigTag.requiresLanguageReload(configOption)) {
|
||||
//Reload the language loader
|
||||
Stargate.getStargateConfig().getLanguageLoader().reload();
|
||||
//Re-draw all portal signs
|
||||
for (Portal portal : PortalRegistry.getAllPortals()) {
|
||||
portal.drawSign();
|
||||
}
|
||||
} else if (ConfigTag.requiresEconomyReload(configOption)) {
|
||||
//Load or unload Vault and Economy as necessary
|
||||
Stargate.getStargateConfig().reloadEconomy();
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
|
74
src/main/java/net/knarcraft/stargate/config/ConfigTag.java
Normal file
74
src/main/java/net/knarcraft/stargate/config/ConfigTag.java
Normal file
@ -0,0 +1,74 @@
|
||||
package net.knarcraft.stargate.config;
|
||||
|
||||
import java.util.Arrays;
|
||||
|
||||
/**
|
||||
* A config tag groups config values by a property
|
||||
*/
|
||||
public enum ConfigTag {
|
||||
|
||||
COLOR(new ConfigOption[]{ConfigOption.FREE_GATES_COLOR, ConfigOption.MAIN_SIGN_COLOR, ConfigOption.HIGHLIGHT_SIGN_COLOR}),
|
||||
FOLDER(new ConfigOption[]{ConfigOption.GATE_FOLDER, ConfigOption.PORTAL_FOLDER});
|
||||
|
||||
private final ConfigOption[] taggedOptions;
|
||||
|
||||
/**
|
||||
* Instantiates a new config tag
|
||||
*
|
||||
* @param taggedOptions <p>The config options included in this tag</p>
|
||||
*/
|
||||
ConfigTag(ConfigOption[] taggedOptions) {
|
||||
this.taggedOptions = taggedOptions;
|
||||
}
|
||||
|
||||
/**
|
||||
* Checks whether a config tag includes the given config option
|
||||
*
|
||||
* @param option <p>The config option to check</p>
|
||||
* @return <p>True of the config option is tagged</p>
|
||||
*/
|
||||
public boolean isTagged(ConfigOption option) {
|
||||
return Arrays.stream(taggedOptions).anyMatch((item) -> item == option);
|
||||
}
|
||||
|
||||
/**
|
||||
* Checks whether a given config option requires a full reload to take effect
|
||||
*
|
||||
* @param option <p>The config option to check</p>
|
||||
* @return <p>True if changing the config option requires a full reload to take effect</p>
|
||||
*/
|
||||
public static boolean requiresFullReload(ConfigOption option) {
|
||||
return FOLDER.isTagged(option);
|
||||
}
|
||||
|
||||
/**
|
||||
* Checks whether a given config option requires a portal reload to take effect
|
||||
*
|
||||
* @param option <p>The config option to check</p>
|
||||
* @return <p>True if changing the config option requires a portal reload to take effect</p>
|
||||
*/
|
||||
public static boolean requiresPortalReload(ConfigOption option) {
|
||||
return COLOR.isTagged(option) || FOLDER.isTagged(option) || option == ConfigOption.VERIFY_PORTALS;
|
||||
}
|
||||
|
||||
/**
|
||||
* Checks whether a given config option requires the language loader to be reloaded
|
||||
*
|
||||
* @param option <p>The config option to check</p>
|
||||
* @return <p>True if the language loader requires a reload</p>
|
||||
*/
|
||||
public static boolean requiresLanguageReload(ConfigOption option) {
|
||||
return option == ConfigOption.LANGUAGE;
|
||||
}
|
||||
|
||||
/**
|
||||
* Checks whether a given config option requires economy to be reloaded
|
||||
*
|
||||
* @param option <p>The config option to check</p>
|
||||
* @return <p>True if economy requires a reload</p>
|
||||
*/
|
||||
public static boolean requiresEconomyReload(ConfigOption option) {
|
||||
return option == ConfigOption.USE_ECONOMY;
|
||||
}
|
||||
|
||||
}
|
@ -31,7 +31,12 @@ public final class EconomyConfig {
|
||||
*/
|
||||
public EconomyConfig(Map<ConfigOption, Object> configOptions) {
|
||||
this.configOptions = configOptions;
|
||||
loadEconomyConfig(configOptions);
|
||||
try {
|
||||
String freeColor = (String) configOptions.get(ConfigOption.FREE_GATES_COLOR);
|
||||
PortalSignDrawer.setFreeColor(ChatColor.valueOf(freeColor.toUpperCase()));
|
||||
} catch (IllegalArgumentException | NullPointerException ignored) {
|
||||
PortalSignDrawer.setFreeColor(ChatColor.DARK_GREEN);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
@ -220,21 +225,6 @@ public final class EconomyConfig {
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Loads all config values related to economy
|
||||
*
|
||||
* @param configOptions <p>The loaded config options to get values from</p>
|
||||
*/
|
||||
private void loadEconomyConfig(Map<ConfigOption, Object> configOptions) {
|
||||
|
||||
try {
|
||||
String freeColor = (String) configOptions.get(ConfigOption.FREE_GATES_COLOR);
|
||||
PortalSignDrawer.setFreeColor(ChatColor.valueOf(freeColor.toUpperCase()));
|
||||
} catch (IllegalArgumentException | NullPointerException ignored) {
|
||||
PortalSignDrawer.setFreeColor(ChatColor.DARK_GREEN);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Determines if a player can do a gate action for free
|
||||
*
|
||||
|
@ -46,8 +46,6 @@ public final class StargateConfig {
|
||||
private String portalFolder;
|
||||
private String languageName = "en";
|
||||
|
||||
private boolean debuggingEnabled = false;
|
||||
private boolean permissionDebuggingEnabled = false;
|
||||
private final Map<ConfigOption, Object> configOptions;
|
||||
|
||||
/**
|
||||
@ -65,6 +63,18 @@ public final class StargateConfig {
|
||||
languageLoader = new LanguageLoader(dataFolderPath + "/lang/");
|
||||
}
|
||||
|
||||
/**
|
||||
* Gets a direct reference to the config option map
|
||||
*
|
||||
* <p>This reference can be used to alter the value of config options. Values should only be altered after it's
|
||||
* been verified that the value is valid.</p>
|
||||
*
|
||||
* @return <p>A reference to the config options map</p>
|
||||
*/
|
||||
public Map<ConfigOption, Object> getConfigOptionsReference() {
|
||||
return configOptions;
|
||||
}
|
||||
|
||||
/**
|
||||
* Finish the config setup by loading languages, gates and portals, and loading economy if vault is loaded
|
||||
*/
|
||||
@ -81,7 +91,7 @@ public final class StargateConfig {
|
||||
languageLoader.reload();
|
||||
|
||||
messageSender = new MessageSender(languageLoader);
|
||||
if (debuggingEnabled) {
|
||||
if (isDebuggingEnabled()) {
|
||||
languageLoader.debug();
|
||||
}
|
||||
|
||||
@ -130,7 +140,7 @@ public final class StargateConfig {
|
||||
* @return <p>Whether debugging is enabled</p>
|
||||
*/
|
||||
public boolean isDebuggingEnabled() {
|
||||
return debuggingEnabled;
|
||||
return (boolean) configOptions.get(ConfigOption.DEBUG);
|
||||
}
|
||||
|
||||
/**
|
||||
@ -139,7 +149,7 @@ public final class StargateConfig {
|
||||
* @return <p>Whether permission debugging is enabled</p>
|
||||
*/
|
||||
public boolean isPermissionDebuggingEnabled() {
|
||||
return permissionDebuggingEnabled;
|
||||
return (boolean) configOptions.get(ConfigOption.PERMISSION_DEBUG);
|
||||
}
|
||||
|
||||
/**
|
||||
@ -186,6 +196,17 @@ public final class StargateConfig {
|
||||
* Un-loads all loaded data
|
||||
*/
|
||||
private void unload() {
|
||||
//De-activate, close and unload all loaded portals
|
||||
unloadAllPortals();
|
||||
|
||||
//Clear all loaded gates
|
||||
GateHandler.clearGates();
|
||||
}
|
||||
|
||||
/**
|
||||
* Un-loads all loaded portals
|
||||
*/
|
||||
public void unloadAllPortals() {
|
||||
//De-activate all currently active portals
|
||||
for (Portal activePortal : activePortalsQueue) {
|
||||
activePortal.getPortalActivator().deactivate();
|
||||
@ -201,9 +222,6 @@ public final class StargateConfig {
|
||||
|
||||
//Clear all loaded portals
|
||||
PortalRegistry.clearPortals();
|
||||
|
||||
//Clear all loaded gates
|
||||
GateHandler.clearGates();
|
||||
}
|
||||
|
||||
/**
|
||||
@ -256,7 +274,7 @@ public final class StargateConfig {
|
||||
//Update the language loader in case the loaded language changed
|
||||
languageLoader.setChosenLanguage(languageName);
|
||||
languageLoader.reload();
|
||||
if (debuggingEnabled) {
|
||||
if (isDebuggingEnabled()) {
|
||||
languageLoader.debug();
|
||||
}
|
||||
|
||||
@ -267,7 +285,7 @@ public final class StargateConfig {
|
||||
/**
|
||||
* Starts the listener for listening to BungeeCord messages
|
||||
*/
|
||||
private void startStopBungeeListener(boolean start) {
|
||||
public void startStopBungeeListener(boolean start) {
|
||||
Messenger messenger = Bukkit.getMessenger();
|
||||
String bungeeChannel = "BungeeCord";
|
||||
|
||||
@ -283,7 +301,7 @@ public final class StargateConfig {
|
||||
/**
|
||||
* Reloads economy by enabling or disabling it as necessary
|
||||
*/
|
||||
private void reloadEconomy() {
|
||||
public void reloadEconomy() {
|
||||
EconomyConfig economyConfig = getEconomyConfig();
|
||||
if (economyConfig.isEconomyEnabled() && economyConfig.getEconomy() == null) {
|
||||
setupVaultEconomy();
|
||||
@ -342,10 +360,6 @@ public final class StargateConfig {
|
||||
portalFolder = (String) configOptions.get(ConfigOption.PORTAL_FOLDER);
|
||||
gateFolder = (String) configOptions.get(ConfigOption.GATE_FOLDER);
|
||||
|
||||
//Get enabled debug settings from the config
|
||||
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) {
|
||||
GateHandler.writeDefaultGatesToFolder(gateFolder);
|
||||
|
@ -190,13 +190,13 @@ public final class StargateGateConfig {
|
||||
private void loadSignColor(String mainSignColor, String highlightSignColor) {
|
||||
if (mainSignColor != null && highlightSignColor != null) {
|
||||
try {
|
||||
PortalSignDrawer.setColors(ChatColor.valueOf(mainSignColor.toUpperCase()),
|
||||
ChatColor.valueOf(highlightSignColor.toUpperCase()));
|
||||
return;
|
||||
PortalSignDrawer.setMainColor(ChatColor.valueOf(highlightSignColor.toUpperCase()));
|
||||
PortalSignDrawer.setHighlightColor(ChatColor.valueOf(highlightSignColor.toUpperCase()));
|
||||
} catch (IllegalArgumentException | NullPointerException ignored) {
|
||||
}
|
||||
}
|
||||
Stargate.logWarning("You have specified an invalid color in your config.yml. Defaulting to BLACK and WHITE");
|
||||
PortalSignDrawer.setColors(ChatColor.BLACK, ChatColor.WHITE);
|
||||
PortalSignDrawer.setMainColor(ChatColor.BLACK);
|
||||
PortalSignDrawer.setHighlightColor(ChatColor.WHITE);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -29,19 +29,27 @@ public class PortalSignDrawer {
|
||||
}
|
||||
|
||||
/**
|
||||
* Sets the main and highlighting sign colors
|
||||
* Sets the highlighting sign color
|
||||
*
|
||||
* <p>The main sign color is used for most text on the sign, while the highlighting color is used for the markings
|
||||
* around portal names and network names ('>','<','-',')','(')</p>
|
||||
* <p>The highlighting color is used for the markings around portal names and network names ('>','<','-',')','(').</p>
|
||||
*
|
||||
* @param newMainColor <p>The new main sign color</p>
|
||||
* @param newHighlightColor <p>The new highlight color</p>
|
||||
*/
|
||||
public static void setColors(ChatColor newMainColor, ChatColor newHighlightColor) {
|
||||
mainColor = newMainColor;
|
||||
public static void setHighlightColor(ChatColor newHighlightColor) {
|
||||
highlightColor = newHighlightColor;
|
||||
}
|
||||
|
||||
/**
|
||||
* Sets the main sign color
|
||||
*
|
||||
* <p>The main sign color is used for most text on the sign.</p>
|
||||
*
|
||||
* @param newMainColor <p>The new main sign color</p>
|
||||
*/
|
||||
public static void setMainColor(ChatColor newMainColor) {
|
||||
mainColor = newMainColor;
|
||||
}
|
||||
|
||||
/**
|
||||
* Sets the color to use for marking free stargates
|
||||
*
|
||||
|
Loading…
x
Reference in New Issue
Block a user