Config load improvements

This commit is contained in:
4drian3d 2022-03-20 19:02:52 +00:00
parent a54400f21c
commit c4b0d7dbd6
2 changed files with 32 additions and 12 deletions

View File

@ -14,15 +14,19 @@ public final class AuthMeConfig {
private final EnsureAuthServer ensure; private final EnsureAuthServer ensure;
public AuthMeConfig(@NotNull Toml toml){ public AuthMeConfig(@NotNull Toml toml){
this.authServers = toml.getList("authServers", List.of("auth1", "auth2")); this.authServers = ConfigUtils.listOrElse(toml, "authServers",
this.serverOnLogin = ConfigUtils.getOrElse(toml, "SendOnLogin", new ServerOnLogin(false, List.of("lobby1", "lobby2"))); () -> List.of("auth1", "auth2"));
this.commands = ConfigUtils.getOrElse(toml, "Commands", new Commands(Set.of("login", "register", "l", "reg", "email", "captcha"),"<red>You cannot execute commands if you are not logged in yet")); this.serverOnLogin = ConfigUtils.getObjectOrElse(toml, "SendOnLogin", ServerOnLogin.class,
this.ensure = ConfigUtils.getOrElse(toml, "EnsureAuthServer", new EnsureAuthServer(false, "<red>You could not connect to a login server, please try again later")); () -> new ServerOnLogin(false, List.of("lobby1", "lobby2")));
this.commands = ConfigUtils.getObjectOrElse(toml, "Commands", Commands.class,
() -> new Commands(Set.of("login", "register", "l", "reg", "email", "captcha"), "<red>You cannot execute commands if you are not logged in yet"));
this.ensure = ConfigUtils.getObjectOrElse(toml, "EnsureAuthServer", EnsureAuthServer.class,
() -> new EnsureAuthServer(false, "<red>You could not connect to a login server, please try again later"));
} }
public static class ServerOnLogin { public static class ServerOnLogin {
private boolean sendToServerOnLogin; private final boolean sendToServerOnLogin;
private List<String> teleportServers; private final List<String> teleportServers;
public ServerOnLogin(boolean sendToServerOnLogin, List<String> teleportServers){ public ServerOnLogin(boolean sendToServerOnLogin, List<String> teleportServers){
this.sendToServerOnLogin = sendToServerOnLogin; this.sendToServerOnLogin = sendToServerOnLogin;
@ -39,8 +43,8 @@ public final class AuthMeConfig {
} }
public static class Commands { public static class Commands {
private Set<String> allowedCommands; private final Set<String> allowedCommands;
private String blockedCommandMessage; private final String blockedCommandMessage;
public Commands(Set<String> allowedCommands, String blockedCommandMessage){ public Commands(Set<String> allowedCommands, String blockedCommandMessage){
this.allowedCommands = allowedCommands; this.allowedCommands = allowedCommands;
@ -57,8 +61,8 @@ public final class AuthMeConfig {
} }
public static class EnsureAuthServer { public static class EnsureAuthServer {
private boolean ensureFirstServerIsAuthServer; private final boolean ensureFirstServerIsAuthServer;
private String disconnectMessage; private final String disconnectMessage;
public EnsureAuthServer(boolean ensureFirstServerIsAuthServer, String disconnectMessage){ public EnsureAuthServer(boolean ensureFirstServerIsAuthServer, String disconnectMessage){
this.ensureFirstServerIsAuthServer = ensureFirstServerIsAuthServer; this.ensureFirstServerIsAuthServer = ensureFirstServerIsAuthServer;

View File

@ -1,5 +1,10 @@
package com.glyart.authmevelocity.proxy.config; package com.glyart.authmevelocity.proxy.config;
import java.util.List;
import java.util.function.Supplier;
import java.lang.reflect.ParameterizedType;
import com.moandjiezana.toml.Toml; import com.moandjiezana.toml.Toml;
import com.velocitypowered.api.proxy.Player; import com.velocitypowered.api.proxy.Player;
@ -16,9 +21,20 @@ public final class ConfigUtils {
} }
@SuppressWarnings("unchecked") @SuppressWarnings("unchecked")
static <T>T getOrElse(Toml config, String key, T defaultValue){ static <T>T getObjectOrElse(Toml config, String key, Supplier<T> defaultValue){
Toml configTable = config.getTable(key); Toml configTable = config.getTable(key);
return configTable == null ? defaultValue : (T)configTable.to(defaultValue.getClass()); return configTable == null ? defaultValue.get() : (T)configTable.to((Class<T>)((ParameterizedType)defaultValue.getClass()
.getGenericInterfaces()[0]).getActualTypeArguments()[0]);
}
static <T>T getObjectOrElse(Toml config, String key, Class<T> clazz, Supplier<T> defaultValue){
Toml configTable = config.getTable(key);
return configTable == null ? defaultValue.get() : configTable.to(clazz);
}
static List<String> listOrElse(Toml config, String key, Supplier<List<String>> defaultList){
List<String> list = config.getList(key);
return list != null ? list : defaultList.get();
} }
private ConfigUtils(){} private ConfigUtils(){}
} }