Use default values if some config value is null

This commit is contained in:
4drian3d 2022-02-18 17:58:47 -05:00
parent c3d33b257b
commit b4c3030a56
2 changed files with 30 additions and 7 deletions

View File

@ -1,7 +1,6 @@
package com.glyart.authmevelocity.proxy.config; package com.glyart.authmevelocity.proxy.config;
import java.util.List; import java.util.List;
import java.util.Objects;
import java.util.Set; import java.util.Set;
import com.moandjiezana.toml.Toml; import com.moandjiezana.toml.Toml;
@ -15,16 +14,21 @@ public final class AuthMeConfig {
private final EnsureAuthServer ensure; private final EnsureAuthServer ensure;
public AuthMeConfig(@NotNull Toml toml){ public AuthMeConfig(@NotNull Toml toml){
this.authServers = Objects.requireNonNull(toml.getList("authServers"), "the list of auth servers is not available, please check your configuration for any failure"); this.authServers = toml.getList("authServers", List.of("auth1", "auth2"));
this.serverOnLogin = Objects.requireNonNull(toml.getTable("SendOnLogin"), "SendOnLogin options are not available, check your configuration").to(ServerOnLogin.class); this.serverOnLogin = ConfigUtils.getOrElse(toml, "SendOnLogin", new ServerOnLogin(false, List.of("lobby1", "lobby2")));
this.commands = Objects.requireNonNull(toml.getTable("Commands"), "Commands options are not available, check your configuration").to(Commands.class); this.commands = ConfigUtils.getOrElse(toml, "Commands", new Commands(Set.of("login", "register", "l", "reg", "email", "captcha"),"&4You cannot execute commands if you are not logged in yet"));
this.ensure = Objects.requireNonNull(toml.getTable("EnsureAuthServer"), "EnsureAuthServer options are not available, check your configuration").to(EnsureAuthServer.class); this.ensure = ConfigUtils.getOrElse(toml, "EnsureAuthServer", new EnsureAuthServer(false, "&4You could not connect to a login server, please try again later"));
} }
public static class ServerOnLogin { public static class ServerOnLogin {
private boolean sendToServerOnLogin; private boolean sendToServerOnLogin;
private List<String> teleportServers; private List<String> teleportServers;
public ServerOnLogin(boolean sendToServerOnLogin, List<String> teleportServers){
this.sendToServerOnLogin = sendToServerOnLogin;
this.teleportServers = teleportServers;
}
public boolean sendToServer(){ public boolean sendToServer(){
return this.sendToServerOnLogin; return this.sendToServerOnLogin;
} }
@ -38,6 +42,11 @@ public final class AuthMeConfig {
private Set<String> allowedCommands; private Set<String> allowedCommands;
private String blockedCommandMessage; private String blockedCommandMessage;
public Commands(Set<String> allowedCommands, String blockedCommandMessage){
this.allowedCommands = allowedCommands;
this.blockedCommandMessage = blockedCommandMessage;
}
public @NotNull Set<String> getAllowedCommands(){ public @NotNull Set<String> getAllowedCommands(){
return this.allowedCommands; return this.allowedCommands;
} }
@ -51,6 +60,11 @@ public final class AuthMeConfig {
private boolean ensureFirstServerIsAuthServer; private boolean ensureFirstServerIsAuthServer;
private String disconnectMessage; private String disconnectMessage;
public EnsureAuthServer(boolean ensureFirstServerIsAuthServer, String disconnectMessage){
this.ensureFirstServerIsAuthServer = ensureFirstServerIsAuthServer;
this.disconnectMessage = disconnectMessage;
}
public boolean ensureAuthServer(){ public boolean ensureAuthServer(){
return this.ensureFirstServerIsAuthServer; return this.ensureFirstServerIsAuthServer;
} }

View File

@ -1,16 +1,25 @@
package com.glyart.authmevelocity.proxy.config; package com.glyart.authmevelocity.proxy.config;
import com.moandjiezana.toml.Toml;
import com.velocitypowered.api.proxy.Player; import com.velocitypowered.api.proxy.Player;
import net.kyori.adventure.text.serializer.legacy.LegacyComponentSerializer; import net.kyori.adventure.text.serializer.legacy.LegacyComponentSerializer;
public final class ConfigUtils { public final class ConfigUtils {
public static final LegacyComponentSerializer SERIALIZER = LegacyComponentSerializer.builder().character('&').hexColors().build(); public static final LegacyComponentSerializer SERIALIZER = LegacyComponentSerializer.builder()
.character('&').hexColors().hexCharacter('#').build();
public static void sendBlockedMessage(Player player, AuthMeConfig config){ public static void sendBlockedMessage(Player player, AuthMeConfig config){
String blockedMessage = config.getCommandsConfig().getBlockedMessage(); String blockedMessage = config.getCommandsConfig().getBlockedMessage();
if(!blockedMessage.isBlank()){ if(!blockedMessage.isBlank()){
player.sendMessage(SERIALIZER.deserialize(blockedMessage)); player.sendMessage(SERIALIZER.deserialize(blockedMessage));
} }
} }
@SuppressWarnings("unchecked")
static <T extends Object>T getOrElse(Toml config, String key, T defaultValue){
Toml configTable = config.getTable(key);
return configTable == null ? defaultValue : (T)configTable.to(defaultValue.getClass());
}
private ConfigUtils(){} private ConfigUtils(){}
} }