Initial Velocity support

This commit is contained in:
Frank van der Heijden 2021-07-15 17:55:44 +02:00
parent 4ffa7f1b99
commit 4390dc2c56
No known key found for this signature in database
GPG key ID: B808721C2DD5B5B8
36 changed files with 1455 additions and 41 deletions

View file

@ -3,7 +3,7 @@ package net.frankheijden.serverutils.common.config;
/**
* The general common config class.
*/
public class Config extends YamlResource {
public class Config extends ServerUtilsResource {
private static Config instance;

View file

@ -8,7 +8,7 @@ import net.frankheijden.serverutils.common.utils.StringUtils;
/**
* The general common messenger class.
*/
public class Messenger extends YamlResource {
public class Messenger extends ServerUtilsResource {
private static Messenger instance;
private static final ServerUtilsPlugin plugin = ServerUtilsApp.getPlugin();

View file

@ -6,9 +6,9 @@ import java.util.List;
import java.util.Map;
/**
* A wrap for a Yaml Configuration file.
* A wrap for a Configuration file.
*/
public interface YamlConfig {
public interface ServerUtilsConfig {
/**
* Retrieves the value at a given path.
@ -69,7 +69,7 @@ public interface YamlConfig {
* @param def The defaults to copy values over from.
* @param conf The configuration to copy the defaults to.
*/
static void addDefaults(YamlConfig def, YamlConfig conf) {
static void addDefaults(ServerUtilsConfig def, ServerUtilsConfig conf) {
addDefaults(def, conf, "");
}
@ -79,13 +79,13 @@ public interface YamlConfig {
* @param conf The configuration to copy the defaults to.
* @param root The current root path of the iteration.
*/
static void addDefaults(YamlConfig def, YamlConfig conf, String root) {
static void addDefaults(ServerUtilsConfig def, ServerUtilsConfig conf, String root) {
if (def == null) return;
for (String key : def.getKeys()) {
String newKey = (root.isEmpty() ? "" : root + ".") + key;
Object value = def.get(key);
if (value instanceof YamlConfig) {
addDefaults((YamlConfig) value, conf, newKey);
if (value instanceof ServerUtilsConfig) {
addDefaults((ServerUtilsConfig) value, conf, newKey);
} else if (conf.get(newKey) == null) {
conf.set(newKey, value);
}
@ -95,22 +95,22 @@ public interface YamlConfig {
/**
* Removes unused keys from the configuration.
*/
static void removeOldKeys(YamlConfig def, YamlConfig conf) {
static void removeOldKeys(ServerUtilsConfig def, ServerUtilsConfig conf) {
removeOldKeys(def, conf, "");
}
/**
* Removes unused keys from the configuration, starting from the root node.
*/
static void removeOldKeys(YamlConfig def, YamlConfig conf, String root) {
static void removeOldKeys(ServerUtilsConfig def, ServerUtilsConfig conf, String root) {
if (def == null) return;
for (String key : conf.getKeys()) {
String defKey = (root.isEmpty() ? "" : root + ".") + key;
Object value = conf.get(key);
if (def.get(defKey) == null) {
conf.set(key, null);
} else if (value instanceof YamlConfig) {
removeOldKeys(def, (YamlConfig) value, defKey);
} else if (value instanceof ServerUtilsConfig) {
removeOldKeys(def, (ServerUtilsConfig) value, defKey);
}
}
}
@ -121,9 +121,9 @@ public interface YamlConfig {
* @param conf The Configuration where the defaults will be applied to.
* @return The loaded Configuration of the file with defaults.
*/
static YamlConfig init(YamlConfig def, YamlConfig conf) {
YamlConfig.addDefaults(def, conf);
YamlConfig.removeOldKeys(def, conf);
static ServerUtilsConfig init(ServerUtilsConfig def, ServerUtilsConfig conf) {
ServerUtilsConfig.addDefaults(def, conf);
ServerUtilsConfig.removeOldKeys(def, conf);
try {
conf.save();

View file

@ -7,13 +7,13 @@ import net.frankheijden.serverutils.common.entities.ServerUtilsPlugin;
import net.frankheijden.serverutils.common.providers.ResourceProvider;
/**
* A class which provides functionality for loading and setting defaults of Yaml Configurations.
* A class which provides functionality for loading and setting defaults of Configurations.
*/
public class YamlResource {
public class ServerUtilsResource {
private static final ServerUtilsPlugin plugin = ServerUtilsApp.getPlugin();
private final YamlConfig config;
private final ServerUtilsConfig config;
/**
* Creates a new YamlResource instance.
@ -21,18 +21,18 @@ public class YamlResource {
* @param fileName The destination file.
* @param resource The resource from the jar file.
*/
public YamlResource(String fileName, String resource) {
public ServerUtilsResource(String fileName, String resource) {
ResourceProvider provider = plugin.getResourceProvider();
InputStream is = provider.getResource(resource);
File file = plugin.copyResourceIfNotExists(fileName, resource);
config = YamlConfig.init(provider.load(is), provider.load(file));
config = ServerUtilsConfig.init(provider.load(is), provider.load(file));
}
/**
* Retrieves the YamlConfig of this resource.
* @return The YamlConfig.
*/
public YamlConfig getConfig() {
public ServerUtilsConfig getConfig() {
return config;
}
}

View file

@ -2,13 +2,13 @@ package net.frankheijden.serverutils.common.providers;
import java.io.File;
import java.io.InputStream;
import net.frankheijden.serverutils.common.config.YamlConfig;
import net.frankheijden.serverutils.common.config.ServerUtilsConfig;
public interface ResourceProvider {
InputStream getResource(String resource);
YamlConfig load(InputStream is);
ServerUtilsConfig load(InputStream is);
YamlConfig load(File file);
ServerUtilsConfig load(File file);
}

View file

@ -12,7 +12,7 @@ import java.util.logging.Level;
import net.frankheijden.serverutils.common.ServerUtilsApp;
import net.frankheijden.serverutils.common.config.Config;
import net.frankheijden.serverutils.common.config.Messenger;
import net.frankheijden.serverutils.common.config.YamlConfig;
import net.frankheijden.serverutils.common.config.ServerUtilsConfig;
import net.frankheijden.serverutils.common.entities.LoadResult;
import net.frankheijden.serverutils.common.entities.Result;
import net.frankheijden.serverutils.common.entities.ServerCommandSender;
@ -27,7 +27,7 @@ import net.frankheijden.serverutilsupdater.common.Updater;
public class UpdateCheckerTask implements Runnable {
private static final ServerUtilsPlugin plugin = ServerUtilsApp.getPlugin();
private static final YamlConfig config = Config.getInstance().getConfig();
private static final ServerUtilsConfig config = Config.getInstance().getConfig();
private final ServerCommandSender sender;
private final boolean download;