From d97688a480d16668acded70c41174f39ad484557 Mon Sep 17 00:00:00 2001 From: games647 Date: Tue, 20 Jun 2023 16:23:23 +0200 Subject: [PATCH] Improve logging for velocity module --- .../velocity/CommandForwardVelocity.java | 42 ++----------- .../velocity/MessageListener.java | 63 ++++++++++++++----- 2 files changed, 51 insertions(+), 54 deletions(-) diff --git a/velocity/src/main/java/com/github/games647/commandforward/velocity/CommandForwardVelocity.java b/velocity/src/main/java/com/github/games647/commandforward/velocity/CommandForwardVelocity.java index ebe0b33..2cb5e1c 100644 --- a/velocity/src/main/java/com/github/games647/commandforward/velocity/CommandForwardVelocity.java +++ b/velocity/src/main/java/com/github/games647/commandforward/velocity/CommandForwardVelocity.java @@ -1,24 +1,14 @@ package com.github.games647.commandforward.velocity; import com.google.inject.Inject; -import com.velocitypowered.api.command.CommandSource; import com.velocitypowered.api.event.Subscribe; import com.velocitypowered.api.event.proxy.ProxyInitializeEvent; import com.velocitypowered.api.plugin.Plugin; -import com.velocitypowered.api.plugin.annotation.DataDirectory; import com.velocitypowered.api.proxy.ProxyServer; import com.velocitypowered.api.proxy.messages.ChannelIdentifier; import com.velocitypowered.api.proxy.messages.MinecraftChannelIdentifier; -import com.velocitypowered.api.proxy.server.RegisteredServer; -import net.kyori.adventure.text.Component; -import net.kyori.adventure.text.format.NamedTextColor; import org.slf4j.Logger; -import java.nio.file.Path; -import java.util.ArrayList; -import java.util.List; -import java.util.Optional; - /** * * Velocity support for CommandForward plugin @@ -32,7 +22,7 @@ @Plugin( id = "commandforward", name = "CommandForward", - version = "0.4.0", + version = "0.5.0", description = "Forwards commands from Bukkit to BungeeCord (Or Velocity) to execute it there", authors = {"games647", "https://github.com/games647/CommandForward/graphs/contributors"} ) @@ -40,16 +30,11 @@ public class CommandForwardVelocity { private final ChannelIdentifier MESSAGE_CHANNEL = MinecraftChannelIdentifier.from("commandforward:cmd"); - private static Optional instance; private final ProxyServer proxyServer; private final Logger logger; - private final List lobbies = new ArrayList<>(); - private final List bedwars = new ArrayList<>(); - @Inject - public CommandForwardVelocity(ProxyServer proxyServer, Logger logger, @DataDirectory Path dataDirectory) { - instance = Optional.of(this); + public CommandForwardVelocity(ProxyServer proxyServer, Logger logger) { this.proxyServer = proxyServer; this.logger = logger; } @@ -58,27 +43,8 @@ public CommandForwardVelocity(ProxyServer proxyServer, Logger logger, @DataDirec public void onProxyInit(ProxyInitializeEvent event) { // Register the custom messaging channel proxyServer.getChannelRegistrar().register(MESSAGE_CHANNEL); - // Register an event handler to catch messages for it - proxyServer.getEventManager().register(this, new MessageListener(MESSAGE_CHANNEL)); - - } - /** - * Print an error message - * - * @param source Sender that execute the current command - * @param message Message to send to command sender - */ - private void sendErrorMessage(CommandSource source, String message) { - Component textComponent = Component.text(String.format("[%s] %s", "CommandForward", message), NamedTextColor.RED); - source.sendMessage(textComponent); - } - - public static CommandForwardVelocity getInstance() { - return instance.orElseThrow(IllegalAccessError::new); - } - - public ProxyServer getProxyServer() { - return proxyServer; + // Register an event handler to catch messages for it + proxyServer.getEventManager().register(this, new MessageListener(proxyServer, logger, MESSAGE_CHANNEL)); } } diff --git a/velocity/src/main/java/com/github/games647/commandforward/velocity/MessageListener.java b/velocity/src/main/java/com/github/games647/commandforward/velocity/MessageListener.java index 8c1eeda..fbe1692 100644 --- a/velocity/src/main/java/com/github/games647/commandforward/velocity/MessageListener.java +++ b/velocity/src/main/java/com/github/games647/commandforward/velocity/MessageListener.java @@ -8,50 +8,81 @@ import com.velocitypowered.api.event.connection.PluginMessageEvent; import com.velocitypowered.api.plugin.PluginManager; import com.velocitypowered.api.proxy.Player; +import com.velocitypowered.api.proxy.ProxyServer; import com.velocitypowered.api.proxy.ServerConnection; import com.velocitypowered.api.proxy.messages.ChannelIdentifier; +import net.kyori.adventure.audience.Audience; +import net.kyori.adventure.text.Component; +import net.kyori.adventure.text.format.NamedTextColor; +import org.slf4j.Logger; public class MessageListener { + + private final ProxyServer server; + private final Logger logger; private final ChannelIdentifier identifier; - public MessageListener(ChannelIdentifier identifier){ + public MessageListener(ProxyServer server, Logger logger, ChannelIdentifier identifier) { + this.server = server; + this.logger = logger; this.identifier = identifier; } @Subscribe - public void onPluginMessageEvent(PluginMessageEvent event){ + public void onPluginMessageEvent(PluginMessageEvent event) { // Received plugin message, check channel identifier matches - if(event.getIdentifier().equals(identifier)){ + if (event.getIdentifier().equals(identifier)) { // Since this message was meant for this listener set it to handled // We do this so the message doesn't get routed through. event.setResult(PluginMessageEvent.ForwardResult.handled()); - if(event.getSource() instanceof ServerConnection){ + if (event.getSource() instanceof ServerConnection) { // Read the data written to the message - Player p = ((ServerConnection) event.getSource()).getPlayer(); + Player player = ((ServerConnection) event.getSource()).getPlayer(); ByteArrayDataInput in = ByteStreams.newDataInput(event.getData()); - parseMessage(p, in); + parseMessage(player, in); } } } - private void parseMessage(CommandSource source, ByteArrayDataInput dataInput) { - final boolean isPlayer = dataInput.readBoolean(); - final String command = dataInput.readUTF(); - final String arguments = dataInput.readUTF(); - final CommandSource invoker = (isPlayer) ? source : CommandForwardVelocity.getInstance().getProxyServer().getConsoleCommandSource(); + private void parseMessage(Player source, ByteArrayDataInput dataInput) { + boolean isPlayer = dataInput.readBoolean(); + String command = dataInput.readUTF(); + String arguments = dataInput.readUTF(); + CommandSource invoker = (isPlayer) ? source : server.getConsoleCommandSource(); + + String username = source.getUsername(); + logger.info("Received new forward command '{}' with Args: '{}' from {}", command, arguments, username); invokeCommand(invoker, dataInput.readBoolean(), command, arguments); } private void invokeCommand(CommandSource invoker, boolean isOp, String command, String arguments) { - PluginManager pluginManager = CommandForwardVelocity.getInstance().getProxyServer().getPluginManager(); - CommandManager commandManager = CommandForwardVelocity.getInstance().getProxyServer().getCommandManager(); + PluginManager pluginManager = server.getPluginManager(); + CommandManager commandManager = server.getCommandManager(); // TODO implement isOp handle progress - - commandManager.executeAsync(invoker, command + " " + arguments); + commandManager.executeImmediatelyAsync(invoker, command + ' ' + arguments) + .thenAccept(success -> { + if (!success) { + sendErrorMessage(invoker, "Failed to find command"); + } + }) + .exceptionally(error -> { + logger.warn("Failed to invoke forwarded command", error); + return null; + }); } - + /** + * Print an error message + * + * @param source Sender that execute the current command + * @param message Message to send to command sender + */ + private void sendErrorMessage(Audience source, String message) { + String msg = String.format("[%s] %s", "CommandForward", message); + Component textComponent = Component.text(msg, NamedTextColor.RED); + source.sendMessage(textComponent); + } }