From ec314e54b9d41402884cbe862ac61a5a9dd69a70 Mon Sep 17 00:00:00 2001 From: 4drian3d Date: Sat, 13 Nov 2021 20:51:08 -0500 Subject: [PATCH] API and code improvements --- .../proxy/AuthMeVelocityPlugin.java | 14 ++++++++---- .../proxy/AuthmeVelocityAPI.java | 17 +++++++------- .../proxy/config/AuthMeConfig.java | 3 ++- .../proxy/listener/FastLoginListener.java | 4 +--- .../proxy/listener/ProxyListener.java | 22 ++++++------------- proxy/src/main/resources/velocity-plugin.json | 2 +- 6 files changed, 30 insertions(+), 32 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 f509b7c..1fd41a0 100644 --- a/proxy/src/main/java/com/glyart/authmevelocity/proxy/AuthMeVelocityPlugin.java +++ b/proxy/src/main/java/com/glyart/authmevelocity/proxy/AuthMeVelocityPlugin.java @@ -18,15 +18,17 @@ import java.util.Set; import java.util.UUID; public class AuthMeVelocityPlugin { - private static ProxyServer proxy; + private final ProxyServer proxy; private final Logger logger; private final Path pluginDirectory; + private static AuthMeVelocityPlugin plugin; protected static final Set loggedPlayers = Collections.synchronizedSet(new HashSet()); @Inject - public AuthMeVelocityPlugin(ProxyServer server, Logger logger, @DataDirectory Path dataDirectory) { - proxy = server; + public AuthMeVelocityPlugin(ProxyServer proxy, Logger logger, @DataDirectory Path dataDirectory) { + plugin = this; + this.proxy = proxy; this.logger = logger; this.pluginDirectory = dataDirectory; } @@ -48,7 +50,11 @@ public class AuthMeVelocityPlugin { } } - protected static ProxyServer getProxy(){ + protected ProxyServer getProxy(){ return proxy; } + + public static AuthMeVelocityPlugin getInstance(){ + return plugin; + } } 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 c178848..226fa0a 100644 --- a/proxy/src/main/java/com/glyart/authmevelocity/proxy/AuthmeVelocityAPI.java +++ b/proxy/src/main/java/com/glyart/authmevelocity/proxy/AuthmeVelocityAPI.java @@ -5,13 +5,15 @@ import java.util.function.Predicate; import com.velocitypowered.api.proxy.Player; +import org.jetbrains.annotations.NotNull; + public class AuthmeVelocityAPI { /** * Check if the player is logged in or not * @param player the player * @return if the player is logged in or not */ - public static boolean isLogged(Player player){ + public static boolean isLogged(@NotNull Player player){ final UUID playerUUID = player.getUniqueId(); return AuthMeVelocityPlugin.loggedPlayers.contains(playerUUID); } @@ -19,19 +21,18 @@ public class AuthmeVelocityAPI { /** * Adds a player to the list of logged in players * @param player the new logged player + * @return if the player was succesfully added */ - public static void addPlayer(Player player){ + public static boolean addPlayer(@NotNull Player player){ final UUID playerUUID = player.getUniqueId(); - if(!AuthmeVelocityAPI.isLogged(player)){ - AuthMeVelocityPlugin.loggedPlayers.add(playerUUID); - } + return AuthMeVelocityPlugin.loggedPlayers.add(playerUUID); } /** * Removes a player from the list of logged-in players * @param player the unlogged player */ - public static void removePlayer(Player player){ + public static void removePlayer(@NotNull Player player){ final UUID playerUUID = player.getUniqueId(); if(AuthmeVelocityAPI.isLogged(player)){ AuthMeVelocityPlugin.loggedPlayers.remove(playerUUID); @@ -42,9 +43,9 @@ public class AuthmeVelocityAPI { * Removes players who meet the established condition * @param predicate the condition */ - public static void removePlayerIf(Predicate predicate){ + public static void removePlayerIf(@NotNull Predicate predicate){ AuthMeVelocityPlugin.loggedPlayers.stream() - .map(uuid -> AuthMeVelocityPlugin.getProxy().getPlayer(uuid).orElse(null)) + .map(uuid -> AuthMeVelocityPlugin.getInstance().getProxy().getPlayer(uuid).orElseThrow()) .filter(predicate) .forEach(player -> AuthMeVelocityPlugin.loggedPlayers.remove(player.getUniqueId())); } 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 c462692..604ffe9 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 @@ -5,6 +5,7 @@ import java.nio.file.Path; import java.util.List; import java.util.Set; +import org.jetbrains.annotations.NotNull; import org.slf4j.Logger; import org.spongepowered.configurate.CommentedConfigurationNode; import org.spongepowered.configurate.ConfigurateException; @@ -13,7 +14,7 @@ import org.spongepowered.configurate.objectmapping.meta.Comment; import org.spongepowered.configurate.yaml.YamlConfigurationLoader; public class AuthMeConfig { - public static void loadConfig(Path path, Logger logger){ + public static void loadConfig(@NotNull Path path, @NotNull Logger logger){ File configFile = new File(path.toFile(), "config.yml"); final YamlConfigurationLoader loader = YamlConfigurationLoader.builder() .defaultOptions(opts -> opts.shouldCopyDefaults(true)) diff --git a/proxy/src/main/java/com/glyart/authmevelocity/proxy/listener/FastLoginListener.java b/proxy/src/main/java/com/glyart/authmevelocity/proxy/listener/FastLoginListener.java index bf5b70f..ef03a1a 100644 --- a/proxy/src/main/java/com/glyart/authmevelocity/proxy/listener/FastLoginListener.java +++ b/proxy/src/main/java/com/glyart/authmevelocity/proxy/listener/FastLoginListener.java @@ -16,8 +16,6 @@ public class FastLoginListener { @Subscribe public void onAutoLogin(VelocityFastLoginAutoLoginEvent event){ Optional autoLoginPlayer = server.getPlayer(event.getProfile().getName()); - if(autoLoginPlayer.isPresent()){ - AuthmeVelocityAPI.addPlayer(autoLoginPlayer.get()); - } + autoLoginPlayer.ifPresent(AuthmeVelocityAPI::addPlayer); } } 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 87ad509..9b640be 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 @@ -51,17 +51,14 @@ public class ProxyListener { if (!optionalPlayer.isPresent()) return; Player loggedPlayer = optionalPlayer.get(); - if (!AuthmeVelocityAPI.isLogged(loggedPlayer)){ - AuthmeVelocityAPI.addPlayer(loggedPlayer); - + if (AuthmeVelocityAPI.addPlayer(loggedPlayer)){ RegisteredServer loginServer = loggedPlayer.getCurrentServer().orElseThrow().getServer(); proxy.getEventManager().fireAndForget(new ProxyLoginEvent(loggedPlayer, loginServer)); if(config.sendToServer()){ List serverList = config.getTeleportServers(); String randomServer = serverList.get(rm.nextInt(serverList.size())); Optional optionalServer = proxy.getServer(randomServer); - if(optionalServer.isPresent()){ - RegisteredServer serverToSend = optionalServer.get(); + optionalServer.ifPresentOrElse(serverToSend -> { try{ if(!loggedPlayer.createConnectionRequest(serverToSend).connect().get().isSuccessful()){ logger.info("Unable to connect the player {} to the server {}", @@ -74,9 +71,7 @@ public class ProxyListener { serverToSend.getServerInfo().getName(), exception); } - } else{ - logger.info("The server {} does not exist", randomServer); - } + }, () -> logger.info("The server {} does not exist", randomServer)); } } } @@ -88,15 +83,12 @@ public class ProxyListener { @Subscribe public void onCommandExecute(final CommandExecuteEvent event) { - if (!(event.getCommandSource() instanceof Player player)) return; - - if (AuthmeVelocityAPI.isLogged(player)) return; + if (!(event.getCommandSource() instanceof Player player) || AuthmeVelocityAPI.isLogged(player)) + return; Optional server = player.getCurrentServer(); - boolean isAuthServer = server.isPresent() && - config.getAuthServers().contains(server.get().getServerInfo().getName()); - if (isAuthServer) { + if (server.isPresent() && config.getAuthServers().contains(server.get().getServerInfo().getName())) { event.setResult(CommandExecuteEvent.CommandResult.forwardToServer()); } else { event.setResult(CommandExecuteEvent.CommandResult.denied()); @@ -130,7 +122,7 @@ public class ProxyListener { @Subscribe public void onTabComplete(TabCompleteEvent event){ - Player player = event.getPlayer(); + final Player player = event.getPlayer(); if (!AuthmeVelocityAPI.isLogged(player)){ event.getSuggestions().clear(); } diff --git a/proxy/src/main/resources/velocity-plugin.json b/proxy/src/main/resources/velocity-plugin.json index a537d3e..6d37123 100644 --- a/proxy/src/main/resources/velocity-plugin.json +++ b/proxy/src/main/resources/velocity-plugin.json @@ -4,7 +4,7 @@ "version":"${project.version}", "url":"https://github.com/4drian3d/AuthMeVelocity", "description":"This plugin adds the support for AuthMeReloaded to Velocity.", - "authors":["xQuickGlare"], + "authors":["xQuickGlare", "4drian3d"], "dependencies":[ { "id":"fastlogin",