From dbe27e92d219cb52808edba0eb04242b78efa2d5 Mon Sep 17 00:00:00 2001 From: Lixfel Date: Sat, 29 Jun 2024 08:46:02 +0200 Subject: [PATCH] Fix command logging Signed-off-by: Lixfel --- .../velocitycore/listeners/ChatListener.java | 23 ++++++++++++++++--- 1 file changed, 20 insertions(+), 3 deletions(-) diff --git a/src/de/steamwar/velocitycore/listeners/ChatListener.java b/src/de/steamwar/velocitycore/listeners/ChatListener.java index a5fbb7a..ad40d31 100644 --- a/src/de/steamwar/velocitycore/listeners/ChatListener.java +++ b/src/de/steamwar/velocitycore/listeners/ChatListener.java @@ -19,11 +19,13 @@ package de.steamwar.velocitycore.listeners; +import com.velocitypowered.api.command.CommandSource; import com.velocitypowered.api.event.PostOrder; import com.velocitypowered.api.event.Subscribe; import com.velocitypowered.api.event.command.CommandExecuteEvent; import com.velocitypowered.api.event.player.PlayerChatEvent; import com.velocitypowered.api.event.player.TabCompleteEvent; +import com.velocitypowered.api.proxy.ConsoleCommandSource; import com.velocitypowered.api.proxy.Player; import de.steamwar.messages.Chatter; import de.steamwar.messages.ChatterGroup; @@ -43,10 +45,13 @@ import java.util.Arrays; import java.util.List; import java.util.concurrent.TimeUnit; import java.util.logging.Level; +import java.util.logging.Logger; import java.util.stream.Collectors; public class ChatListener extends BasicListener { + private static final Logger rootLogger = Logger.getGlobal(); + private static final List rankedModes = ArenaMode.getAllModes().stream().filter(ArenaMode::isRanked).map(ArenaMode::getSchemType).toList(); @Subscribe(order = PostOrder.FIRST) @@ -67,8 +72,20 @@ public class ChatListener extends BasicListener { @Subscribe(order = PostOrder.LAST) public void logCommands(CommandExecuteEvent e) { - if(e.getResult().isAllowed()) - VelocityCore.getLogger().log(Level.INFO, "%s -> executed command %s".formatted(e.getCommandSource(), e.getCommand())); + String command = e.getCommand(); + int space = command.indexOf(' '); + if(e.getResult().isAllowed() && VelocityCore.getProxy().getCommandManager().hasCommand(space != -1 ? command.substring(0, space) : command)) { + CommandSource source = e.getCommandSource(); + String name; + if(source instanceof Player player) + name = player.getUsername(); + else if(source instanceof ConsoleCommandSource) + name = "«CONSOLE»"; + else + name = source.toString(); + + rootLogger.log(Level.INFO, "%s -> executed command /%s".formatted(name, command)); + } } @Subscribe @@ -100,7 +117,7 @@ public class ChatListener extends BasicListener { private static boolean isMistypedCommand(Player player, String message) { String command = message.substring(1); - boolean isCommand = message.startsWith("7") && command.split(" ", 2)[0].matches("[7/]?[A-Za-z]+"); + boolean isCommand = message.startsWith("7") && command.matches("^[7/]?[A-Za-z]+"); if(isCommand && Boolean.FALSE.equals(VelocityCore.getProxy().getCommandManager().executeAsync(player, command).join())) { if(command.startsWith("7")) command = "/" + command.substring(1);