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:
@ -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.
|
||||
Stargate.getStargateConfig().reload(commandSender);
|
||||
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();
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
|
Reference in New Issue
Block a user