From c4b0d7dbd647166b2fc7332ec2e5ad2cfde4af1b Mon Sep 17 00:00:00 2001 From: 4drian3d Date: Sun, 20 Mar 2022 19:02:52 +0000 Subject: [PATCH] Config load improvements --- .../proxy/config/AuthMeConfig.java | 24 +++++++++++-------- .../proxy/config/ConfigUtils.java | 20 ++++++++++++++-- 2 files changed, 32 insertions(+), 12 deletions(-) diff --git a/proxy/src/main/java/com/glyart/authmevelocity/proxy/config/AuthMeConfig.java b/proxy/src/main/java/com/glyart/authmevelocity/proxy/config/AuthMeConfig.java index 01eb39a..adde8ca 100644 --- a/proxy/src/main/java/com/glyart/authmevelocity/proxy/config/AuthMeConfig.java +++ b/proxy/src/main/java/com/glyart/authmevelocity/proxy/config/AuthMeConfig.java @@ -14,15 +14,19 @@ public final class AuthMeConfig { private final EnsureAuthServer ensure; public AuthMeConfig(@NotNull Toml toml){ - this.authServers = toml.getList("authServers", List.of("auth1", "auth2")); - this.serverOnLogin = ConfigUtils.getOrElse(toml, "SendOnLogin", new ServerOnLogin(false, List.of("lobby1", "lobby2"))); - this.commands = ConfigUtils.getOrElse(toml, "Commands", new Commands(Set.of("login", "register", "l", "reg", "email", "captcha"),"You cannot execute commands if you are not logged in yet")); - this.ensure = ConfigUtils.getOrElse(toml, "EnsureAuthServer", new EnsureAuthServer(false, "You could not connect to a login server, please try again later")); + this.authServers = ConfigUtils.listOrElse(toml, "authServers", + () -> List.of("auth1", "auth2")); + this.serverOnLogin = ConfigUtils.getObjectOrElse(toml, "SendOnLogin", ServerOnLogin.class, + () -> 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"), "You cannot execute commands if you are not logged in yet")); + this.ensure = ConfigUtils.getObjectOrElse(toml, "EnsureAuthServer", EnsureAuthServer.class, + () -> new EnsureAuthServer(false, "You could not connect to a login server, please try again later")); } public static class ServerOnLogin { - private boolean sendToServerOnLogin; - private List teleportServers; + private final boolean sendToServerOnLogin; + private final List teleportServers; public ServerOnLogin(boolean sendToServerOnLogin, List teleportServers){ this.sendToServerOnLogin = sendToServerOnLogin; @@ -39,8 +43,8 @@ public final class AuthMeConfig { } public static class Commands { - private Set allowedCommands; - private String blockedCommandMessage; + private final Set allowedCommands; + private final String blockedCommandMessage; public Commands(Set allowedCommands, String blockedCommandMessage){ this.allowedCommands = allowedCommands; @@ -57,8 +61,8 @@ public final class AuthMeConfig { } public static class EnsureAuthServer { - private boolean ensureFirstServerIsAuthServer; - private String disconnectMessage; + private final boolean ensureFirstServerIsAuthServer; + private final String disconnectMessage; public EnsureAuthServer(boolean ensureFirstServerIsAuthServer, String disconnectMessage){ this.ensureFirstServerIsAuthServer = ensureFirstServerIsAuthServer; diff --git a/proxy/src/main/java/com/glyart/authmevelocity/proxy/config/ConfigUtils.java b/proxy/src/main/java/com/glyart/authmevelocity/proxy/config/ConfigUtils.java index 7e4556e..b5097ff 100644 --- a/proxy/src/main/java/com/glyart/authmevelocity/proxy/config/ConfigUtils.java +++ b/proxy/src/main/java/com/glyart/authmevelocity/proxy/config/ConfigUtils.java @@ -1,5 +1,10 @@ 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.velocitypowered.api.proxy.Player; @@ -16,9 +21,20 @@ public final class ConfigUtils { } @SuppressWarnings("unchecked") - static T getOrElse(Toml config, String key, T defaultValue){ + static T getObjectOrElse(Toml config, String key, Supplier defaultValue){ Toml configTable = config.getTable(key); - return configTable == null ? defaultValue : (T)configTable.to(defaultValue.getClass()); + return configTable == null ? defaultValue.get() : (T)configTable.to((Class)((ParameterizedType)defaultValue.getClass() + .getGenericInterfaces()[0]).getActualTypeArguments()[0]); + } + + static T getObjectOrElse(Toml config, String key, Class clazz, Supplier defaultValue){ + Toml configTable = config.getTable(key); + return configTable == null ? defaultValue.get() : configTable.to(clazz); + } + + static List listOrElse(Toml config, String key, Supplier> defaultList){ + List list = config.getList(key); + return list != null ? list : defaultList.get(); } private ConfigUtils(){} }