Add config migration feature
This commit is contained in:
parent
5aeef212dc
commit
25fdec31b1
16 changed files with 252 additions and 151 deletions
|
|
@ -68,6 +68,11 @@ public class BukkitYamlConfig implements ServerUtilsConfig {
|
||||||
return config.getBoolean(path);
|
return config.getBoolean(path);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public int getInt(String path) {
|
||||||
|
return config.getInt(path, -1);
|
||||||
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public Collection<? extends String> getKeys() {
|
public Collection<? extends String> getKeys() {
|
||||||
return config.getKeys(false);
|
return config.getKeys(false);
|
||||||
|
|
|
||||||
|
|
@ -1,26 +1,34 @@
|
||||||
{
|
{
|
||||||
|
"commands": {
|
||||||
"serverutils": {
|
"serverutils": {
|
||||||
"subcommands": {
|
"subcommands": {
|
||||||
"enableplugin": {
|
"enableplugin": {
|
||||||
"main": "enableplugin",
|
"main": "enableplugin",
|
||||||
"aliases": ["ep"],
|
"aliases": [
|
||||||
|
"ep"
|
||||||
|
],
|
||||||
"permission": "serverutils.enableplugin",
|
"permission": "serverutils.enableplugin",
|
||||||
"description": "Enables the specified plugin.",
|
"description": "Enables the specified plugin.",
|
||||||
"display-in-help": true
|
"display-in-help": true
|
||||||
},
|
},
|
||||||
"disableplugin": {
|
"disableplugin": {
|
||||||
"main": "disableplugin",
|
"main": "disableplugin",
|
||||||
"aliases": ["dp"],
|
"aliases": [
|
||||||
|
"dp"
|
||||||
|
],
|
||||||
"permission": "serverutils.disableplugin",
|
"permission": "serverutils.disableplugin",
|
||||||
"description": "Disables the specified plugin.",
|
"description": "Disables the specified plugin.",
|
||||||
"display-in-help": true
|
"display-in-help": true
|
||||||
},
|
},
|
||||||
"reloadconfig": {
|
"reloadconfig": {
|
||||||
"main": "reloadconfig",
|
"main": "reloadconfig",
|
||||||
"aliases": ["rc"],
|
"aliases": [
|
||||||
|
"rc"
|
||||||
|
],
|
||||||
"permission": "serverutils.reloadconfig",
|
"permission": "serverutils.reloadconfig",
|
||||||
"description": "Reloads particular server configurations."
|
"description": "Reloads particular server configurations."
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -75,6 +75,11 @@ public class BungeeYamlConfig implements ServerUtilsConfig {
|
||||||
return config.getBoolean(path);
|
return config.getBoolean(path);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public int getInt(String path) {
|
||||||
|
return config.getInt(path, -1);
|
||||||
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public Collection<? extends String> getKeys() {
|
public Collection<? extends String> getKeys() {
|
||||||
return config.getKeys();
|
return config.getKeys();
|
||||||
|
|
|
||||||
|
|
@ -1,13 +1,17 @@
|
||||||
{
|
{
|
||||||
|
"commands": {
|
||||||
"plugins": {
|
"plugins": {
|
||||||
"flags": {
|
"flags": {
|
||||||
"modules": {
|
"modules": {
|
||||||
"main": "modules",
|
"main": "modules",
|
||||||
"aliases": ["m"],
|
"aliases": [
|
||||||
|
"m"
|
||||||
|
],
|
||||||
"permission": "serverutils.plugins.modules",
|
"permission": "serverutils.plugins.modules",
|
||||||
"description": "Displays the proxy modules.",
|
"description": "Displays the proxy modules.",
|
||||||
"display-in-help": true
|
"display-in-help": true
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -83,7 +83,7 @@ public abstract class CommandServerUtils<U extends ServerUtilsPlugin<P, ?, C, ?>
|
||||||
FormatBuilder builder = FormatBuilder.create(plugin.getMessagesResource().getMessage("serverutils.help.format"))
|
FormatBuilder builder = FormatBuilder.create(plugin.getMessagesResource().getMessage("serverutils.help.format"))
|
||||||
.orderedKeys("%command%", "%help%");
|
.orderedKeys("%command%", "%help%");
|
||||||
|
|
||||||
ServerUtilsConfig config = plugin.getCommandsResource().getConfig();
|
ServerUtilsConfig config = (ServerUtilsConfig) plugin.getCommandsResource().getConfig().get("commands");
|
||||||
for (String commandName : config.getKeys()) {
|
for (String commandName : config.getKeys()) {
|
||||||
ServerUtilsConfig commandConfig = (ServerUtilsConfig) config.get(commandName);
|
ServerUtilsConfig commandConfig = (ServerUtilsConfig) config.get(commandName);
|
||||||
CommandElement commandElement = parseElement(commandConfig);
|
CommandElement commandElement = parseElement(commandConfig);
|
||||||
|
|
|
||||||
|
|
@ -23,7 +23,8 @@ public abstract class ServerUtilsCommand<U extends ServerUtilsPlugin<?, ?, C, ?>
|
||||||
protected ServerUtilsCommand(U plugin, String commandName) {
|
protected ServerUtilsCommand(U plugin, String commandName) {
|
||||||
this.plugin = plugin;
|
this.plugin = plugin;
|
||||||
this.commandName = commandName;
|
this.commandName = commandName;
|
||||||
this.commandConfig = (ServerUtilsConfig) plugin.getCommandsResource().getConfig().get(commandName);
|
this.commandConfig = (ServerUtilsConfig) plugin.getCommandsResource().getConfig()
|
||||||
|
.get("commands." + commandName);
|
||||||
this.arguments = new HashMap<>();
|
this.arguments = new HashMap<>();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -12,4 +12,9 @@ public class CommandsResource extends ServerUtilsResource {
|
||||||
public CommandsResource(ServerUtilsPlugin<?, ?, ?, ?> plugin) {
|
public CommandsResource(ServerUtilsPlugin<?, ?, ?, ?> plugin) {
|
||||||
super(plugin, COMMANDS_RESOURCE);
|
super(plugin, COMMANDS_RESOURCE);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void migrate(int currentConfigVersion) {
|
||||||
|
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -9,4 +9,9 @@ public class ConfigResource extends ServerUtilsResource {
|
||||||
public ConfigResource(ServerUtilsPlugin<?, ?, ?, ?> plugin) {
|
public ConfigResource(ServerUtilsPlugin<?, ?, ?, ?> plugin) {
|
||||||
super(plugin, CONFIG_RESOURCE);
|
super(plugin, CONFIG_RESOURCE);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void migrate(int currentConfigVersion) {
|
||||||
|
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -8,13 +8,17 @@ import com.google.gson.JsonPrimitive;
|
||||||
import com.google.gson.reflect.TypeToken;
|
import com.google.gson.reflect.TypeToken;
|
||||||
import java.io.File;
|
import java.io.File;
|
||||||
import java.io.IOException;
|
import java.io.IOException;
|
||||||
|
import java.io.InputStreamReader;
|
||||||
import java.nio.charset.StandardCharsets;
|
import java.nio.charset.StandardCharsets;
|
||||||
import java.nio.file.Files;
|
import java.nio.file.Files;
|
||||||
import java.nio.file.StandardOpenOption;
|
import java.nio.file.StandardOpenOption;
|
||||||
import java.util.ArrayList;
|
import java.util.ArrayList;
|
||||||
import java.util.Collection;
|
import java.util.Collection;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
|
import java.util.Locale;
|
||||||
import java.util.Map;
|
import java.util.Map;
|
||||||
|
import net.frankheijden.serverutils.common.entities.ServerUtilsPlugin;
|
||||||
|
import net.frankheijden.serverutils.common.providers.ResourceProvider;
|
||||||
|
|
||||||
public class JsonConfig implements ServerUtilsConfig {
|
public class JsonConfig implements ServerUtilsConfig {
|
||||||
|
|
||||||
|
|
@ -32,11 +36,34 @@ public class JsonConfig implements ServerUtilsConfig {
|
||||||
this.config = config;
|
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() {
|
public JsonObject getConfig() {
|
||||||
return config;
|
return config;
|
||||||
}
|
}
|
||||||
|
|
||||||
private JsonElement getJsonElement(String path) {
|
/**
|
||||||
|
* Retrieves the JsonElement at given path.
|
||||||
|
*/
|
||||||
|
public JsonElement getJsonElement(String path) {
|
||||||
JsonElement result = config;
|
JsonElement result = config;
|
||||||
|
|
||||||
for (String memberName : path.split("\\.")) {
|
for (String memberName : path.split("\\.")) {
|
||||||
|
|
@ -137,6 +164,13 @@ public class JsonConfig implements ServerUtilsConfig {
|
||||||
return element.getAsBoolean();
|
return element.getAsBoolean();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public int getInt(String path) {
|
||||||
|
JsonElement element = getJsonElement(path);
|
||||||
|
if (element == null) return -1;
|
||||||
|
return element.getAsNumber().intValue();
|
||||||
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public Collection<? extends String> getKeys() {
|
public Collection<? extends String> getKeys() {
|
||||||
return config.keySet();
|
return config.keySet();
|
||||||
|
|
|
||||||
|
|
@ -53,4 +53,11 @@ public class MessagesResource extends ServerUtilsResource {
|
||||||
sender.sendMessage(plugin.getChatProvider().color(message));
|
sender.sendMessage(plugin.getChatProvider().color(message));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void migrate(int currentConfigVersion) {
|
||||||
|
if (currentConfigVersion <= 1) {
|
||||||
|
reset("serverutils.help.format");
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -1,17 +1,12 @@
|
||||||
package net.frankheijden.serverutils.common.config;
|
package net.frankheijden.serverutils.common.config;
|
||||||
|
|
||||||
import com.google.gson.JsonElement;
|
import com.google.gson.JsonElement;
|
||||||
import com.google.gson.JsonObject;
|
|
||||||
import java.io.IOException;
|
import java.io.IOException;
|
||||||
import java.io.InputStreamReader;
|
|
||||||
import java.nio.file.Files;
|
import java.nio.file.Files;
|
||||||
import java.nio.file.Path;
|
import java.nio.file.Path;
|
||||||
import java.util.Collection;
|
import java.util.Collection;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
import java.util.Locale;
|
|
||||||
import java.util.Map;
|
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;
|
import net.frankheijden.serverutils.common.providers.ResourceProvider;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|
@ -61,6 +56,13 @@ public interface ServerUtilsConfig {
|
||||||
*/
|
*/
|
||||||
boolean getBoolean(String path);
|
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.
|
* Retrieves the key nodes at the current level.
|
||||||
* @return The keys.
|
* @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 <U extends ServerUtilsPlugin<P, T, C, S>, P, T, C extends ServerCommandSender<S>, S> ServerUtilsConfig load(
|
static ServerUtilsConfig init(
|
||||||
U plugin,
|
ServerUtilsConfig def,
|
||||||
Path path,
|
ResourceProvider provider,
|
||||||
String resource
|
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)) {
|
if (!Files.exists(path)) {
|
||||||
try {
|
try {
|
||||||
Files.createFile(path);
|
Files.createFile(path);
|
||||||
|
|
@ -176,6 +163,6 @@ public interface ServerUtilsConfig {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
return init(generalConfig, provider.load(path.toFile()));
|
return init(def, provider.load(path.toFile()));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -1,31 +1,61 @@
|
||||||
package net.frankheijden.serverutils.common.config;
|
package net.frankheijden.serverutils.common.config;
|
||||||
|
|
||||||
|
import java.io.IOException;
|
||||||
import net.frankheijden.serverutils.common.entities.ServerUtilsPlugin;
|
import net.frankheijden.serverutils.common.entities.ServerUtilsPlugin;
|
||||||
|
|
||||||
public class ServerUtilsResource {
|
public abstract class ServerUtilsResource {
|
||||||
|
|
||||||
protected final ServerUtilsPlugin<?, ?, ?, ?> plugin;
|
protected final ServerUtilsPlugin<?, ?, ?, ?> plugin;
|
||||||
protected final ServerUtilsConfig config;
|
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.plugin = plugin;
|
||||||
this.config = config;
|
this.config = config;
|
||||||
|
this.defaultConfig = defaultConfig;
|
||||||
}
|
}
|
||||||
|
|
||||||
protected ServerUtilsResource(ServerUtilsPlugin<?, ?, ?, ?> plugin, String resourceName) {
|
protected ServerUtilsResource(ServerUtilsPlugin<?, ?, ?, ?> plugin, String resourceName) {
|
||||||
this(
|
this.plugin = plugin;
|
||||||
plugin,
|
this.defaultConfig = JsonConfig.load(plugin.getResourceProvider(), plugin.getPlatform(), resourceName);
|
||||||
ServerUtilsConfig.load(
|
this.config = ServerUtilsConfig.init(
|
||||||
plugin,
|
this.defaultConfig,
|
||||||
|
plugin.getResourceProvider(),
|
||||||
plugin.getDataFolder().toPath().resolve(
|
plugin.getDataFolder().toPath().resolve(
|
||||||
resourceName + plugin.getResourceProvider().getResourceExtension()
|
resourceName + plugin.getResourceProvider().getResourceExtension()
|
||||||
),
|
|
||||||
resourceName
|
|
||||||
)
|
)
|
||||||
);
|
);
|
||||||
|
this.migrate();
|
||||||
}
|
}
|
||||||
|
|
||||||
public ServerUtilsConfig getConfig() {
|
public ServerUtilsConfig getConfig() {
|
||||||
return config;
|
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);
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -1,4 +1,6 @@
|
||||||
{
|
{
|
||||||
|
"config-version": 1,
|
||||||
|
"commands": {
|
||||||
"plugins": {
|
"plugins": {
|
||||||
"main": "%prefix%plugins",
|
"main": "%prefix%plugins",
|
||||||
"aliases": ["%prefix%pl"],
|
"aliases": ["%prefix%pl"],
|
||||||
|
|
@ -86,4 +88,5 @@
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -1,4 +1,5 @@
|
||||||
{
|
{
|
||||||
|
"config-version": 1,
|
||||||
"settings": {
|
"settings": {
|
||||||
"disable-plugins-command": false,
|
"disable-plugins-command": false,
|
||||||
"check-updates-boot": true,
|
"check-updates-boot": true,
|
||||||
|
|
|
||||||
|
|
@ -1,4 +1,5 @@
|
||||||
{
|
{
|
||||||
|
"config-version": 2,
|
||||||
"serverutils": {
|
"serverutils": {
|
||||||
"success": "&3Successfully %action%ed &b%what%&3!",
|
"success": "&3Successfully %action%ed &b%what%&3!",
|
||||||
"warning": "&3Successfully %action%ed &b%what%&3, but with warnings.",
|
"warning": "&3Successfully %action%ed &b%what%&3, but with warnings.",
|
||||||
|
|
|
||||||
|
|
@ -74,6 +74,11 @@ public class VelocityTomlConfig implements ServerUtilsConfig {
|
||||||
return config.get(path);
|
return config.get(path);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public int getInt(String path) {
|
||||||
|
return config.getOrElse(path, -1);
|
||||||
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public Collection<? extends String> getKeys() {
|
public Collection<? extends String> getKeys() {
|
||||||
return new HashSet<>(config.valueMap().keySet());
|
return new HashSet<>(config.valueMap().keySet());
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue