Skip to content

Commit

Permalink
Improve logging for velocity module
Browse files Browse the repository at this point in the history
  • Loading branch information
games647 committed Jun 20, 2023
1 parent 1866959 commit d97688a
Show file tree
Hide file tree
Showing 2 changed files with 51 additions and 54 deletions.
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -32,24 +22,19 @@
@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"}
)
public class CommandForwardVelocity {

private final ChannelIdentifier MESSAGE_CHANNEL = MinecraftChannelIdentifier.from("commandforward:cmd");

private static Optional<CommandForwardVelocity> instance;
private final ProxyServer proxyServer;
private final Logger logger;

private final List<RegisteredServer> lobbies = new ArrayList<>();
private final List<RegisteredServer> 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;
}
Expand All @@ -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));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
}

0 comments on commit d97688a

Please sign in to comment.