From f1bc19ac3daeb9d18cc24121be517e3b5ae6de36 Mon Sep 17 00:00:00 2001 From: 4drian3d Date: Sat, 13 Nov 2021 17:00:55 -0500 Subject: [PATCH 1/5] Migrate to Configurate - Code improvements --- pom.xml | 36 --------- proxy/pom.xml | 43 +++++++++- .../proxy/AuthMeVelocityPlugin.java | 22 +++--- .../proxy/AuthmeVelocityAPI.java | 2 + .../proxy/config/AuthMeConfig.java | 79 ++++++++++++++----- .../proxy/listener/ProxyListener.java | 36 +++++---- 6 files changed, 129 insertions(+), 89 deletions(-) diff --git a/pom.xml b/pom.xml index 1cae1c5..b25adb4 100644 --- a/pom.xml +++ b/pom.xml @@ -38,42 +38,6 @@ 16 - - org.apache.maven.plugins - maven-shade-plugin - 3.3.1-SNAPSHOT - - - package - - shade - - - false - true - - - de.leonhard - com.glyart.authmevelocity.libs.simplixstorage - - - - - - - - - *:* - - META-INF/maven/ - META-INF/*.MF - - - - - - - diff --git a/proxy/pom.xml b/proxy/pom.xml index e96e374..5ad537d 100644 --- a/proxy/pom.xml +++ b/proxy/pom.xml @@ -39,10 +39,9 @@ provided - com.github.simplix-softworks - SimplixStorage - 3.2.3 - compile + org.spongepowered + configurate-yaml + 4.1.2 com.github.games647 @@ -59,6 +58,42 @@ maven-jar-plugin 3.2.1-SNAPSHOT + + org.apache.maven.plugins + maven-shade-plugin + 3.3.1-SNAPSHOT + + + package + + shade + + + false + true + + + org.spongepowered + com.glyart.authmevelocity.libs.configurate + + + + + + + + + *:* + + META-INF/maven/ + META-INF/*.MF + + + + + + + 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 05339bb..f509b7c 100644 --- a/proxy/src/main/java/com/glyart/authmevelocity/proxy/AuthMeVelocityPlugin.java +++ b/proxy/src/main/java/com/glyart/authmevelocity/proxy/AuthMeVelocityPlugin.java @@ -6,12 +6,12 @@ import com.glyart.authmevelocity.proxy.listener.ProxyListener; import com.google.inject.Inject; import com.velocitypowered.api.event.Subscribe; import com.velocitypowered.api.event.proxy.ProxyInitializeEvent; +import com.velocitypowered.api.plugin.annotation.DataDirectory; import com.velocitypowered.api.proxy.ProxyServer; import com.velocitypowered.api.proxy.messages.MinecraftChannelIdentifier; import org.slf4j.Logger; -import de.leonhard.storage.Yaml; - +import java.nio.file.Path; import java.util.Collections; import java.util.HashSet; import java.util.Set; @@ -20,14 +20,15 @@ import java.util.UUID; public class AuthMeVelocityPlugin { private static ProxyServer proxy; private final Logger logger; - private static Yaml config = new Yaml("config", "plugins/AuthmeVelocity"); + private final Path pluginDirectory; protected static final Set loggedPlayers = Collections.synchronizedSet(new HashSet()); @Inject - public AuthMeVelocityPlugin(ProxyServer server, Logger logger) { + public AuthMeVelocityPlugin(ProxyServer server, Logger logger, @DataDirectory Path dataDirectory) { proxy = server; this.logger = logger; + this.pluginDirectory = dataDirectory; } @Subscribe @@ -38,18 +39,15 @@ public class AuthMeVelocityPlugin { if(proxy.getPluginManager().getPlugin("fastlogin").isPresent()){ proxy.getEventManager().register(this, new FastLoginListener(proxy)); } - AuthMeConfig.defaultConfig(); + AuthMeConfig.loadConfig(pluginDirectory, logger); logger.info("-- AuthMeVelocity enabled --"); - logger.info("AuthServers: " + config.getList("authservers")); - if(config.getBoolean("teleport.send-to-server-after-login")){ - logger.info("LobbyServers: " + config.getList("teleport.servers")); + var config = AuthMeConfig.getConfig(); + logger.info("AuthServers: {}", config.getAuthServers()); + if(config.sendToServer()){ + logger.info("LobbyServers: {}", config.getTeleportServers()); } } - public static Yaml getConfig(){ - return config; - } - protected static ProxyServer getProxy(){ return proxy; } 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 dd0d37d..c178848 100644 --- a/proxy/src/main/java/com/glyart/authmevelocity/proxy/AuthmeVelocityAPI.java +++ b/proxy/src/main/java/com/glyart/authmevelocity/proxy/AuthmeVelocityAPI.java @@ -48,4 +48,6 @@ public class AuthmeVelocityAPI { .filter(predicate) .forEach(player -> AuthMeVelocityPlugin.loggedPlayers.remove(player.getUniqueId())); } + + private AuthmeVelocityAPI(){} } 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 d9a29e9..c462692 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 @@ -1,28 +1,67 @@ package com.glyart.authmevelocity.proxy.config; +import java.io.File; +import java.nio.file.Path; import java.util.List; +import java.util.Set; -import com.glyart.authmevelocity.proxy.AuthMeVelocityPlugin; +import org.slf4j.Logger; +import org.spongepowered.configurate.CommentedConfigurationNode; +import org.spongepowered.configurate.ConfigurateException; +import org.spongepowered.configurate.objectmapping.ConfigSerializable; +import org.spongepowered.configurate.objectmapping.meta.Comment; +import org.spongepowered.configurate.yaml.YamlConfigurationLoader; -import de.leonhard.storage.Yaml; +public class AuthMeConfig { + public static void loadConfig(Path path, Logger logger){ + File configFile = new File(path.toFile(), "config.yml"); + final YamlConfigurationLoader loader = YamlConfigurationLoader.builder() + .defaultOptions(opts -> opts.shouldCopyDefaults(true)) + .file(configFile) + .build(); -public interface AuthMeConfig { - public static void defaultConfig(){ - Yaml config = AuthMeVelocityPlugin.getConfig(); - config.setDefault( - "authservers", - List.of( - "auth1", - "auth2" - )); - config.setDefault( - "teleport.send-to-server-after-login", - false); - config.setDefault( - "teleport.servers", - List.of( - "lobby1", - "lobby2" - )); + try { + CommentedConfigurationNode node = loader.load(); + config = node.get(Config.class); + node.set(Config.class, config); + loader.save(node); + } catch (ConfigurateException exception){ + logger.error("Could not load configuration: {}", exception.getMessage()); + } } + @ConfigSerializable + public static class Config { + + @Comment("List of authservers") + private Set authservers = Set.of( + "auth1", + "auth2" + ); + + @Comment("Send each player to another server on login?") + private boolean sendToServerOnLogin = false; + + @Comment("List of teleport to servers") + private List teleportServers = List.of( + "lobby1", + "lobby2" + ); + + public Set getAuthServers(){ + return authservers; + } + + public boolean sendToServer(){ + return sendToServerOnLogin; + } + + public List getTeleportServers(){ + return teleportServers; + } + } + private static Config config; + public static Config getConfig(){ + return config; + } + private AuthMeConfig(){} } 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 425af9e..87ad509 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,7 +1,7 @@ package com.glyart.authmevelocity.proxy.listener; -import com.glyart.authmevelocity.proxy.AuthMeVelocityPlugin; import com.glyart.authmevelocity.proxy.AuthmeVelocityAPI; +import com.glyart.authmevelocity.proxy.config.AuthMeConfig; import com.glyart.authmevelocity.proxy.event.ProxyLoginEvent; import com.google.common.io.ByteArrayDataInput; import com.velocitypowered.api.event.Subscribe; @@ -25,39 +25,41 @@ import java.util.UUID; import java.util.concurrent.ExecutionException; public class ProxyListener { - private final ProxyServer server; + private final ProxyServer proxy; private final Logger logger; + private final Random rm; + private AuthMeConfig.Config config; - public ProxyListener(ProxyServer server, Logger logger) { - this.server = server; + public ProxyListener(ProxyServer proxy, Logger logger) { + this.proxy = proxy; this.logger = logger; + this.rm = new Random(); + this.config = AuthMeConfig.getConfig(); } @Subscribe public void onPluginMessage(final PluginMessageEvent event) { - if (!(event.getSource() instanceof ServerConnection)) return; - - if (!event.getIdentifier().getId().equals("authmevelocity:main")) return; + if (!(event.getSource() instanceof ServerConnection) || !event.getIdentifier().getId().equals("authmevelocity:main")) + return; ByteArrayDataInput input = event.dataAsDataStream(); String sChannel = input.readUTF(); if (!sChannel.equals("LOGIN")) return; String user = input.readUTF(); - Optional optionalPlayer = server.getPlayer(UUID.fromString(user)); + Optional optionalPlayer = proxy.getPlayer(UUID.fromString(user)); if (!optionalPlayer.isPresent()) return; Player loggedPlayer = optionalPlayer.get(); if (!AuthmeVelocityAPI.isLogged(loggedPlayer)){ AuthmeVelocityAPI.addPlayer(loggedPlayer); - RegisteredServer loginServer = loggedPlayer.getCurrentServer().get().getServer(); - server.getEventManager().fireAndForget(new ProxyLoginEvent(loggedPlayer, loginServer)); - if(AuthMeVelocityPlugin.getConfig().getBoolean("teleport.send-to-server-after-login")){ - Random rm = new Random(); - List serverList = AuthMeVelocityPlugin.getConfig().getStringList("teleport.servers"); + 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 = server.getServer(randomServer); + Optional optionalServer = proxy.getServer(randomServer); if(optionalServer.isPresent()){ RegisteredServer serverToSend = optionalServer.get(); try{ @@ -92,7 +94,7 @@ public class ProxyListener { Optional server = player.getCurrentServer(); boolean isAuthServer = server.isPresent() && - AuthMeVelocityPlugin.getConfig().getList("authservers").contains(server.get().getServerInfo().getName()); + config.getAuthServers().contains(server.get().getServerInfo().getName()); if (isAuthServer) { event.setResult(CommandExecuteEvent.CommandResult.forwardToServer()); @@ -107,7 +109,7 @@ public class ProxyListener { if (AuthmeVelocityAPI.isLogged(player)) return; Optional server = player.getCurrentServer(); - if (server.isPresent() && AuthMeVelocityPlugin.getConfig().getList("authservers").contains(server.get().getServerInfo().getName())) { + if (server.isPresent() && config.getAuthServers().contains(server.get().getServerInfo().getName())) { return; } @@ -119,7 +121,7 @@ public class ProxyListener { if (AuthmeVelocityAPI.isLogged(event.getPlayer())) return; Optional server = event.getResult().getServer(); - if (server.isPresent() && AuthMeVelocityPlugin.getConfig().getList("authservers").contains(server.get().getServerInfo().getName())) { + if (server.isPresent() && config.getAuthServers().contains(server.get().getServerInfo().getName())) { return; } From ec314e54b9d41402884cbe862ac61a5a9dd69a70 Mon Sep 17 00:00:00 2001 From: 4drian3d Date: Sat, 13 Nov 2021 20:51:08 -0500 Subject: [PATCH 2/5] 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", From 5b32a7140963fab99da84012c1a156f7851ef4ca Mon Sep 17 00:00:00 2001 From: 4drian3d Date: Sun, 14 Nov 2021 08:05:38 -0500 Subject: [PATCH 3/5] Improved Configuration - Changed yaml format to hocon --- proxy/pom.xml | 2 +- .../proxy/AuthMeVelocityPlugin.java | 4 +- .../proxy/config/AuthMeConfig.java | 38 ++++++++++++------- .../proxy/listener/ProxyListener.java | 4 +- 4 files changed, 30 insertions(+), 18 deletions(-) diff --git a/proxy/pom.xml b/proxy/pom.xml index 5ad537d..7235f11 100644 --- a/proxy/pom.xml +++ b/proxy/pom.xml @@ -40,7 +40,7 @@ org.spongepowered - configurate-yaml + configurate-hocon 4.1.2 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 1fd41a0..becfff0 100644 --- a/proxy/src/main/java/com/glyart/authmevelocity/proxy/AuthMeVelocityPlugin.java +++ b/proxy/src/main/java/com/glyart/authmevelocity/proxy/AuthMeVelocityPlugin.java @@ -45,8 +45,8 @@ public class AuthMeVelocityPlugin { logger.info("-- AuthMeVelocity enabled --"); var config = AuthMeConfig.getConfig(); logger.info("AuthServers: {}", config.getAuthServers()); - if(config.sendToServer()){ - logger.info("LobbyServers: {}", config.getTeleportServers()); + if(config.getToServerOptions().sendToServer()){ + logger.info("LobbyServers: {}", config.getToServerOptions().getTeleportServers()); } } 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 604ffe9..0acf20b 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,20 +9,20 @@ import org.jetbrains.annotations.NotNull; 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; -import org.spongepowered.configurate.yaml.YamlConfigurationLoader; public class AuthMeConfig { public static void loadConfig(@NotNull Path path, @NotNull Logger logger){ - File configFile = new File(path.toFile(), "config.yml"); - final YamlConfigurationLoader loader = YamlConfigurationLoader.builder() + File configFile = new File(path.toFile(), "config.conf"); + final HoconConfigurationLoader loader = HoconConfigurationLoader.builder() .defaultOptions(opts -> opts.shouldCopyDefaults(true)) .file(configFile) .build(); try { - CommentedConfigurationNode node = loader.load(); + final CommentedConfigurationNode node = loader.load(); config = node.get(Config.class); node.set(Config.class, config); loader.save(node); @@ -30,34 +30,46 @@ public class AuthMeConfig { logger.error("Could not load configuration: {}", exception.getMessage()); } } + @ConfigSerializable public static class Config { - @Comment("List of authservers") + @Comment("List of login/registration servers") private Set authservers = Set.of( "auth1", "auth2" ); - @Comment("Send each player to another server on login?") + private ServerOnLogin send = new ServerOnLogin(); + + public Set getAuthServers(){ + return this.authservers; + } + + public ServerOnLogin getToServerOptions(){ + return this.send; + } + } + @ConfigSerializable + public static class ServerOnLogin { + @Comment("Send logged in players to another server?") private boolean sendToServerOnLogin = false; - @Comment("List of teleport to servers") + @Comment(""" + List of servers to send + One of these servers will be chosen at random + """) private List teleportServers = List.of( "lobby1", "lobby2" ); - public Set getAuthServers(){ - return authservers; - } - public boolean sendToServer(){ - return sendToServerOnLogin; + return this.sendToServerOnLogin; } public List getTeleportServers(){ - return teleportServers; + return this.teleportServers; } } private static Config config; 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 9b640be..320011f 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 @@ -54,8 +54,8 @@ public class ProxyListener { if (AuthmeVelocityAPI.addPlayer(loggedPlayer)){ RegisteredServer loginServer = loggedPlayer.getCurrentServer().orElseThrow().getServer(); proxy.getEventManager().fireAndForget(new ProxyLoginEvent(loggedPlayer, loginServer)); - if(config.sendToServer()){ - List serverList = config.getTeleportServers(); + if(config.getToServerOptions().sendToServer()){ + List serverList = config.getToServerOptions().getTeleportServers(); String randomServer = serverList.get(rm.nextInt(serverList.size())); Optional optionalServer = proxy.getServer(randomServer); optionalServer.ifPresentOrElse(serverToSend -> { From 914e15ff70326d3b1d3e958b7912cda3f9880e22 Mon Sep 17 00:00:00 2001 From: 4drian3d Date: Sun, 14 Nov 2021 20:15:36 -0500 Subject: [PATCH 4/5] Performance improvements - Use async event listener on velocity --- pom.xml | 2 +- proxy/pom.xml | 2 +- .../glyart/authmevelocity/proxy/AuthmeVelocityAPI.java | 7 +++---- .../authmevelocity/proxy/listener/ProxyListener.java | 6 ++++-- spigot/pom.xml | 2 +- .../authmevelocity/spigot/AuthMeVelocityPlugin.java | 9 +++++---- .../authmevelocity/spigot/events/PreSendLoginEvent.java | 4 ++-- .../authmevelocity/spigot/listeners/AuthMeListener.java | 8 ++++---- 8 files changed, 21 insertions(+), 19 deletions(-) diff --git a/pom.xml b/pom.xml index b25adb4..b7a25f0 100644 --- a/pom.xml +++ b/pom.xml @@ -7,7 +7,7 @@ com.glyart.authmevelocity parent pom - 1.2.0 + 1.3.0 16 diff --git a/proxy/pom.xml b/proxy/pom.xml index 7235f11..b3a30b6 100644 --- a/proxy/pom.xml +++ b/proxy/pom.xml @@ -5,7 +5,7 @@ parent com.glyart.authmevelocity - 1.2.0 + 1.3.0 4.0.0 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 226fa0a..ee9f67b 100644 --- a/proxy/src/main/java/com/glyart/authmevelocity/proxy/AuthmeVelocityAPI.java +++ b/proxy/src/main/java/com/glyart/authmevelocity/proxy/AuthmeVelocityAPI.java @@ -31,12 +31,11 @@ public class AuthmeVelocityAPI { /** * Removes a player from the list of logged-in players * @param player the unlogged player + * @return if the player was succesfully removed */ - public static void removePlayer(@NotNull Player player){ + public static boolean removePlayer(@NotNull Player player){ final UUID playerUUID = player.getUniqueId(); - if(AuthmeVelocityAPI.isLogged(player)){ - AuthMeVelocityPlugin.loggedPlayers.remove(playerUUID); - } + return AuthMeVelocityPlugin.loggedPlayers.remove(playerUUID); } /** 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 320011f..4b561b8 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 @@ -4,6 +4,7 @@ import com.glyart.authmevelocity.proxy.AuthmeVelocityAPI; import com.glyart.authmevelocity.proxy.config.AuthMeConfig; import com.glyart.authmevelocity.proxy.event.ProxyLoginEvent; import com.google.common.io.ByteArrayDataInput; +import com.velocitypowered.api.event.EventTask; import com.velocitypowered.api.event.Subscribe; import com.velocitypowered.api.event.command.CommandExecuteEvent; import com.velocitypowered.api.event.connection.DisconnectEvent; @@ -121,10 +122,11 @@ public class ProxyListener { } @Subscribe - public void onTabComplete(TabCompleteEvent event){ + public EventTask onTabComplete(TabCompleteEvent event){ final Player player = event.getPlayer(); if (!AuthmeVelocityAPI.isLogged(player)){ - event.getSuggestions().clear(); + return EventTask.async(() -> event.getSuggestions().clear()); } + return null; } } diff --git a/spigot/pom.xml b/spigot/pom.xml index 8810dda..40a8f1e 100644 --- a/spigot/pom.xml +++ b/spigot/pom.xml @@ -5,7 +5,7 @@ parent com.glyart.authmevelocity - 1.2.0 + 1.3.0 4.0.0 diff --git a/spigot/src/main/java/com/glyart/authmevelocity/spigot/AuthMeVelocityPlugin.java b/spigot/src/main/java/com/glyart/authmevelocity/spigot/AuthMeVelocityPlugin.java index 29acb78..fc05ac4 100644 --- a/spigot/src/main/java/com/glyart/authmevelocity/spigot/AuthMeVelocityPlugin.java +++ b/spigot/src/main/java/com/glyart/authmevelocity/spigot/AuthMeVelocityPlugin.java @@ -5,17 +5,18 @@ import com.google.common.io.ByteArrayDataOutput; import com.google.common.io.ByteStreams; import org.bukkit.entity.Player; import org.bukkit.plugin.java.JavaPlugin; +import org.jetbrains.annotations.NotNull; public class AuthMeVelocityPlugin extends JavaPlugin { @Override public void onEnable() { - getServer().getMessenger().registerOutgoingPluginChannel(this, "authmevelocity:main"); - getServer().getPluginManager().registerEvents(new AuthMeListener(this), this); + this.getServer().getMessenger().registerOutgoingPluginChannel(this, "authmevelocity:main"); + this.getServer().getPluginManager().registerEvents(new AuthMeListener(this), this); - getLogger().info("AuthMeVelocity enabled."); + this.getLogger().info("AuthMeVelocity enabled."); } - public void sendLoginToProxy(Player player) { + public void sendLoginToProxy(@NotNull final Player player) { ByteArrayDataOutput out = ByteStreams.newDataOutput(); out.writeUTF("LOGIN"); out.writeUTF(player.getUniqueId().toString()); diff --git a/spigot/src/main/java/com/glyart/authmevelocity/spigot/events/PreSendLoginEvent.java b/spigot/src/main/java/com/glyart/authmevelocity/spigot/events/PreSendLoginEvent.java index 50d5af6..ae163cb 100644 --- a/spigot/src/main/java/com/glyart/authmevelocity/spigot/events/PreSendLoginEvent.java +++ b/spigot/src/main/java/com/glyart/authmevelocity/spigot/events/PreSendLoginEvent.java @@ -10,8 +10,8 @@ public class PreSendLoginEvent extends PlayerEvent implements Cancellable { private static final HandlerList HANDLERS = new HandlerList(); private boolean isCancelled; - public PreSendLoginEvent(@NotNull Player player) { - super(player); + public PreSendLoginEvent(@NotNull final Player player, boolean async) { + super(player, async); } @Override diff --git a/spigot/src/main/java/com/glyart/authmevelocity/spigot/listeners/AuthMeListener.java b/spigot/src/main/java/com/glyart/authmevelocity/spigot/listeners/AuthMeListener.java index 0a0b864..ca6c10b 100644 --- a/spigot/src/main/java/com/glyart/authmevelocity/spigot/listeners/AuthMeListener.java +++ b/spigot/src/main/java/com/glyart/authmevelocity/spigot/listeners/AuthMeListener.java @@ -19,10 +19,10 @@ public class AuthMeListener implements Listener { @EventHandler public void onLogin(LoginEvent event) { - Player player = event.getPlayer(); - PreSendLoginEvent sendloginevent = new PreSendLoginEvent(player); - Bukkit.getPluginManager().callEvent(sendloginevent); - if(!sendloginevent.isCancelled()){ + final Player player = event.getPlayer(); + PreSendLoginEvent preSendLoginEvent = new PreSendLoginEvent(player, false); + Bukkit.getPluginManager().callEvent(preSendLoginEvent); + if(!preSendLoginEvent.isCancelled()){ plugin.sendLoginToProxy(player); } } From 4ee6e847e9333159ce37e478ba86a268c65887ab Mon Sep 17 00:00:00 2001 From: 4drian3d Date: Sun, 14 Nov 2021 21:03:36 -0500 Subject: [PATCH 5/5] Added config header - Cache hocon builder --- .../authmevelocity/proxy/config/AuthMeConfig.java | 14 ++++++++++++-- 1 file changed, 12 insertions(+), 2 deletions(-) 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 0acf20b..3d88bdf 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 @@ -14,10 +14,20 @@ import org.spongepowered.configurate.objectmapping.ConfigSerializable; import org.spongepowered.configurate.objectmapping.meta.Comment; public class AuthMeConfig { + private static final String HEADER = """ + AuthmeVelocity Proxy + + Original Developer: xQuickGlare + Actual Developer: 4drian3d + """; + private static final HoconConfigurationLoader.Builder configBuilder = HoconConfigurationLoader.builder() + .defaultOptions(opts -> opts + .shouldCopyDefaults(true) + .header(HEADER) + ); public static void loadConfig(@NotNull Path path, @NotNull Logger logger){ File configFile = new File(path.toFile(), "config.conf"); - final HoconConfigurationLoader loader = HoconConfigurationLoader.builder() - .defaultOptions(opts -> opts.shouldCopyDefaults(true)) + final HoconConfigurationLoader loader = configBuilder .file(configFile) .build();