Removed LegacySerializer

- Use a supplier instead of create a new object on config load
This commit is contained in:
4drian3d 2022-03-18 16:55:57 +00:00
parent b2e55e0bae
commit 8668feb8d0
3 changed files with 9 additions and 20 deletions

View File

@ -15,9 +15,9 @@ public final class AuthMeConfig {
public AuthMeConfig(@NotNull Toml toml){ public AuthMeConfig(@NotNull Toml toml){
this.authServers = toml.getList("authServers", List.of("auth1", "auth2")); this.authServers = toml.getList("authServers", List.of("auth1", "auth2"));
this.serverOnLogin = ConfigUtils.getOrElse(toml, "SendOnLogin", new ServerOnLogin(false, List.of("lobby1", "lobby2"))); 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"),"<red>You cannot execute commands if you are not logged in yet")); 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.ensure = ConfigUtils.getOrElse(toml, "EnsureAuthServer", new EnsureAuthServer(false, "<red>You could not connect to a login server, please try again later")); this.ensure = ConfigUtils.getOrElse(toml, "EnsureAuthServer", () -> new EnsureAuthServer(false, "<red>You could not connect to a login server, please try again later"));
} }
public static class ServerOnLogin { public static class ServerOnLogin {

View File

@ -1,35 +1,26 @@
package com.glyart.authmevelocity.proxy.config; package com.glyart.authmevelocity.proxy.config;
import java.util.function.Supplier;
import com.moandjiezana.toml.Toml; import com.moandjiezana.toml.Toml;
import com.velocitypowered.api.proxy.Player; import com.velocitypowered.api.proxy.Player;
import net.kyori.adventure.text.minimessage.MiniMessage; import net.kyori.adventure.text.minimessage.MiniMessage;
import net.kyori.adventure.text.serializer.legacy.LegacyComponentSerializer;
public final class ConfigUtils { public final class ConfigUtils {
/**
* Legacy Serializer
* @deprecated use {@link ConfigUtils#MINIMESSAGE}
*/
@Deprecated(forRemoval = true)
public static final LegacyComponentSerializer SERIALIZER = LegacyComponentSerializer.builder()
.character('&').hexColors().hexCharacter('#').build();
public static final MiniMessage MINIMESSAGE = MiniMessage.miniMessage(); public static final MiniMessage MINIMESSAGE = MiniMessage.miniMessage();
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(blockedMessage.indexOf('&') != -1 player.sendMessage(MINIMESSAGE.deserialize(blockedMessage));
? SERIALIZER.deserialize(blockedMessage)
: MINIMESSAGE.deserialize(blockedMessage)
);
} }
} }
@SuppressWarnings("unchecked") @SuppressWarnings("unchecked")
static <T>T getOrElse(Toml config, String key, T defaultValue){ static <T>T getOrElse(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(defaultValue.getClass());
} }
private ConfigUtils(){} private ConfigUtils(){}
} }

View File

@ -116,9 +116,7 @@ public final class ProxyListener {
continuation.resume(); continuation.resume();
logger.error("Cannot send the player {} to an auth server", event.getPlayer().getUsername()); logger.error("Cannot send the player {} to an auth server", event.getPlayer().getUsername());
String disconnectMessage = config.getEnsureOptions().getDisconnectMessage(); String disconnectMessage = config.getEnsureOptions().getDisconnectMessage();
event.getPlayer().disconnect(disconnectMessage.indexOf('&') != -1 event.getPlayer().disconnect(ConfigUtils.MINIMESSAGE.deserialize(disconnectMessage));
? ConfigUtils.SERIALIZER.deserialize(config.getEnsureOptions().getDisconnectMessage())
: ConfigUtils.MINIMESSAGE.deserialize(disconnectMessage));
return; return;
} }
event.setInitialServer(server); event.setInitialServer(server);