diff --git a/Bukkit/src/main/java/net/frankheijden/serverutils/bukkit/entities/BukkitYamlConfig.java b/Bukkit/src/main/java/net/frankheijden/serverutils/bukkit/entities/BukkitYamlConfig.java index d10794b..4788e30 100644 --- a/Bukkit/src/main/java/net/frankheijden/serverutils/bukkit/entities/BukkitYamlConfig.java +++ b/Bukkit/src/main/java/net/frankheijden/serverutils/bukkit/entities/BukkitYamlConfig.java @@ -68,6 +68,11 @@ public class BukkitYamlConfig implements ServerUtilsConfig { return config.getBoolean(path); } + @Override + public int getInt(String path) { + return config.getInt(path, -1); + } + @Override public Collection getKeys() { return config.getKeys(false); diff --git a/Bukkit/src/main/resources/bukkit-commands.json b/Bukkit/src/main/resources/bukkit-commands.json index 3b20765..6f7500c 100644 --- a/Bukkit/src/main/resources/bukkit-commands.json +++ b/Bukkit/src/main/resources/bukkit-commands.json @@ -1,25 +1,33 @@ { - "serverutils": { - "subcommands": { - "enableplugin": { - "main": "enableplugin", - "aliases": ["ep"], - "permission": "serverutils.enableplugin", - "description": "Enables the specified plugin.", - "display-in-help": true - }, - "disableplugin": { - "main": "disableplugin", - "aliases": ["dp"], - "permission": "serverutils.disableplugin", - "description": "Disables the specified plugin.", - "display-in-help": true - }, - "reloadconfig": { - "main": "reloadconfig", - "aliases": ["rc"], - "permission": "serverutils.reloadconfig", - "description": "Reloads particular server configurations." + "commands": { + "serverutils": { + "subcommands": { + "enableplugin": { + "main": "enableplugin", + "aliases": [ + "ep" + ], + "permission": "serverutils.enableplugin", + "description": "Enables the specified plugin.", + "display-in-help": true + }, + "disableplugin": { + "main": "disableplugin", + "aliases": [ + "dp" + ], + "permission": "serverutils.disableplugin", + "description": "Disables the specified plugin.", + "display-in-help": true + }, + "reloadconfig": { + "main": "reloadconfig", + "aliases": [ + "rc" + ], + "permission": "serverutils.reloadconfig", + "description": "Reloads particular server configurations." + } } } } diff --git a/Bungee/src/main/java/net/frankheijden/serverutils/bungee/entities/BungeeYamlConfig.java b/Bungee/src/main/java/net/frankheijden/serverutils/bungee/entities/BungeeYamlConfig.java index 49673df..0e612de 100644 --- a/Bungee/src/main/java/net/frankheijden/serverutils/bungee/entities/BungeeYamlConfig.java +++ b/Bungee/src/main/java/net/frankheijden/serverutils/bungee/entities/BungeeYamlConfig.java @@ -75,6 +75,11 @@ public class BungeeYamlConfig implements ServerUtilsConfig { return config.getBoolean(path); } + @Override + public int getInt(String path) { + return config.getInt(path, -1); + } + @Override public Collection getKeys() { return config.getKeys(); diff --git a/Bungee/src/main/resources/bungee-commands.json b/Bungee/src/main/resources/bungee-commands.json index 96aa9b1..9bf5f80 100644 --- a/Bungee/src/main/resources/bungee-commands.json +++ b/Bungee/src/main/resources/bungee-commands.json @@ -1,12 +1,16 @@ { - "plugins": { - "flags": { - "modules": { - "main": "modules", - "aliases": ["m"], - "permission": "serverutils.plugins.modules", - "description": "Displays the proxy modules.", - "display-in-help": true + "commands": { + "plugins": { + "flags": { + "modules": { + "main": "modules", + "aliases": [ + "m" + ], + "permission": "serverutils.plugins.modules", + "description": "Displays the proxy modules.", + "display-in-help": true + } } } } diff --git a/Common/src/main/java/net/frankheijden/serverutils/common/commands/CommandServerUtils.java b/Common/src/main/java/net/frankheijden/serverutils/common/commands/CommandServerUtils.java index 55a7c34..1779dae 100644 --- a/Common/src/main/java/net/frankheijden/serverutils/common/commands/CommandServerUtils.java +++ b/Common/src/main/java/net/frankheijden/serverutils/common/commands/CommandServerUtils.java @@ -83,7 +83,7 @@ public abstract class CommandServerUtils FormatBuilder builder = FormatBuilder.create(plugin.getMessagesResource().getMessage("serverutils.help.format")) .orderedKeys("%command%", "%help%"); - ServerUtilsConfig config = plugin.getCommandsResource().getConfig(); + ServerUtilsConfig config = (ServerUtilsConfig) plugin.getCommandsResource().getConfig().get("commands"); for (String commandName : config.getKeys()) { ServerUtilsConfig commandConfig = (ServerUtilsConfig) config.get(commandName); CommandElement commandElement = parseElement(commandConfig); diff --git a/Common/src/main/java/net/frankheijden/serverutils/common/commands/ServerUtilsCommand.java b/Common/src/main/java/net/frankheijden/serverutils/common/commands/ServerUtilsCommand.java index 2664bf5..dc6d4fe 100644 --- a/Common/src/main/java/net/frankheijden/serverutils/common/commands/ServerUtilsCommand.java +++ b/Common/src/main/java/net/frankheijden/serverutils/common/commands/ServerUtilsCommand.java @@ -23,7 +23,8 @@ public abstract class ServerUtilsCommand protected ServerUtilsCommand(U plugin, String commandName) { this.plugin = plugin; this.commandName = commandName; - this.commandConfig = (ServerUtilsConfig) plugin.getCommandsResource().getConfig().get(commandName); + this.commandConfig = (ServerUtilsConfig) plugin.getCommandsResource().getConfig() + .get("commands." + commandName); this.arguments = new HashMap<>(); } diff --git a/Common/src/main/java/net/frankheijden/serverutils/common/config/CommandsResource.java b/Common/src/main/java/net/frankheijden/serverutils/common/config/CommandsResource.java index 4915b8b..5356c73 100644 --- a/Common/src/main/java/net/frankheijden/serverutils/common/config/CommandsResource.java +++ b/Common/src/main/java/net/frankheijden/serverutils/common/config/CommandsResource.java @@ -12,4 +12,9 @@ public class CommandsResource extends ServerUtilsResource { public CommandsResource(ServerUtilsPlugin plugin) { super(plugin, COMMANDS_RESOURCE); } + + @Override + public void migrate(int currentConfigVersion) { + + } } diff --git a/Common/src/main/java/net/frankheijden/serverutils/common/config/ConfigResource.java b/Common/src/main/java/net/frankheijden/serverutils/common/config/ConfigResource.java index c11d690..1b08bb0 100644 --- a/Common/src/main/java/net/frankheijden/serverutils/common/config/ConfigResource.java +++ b/Common/src/main/java/net/frankheijden/serverutils/common/config/ConfigResource.java @@ -9,4 +9,9 @@ public class ConfigResource extends ServerUtilsResource { public ConfigResource(ServerUtilsPlugin plugin) { super(plugin, CONFIG_RESOURCE); } + + @Override + public void migrate(int currentConfigVersion) { + + } } diff --git a/Common/src/main/java/net/frankheijden/serverutils/common/config/JsonConfig.java b/Common/src/main/java/net/frankheijden/serverutils/common/config/JsonConfig.java index 17affcb..c3490c4 100644 --- a/Common/src/main/java/net/frankheijden/serverutils/common/config/JsonConfig.java +++ b/Common/src/main/java/net/frankheijden/serverutils/common/config/JsonConfig.java @@ -8,13 +8,17 @@ import com.google.gson.JsonPrimitive; import com.google.gson.reflect.TypeToken; import java.io.File; import java.io.IOException; +import java.io.InputStreamReader; import java.nio.charset.StandardCharsets; import java.nio.file.Files; import java.nio.file.StandardOpenOption; import java.util.ArrayList; import java.util.Collection; import java.util.List; +import java.util.Locale; import java.util.Map; +import net.frankheijden.serverutils.common.entities.ServerUtilsPlugin; +import net.frankheijden.serverutils.common.providers.ResourceProvider; public class JsonConfig implements ServerUtilsConfig { @@ -32,11 +36,34 @@ public class JsonConfig implements ServerUtilsConfig { this.config = config; } + /** + * Loads a resource from the jar file. + */ + public static JsonConfig load(ResourceProvider provider, ServerUtilsPlugin.Platform platform, String resourceName) { + // Create the platform JsonConfig by merging the platformConfig with the generalConfig + JsonConfig generalConfig = new JsonConfig(JsonConfig.gson.fromJson( + new InputStreamReader(provider.getRawResource(resourceName + ".json")), + JsonObject.class + )); + + String platformResource = platform.name().toLowerCase(Locale.ENGLISH) + '-' + resourceName; + JsonConfig platformConfig = new JsonConfig(JsonConfig.gson.fromJson( + new InputStreamReader(provider.getRawResource(platformResource + ".json")), + JsonObject.class + )); + ServerUtilsConfig.addDefaults(platformConfig, generalConfig); + + return generalConfig; + } + public JsonObject getConfig() { return config; } - private JsonElement getJsonElement(String path) { + /** + * Retrieves the JsonElement at given path. + */ + public JsonElement getJsonElement(String path) { JsonElement result = config; for (String memberName : path.split("\\.")) { @@ -137,6 +164,13 @@ public class JsonConfig implements ServerUtilsConfig { return element.getAsBoolean(); } + @Override + public int getInt(String path) { + JsonElement element = getJsonElement(path); + if (element == null) return -1; + return element.getAsNumber().intValue(); + } + @Override public Collection getKeys() { return config.keySet(); diff --git a/Common/src/main/java/net/frankheijden/serverutils/common/config/MessagesResource.java b/Common/src/main/java/net/frankheijden/serverutils/common/config/MessagesResource.java index 9208285..c3220c7 100644 --- a/Common/src/main/java/net/frankheijden/serverutils/common/config/MessagesResource.java +++ b/Common/src/main/java/net/frankheijden/serverutils/common/config/MessagesResource.java @@ -53,4 +53,11 @@ public class MessagesResource extends ServerUtilsResource { sender.sendMessage(plugin.getChatProvider().color(message)); } } + + @Override + public void migrate(int currentConfigVersion) { + if (currentConfigVersion <= 1) { + reset("serverutils.help.format"); + } + } } diff --git a/Common/src/main/java/net/frankheijden/serverutils/common/config/ServerUtilsConfig.java b/Common/src/main/java/net/frankheijden/serverutils/common/config/ServerUtilsConfig.java index 13420ea..028f4b3 100644 --- a/Common/src/main/java/net/frankheijden/serverutils/common/config/ServerUtilsConfig.java +++ b/Common/src/main/java/net/frankheijden/serverutils/common/config/ServerUtilsConfig.java @@ -1,17 +1,12 @@ package net.frankheijden.serverutils.common.config; import com.google.gson.JsonElement; -import com.google.gson.JsonObject; import java.io.IOException; -import java.io.InputStreamReader; import java.nio.file.Files; import java.nio.file.Path; import java.util.Collection; import java.util.List; -import java.util.Locale; import java.util.Map; -import net.frankheijden.serverutils.common.entities.ServerCommandSender; -import net.frankheijden.serverutils.common.entities.ServerUtilsPlugin; import net.frankheijden.serverutils.common.providers.ResourceProvider; /** @@ -61,6 +56,13 @@ public interface ServerUtilsConfig { */ boolean getBoolean(String path); + /** + * Retrieves an integer from a path. + * @param path The path. + * @return The integer at given path. + */ + int getInt(String path); + /** * Retrieves the key nodes at the current level. * @return The keys. @@ -146,28 +148,13 @@ public interface ServerUtilsConfig { } /** - * Loads a resource from the jar file. + * Loads a resource from the jar file and writes it to the given path, applying defaults if needed. */ - static , P, T, C extends ServerCommandSender, S> ServerUtilsConfig load( - U plugin, - Path path, - String resource + static ServerUtilsConfig init( + ServerUtilsConfig def, + ResourceProvider provider, + Path path ) { - ResourceProvider provider = plugin.getResourceProvider(); - - // Create the platform JsonConfig by merging the platformResource with the common resource - JsonConfig generalConfig = new JsonConfig(JsonConfig.gson.fromJson( - new InputStreamReader(provider.getRawResource(resource + ".json")), - JsonObject.class - )); - - String platformResource = plugin.getPlatform().name().toLowerCase(Locale.ENGLISH) + '-' + resource; - JsonConfig platformConfig = new JsonConfig(JsonConfig.gson.fromJson( - new InputStreamReader(provider.getRawResource(platformResource + ".json")), - JsonObject.class - )); - addDefaults(platformConfig, generalConfig); - if (!Files.exists(path)) { try { Files.createFile(path); @@ -176,6 +163,6 @@ public interface ServerUtilsConfig { } } - return init(generalConfig, provider.load(path.toFile())); + return init(def, provider.load(path.toFile())); } } diff --git a/Common/src/main/java/net/frankheijden/serverutils/common/config/ServerUtilsResource.java b/Common/src/main/java/net/frankheijden/serverutils/common/config/ServerUtilsResource.java index 9a9eb49..2b4fbec 100644 --- a/Common/src/main/java/net/frankheijden/serverutils/common/config/ServerUtilsResource.java +++ b/Common/src/main/java/net/frankheijden/serverutils/common/config/ServerUtilsResource.java @@ -1,31 +1,61 @@ package net.frankheijden.serverutils.common.config; +import java.io.IOException; import net.frankheijden.serverutils.common.entities.ServerUtilsPlugin; -public class ServerUtilsResource { +public abstract class ServerUtilsResource { protected final ServerUtilsPlugin plugin; protected final ServerUtilsConfig config; + protected final JsonConfig defaultConfig; - protected ServerUtilsResource(ServerUtilsPlugin plugin, ServerUtilsConfig config) { + protected ServerUtilsResource( + ServerUtilsPlugin plugin, + ServerUtilsConfig config, + JsonConfig defaultConfig + ) { this.plugin = plugin; this.config = config; + this.defaultConfig = defaultConfig; } protected ServerUtilsResource(ServerUtilsPlugin plugin, String resourceName) { - this( - plugin, - ServerUtilsConfig.load( - plugin, - plugin.getDataFolder().toPath().resolve( - resourceName + plugin.getResourceProvider().getResourceExtension() - ), - resourceName + this.plugin = plugin; + this.defaultConfig = JsonConfig.load(plugin.getResourceProvider(), plugin.getPlatform(), resourceName); + this.config = ServerUtilsConfig.init( + this.defaultConfig, + plugin.getResourceProvider(), + plugin.getDataFolder().toPath().resolve( + resourceName + plugin.getResourceProvider().getResourceExtension() ) ); + this.migrate(); } public ServerUtilsConfig getConfig() { return config; } + + public ServerUtilsConfig getDefaultConfig() { + return defaultConfig; + } + + protected void reset(String path) { + config.set(path, JsonConfig.toObjectValue(defaultConfig.getJsonElement(path))); + } + + /** + * Migrates values in the config. + */ + public void migrate() { + migrate(config.getInt("config-version")); + config.set("config-version", defaultConfig.getInt("config-version")); + try { + config.save(); + } catch (IOException ex) { + ex.printStackTrace(); + } + } + + public abstract void migrate(int currentConfigVersion); } diff --git a/Common/src/main/resources/commands.json b/Common/src/main/resources/commands.json index 7f921c8..db892da 100644 --- a/Common/src/main/resources/commands.json +++ b/Common/src/main/resources/commands.json @@ -1,88 +1,91 @@ { - "plugins": { - "main": "%prefix%plugins", - "aliases": ["%prefix%pl"], - "permission": "serverutils.plugins", - "description": "Displays the enabled plugins.", - "display-in-help": true, - "flags": { - "version": { - "main": "version", - "aliases": ["v"], - "permission": "serverutils.plugins.version", - "description": "Displays the plugin versions.", - "display-in-help": true + "config-version": 1, + "commands": { + "plugins": { + "main": "%prefix%plugins", + "aliases": ["%prefix%pl"], + "permission": "serverutils.plugins", + "description": "Displays the enabled plugins.", + "display-in-help": true, + "flags": { + "version": { + "main": "version", + "aliases": ["v"], + "permission": "serverutils.plugins.version", + "description": "Displays the plugin versions.", + "display-in-help": true + } } - } - }, - "serverutils": { - "main": "%prefix%serverutils", - "aliases": ["%prefix%su"], - "permission": "serverutils.help", - "display-in-help": false, - "subcommands": { - "help": { - "main": "help", - "aliases": [], - "permission": "serverutils.help", - "description": "Displays the help page.", - "display-in-help": true - }, - "reload": { - "main": "reload", - "aliases": [], - "permission": "serverutils.reload", - "description": "Reloads the ServerUtils plugin.", - "display-in-help": true - }, - "loadplugin": { - "main": "loadplugin", - "aliases": ["lp"], - "permission": "serverutils.loadplugin", - "description": "Loads the specified jar file as a plugin.", - "display-in-help": true - }, - "unloadplugin": { - "main": "unloadplugin", - "aliases": ["up"], - "permission": "serverutils.unloadplugin", - "description": "Disables and unloads the specified plugin.", - "display-in-help": true - }, - "reloadplugin": { - "main": "reloadplugin", - "aliases": ["rp"], - "permission": "serverutils.reloadplugin", - "description": "Reloads the specified plugin.", - "display-in-help": true - }, - "watchplugin": { - "main": "watchplugin", - "aliases": ["wp"], - "permission": "serverutils.watchplugin", - "description": "Watches the specified plugin for changes.", - "display-in-help": true - }, - "unwatchplugin": { - "main": "unwatchplugin", - "aliases": ["uwp"], - "permission": "serverutils.watchplugin", - "description": "Stops watching the specified plugin for changes.", - "display-in-help": true - }, - "plugininfo": { - "main": "plugininfo", - "aliases": ["pi"], - "permission": "serverutils.plugininfo", - "description": "Shows information about the specified plugin.", - "display-in-help": true - }, - "commandinfo": { - "main": "commandinfo", - "aliases": ["ci"], - "permission": "serverutils.commandinfo", - "description": "Shows information about the specified command.", - "display-in-help": true + }, + "serverutils": { + "main": "%prefix%serverutils", + "aliases": ["%prefix%su"], + "permission": "serverutils.help", + "display-in-help": false, + "subcommands": { + "help": { + "main": "help", + "aliases": [], + "permission": "serverutils.help", + "description": "Displays the help page.", + "display-in-help": true + }, + "reload": { + "main": "reload", + "aliases": [], + "permission": "serverutils.reload", + "description": "Reloads the ServerUtils plugin.", + "display-in-help": true + }, + "loadplugin": { + "main": "loadplugin", + "aliases": ["lp"], + "permission": "serverutils.loadplugin", + "description": "Loads the specified jar file as a plugin.", + "display-in-help": true + }, + "unloadplugin": { + "main": "unloadplugin", + "aliases": ["up"], + "permission": "serverutils.unloadplugin", + "description": "Disables and unloads the specified plugin.", + "display-in-help": true + }, + "reloadplugin": { + "main": "reloadplugin", + "aliases": ["rp"], + "permission": "serverutils.reloadplugin", + "description": "Reloads the specified plugin.", + "display-in-help": true + }, + "watchplugin": { + "main": "watchplugin", + "aliases": ["wp"], + "permission": "serverutils.watchplugin", + "description": "Watches the specified plugin for changes.", + "display-in-help": true + }, + "unwatchplugin": { + "main": "unwatchplugin", + "aliases": ["uwp"], + "permission": "serverutils.watchplugin", + "description": "Stops watching the specified plugin for changes.", + "display-in-help": true + }, + "plugininfo": { + "main": "plugininfo", + "aliases": ["pi"], + "permission": "serverutils.plugininfo", + "description": "Shows information about the specified plugin.", + "display-in-help": true + }, + "commandinfo": { + "main": "commandinfo", + "aliases": ["ci"], + "permission": "serverutils.commandinfo", + "description": "Shows information about the specified command.", + "display-in-help": true + } } } } diff --git a/Common/src/main/resources/config.json b/Common/src/main/resources/config.json index 6895da7..5196278 100644 --- a/Common/src/main/resources/config.json +++ b/Common/src/main/resources/config.json @@ -1,4 +1,5 @@ { + "config-version": 1, "settings": { "disable-plugins-command": false, "check-updates-boot": true, diff --git a/Common/src/main/resources/messages.json b/Common/src/main/resources/messages.json index 8f16263..3cd99ab 100644 --- a/Common/src/main/resources/messages.json +++ b/Common/src/main/resources/messages.json @@ -1,4 +1,5 @@ { + "config-version": 2, "serverutils": { "success": "&3Successfully %action%ed &b%what%&3!", "warning": "&3Successfully %action%ed &b%what%&3, but with warnings.", diff --git a/Velocity/src/main/java/net/frankheijden/serverutils/velocity/entities/VelocityTomlConfig.java b/Velocity/src/main/java/net/frankheijden/serverutils/velocity/entities/VelocityTomlConfig.java index 64be256..3b68643 100644 --- a/Velocity/src/main/java/net/frankheijden/serverutils/velocity/entities/VelocityTomlConfig.java +++ b/Velocity/src/main/java/net/frankheijden/serverutils/velocity/entities/VelocityTomlConfig.java @@ -74,6 +74,11 @@ public class VelocityTomlConfig implements ServerUtilsConfig { return config.get(path); } + @Override + public int getInt(String path) { + return config.getOrElse(path, -1); + } + @Override public Collection getKeys() { return new HashSet<>(config.valueMap().keySet());