From 1c7c50675ac5d56332a84ae7bf91f3c602a18cd0 Mon Sep 17 00:00:00 2001 From: Adrian3d04 Date: Sat, 13 Aug 2022 14:25:02 +0000 Subject: [PATCH] feat(velocity): Implement Velocity debug mode --- .../common/configuration/Loader.java | 2 -- .../paper/AuthMeVelocityPlugin.java | 2 +- .../velocity/AuthMeVelocityPlugin.java | 9 ++++++ .../velocity/listener/ConnectListener.java | 12 ++++++-- .../velocity/listener/FastLoginListener.java | 12 ++++---- .../listener/PluginMessageListener.java | 29 ++++++++++++++----- .../velocity/listener/ProxyListener.java | 12 ++++++-- .../velocity/utils/AuthmeUtils.java | 6 ++-- 8 files changed, 59 insertions(+), 25 deletions(-) diff --git a/common/src/main/java/me/adrianed/authmevelocity/common/configuration/Loader.java b/common/src/main/java/me/adrianed/authmevelocity/common/configuration/Loader.java index ebfc68f..e87cec3 100644 --- a/common/src/main/java/me/adrianed/authmevelocity/common/configuration/Loader.java +++ b/common/src/main/java/me/adrianed/authmevelocity/common/configuration/Loader.java @@ -7,8 +7,6 @@ import org.slf4j.Logger; import org.spongepowered.configurate.CommentedConfigurationNode; import org.spongepowered.configurate.ConfigurateException; import org.spongepowered.configurate.hocon.HoconConfigurationLoader; -import org.spongepowered.configurate.objectmapping.ConfigSerializable; -import org.spongepowered.configurate.objectmapping.meta.Comment; public final class Loader { diff --git a/paper/src/main/java/me/adrianed/authmevelocity/paper/AuthMeVelocityPlugin.java b/paper/src/main/java/me/adrianed/authmevelocity/paper/AuthMeVelocityPlugin.java index dc6ee22..2446e5d 100644 --- a/paper/src/main/java/me/adrianed/authmevelocity/paper/AuthMeVelocityPlugin.java +++ b/paper/src/main/java/me/adrianed/authmevelocity/paper/AuthMeVelocityPlugin.java @@ -69,7 +69,7 @@ public final class AuthMeVelocityPlugin extends JavaPlugin { public void logDebug(String debug) { if (config.get().debug()) { - getSLF4JLogger().info("[DEBUG] "+debug); + getSLF4JLogger().info("[DEBUG] {}", debug); } } } diff --git a/velocity/src/main/java/me/adrianed/authmevelocity/velocity/AuthMeVelocityPlugin.java b/velocity/src/main/java/me/adrianed/authmevelocity/velocity/AuthMeVelocityPlugin.java index baa5775..73455d6 100644 --- a/velocity/src/main/java/me/adrianed/authmevelocity/velocity/AuthMeVelocityPlugin.java +++ b/velocity/src/main/java/me/adrianed/authmevelocity/velocity/AuthMeVelocityPlugin.java @@ -77,6 +77,7 @@ public final class AuthMeVelocityPlugin implements AuthMeVelocityAPI { new VelocityLibraryManager<>( logger, pluginDirectory, proxy.getPluginManager(), this)); libraries.loadLibraries(); + logDebug("Loaded plugin libraries"); this.config = Loader.loadMainConfig(pluginDirectory, ProxyConfiguration.class, logger); @@ -90,10 +91,12 @@ public final class AuthMeVelocityPlugin implements AuthMeVelocityAPI { proxy.getEventManager().register(this, listener)); if (proxy.getPluginManager().isLoaded("fastlogin")) { + logDebug("Register FastLogin compatibility"); proxy.getEventManager().register(this, new FastLoginListener(proxy, this)); } if (proxy.getPluginManager().isLoaded("miniplaceholders")) { + logDebug("Register MiniPlaceholders compatibility"); AuthMePlaceholders.getExpansion(this).register(); } @@ -166,4 +169,10 @@ public final class AuthMeVelocityPlugin implements AuthMeVelocityAPI { public boolean isAuthServer(String server){ return config.get().authServers().contains(server); } + + public void logDebug(String msg) { + if (config.get().debug()) { + logger.info("[DEBUG] {}", msg); + } + } } diff --git a/velocity/src/main/java/me/adrianed/authmevelocity/velocity/listener/ConnectListener.java b/velocity/src/main/java/me/adrianed/authmevelocity/velocity/listener/ConnectListener.java index 39e3602..4837bae 100644 --- a/velocity/src/main/java/me/adrianed/authmevelocity/velocity/listener/ConnectListener.java +++ b/velocity/src/main/java/me/adrianed/authmevelocity/velocity/listener/ConnectListener.java @@ -34,12 +34,14 @@ public class ConnectListener { public void onInitialServer(PlayerChooseInitialServerEvent event, Continuation continuation){ if(!plugin.config().get().ensureAuthServer().ensureFirstServerIsAuthServer()) { continuation.resume(); + plugin.logDebug("PlayerChooseInitialServerEvent | Not enabled"); return; } Optional optionalSV = event.getInitialServer(); if(optionalSV.isPresent() && plugin.isAuthServer(optionalSV.get())){ continuation.resume(); + plugin.logDebug("PlayerChooseInitialServerEvent | Player is in auth server"); return; } @@ -48,19 +50,22 @@ public class ConnectListener { event.setInitialServer(server); continuation.resume(); if (server == null) { + plugin.logDebug("PlayerChooseInitialServerEvent | Null server"); logger.error("Cannot send the player {} to an auth server", event.getPlayer().getUsername()); } } @Subscribe public void onServerPreConnect(ServerPreConnectEvent event, Continuation continuation) { - if (!event.getResult().isAllowed() && plugin.isLogged(event.getPlayer())) { + if (!event.getResult().isAllowed() || plugin.isLogged(event.getPlayer())) { + plugin.logDebug("ServerPreConnectEvent | Not allowed or player not logged"); continuation.resume(); return; } // this should be present, "event.getResult().isAllowed()" is the "isPresent" check - if(!plugin.isAuthServer(event.getResult().getServer().get())) { + if(!plugin.isAuthServer(event.getResult().getServer().orElse(null))) { + plugin.logDebug("ServerPreConnectEvent | Server is not an auth server"); event.setResult(ServerPreConnectEvent.ServerResult.denied()); } continuation.resume(); @@ -70,7 +75,8 @@ public class ConnectListener { public void onServerPostConnect(ServerPostConnectEvent event) { final Player player = event.getPlayer(); if (plugin.isLogged(player) && plugin.isInAuthServer(player)) { - ByteArrayDataOutput buf = ByteStreams.newDataOutput(); + plugin.logDebug("ServerPostConnectEvent | Already logged player and connected to an Auth server"); + final ByteArrayDataOutput buf = ByteStreams.newDataOutput(); buf.writeUTF("LOGIN"); player.getCurrentServer().ifPresent(sv -> sv.sendPluginMessage(AuthMeVelocityPlugin.AUTHMEVELOCITY_CHANNEL, buf.toByteArray())); diff --git a/velocity/src/main/java/me/adrianed/authmevelocity/velocity/listener/FastLoginListener.java b/velocity/src/main/java/me/adrianed/authmevelocity/velocity/listener/FastLoginListener.java index ce64a57..46b2fd9 100644 --- a/velocity/src/main/java/me/adrianed/authmevelocity/velocity/listener/FastLoginListener.java +++ b/velocity/src/main/java/me/adrianed/authmevelocity/velocity/listener/FastLoginListener.java @@ -4,18 +4,18 @@ import com.github.games647.fastlogin.velocity.event.VelocityFastLoginAutoLoginEv import com.velocitypowered.api.event.Subscribe; import com.velocitypowered.api.proxy.ProxyServer; -import me.adrianed.authmevelocity.api.velocity.AuthMeVelocityAPI; +import me.adrianed.authmevelocity.velocity.AuthMeVelocityPlugin; public class FastLoginListener { private final ProxyServer proxy; - private final AuthMeVelocityAPI api; - public FastLoginListener(ProxyServer proxy, AuthMeVelocityAPI api){ + private final AuthMeVelocityPlugin plugin; + public FastLoginListener(ProxyServer proxy, AuthMeVelocityPlugin plugin){ this.proxy = proxy; - this.api = api; + this.plugin = plugin; } @Subscribe public void onAutoLogin(VelocityFastLoginAutoLoginEvent event){ - proxy.getPlayer(event.getProfile().getName()) - .ifPresent(api::addPlayer); + plugin.logDebug("VelocityFastLoginAutoLoginEvent | Attempt to auto register player"); + proxy.getPlayer(event.getProfile().getName()).ifPresent(plugin::addPlayer); } } diff --git a/velocity/src/main/java/me/adrianed/authmevelocity/velocity/listener/PluginMessageListener.java b/velocity/src/main/java/me/adrianed/authmevelocity/velocity/listener/PluginMessageListener.java index 90e09ff..8173f35 100644 --- a/velocity/src/main/java/me/adrianed/authmevelocity/velocity/listener/PluginMessageListener.java +++ b/velocity/src/main/java/me/adrianed/authmevelocity/velocity/listener/PluginMessageListener.java @@ -43,14 +43,16 @@ public class PluginMessageListener { @Subscribe public void onPluginMessage(final PluginMessageEvent event, Continuation continuation) { - final boolean cancelled = !(event.getSource() instanceof ServerConnection) + final boolean cancelled = !event.getResult().isAllowed() + ||!(event.getSource() instanceof ServerConnection) || !event.getIdentifier().equals(AuthMeVelocityPlugin.AUTHMEVELOCITY_CHANNEL); if (cancelled) { continuation.resume(); + plugin.logDebug("PluginMessageEvent | Not allowed"); return; } - ServerConnection connection = ((ServerConnection)event.getSource()); + final ServerConnection connection = ((ServerConnection)event.getSource()); event.setResult(PluginMessageEvent.ForwardResult.handled()); @@ -63,26 +65,39 @@ public class PluginMessageListener { switch (type) { case LOGIN -> { + plugin.logDebug("PluginMessageEvent | Login type"); if (player != null && plugin.addPlayer(player)){ proxy.getEventManager().fireAndForget(new ProxyLoginEvent(player)); this.createServerConnectionRequest(player, connection); + plugin.logDebug("PluginMessageEvent | Player not null"); } } case LOGOUT -> { + plugin.logDebug("PluginMessageEvent | Logout type"); if (player != null && plugin.removePlayer(player)){ proxy.getEventManager().fireAndForget(new ProxyLogoutEvent(player)); + plugin.logDebug("PluginMessageEvent | Player not null"); } } case REGISTER -> { - if (player != null) + plugin.logDebug("PluginMessageEvent | Register"); + if (player != null) { proxy.getEventManager().fireAndForget(new ProxyRegisterEvent(player)); + plugin.logDebug("PluginMessageEvent | Player not null"); + } } case UNREGISTER -> { - if(player != null) + plugin.logDebug("PluginMessageEvent | Unregister type"); + if(player != null) { + plugin.logDebug("PluginMessageEvent | Player not null"); proxy.getEventManager().fireAndForget(new ProxyUnregisterEvent(player)); + } } - case FORCE_UNREGISTER -> + case FORCE_UNREGISTER -> { proxy.getEventManager().fireAndForget(new ProxyForcedUnregisterEvent(player)); + plugin.logDebug("PluginMessageEvent | Forced Unregister type"); + } + } continuation.resume(); } @@ -100,14 +115,14 @@ public class PluginMessageListener { .thenApply(PreSendOnLoginEvent::getResult) .thenApply(GenericResult::isAllowed) .thenAcceptAsync(allowed -> { - if (!allowed) { + if (!allowed.booleanValue()) { return; } player.createConnectionRequest(server) .connect() .thenApply(Result::isSuccessful) .thenAcceptAsync(result -> { - if(!result) { + if(!result.booleanValue()) { logger.info("Unable to connect the player {} to the server {}", player.getUsername(), server.getServerInfo().getName()); diff --git a/velocity/src/main/java/me/adrianed/authmevelocity/velocity/listener/ProxyListener.java b/velocity/src/main/java/me/adrianed/authmevelocity/velocity/listener/ProxyListener.java index dc6d2b7..e8aa8dc 100644 --- a/velocity/src/main/java/me/adrianed/authmevelocity/velocity/listener/ProxyListener.java +++ b/velocity/src/main/java/me/adrianed/authmevelocity/velocity/listener/ProxyListener.java @@ -28,25 +28,28 @@ public final class ProxyListener { @Subscribe(order = PostOrder.FIRST) public void onCommandExecute(final CommandExecuteEvent event, Continuation continuation) { - if (!(event.getCommandSource() instanceof Player)){ + if (!(event.getCommandSource() instanceof final Player player)){ + plugin.logDebug("CommandExecuteEvent | CommandSource is not a player"); continuation.resume(); return; } - Player player = (Player)event.getCommandSource(); - if (plugin.isLogged(player)) { + plugin.logDebug("CommandExecuteEvent | Player is not logged"); continuation.resume(); return; } if (plugin.isInAuthServer(player)) { + plugin.logDebug("CommandExecuteEvent | Player is in Auth Server"); String command = AuthmeUtils.getFirstArgument(event.getCommand()); if (!plugin.config().get().commands().allowedCommands().contains(command)) { + plugin.logDebug("CommandExecuteEvent | Player executed an blocked command"); sendBlockedMessage(player); event.setResult(CommandExecuteEvent.CommandResult.denied()); } } else { + plugin.logDebug("CommandExecuteEven | Player is not in auth server"); sendBlockedMessage(player); event.setResult(CommandExecuteEvent.CommandResult.denied()); } @@ -56,6 +59,7 @@ public final class ProxyListener { @Subscribe(order = PostOrder.FIRST) public void onPlayerChat(final PlayerChatEvent event) { if (plugin.isNotLogged(event.getPlayer())) { + plugin.logDebug("PlayerChatEvent | Player is not logged"); event.setResult(PlayerChatEvent.ChatResult.denied()); } } @@ -63,6 +67,7 @@ public final class ProxyListener { @Subscribe(order = PostOrder.FIRST) public void onTabComplete(TabCompleteEvent event){ if (plugin.isLogged(event.getPlayer())) { + plugin.logDebug("TabCompleteEvent | Player is already logged"); return; } @@ -73,6 +78,7 @@ public final class ProxyListener { } } + plugin.logDebug("TabCompleteEvent | Not allowed tabcompletion"); event.getSuggestions().clear(); } diff --git a/velocity/src/main/java/me/adrianed/authmevelocity/velocity/utils/AuthmeUtils.java b/velocity/src/main/java/me/adrianed/authmevelocity/velocity/utils/AuthmeUtils.java index f2ebaae..3cdb52b 100644 --- a/velocity/src/main/java/me/adrianed/authmevelocity/velocity/utils/AuthmeUtils.java +++ b/velocity/src/main/java/me/adrianed/authmevelocity/velocity/utils/AuthmeUtils.java @@ -11,12 +11,12 @@ public class AuthmeUtils { * @param string the string * @return the first argument */ - public static @NotNull String getFirstArgument(@NotNull String string){ - int index = Objects.requireNonNull(string).indexOf(" "); + public static @NotNull String getFirstArgument(final @NotNull String string){ + final int index = Objects.requireNonNull(string).indexOf(' '); if (index == -1) { return string; } return string.substring(0, index); } - private AuthmeUtils(){} + private AuthmeUtils() {} }