From 63f7af9046eecad38f9cc9e908a09170e1e48703 Mon Sep 17 00:00:00 2001 From: Adrian Date: Sat, 11 Mar 2023 09:30:14 -0500 Subject: [PATCH] feat: implement injection --- .../configuration/ConfigurationContainer.java | 9 +--- .../paper/AuthMeVelocityPlugin.java | 8 ++-- .../velocity/AuthMePlaceholders.java | 14 ++++--- .../velocity/AuthMeVelocityPlugin.java | 41 +++++++++++-------- .../velocity/commands/AuthMeCommand.java | 15 +++---- .../velocity/listener/ConnectListener.java | 20 ++++----- .../velocity/listener/FastLoginListener.java | 12 +++--- .../listener/PluginMessageListener.java | 32 +++++++-------- .../velocity/listener/ProxyListener.java | 8 ++-- 9 files changed, 78 insertions(+), 81 deletions(-) diff --git a/common/src/main/java/io/github/_4drian3d/authmevelocity/common/configuration/ConfigurationContainer.java b/common/src/main/java/io/github/_4drian3d/authmevelocity/common/configuration/ConfigurationContainer.java index 821ac73..38ad2f0 100644 --- a/common/src/main/java/io/github/_4drian3d/authmevelocity/common/configuration/ConfigurationContainer.java +++ b/common/src/main/java/io/github/_4drian3d/authmevelocity/common/configuration/ConfigurationContainer.java @@ -24,7 +24,7 @@ import org.spongepowered.configurate.CommentedConfigurationNode; import org.spongepowered.configurate.ConfigurateException; import org.spongepowered.configurate.hocon.HoconConfigurationLoader; -public class ConfigurationContainer { +public final class ConfigurationContainer { private C config; private final HoconConfigurationLoader loader; private final Class clazz; @@ -45,16 +45,11 @@ public class ConfigurationContainer { public CompletableFuture reload() { return CompletableFuture.runAsync(() -> { - C newConfig = null; try { final CommentedConfigurationNode node = loader.load(); - newConfig = node.get(clazz); + config = node.get(clazz); } catch (ConfigurateException exception) { throw new CompletionException("Could not load config.conf file", exception); - } finally { - if (newConfig != null) { - config = newConfig; - } } }); } diff --git a/paper/src/main/java/io/github/_4drian3d/authmevelocity/paper/AuthMeVelocityPlugin.java b/paper/src/main/java/io/github/_4drian3d/authmevelocity/paper/AuthMeVelocityPlugin.java index 34ae532..d96dbbc 100644 --- a/paper/src/main/java/io/github/_4drian3d/authmevelocity/paper/AuthMeVelocityPlugin.java +++ b/paper/src/main/java/io/github/_4drian3d/authmevelocity/paper/AuthMeVelocityPlugin.java @@ -75,17 +75,17 @@ public final class AuthMeVelocityPlugin extends JavaPlugin { public void sendMessageToProxy( final Player player, final @NotNull MessageType type, - final @NotNull String playername + final @NotNull String playerName ) { @SuppressWarnings("UnstableApiUsage") final ByteArrayDataOutput out = ByteStreams.newDataOutput(); out.writeUTF(type.toString()); - out.writeUTF(playername); + out.writeUTF(playerName); if (player == null) { - logDebug("MessageToProxy | Null Player, Player Name: " + playername); + logDebug("MessageToProxy | Null Player, Player Name: " + playerName); Bukkit.getServer().sendPluginMessage(this, CHANNEL, out.toByteArray()); } else { - logDebug("MessageToProxy | Player Present: " + player.getName() + ", Player Name: " + playername); + logDebug("MessageToProxy | Player Present: " + player.getName() + ", Player Name: " + playerName); player.sendPluginMessage(this, CHANNEL, out.toByteArray()); } } diff --git a/velocity/src/main/java/io/github/_4drian3d/authmevelocity/velocity/AuthMePlaceholders.java b/velocity/src/main/java/io/github/_4drian3d/authmevelocity/velocity/AuthMePlaceholders.java index 08c2876..6a60b0e 100644 --- a/velocity/src/main/java/io/github/_4drian3d/authmevelocity/velocity/AuthMePlaceholders.java +++ b/velocity/src/main/java/io/github/_4drian3d/authmevelocity/velocity/AuthMePlaceholders.java @@ -17,7 +17,9 @@ package io.github._4drian3d.authmevelocity.velocity; +import com.google.inject.Inject; import com.velocitypowered.api.proxy.Player; +import com.velocitypowered.api.proxy.ProxyServer; import io.github.miniplaceholders.api.Expansion; import net.kyori.adventure.text.minimessage.tag.Tag; @@ -25,10 +27,12 @@ import static io.github.miniplaceholders.api.utils.Components.FALSE_COMPONENT; import static io.github.miniplaceholders.api.utils.Components.TRUE_COMPONENT; final class AuthMePlaceholders { - private AuthMePlaceholders() { - } + @Inject + private AuthMeVelocityPlugin plugin; + @Inject + private ProxyServer proxyServer; - static Expansion getExpansion(AuthMeVelocityPlugin plugin) { + Expansion getExpansion() { return Expansion.builder("authme") .filter(Player.class) // Logged Placeholders @@ -37,7 +41,7 @@ final class AuthMePlaceholders { .globalPlaceholder("is_player_logged", (queue, ctx) -> { String playerName = queue.popOr(() -> "you need to provide a player").value(); return Tag.selfClosingInserting( - plugin.getProxy().getPlayer(playerName) + proxyServer.getPlayer(playerName) .map(plugin::isLogged) .orElse(false) ? TRUE_COMPONENT : FALSE_COMPONENT ); @@ -48,7 +52,7 @@ final class AuthMePlaceholders { .globalPlaceholder("player_in_auth_server", (queue, ctx) -> { String playerName = queue.popOr(() -> "you need to provide a player").value(); return Tag.selfClosingInserting( - plugin.getProxy().getPlayer(playerName) + proxyServer.getPlayer(playerName) .map(plugin::isInAuthServer) .orElse(false) ? TRUE_COMPONENT : FALSE_COMPONENT ); diff --git a/velocity/src/main/java/io/github/_4drian3d/authmevelocity/velocity/AuthMeVelocityPlugin.java b/velocity/src/main/java/io/github/_4drian3d/authmevelocity/velocity/AuthMeVelocityPlugin.java index d0971bb..0a7d71f 100644 --- a/velocity/src/main/java/io/github/_4drian3d/authmevelocity/velocity/AuthMeVelocityPlugin.java +++ b/velocity/src/main/java/io/github/_4drian3d/authmevelocity/velocity/AuthMeVelocityPlugin.java @@ -18,11 +18,14 @@ package io.github._4drian3d.authmevelocity.velocity; import com.google.inject.Inject; +import com.google.inject.Injector; import com.velocitypowered.api.command.CommandSource; +import com.velocitypowered.api.event.EventManager; import com.velocitypowered.api.event.Subscribe; import com.velocitypowered.api.event.proxy.ProxyInitializeEvent; import com.velocitypowered.api.plugin.Dependency; import com.velocitypowered.api.plugin.Plugin; +import com.velocitypowered.api.plugin.PluginManager; import com.velocitypowered.api.plugin.annotation.DataDirectory; import com.velocitypowered.api.proxy.Player; import com.velocitypowered.api.proxy.ProxyServer; @@ -50,11 +53,11 @@ import org.jetbrains.annotations.NotNull; import org.slf4j.Logger; import java.nio.file.Path; -import java.util.List; import java.util.Set; import java.util.UUID; import java.util.concurrent.ConcurrentHashMap; import java.util.function.Predicate; +import java.util.stream.Stream; @Plugin( id = "authmevelocity", @@ -82,12 +85,18 @@ public final class AuthMeVelocityPlugin implements AuthMeVelocityAPI { @Inject private ProxyServer proxy; @Inject + private EventManager eventManager; + @Inject + private PluginManager pluginManager; + @Inject private Logger logger; @Inject @DataDirectory private Path pluginDirectory; @Inject private Metrics.Factory metricsFactory; + @Inject + private Injector injector; private ConfigurationContainer config; final Set loggedPlayers = ConcurrentHashMap.newKeySet(); @@ -97,7 +106,7 @@ public final class AuthMeVelocityPlugin implements AuthMeVelocityAPI { final LibsManager libraries = new LibsManager( new VelocityLibraryManager<>( - logger, pluginDirectory, proxy.getPluginManager(), this)); + logger, pluginDirectory, pluginManager, this)); libraries.loadLibraries(); try { @@ -114,36 +123,32 @@ public final class AuthMeVelocityPlugin implements AuthMeVelocityAPI { proxy.getChannelRegistrar().register(MODERN_CHANNEL, LEGACY_CHANNEL); - List.of( - new ProxyListener(this), - new ConnectListener(this, proxy, logger), - new PluginMessageListener(proxy, logger, this) - ).forEach(listener -> - proxy.getEventManager().register(this, listener)); + Stream.of( + ProxyListener.class, + ConnectListener.class, + PluginMessageListener.class + ).map(injector::getInstance) + .forEach(listener -> eventManager.register(this, listener)); - final boolean fastlogin = proxy.getPluginManager().isLoaded("fastlogin"); + final boolean fastlogin = pluginManager.isLoaded("fastlogin"); metrics.addCustomChart(new SimplePie("fastlogin_compatibility", () -> Boolean.toString(fastlogin))); if (fastlogin) { logDebug("Register FastLogin compatibility"); - proxy.getEventManager().register(this, new FastLoginListener(proxy, this)); + eventManager.register(this, injector.getInstance(FastLoginListener.class)); } - final boolean miniplaceholders = proxy.getPluginManager().isLoaded("miniplaceholders"); + final boolean miniplaceholders = pluginManager.isLoaded("miniplaceholders"); metrics.addCustomChart(new SimplePie("miniplaceholders_compatibility", () -> Boolean.toString(miniplaceholders))); if (miniplaceholders) { logDebug("Register MiniPlaceholders compatibility"); - AuthMePlaceholders.getExpansion(this).register(); + injector.getInstance(AuthMePlaceholders.class).getExpansion().register(); } - AuthMeCommand.register(this, proxy.getCommandManager(), logger); + injector.getInstance(AuthMeCommand.class).register(); this.sendInfoMessage(); } - ProxyServer getProxy(){ - return this.proxy; - } - public void sendInfoMessage() { final CommandSource source = proxy.getConsoleCommandSource(); source.sendMessage(MiniMessage.miniMessage().deserialize( @@ -182,7 +187,7 @@ public final class AuthMeVelocityPlugin implements AuthMeVelocityAPI { @Override public void removePlayerIf(@NotNull Predicate predicate){ - loggedPlayers.removeIf(uuid -> predicate.test(getProxy().getPlayer(uuid).orElse(null))); + loggedPlayers.removeIf(uuid -> predicate.test(proxy.getPlayer(uuid).orElse(null))); } @Override diff --git a/velocity/src/main/java/io/github/_4drian3d/authmevelocity/velocity/commands/AuthMeCommand.java b/velocity/src/main/java/io/github/_4drian3d/authmevelocity/velocity/commands/AuthMeCommand.java index 2fea4e0..65c73c7 100644 --- a/velocity/src/main/java/io/github/_4drian3d/authmevelocity/velocity/commands/AuthMeCommand.java +++ b/velocity/src/main/java/io/github/_4drian3d/authmevelocity/velocity/commands/AuthMeCommand.java @@ -17,6 +17,7 @@ package io.github._4drian3d.authmevelocity.velocity.commands; +import com.google.inject.Inject; import com.mojang.brigadier.Command; import com.mojang.brigadier.builder.LiteralArgumentBuilder; import com.velocitypowered.api.command.BrigadierCommand; @@ -29,14 +30,14 @@ import net.kyori.adventure.text.minimessage.MiniMessage; import org.slf4j.Logger; public class AuthMeCommand { - private AuthMeCommand() { - } + @Inject + private AuthMeVelocityPlugin plugin; + @Inject + private CommandManager manager; + @Inject + private Logger logger; - public static void register( - final AuthMeVelocityPlugin plugin, - final CommandManager manager, - final Logger logger - ) { + public void register() { final var command = LiteralArgumentBuilder.literal("authmevelocity") .requires(src -> src.hasPermission("authmevelocity.commands")) .then(LiteralArgumentBuilder.literal("reload") diff --git a/velocity/src/main/java/io/github/_4drian3d/authmevelocity/velocity/listener/ConnectListener.java b/velocity/src/main/java/io/github/_4drian3d/authmevelocity/velocity/listener/ConnectListener.java index 3835d7b..9d65134 100644 --- a/velocity/src/main/java/io/github/_4drian3d/authmevelocity/velocity/listener/ConnectListener.java +++ b/velocity/src/main/java/io/github/_4drian3d/authmevelocity/velocity/listener/ConnectListener.java @@ -19,6 +19,7 @@ package io.github._4drian3d.authmevelocity.velocity.listener; import com.google.common.io.ByteArrayDataOutput; import com.google.common.io.ByteStreams; +import com.google.inject.Inject; import com.velocitypowered.api.event.Continuation; import com.velocitypowered.api.event.PostOrder; import com.velocitypowered.api.event.Subscribe; @@ -35,19 +36,12 @@ import org.slf4j.Logger; import java.util.Optional; public final class ConnectListener { - private final ProxyServer proxy; - private final Logger logger; - private final AuthMeVelocityPlugin plugin; - - public ConnectListener( - final AuthMeVelocityPlugin plugin, - final ProxyServer proxy, - final Logger logger - ) { - this.plugin = plugin; - this.logger = logger; - this.proxy = proxy; - } + @Inject + private ProxyServer proxy; + @Inject + private Logger logger; + @Inject + private AuthMeVelocityPlugin plugin; @Subscribe(order = PostOrder.LATE) public void onInitialServer( diff --git a/velocity/src/main/java/io/github/_4drian3d/authmevelocity/velocity/listener/FastLoginListener.java b/velocity/src/main/java/io/github/_4drian3d/authmevelocity/velocity/listener/FastLoginListener.java index 8aca1e3..7781575 100644 --- a/velocity/src/main/java/io/github/_4drian3d/authmevelocity/velocity/listener/FastLoginListener.java +++ b/velocity/src/main/java/io/github/_4drian3d/authmevelocity/velocity/listener/FastLoginListener.java @@ -18,17 +18,17 @@ package io.github._4drian3d.authmevelocity.velocity.listener; import com.github.games647.fastlogin.velocity.event.VelocityFastLoginAutoLoginEvent; +import com.google.inject.Inject; import com.velocitypowered.api.event.Subscribe; import com.velocitypowered.api.proxy.ProxyServer; import io.github._4drian3d.authmevelocity.velocity.AuthMeVelocityPlugin; public class FastLoginListener { - private final ProxyServer proxy; - private final AuthMeVelocityPlugin plugin; - public FastLoginListener(ProxyServer proxy, AuthMeVelocityPlugin plugin){ - this.proxy = proxy; - this.plugin = plugin; - } + @Inject + private ProxyServer proxy; + @Inject + private AuthMeVelocityPlugin plugin; + @Subscribe public void onAutoLogin(VelocityFastLoginAutoLoginEvent event){ plugin.logDebug("VelocityFastLoginAutoLoginEvent | Attempt to auto register player"); diff --git a/velocity/src/main/java/io/github/_4drian3d/authmevelocity/velocity/listener/PluginMessageListener.java b/velocity/src/main/java/io/github/_4drian3d/authmevelocity/velocity/listener/PluginMessageListener.java index 2ca3ead..37f8cab 100644 --- a/velocity/src/main/java/io/github/_4drian3d/authmevelocity/velocity/listener/PluginMessageListener.java +++ b/velocity/src/main/java/io/github/_4drian3d/authmevelocity/velocity/listener/PluginMessageListener.java @@ -18,7 +18,9 @@ package io.github._4drian3d.authmevelocity.velocity.listener; import com.google.common.io.ByteArrayDataInput; +import com.google.inject.Inject; import com.velocitypowered.api.event.Continuation; +import com.velocitypowered.api.event.EventManager; import com.velocitypowered.api.event.Subscribe; import com.velocitypowered.api.event.connection.PluginMessageEvent; import com.velocitypowered.api.proxy.Player; @@ -29,22 +31,20 @@ import io.github._4drian3d.authmevelocity.api.velocity.event.*; import io.github._4drian3d.authmevelocity.common.MessageType; import io.github._4drian3d.authmevelocity.velocity.AuthMeVelocityPlugin; import io.github._4drian3d.authmevelocity.velocity.utils.AuthMeUtils; -import org.jetbrains.annotations.NotNull; import org.jetbrains.annotations.Nullable; import org.slf4j.Logger; import java.util.Locale; public class PluginMessageListener { - private final ProxyServer proxy; - private final Logger logger; - private final AuthMeVelocityPlugin plugin; - - public PluginMessageListener(@NotNull ProxyServer proxy, @NotNull Logger logger, AuthMeVelocityPlugin plugin) { - this.proxy = proxy; - this.logger = logger; - this.plugin = plugin; - } + @Inject + private ProxyServer proxy; + @Inject + private EventManager eventManager; + @Inject + private Logger logger; + @Inject + private AuthMeVelocityPlugin plugin; @Subscribe public void onPluginMessage(final PluginMessageEvent event, Continuation continuation) { @@ -73,7 +73,7 @@ public class PluginMessageListener { case LOGIN -> { plugin.logDebug("PluginMessageEvent | Login type"); if (player != null && plugin.addPlayer(player)) { - proxy.getEventManager().fireAndForget(new ProxyLoginEvent(player)); + eventManager.fireAndForget(new ProxyLoginEvent(player)); if (plugin.config().get().sendOnLogin().sendToServerOnLogin()) { this.createServerConnectionRequest(player, connection); } @@ -83,14 +83,14 @@ public class PluginMessageListener { case LOGOUT -> { plugin.logDebug("PluginMessageEvent | Logout type"); if (player != null && plugin.removePlayer(player)){ - proxy.getEventManager().fireAndForget(new ProxyLogoutEvent(player)); + eventManager.fireAndForget(new ProxyLogoutEvent(player)); plugin.logDebug("PluginMessageEvent | Player not null"); } } case REGISTER -> { plugin.logDebug("PluginMessageEvent | Register"); if (player != null) { - proxy.getEventManager().fireAndForget(new ProxyRegisterEvent(player)); + eventManager.fireAndForget(new ProxyRegisterEvent(player)); plugin.logDebug("PluginMessageEvent | Player not null"); } } @@ -98,11 +98,11 @@ public class PluginMessageListener { plugin.logDebug("PluginMessageEvent | Unregister type"); if (player != null) { plugin.logDebug("PluginMessageEvent | Player not null"); - proxy.getEventManager().fireAndForget(new ProxyUnregisterEvent(player)); + eventManager.fireAndForget(new ProxyUnregisterEvent(player)); } } case FORCE_UNREGISTER -> { - proxy.getEventManager().fireAndForget(new ProxyForcedUnregisterEvent(player)); + eventManager.fireAndForget(new ProxyForcedUnregisterEvent(player)); plugin.logDebug("PluginMessageEvent | Forced Unregister type"); } @@ -127,7 +127,7 @@ public class PluginMessageListener { return; } - proxy.getEventManager().fire(new PreSendOnLoginEvent(player, loginServer, toSend.object())) + eventManager.fire(new PreSendOnLoginEvent(player, loginServer, toSend.object())) .thenAccept(event -> { if (!event.getResult().isAllowed()) { return; diff --git a/velocity/src/main/java/io/github/_4drian3d/authmevelocity/velocity/listener/ProxyListener.java b/velocity/src/main/java/io/github/_4drian3d/authmevelocity/velocity/listener/ProxyListener.java index 37ef598..8447e7c 100644 --- a/velocity/src/main/java/io/github/_4drian3d/authmevelocity/velocity/listener/ProxyListener.java +++ b/velocity/src/main/java/io/github/_4drian3d/authmevelocity/velocity/listener/ProxyListener.java @@ -17,6 +17,7 @@ package io.github._4drian3d.authmevelocity.velocity.listener; +import com.google.inject.Inject; import com.velocitypowered.api.event.Continuation; import com.velocitypowered.api.event.EventTask; import com.velocitypowered.api.event.PostOrder; @@ -31,11 +32,8 @@ import io.github._4drian3d.authmevelocity.velocity.utils.AuthMeUtils; import net.kyori.adventure.text.minimessage.MiniMessage; public final class ProxyListener { - private final AuthMeVelocityPlugin plugin; - - public ProxyListener(AuthMeVelocityPlugin plugin) { - this.plugin = plugin; - } + @Inject + private AuthMeVelocityPlugin plugin; @Subscribe public EventTask onDisconnect(final DisconnectEvent event) {