From 5f9764dabc5b79f0d267136f5ece1008a99f0426 Mon Sep 17 00:00:00 2001 From: asofold Date: Thu, 4 Dec 2014 03:39:47 +0100 Subject: [PATCH] Add command "ncp debug player (player1) ...". This is the first simple version, just setting debug for all checks for the player(s). It can only be undone by removing the data, e.g. with "ncp remove (player)", reloading does it too, but is much heavier. --- .../command/NoCheatPlusCommand.java | 2 + .../command/admin/CommandsCommand.java | 5 +- .../command/admin/ReloadCommand.java | 5 ++ .../command/admin/debug/DebugCommand.java | 16 +++++ .../admin/debug/DebugPlayerCommand.java | 58 +++++++++++++++++++ .../nocheatplus/players/DataManager.java | 38 ++++++++++++ NCPPlugin/src/main/resources/plugin.yml | 2 + 7 files changed, 124 insertions(+), 2 deletions(-) create mode 100644 NCPCore/src/main/java/fr/neatmonster/nocheatplus/command/admin/debug/DebugCommand.java create mode 100644 NCPCore/src/main/java/fr/neatmonster/nocheatplus/command/admin/debug/DebugPlayerCommand.java diff --git a/NCPCore/src/main/java/fr/neatmonster/nocheatplus/command/NoCheatPlusCommand.java b/NCPCore/src/main/java/fr/neatmonster/nocheatplus/command/NoCheatPlusCommand.java index fcdf2560..30f45010 100644 --- a/NCPCore/src/main/java/fr/neatmonster/nocheatplus/command/NoCheatPlusCommand.java +++ b/NCPCore/src/main/java/fr/neatmonster/nocheatplus/command/NoCheatPlusCommand.java @@ -25,6 +25,7 @@ import fr.neatmonster.nocheatplus.command.admin.LagCommand; import fr.neatmonster.nocheatplus.command.admin.ReloadCommand; import fr.neatmonster.nocheatplus.command.admin.RemovePlayerCommand; import fr.neatmonster.nocheatplus.command.admin.VersionCommand; +import fr.neatmonster.nocheatplus.command.admin.debug.DebugCommand; import fr.neatmonster.nocheatplus.command.admin.exemption.ExemptCommand; import fr.neatmonster.nocheatplus.command.admin.exemption.ExemptionsCommand; import fr.neatmonster.nocheatplus.command.admin.exemption.UnexemptCommand; @@ -103,6 +104,7 @@ public class NoCheatPlusCommand extends BaseCommand{ new AllowLoginCommand(plugin), new LogCommand(plugin), new ResetCommand(plugin), + new DebugCommand(plugin), }){ addSubCommands(cmd); rootLabels.add(cmd.label); diff --git a/NCPCore/src/main/java/fr/neatmonster/nocheatplus/command/admin/CommandsCommand.java b/NCPCore/src/main/java/fr/neatmonster/nocheatplus/command/admin/CommandsCommand.java index 977e6fda..2fe76bb0 100644 --- a/NCPCore/src/main/java/fr/neatmonster/nocheatplus/command/admin/CommandsCommand.java +++ b/NCPCore/src/main/java/fr/neatmonster/nocheatplus/command/admin/CommandsCommand.java @@ -25,13 +25,14 @@ public class CommandsCommand extends BaseCommand { "/ delay [delay=(ticks)] (command)...: delay a command", "/ denylogin [delay=(ticks)] (player) (minutes) [(reason)...]", "More administrative commands:", + "/ log counters: Show some stats/debug counters summary.", + "/ reset counters: Reset some stats/debug counters", + "/ debug player (player): Log debug info for the player.", "/ denylist: Show players, currently denied to log in.", "/ allowlogin (player): Allow a player to login again.", "/ exemptions (player): Show exemptions.", "/ exempt (player) [(check type)]: Exempt a player.", "/ unexempt (player) [(check type)]: Unexempt a player.", - "/ log counters: Show some stats/debug counters summary.", - "/ reset counters: Reset some stats/debug counters", }; final String allCommands; diff --git a/NCPCore/src/main/java/fr/neatmonster/nocheatplus/command/admin/ReloadCommand.java b/NCPCore/src/main/java/fr/neatmonster/nocheatplus/command/admin/ReloadCommand.java index 37226f3b..c2afd79f 100644 --- a/NCPCore/src/main/java/fr/neatmonster/nocheatplus/command/admin/ReloadCommand.java +++ b/NCPCore/src/main/java/fr/neatmonster/nocheatplus/command/admin/ReloadCommand.java @@ -59,13 +59,18 @@ public class ReloadCommand extends BaseCommand { // Remove all cached configs. DataManager.clearConfigs(); // There you have to add XConfig.clear() form now on. + // Remove some checks data. + // TODO: Better concept (INotifyReload). for (final CheckType checkType : new CheckType[]{ CheckType.BLOCKBREAK, CheckType.FIGHT, }){ DataManager.clearData(checkType); } + // Reset debug flags to default (temp, heavy). + DataManager.restoreDefaultDebugFlags(); + // Tell the registered listeners to adapt to new config, first sort them (!). Collections.sort(notifyReload, Order.cmpSetupOrder); for (final INotifyReload component : notifyReload){ diff --git a/NCPCore/src/main/java/fr/neatmonster/nocheatplus/command/admin/debug/DebugCommand.java b/NCPCore/src/main/java/fr/neatmonster/nocheatplus/command/admin/debug/DebugCommand.java new file mode 100644 index 00000000..f2ea7564 --- /dev/null +++ b/NCPCore/src/main/java/fr/neatmonster/nocheatplus/command/admin/debug/DebugCommand.java @@ -0,0 +1,16 @@ +package fr.neatmonster.nocheatplus.command.admin.debug; + +import org.bukkit.plugin.java.JavaPlugin; + +import fr.neatmonster.nocheatplus.command.BaseCommand; +import fr.neatmonster.nocheatplus.permissions.Permissions; + +public class DebugCommand extends BaseCommand { + + public DebugCommand(JavaPlugin access) { + super(access, "debug", Permissions.COMMAND_DEBUG); + addSubCommands(new DebugPlayerCommand(access)); + // TODO: Sub command check, plus check type, plus -(-)off switch + } + +} diff --git a/NCPCore/src/main/java/fr/neatmonster/nocheatplus/command/admin/debug/DebugPlayerCommand.java b/NCPCore/src/main/java/fr/neatmonster/nocheatplus/command/admin/debug/DebugPlayerCommand.java new file mode 100644 index 00000000..a5522130 --- /dev/null +++ b/NCPCore/src/main/java/fr/neatmonster/nocheatplus/command/admin/debug/DebugPlayerCommand.java @@ -0,0 +1,58 @@ +package fr.neatmonster.nocheatplus.command.admin.debug; + +import java.util.List; + +import org.bukkit.command.Command; +import org.bukkit.command.CommandSender; +import org.bukkit.entity.Player; +import org.bukkit.plugin.java.JavaPlugin; + +import fr.neatmonster.nocheatplus.checks.CheckType; +import fr.neatmonster.nocheatplus.checks.access.CheckDataFactory; +import fr.neatmonster.nocheatplus.checks.access.ICheckData; +import fr.neatmonster.nocheatplus.command.BaseCommand; +import fr.neatmonster.nocheatplus.players.DataManager; + +public class DebugPlayerCommand extends BaseCommand { + + public DebugPlayerCommand(JavaPlugin plugin) { + super(plugin, "player", null); + } + + @Override + public List onTabComplete(CommandSender sender, Command command, String alias, String[] args) { + return null; // Tab-complete player names. + } + + @Override + public boolean onCommand(CommandSender sender, Command command, String alias, String[] args) { + + // TODO: This is a minimal version just to turn it on (!). Further: check types, -off. + for (int i = 2; i < args.length; i++) { + final String name = args[i]; + final Player player = DataManager.getPlayer(name); + if (player == null) { + sender.sendMessage("Not online: " + name); + } else { + setDebugAll(player); + sender.sendMessage("Set all checks to debug for player: " + player.getName()); + } + } + return true; + } + + private void setDebugAll(final Player player) { + for (final CheckType type : CheckType.values()) { + CheckDataFactory factory = type.getDataFactory(); + if (factory != null) { + ICheckData data = factory.getData(player); + if (data != null) { + data.setDebug(true); + } + } + } + } + + + +} diff --git a/NCPCore/src/main/java/fr/neatmonster/nocheatplus/players/DataManager.java b/NCPCore/src/main/java/fr/neatmonster/nocheatplus/players/DataManager.java index 0442a856..6c294b35 100644 --- a/NCPCore/src/main/java/fr/neatmonster/nocheatplus/players/DataManager.java +++ b/NCPCore/src/main/java/fr/neatmonster/nocheatplus/players/DataManager.java @@ -23,7 +23,10 @@ import org.bukkit.event.player.PlayerQuitEvent; import fr.neatmonster.nocheatplus.checks.CheckType; import fr.neatmonster.nocheatplus.checks.ViolationHistory; +import fr.neatmonster.nocheatplus.checks.access.CheckConfigFactory; import fr.neatmonster.nocheatplus.checks.access.CheckDataFactory; +import fr.neatmonster.nocheatplus.checks.access.ICheckConfig; +import fr.neatmonster.nocheatplus.checks.access.ICheckData; import fr.neatmonster.nocheatplus.checks.blockbreak.BlockBreakConfig; import fr.neatmonster.nocheatplus.checks.blockinteract.BlockInteractConfig; import fr.neatmonster.nocheatplus.checks.blockplace.BlockPlaceConfig; @@ -33,6 +36,7 @@ import fr.neatmonster.nocheatplus.checks.combined.CombinedData; import fr.neatmonster.nocheatplus.checks.fight.FightConfig; import fr.neatmonster.nocheatplus.checks.inventory.InventoryConfig; import fr.neatmonster.nocheatplus.checks.moving.MovingConfig; +import fr.neatmonster.nocheatplus.compat.BridgeMisc; import fr.neatmonster.nocheatplus.components.ComponentRegistry; import fr.neatmonster.nocheatplus.components.ComponentWithName; import fr.neatmonster.nocheatplus.components.ConsistencyChecker; @@ -297,6 +301,40 @@ public class DataManager implements Listener, INotifyReload, INeedConfig, Compon instance.playerData.clear(); // TODO } } + + /** + * Restore the default debug flags within player data, as given in + * corresponding configurations. This only yields the correct result, if the + * the data uses the same configuration for initialization which is + * registered under the same check type. + */ + public static void restoreDefaultDebugFlags() { + final Player[] players = BridgeMisc.getOnlinePlayers(); + for (final CheckType checkType : CheckType.values()) { + final CheckConfigFactory configFactory = checkType.getConfigFactory(); + if (configFactory == null) { + continue; + } + final CheckDataFactory dataFactory = checkType.getDataFactory(); + if (dataFactory == null) { + continue; + } + for (int i = 0; i < players.length; i++) { + final Player player = players[i]; + final ICheckConfig config = configFactory.getConfig(player); + if (config == null) { + continue; + } + final ICheckData data = dataFactory.getData(player); + if (data == null) { + continue; + } + if (config.getDebug() != data.getDebug()) { + data.setDebug(config.getDebug()); + } + } + } + } /** * Remove the player data for a given player and a given check type. CheckType.ALL and null will be interpreted as removing all data.
diff --git a/NCPPlugin/src/main/resources/plugin.yml b/NCPPlugin/src/main/resources/plugin.yml index ea33848f..23710425 100644 --- a/NCPPlugin/src/main/resources/plugin.yml +++ b/NCPPlugin/src/main/resources/plugin.yml @@ -313,6 +313,8 @@ permissions: description: Show various stats/debugging information. [Incomplete, experimental.] nocheatplus.command.reset: description: Reset statistics or debugging counters. + nocheatplus.command.debug: + description: Start logging debug information for a player (lots of output, log file). # Legacy: nocheatplus.command.tempkick: description: Obsolete, use nocheatplus.command.denylogin instead.