From 581e615363fc14e61ba0d95365d0d32974e7d83f Mon Sep 17 00:00:00 2001 From: 4drian3d Date: Wed, 27 Jul 2022 16:20:37 +0000 Subject: [PATCH 1/6] feat: Implement /authmevelocity command resolves #41 --- pom.xml | 2 +- proxy/pom.xml | 2 +- .../proxy/AuthMeVelocityPlugin.java | 63 ++++++++++++++----- .../proxy/commands/AuthmeCommand.java | 44 +++++++++++++ spigot/pom.xml | 2 +- 5 files changed, 93 insertions(+), 20 deletions(-) create mode 100644 proxy/src/main/java/com/glyart/authmevelocity/proxy/commands/AuthmeCommand.java diff --git a/pom.xml b/pom.xml index 29ecfe5..f128018 100644 --- a/pom.xml +++ b/pom.xml @@ -7,7 +7,7 @@ com.glyart.authmevelocity parent pom - 2.1.1 + 2.2.0 11 diff --git a/proxy/pom.xml b/proxy/pom.xml index 5a121f4..d0e5674 100644 --- a/proxy/pom.xml +++ b/proxy/pom.xml @@ -5,7 +5,7 @@ parent com.glyart.authmevelocity - 2.1.1 + 2.2.0 4.0.0 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 4473912..de40a4b 100644 --- a/proxy/src/main/java/com/glyart/authmevelocity/proxy/AuthMeVelocityPlugin.java +++ b/proxy/src/main/java/com/glyart/authmevelocity/proxy/AuthMeVelocityPlugin.java @@ -1,11 +1,13 @@ package com.glyart.authmevelocity.proxy; +import com.glyart.authmevelocity.proxy.commands.AuthmeCommand; import com.glyart.authmevelocity.proxy.config.AuthMeConfig; import com.glyart.authmevelocity.proxy.listener.FastLoginListener; import com.glyart.authmevelocity.proxy.listener.PluginMessageListener; import com.glyart.authmevelocity.proxy.listener.ProxyListener; import com.google.inject.Inject; import com.moandjiezana.toml.Toml; +import com.velocitypowered.api.command.CommandSource; import com.velocitypowered.api.event.Subscribe; import com.velocitypowered.api.event.proxy.ProxyInitializeEvent; import com.velocitypowered.api.plugin.annotation.DataDirectory; @@ -13,14 +15,19 @@ import com.velocitypowered.api.proxy.messages.ChannelIdentifier; import com.velocitypowered.api.proxy.messages.MinecraftChannelIdentifier; +import net.kyori.adventure.text.Component; +import net.kyori.adventure.text.minimessage.MiniMessage; + import org.slf4j.Logger; import java.io.IOException; import java.io.InputStream; import java.nio.file.Files; import java.nio.file.Path; +import java.util.ArrayList; import java.util.Collections; import java.util.HashSet; +import java.util.List; import java.util.Objects; import java.util.Set; import java.util.UUID; @@ -32,7 +39,9 @@ public class AuthMeVelocityPlugin { private final Logger logger; private final Path pluginDirectory; private AuthmeVelocityAPI api; + private AuthMeConfig config; + private final List listeners = new ArrayList<>(3); protected final Set loggedPlayers = Collections.synchronizedSet(new HashSet<>()); @Inject @@ -44,30 +53,18 @@ public AuthMeVelocityPlugin(ProxyServer proxy, Logger logger, @DataDirectory Pat @Subscribe public void onProxyInitialization(ProxyInitializeEvent event) { - Toml toml = this.loadConfig(pluginDirectory); - if (toml == null) { + if (this.reload()) { logger.warn("Failed to load config.toml. Shutting down."); return; } - AuthMeConfig config = new AuthMeConfig(toml); - this.api = new AuthmeVelocityAPI(this, config); - proxy.getChannelRegistrar().register(AUTHMEVELOCITY_CHANNEL); - 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")) { - proxy.getEventManager().register(this, new FastLoginListener(proxy, api)); - } if (proxy.getPluginManager().isLoaded("miniplaceholders")) { AuthmePlaceholders.getExpansion(this).register(); } - logger.info("-- AuthMeVelocity enabled --"); - logger.info("AuthServers: {}", config.getAuthServers()); - if (config.getToServerOptions().sendToServer()) { - logger.info("LobbyServers: {}", config.getToServerOptions().getTeleportServers()); - } + AuthmeCommand.register(this, proxy.getCommandManager()); + + this.sendInfoMessage(); } protected ProxyServer getProxy(){ @@ -95,7 +92,39 @@ private Toml loadConfig(Path path){ } catch (IOException ex) { logger.info("An error ocurred on configuration initialization", ex); return null; + } + } + + public boolean reload() { + Toml toml = this.loadConfig(pluginDirectory); + if (toml == null) { + return false; + } + this.config = new AuthMeConfig(toml); + this.api = new AuthmeVelocityAPI(this, config); + proxy.getChannelRegistrar().register(AUTHMEVELOCITY_CHANNEL); + + listeners.forEach(listener -> proxy.getEventManager().unregisterListener(this, listener)); + + listeners.add(new ProxyListener(config, api, logger, proxy)); + listeners.add(new PluginMessageListener(proxy, logger, config, api)); + + if (proxy.getPluginManager().isLoaded("fastlogin")) { + listeners.add(new FastLoginListener(proxy, api)); + } + + listeners.forEach(listener -> proxy.getEventManager().register(this, listener)); + return true; + } + + public void sendInfoMessage() { + CommandSource source = proxy.getConsoleCommandSource(); + source.sendMessage(Component.text("-- AuthMeVelocity enabled --")); + source.sendMessage(MiniMessage.miniMessage().deserialize( + "AuthServers: " + config.getAuthServers())); + if (config.getToServerOptions().sendToServer()) { + source.sendMessage(MiniMessage.miniMessage().deserialize( + "LobbyServers: " + config.getToServerOptions().getTeleportServers())); } - } } diff --git a/proxy/src/main/java/com/glyart/authmevelocity/proxy/commands/AuthmeCommand.java b/proxy/src/main/java/com/glyart/authmevelocity/proxy/commands/AuthmeCommand.java new file mode 100644 index 0000000..17d0ae4 --- /dev/null +++ b/proxy/src/main/java/com/glyart/authmevelocity/proxy/commands/AuthmeCommand.java @@ -0,0 +1,44 @@ +package com.glyart.authmevelocity.proxy.commands; + +import com.glyart.authmevelocity.proxy.AuthMeVelocityPlugin; +import com.mojang.brigadier.Command; +import com.mojang.brigadier.builder.LiteralArgumentBuilder; +import com.mojang.brigadier.tree.LiteralCommandNode; +import com.velocitypowered.api.command.BrigadierCommand; +import com.velocitypowered.api.command.CommandManager; +import com.velocitypowered.api.command.CommandMeta; +import com.velocitypowered.api.command.CommandSource; + +import net.kyori.adventure.text.Component; +import net.kyori.adventure.text.format.NamedTextColor; + +public class AuthmeCommand { + private AuthmeCommand() {} + + public static void register(AuthMeVelocityPlugin plugin, CommandManager manager) { + LiteralCommandNode command = LiteralArgumentBuilder.literal("authmevelocity") + .requires(src -> src.hasPermission("authmevelocity.commands")) + .then(LiteralArgumentBuilder.literal("reload") + .executes(cmd -> { + CommandSource source = cmd.getSource(); + if (plugin.reload()) { + plugin.sendInfoMessage(); + source.sendMessage(Component.text("AuthmeVelocity was correctly reloaded", NamedTextColor.GREEN)); + } else { + source.sendMessage(Component.text( + "There was an error while reloading the configuration. Check the server console", NamedTextColor.DARK_RED)); + } + return Command.SINGLE_SUCCESS; + }) + ).build(); + + BrigadierCommand brigadier = new BrigadierCommand(command); + CommandMeta meta = manager.metaBuilder(brigadier) + .plugin(plugin) + .aliases("vauthme", "authmev") + .build(); + + manager.register(meta, brigadier); + + } +} diff --git a/spigot/pom.xml b/spigot/pom.xml index 117aa15..6779597 100644 --- a/spigot/pom.xml +++ b/spigot/pom.xml @@ -5,7 +5,7 @@ parent com.glyart.authmevelocity - 2.1.1 + 2.2.0 4.0.0 From 3395a5410403920d6bac896bc130d2a42faacc9c Mon Sep 17 00:00:00 2001 From: 4drian3d Date: Wed, 27 Jul 2022 16:23:27 +0000 Subject: [PATCH 2/6] fix: Clear listeners list on unregister --- .../com/glyart/authmevelocity/proxy/AuthMeVelocityPlugin.java | 1 + 1 file changed, 1 insertion(+) 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 de40a4b..70f1cb3 100644 --- a/proxy/src/main/java/com/glyart/authmevelocity/proxy/AuthMeVelocityPlugin.java +++ b/proxy/src/main/java/com/glyart/authmevelocity/proxy/AuthMeVelocityPlugin.java @@ -105,6 +105,7 @@ public boolean reload() { proxy.getChannelRegistrar().register(AUTHMEVELOCITY_CHANNEL); listeners.forEach(listener -> proxy.getEventManager().unregisterListener(this, listener)); + listeners.clear(); listeners.add(new ProxyListener(config, api, logger, proxy)); listeners.add(new PluginMessageListener(proxy, logger, config, api)); From aead3e144c9b124a2a26178f75be26d323d3013c Mon Sep 17 00:00:00 2001 From: 4drian3d Date: Wed, 27 Jul 2022 16:36:36 +0000 Subject: [PATCH 3/6] fix: Fixed multiple ChannelIdentifier registration --- .../com/glyart/authmevelocity/proxy/AuthMeVelocityPlugin.java | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) 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 70f1cb3..0c8423c 100644 --- a/proxy/src/main/java/com/glyart/authmevelocity/proxy/AuthMeVelocityPlugin.java +++ b/proxy/src/main/java/com/glyart/authmevelocity/proxy/AuthMeVelocityPlugin.java @@ -58,6 +58,8 @@ public void onProxyInitialization(ProxyInitializeEvent event) { return; } + proxy.getChannelRegistrar().register(AUTHMEVELOCITY_CHANNEL); + if (proxy.getPluginManager().isLoaded("miniplaceholders")) { AuthmePlaceholders.getExpansion(this).register(); } @@ -100,9 +102,9 @@ public boolean reload() { if (toml == null) { return false; } + this.config = new AuthMeConfig(toml); this.api = new AuthmeVelocityAPI(this, config); - proxy.getChannelRegistrar().register(AUTHMEVELOCITY_CHANNEL); listeners.forEach(listener -> proxy.getEventManager().unregisterListener(this, listener)); listeners.clear(); From ec93099691be398b5feba0c3813624a652783406 Mon Sep 17 00:00:00 2001 From: 4drian3d Date: Wed, 27 Jul 2022 16:45:33 +0000 Subject: [PATCH 4/6] fix: Fixed incorrect check in plugin initialization --- .../com/glyart/authmevelocity/proxy/AuthMeVelocityPlugin.java | 2 +- .../com/glyart/authmevelocity/proxy/commands/AuthmeCommand.java | 2 +- 2 files changed, 2 insertions(+), 2 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 0c8423c..bc92f02 100644 --- a/proxy/src/main/java/com/glyart/authmevelocity/proxy/AuthMeVelocityPlugin.java +++ b/proxy/src/main/java/com/glyart/authmevelocity/proxy/AuthMeVelocityPlugin.java @@ -53,7 +53,7 @@ public AuthMeVelocityPlugin(ProxyServer proxy, Logger logger, @DataDirectory Pat @Subscribe public void onProxyInitialization(ProxyInitializeEvent event) { - if (this.reload()) { + if (!this.reload()) { logger.warn("Failed to load config.toml. Shutting down."); return; } diff --git a/proxy/src/main/java/com/glyart/authmevelocity/proxy/commands/AuthmeCommand.java b/proxy/src/main/java/com/glyart/authmevelocity/proxy/commands/AuthmeCommand.java index 17d0ae4..106cf6e 100644 --- a/proxy/src/main/java/com/glyart/authmevelocity/proxy/commands/AuthmeCommand.java +++ b/proxy/src/main/java/com/glyart/authmevelocity/proxy/commands/AuthmeCommand.java @@ -23,7 +23,7 @@ public static void register(AuthMeVelocityPlugin plugin, CommandManager manager) CommandSource source = cmd.getSource(); if (plugin.reload()) { plugin.sendInfoMessage(); - source.sendMessage(Component.text("AuthmeVelocity was correctly reloaded", NamedTextColor.GREEN)); + source.sendMessage(Component.text("AuthmeVelocity has been successfully reloaded", NamedTextColor.GREEN)); } else { source.sendMessage(Component.text( "There was an error while reloading the configuration. Check the server console", NamedTextColor.DARK_RED)); From 14f38046e7d7c335871f9f3f67f87ecca2a50fa8 Mon Sep 17 00:00:00 2001 From: 4drian3d Date: Wed, 27 Jul 2022 16:52:04 +0000 Subject: [PATCH 5/6] feat: Improved reload messages --- .../authmevelocity/proxy/AuthMeVelocityPlugin.java | 4 ++-- .../authmevelocity/proxy/commands/AuthmeCommand.java | 10 +++++----- 2 files changed, 7 insertions(+), 7 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 bc92f02..4c6c6ad 100644 --- a/proxy/src/main/java/com/glyart/authmevelocity/proxy/AuthMeVelocityPlugin.java +++ b/proxy/src/main/java/com/glyart/authmevelocity/proxy/AuthMeVelocityPlugin.java @@ -15,7 +15,6 @@ import com.velocitypowered.api.proxy.messages.ChannelIdentifier; import com.velocitypowered.api.proxy.messages.MinecraftChannelIdentifier; -import net.kyori.adventure.text.Component; import net.kyori.adventure.text.minimessage.MiniMessage; import org.slf4j.Logger; @@ -122,7 +121,8 @@ public boolean reload() { public void sendInfoMessage() { CommandSource source = proxy.getConsoleCommandSource(); - source.sendMessage(Component.text("-- AuthMeVelocity enabled --")); + source.sendMessage(MiniMessage.miniMessage().deserialize( + " --- AuthMeVelocity ---")); source.sendMessage(MiniMessage.miniMessage().deserialize( "AuthServers: " + config.getAuthServers())); if (config.getToServerOptions().sendToServer()) { diff --git a/proxy/src/main/java/com/glyart/authmevelocity/proxy/commands/AuthmeCommand.java b/proxy/src/main/java/com/glyart/authmevelocity/proxy/commands/AuthmeCommand.java index 106cf6e..676225a 100644 --- a/proxy/src/main/java/com/glyart/authmevelocity/proxy/commands/AuthmeCommand.java +++ b/proxy/src/main/java/com/glyart/authmevelocity/proxy/commands/AuthmeCommand.java @@ -9,8 +9,7 @@ import com.velocitypowered.api.command.CommandMeta; import com.velocitypowered.api.command.CommandSource; -import net.kyori.adventure.text.Component; -import net.kyori.adventure.text.format.NamedTextColor; +import net.kyori.adventure.text.minimessage.MiniMessage; public class AuthmeCommand { private AuthmeCommand() {} @@ -23,10 +22,11 @@ public static void register(AuthMeVelocityPlugin plugin, CommandManager manager) CommandSource source = cmd.getSource(); if (plugin.reload()) { plugin.sendInfoMessage(); - source.sendMessage(Component.text("AuthmeVelocity has been successfully reloaded", NamedTextColor.GREEN)); + source.sendMessage(MiniMessage.miniMessage().deserialize( + "AuthmeVelocity has been successfully reloaded")); } else { - source.sendMessage(Component.text( - "There was an error while reloading the configuration. Check the server console", NamedTextColor.DARK_RED)); + source.sendMessage(MiniMessage.miniMessage().deserialize( + "There was an error while reloading the configuration. Check the server console")); } return Command.SINGLE_SUCCESS; }) From 609a02e3d8d18b5ab7a5e9c85312407ff962f0bb Mon Sep 17 00:00:00 2001 From: 4drian3d Date: Wed, 27 Jul 2022 16:58:30 +0000 Subject: [PATCH 6/6] fix: Catch IllegalStateException on invalid configuration load --- .../glyart/authmevelocity/proxy/AuthMeVelocityPlugin.java | 7 +++++-- 1 file changed, 5 insertions(+), 2 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 4c6c6ad..a4ddc1e 100644 --- a/proxy/src/main/java/com/glyart/authmevelocity/proxy/AuthMeVelocityPlugin.java +++ b/proxy/src/main/java/com/glyart/authmevelocity/proxy/AuthMeVelocityPlugin.java @@ -91,9 +91,12 @@ private Toml loadConfig(Path path){ return new Toml().read(Files.newInputStream(configPath)); } catch (IOException ex) { - logger.info("An error ocurred on configuration initialization", ex); + logger.error("An error ocurred on configuration initialization", ex); return null; - } + } catch(IllegalStateException ex) { + logger.error("Invalid configuration provided", ex); + return null; + } } public boolean reload() {