diff --git a/pom.xml b/pom.xml index 1cae1c5..b7a25f0 100644 --- a/pom.xml +++ b/pom.xml @@ -7,7 +7,7 @@ com.glyart.authmevelocity parent pom - 1.2.0 + 1.3.0 16 @@ -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..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 @@ -39,10 +39,9 @@ provided - com.github.simplix-softworks - SimplixStorage - 3.2.3 - compile + org.spongepowered + configurate-hocon + 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..becfff0 100644 --- a/proxy/src/main/java/com/glyart/authmevelocity/proxy/AuthMeVelocityPlugin.java +++ b/proxy/src/main/java/com/glyart/authmevelocity/proxy/AuthMeVelocityPlugin.java @@ -6,28 +6,31 @@ 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; import java.util.UUID; public class AuthMeVelocityPlugin { - private static ProxyServer proxy; + private final ProxyServer proxy; private final Logger logger; - private static Yaml config = new Yaml("config", "plugins/AuthmeVelocity"); + private final Path pluginDirectory; + private static AuthMeVelocityPlugin plugin; protected static final Set loggedPlayers = Collections.synchronizedSet(new HashSet()); @Inject - public AuthMeVelocityPlugin(ProxyServer server, Logger logger) { - proxy = server; + public AuthMeVelocityPlugin(ProxyServer proxy, Logger logger, @DataDirectory Path dataDirectory) { + plugin = this; + this.proxy = proxy; this.logger = logger; + this.pluginDirectory = dataDirectory; } @Subscribe @@ -38,19 +41,20 @@ 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.getToServerOptions().sendToServer()){ + logger.info("LobbyServers: {}", config.getToServerOptions().getTeleportServers()); } } - public static Yaml getConfig(){ - return config; - } - - 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 dd0d37d..ee9f67b 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,33 +21,33 @@ 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 + * @return if the player was succesfully removed */ - public static void removePlayer(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); } /** * 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())); } + + 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..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 @@ -1,28 +1,90 @@ 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.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 de.leonhard.storage.Yaml; +public class AuthMeConfig { + private static final String HEADER = """ + AuthmeVelocity Proxy -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" - )); + 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 = configBuilder + .file(configFile) + .build(); + + try { + final 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 login/registration servers") + private Set authservers = Set.of( + "auth1", + "auth2" + ); + + 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 servers to send + One of these servers will be chosen at random + """) + private List teleportServers = List.of( + "lobby1", + "lobby2" + ); + + public boolean sendToServer(){ + return this.sendToServerOnLogin; + } + + public List getTeleportServers(){ + return this.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/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 425af9e..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 @@ -1,9 +1,10 @@ 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.EventTask; import com.velocitypowered.api.event.Subscribe; import com.velocitypowered.api.event.command.CommandExecuteEvent; import com.velocitypowered.api.event.connection.DisconnectEvent; @@ -25,41 +26,40 @@ 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"); + if (AuthmeVelocityAPI.addPlayer(loggedPlayer)){ + RegisteredServer loginServer = loggedPlayer.getCurrentServer().orElseThrow().getServer(); + proxy.getEventManager().fireAndForget(new ProxyLoginEvent(loggedPlayer, loginServer)); + if(config.getToServerOptions().sendToServer()){ + List serverList = config.getToServerOptions().getTeleportServers(); String randomServer = serverList.get(rm.nextInt(serverList.size())); - Optional optionalServer = server.getServer(randomServer); - if(optionalServer.isPresent()){ - RegisteredServer serverToSend = optionalServer.get(); + Optional optionalServer = proxy.getServer(randomServer); + optionalServer.ifPresentOrElse(serverToSend -> { try{ if(!loggedPlayer.createConnectionRequest(serverToSend).connect().get().isSuccessful()){ logger.info("Unable to connect the player {} to the server {}", @@ -72,9 +72,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)); } } } @@ -86,15 +84,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() && - AuthMeVelocityPlugin.getConfig().getList("authservers").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()); @@ -107,7 +102,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 +114,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; } @@ -127,10 +122,11 @@ public class ProxyListener { } @Subscribe - public void onTabComplete(TabCompleteEvent event){ - Player player = event.getPlayer(); + 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/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", 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); } }