From 38c11882f63c6cabbcd3554d9f176eb1b8f5fffa Mon Sep 17 00:00:00 2001 From: 4drian3d Date: Sun, 13 Feb 2022 18:20:30 -0500 Subject: [PATCH] Add "Ensure first server is an Auth Server" config - Add isAuthServer(String) to API - Add support for HEX colors in messages --- .../proxy/AuthMeVelocityPlugin.java | 2 +- .../proxy/AuthmeVelocityAPI.java | 9 ++++ .../proxy/config/AuthMeConfig.java | 20 +++++++ .../proxy/config/ConfigUtils.java | 5 +- .../proxy/listener/ProxyListener.java | 53 +++++++++++++++++-- proxy/src/main/resources/config.toml | 9 +++- 6 files changed, 89 insertions(+), 9 deletions(-) diff --git a/proxy/src/main/java/com/glyart/authmevelocity/proxy/AuthMeVelocityPlugin.java b/proxy/src/main/java/com/glyart/authmevelocity/proxy/AuthMeVelocityPlugin.java index 9133f8e..67c4603 100644 --- a/proxy/src/main/java/com/glyart/authmevelocity/proxy/AuthMeVelocityPlugin.java +++ b/proxy/src/main/java/com/glyart/authmevelocity/proxy/AuthMeVelocityPlugin.java @@ -49,7 +49,7 @@ public class AuthMeVelocityPlugin { AuthMeConfig config = Objects.requireNonNull(new AuthMeConfig(toml), "configuration cannot be null"); this.api = new AuthmeVelocityAPI(this, config); proxy.getChannelRegistrar().register(MinecraftChannelIdentifier.create("authmevelocity", "main")); - proxy.getEventManager().register(this, new ProxyListener(config, api)); + proxy.getEventManager().register(this, new ProxyListener(config, api, logger, proxy)); proxy.getEventManager().register(this, new PluginMessageListener(proxy, logger, config, api)); if(proxy.getPluginManager().isLoaded("fastlogin")){ diff --git a/proxy/src/main/java/com/glyart/authmevelocity/proxy/AuthmeVelocityAPI.java b/proxy/src/main/java/com/glyart/authmevelocity/proxy/AuthmeVelocityAPI.java index 73f79c2..efb3182 100644 --- a/proxy/src/main/java/com/glyart/authmevelocity/proxy/AuthmeVelocityAPI.java +++ b/proxy/src/main/java/com/glyart/authmevelocity/proxy/AuthmeVelocityAPI.java @@ -86,4 +86,13 @@ public final class AuthmeVelocityAPI { public boolean isAuthServer(@NotNull ServerConnection connection){ return config.getAuthServers().contains(connection.getServerInfo().getName()); } + + /** + * Checks if a string is an name of an auth server + * @param server the server name + * @return if the server is an auth serverr + */ + public boolean isAuthServer(@NotNull String server){ + return config.getAuthServers().contains(server); + } } 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 5d712e5..8528e51 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 @@ -9,11 +9,13 @@ public final class AuthMeConfig { private final List authServers; private final ServerOnLogin serverOnLogin; private final Commands commands; + private final EnsureAuthServer ensure; public AuthMeConfig(Toml toml){ this.authServers = toml.getList("authServers"); this.serverOnLogin = toml.getTable("SendOnLogin").to(ServerOnLogin.class); this.commands = toml.getTable("Commands").to(Commands.class); + this.ensure = toml.getTable("EnsureAuthServer").to(EnsureAuthServer.class); } public static class ServerOnLogin { @@ -42,6 +44,20 @@ public final class AuthMeConfig { } } + public static class EnsureAuthServer { + private boolean ensureFirstServerIsAuthServer; + private String disconnectMessage; + + public boolean ensureAuthServer(){ + return this.ensureFirstServerIsAuthServer; + } + + public String getDisconnectMessage(){ + return this.disconnectMessage; + } + + } + public Commands getCommandsConfig(){ return this.commands; } @@ -50,6 +66,10 @@ public final class AuthMeConfig { return this.serverOnLogin; } + public EnsureAuthServer getEnsureOptions(){ + return this.ensure; + } + public List getAuthServers(){ return this.authServers; } 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 919ca40..5090022 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 @@ -5,12 +5,11 @@ import com.velocitypowered.api.proxy.Player; import net.kyori.adventure.text.serializer.legacy.LegacyComponentSerializer; public final class ConfigUtils { + public static final LegacyComponentSerializer SERIALIZER = LegacyComponentSerializer.builder().character('&').hexColors().build(); public static void sendBlockedMessage(Player player, AuthMeConfig config){ String blockedMessage = config.getCommandsConfig().getBlockedMessage(); if(!blockedMessage.isBlank()){ - player.sendMessage( - LegacyComponentSerializer.legacyAmpersand().deserialize( - blockedMessage)); + player.sendMessage(SERIALIZER.deserialize(blockedMessage)); } } private ConfigUtils(){} diff --git a/proxy/src/main/java/com/glyart/authmevelocity/proxy/listener/ProxyListener.java b/proxy/src/main/java/com/glyart/authmevelocity/proxy/listener/ProxyListener.java index 778d3e8..986bc9b 100644 --- a/proxy/src/main/java/com/glyart/authmevelocity/proxy/listener/ProxyListener.java +++ b/proxy/src/main/java/com/glyart/authmevelocity/proxy/listener/ProxyListener.java @@ -1,28 +1,42 @@ package com.glyart.authmevelocity.proxy.listener; +import java.util.Optional; + +import com.glyart.authmevelocity.proxy.AuthMeVelocityPlugin; import com.glyart.authmevelocity.proxy.AuthmeVelocityAPI; import com.glyart.authmevelocity.proxy.config.AuthMeConfig; import com.glyart.authmevelocity.proxy.config.ConfigUtils; import com.glyart.authmevelocity.proxy.utils.AuthmeUtils; import com.velocitypowered.api.event.Continuation; import com.velocitypowered.api.event.EventTask; +import com.velocitypowered.api.event.PostOrder; import com.velocitypowered.api.event.Subscribe; import com.velocitypowered.api.event.command.CommandExecuteEvent; import com.velocitypowered.api.event.connection.DisconnectEvent; import com.velocitypowered.api.event.player.PlayerChatEvent; +import com.velocitypowered.api.event.player.PlayerChooseInitialServerEvent; import com.velocitypowered.api.event.player.ServerPreConnectEvent; import com.velocitypowered.api.event.player.TabCompleteEvent; import com.velocitypowered.api.proxy.Player; +import com.velocitypowered.api.proxy.ProxyServer; +import com.velocitypowered.api.proxy.server.RegisteredServer; import org.jetbrains.annotations.NotNull; +import org.jetbrains.annotations.Nullable; +import org.slf4j.Logger; public final class ProxyListener { private final AuthMeConfig config; private final AuthmeVelocityAPI api; + private final ProxyServer proxy; + private final Logger logger; - public ProxyListener(@NotNull AuthMeConfig config, AuthmeVelocityAPI api) { + public ProxyListener(@NotNull AuthMeConfig config, AuthmeVelocityAPI api, Logger logger, ProxyServer proxy) { this.config = config; this.api = api; + this.plugin = plugin; + this.logger = logger; + this.proxy = proxy; } @Subscribe @@ -30,7 +44,7 @@ public final class ProxyListener { api.removePlayer(event.getPlayer()); } - @Subscribe + @Subscribe(order = PostOrder.FIRST) public void onCommandExecute(final CommandExecuteEvent event, Continuation continuation) { if (!(event.getCommandSource() instanceof Player)){ continuation.resume(); @@ -57,7 +71,7 @@ public final class ProxyListener { continuation.resume(); } - @Subscribe + @Subscribe(order = PostOrder.FIRST) public void onPlayerChat(final PlayerChatEvent event) { if (!api.isLogged(event.getPlayer())) { event.setResult(PlayerChatEvent.ChatResult.denied()); @@ -79,11 +93,42 @@ public final class ProxyListener { continuation.resume(); } - @Subscribe + @Subscribe(order = PostOrder.FIRST) public EventTask onTabComplete(TabCompleteEvent event){ if (!api.isLogged(event.getPlayer())){ return EventTask.async(() -> event.getSuggestions().clear()); } return null; } + + @Subscribe(order = PostOrder.LATE) + public void onInitialServer(PlayerChooseInitialServerEvent event, Continuation continuation){ + if(!config.getEnsureOptions().ensureAuthServer()){ + continuation.resume(); + return; + } + Optional optionalSV = event.getInitialServer(); + if(optionalSV.isPresent() && api.isAuthServer(optionalSV.get())){ + continuation.resume(); + return; + } + RegisteredServer server = getAvailableServer(); + if(server == null) { + continuation.resume(); + logger.error("Cannot send the player {} to an auth server", event.getPlayer().getUsername()); + event.getPlayer().disconnect(ConfigUtils.SERIALIZER.deserialize(config.getEnsureOptions().getDisconnectMessage())); + return; + } + event.setInitialServer(server); + continuation.resume(); + + } + + private @Nullable RegisteredServer getAvailableServer(){ + for(String sv : config.getAuthServers()){ + Optional opt = proxy.getServer(sv); + if(opt.isPresent()) return opt.get(); + } + return null; + } } diff --git a/proxy/src/main/resources/config.toml b/proxy/src/main/resources/config.toml index 6b4556c..3033678 100644 --- a/proxy/src/main/resources/config.toml +++ b/proxy/src/main/resources/config.toml @@ -19,4 +19,11 @@ authServers = ["auth1", "auth2"] # Sets the message to send in case a non-logged-in player executes an unauthorized command # To deactivate the message, leave it empty - blockedCommandMessage = "&4You cannot execute commands if you are not logged in yet" \ No newline at end of file + blockedCommandMessage = "&4You cannot execute commands if you are not logged in yet" + +[EnsureAuthServer] + # Ensure that the first server to which players connect is an auth server + ensureFirstServerIsAuthServer = false + + # Message to be sent to the player in case no auth server is available + disconnectMessage = "&4You could not connect to a login server, please try again later" \ No newline at end of file