From 7bcae640e9b3f1b9905e1cdcc37dea41bf52bb1c Mon Sep 17 00:00:00 2001 From: asofold Date: Sat, 3 Oct 2015 00:19:45 +0200 Subject: [PATCH] Do debug log for block break and place and dig/place packets. Dig/place is only logged if a debug flag is set in the config, it won't activate with on-the-fly per player debug logging. --- .../checks/net/protocollib/DebugAdapter.java | 30 +++ .../net/protocollib/ProtocolLibComponent.java | 5 +- .../checks/blockbreak/BlockBreakListener.java | 195 +++++++++--------- .../checks/blockplace/BlockPlaceListener.java | 10 +- 4 files changed, 143 insertions(+), 97 deletions(-) create mode 100644 NCPCompatProtocolLib/src/main/java/fr/neatmonster/nocheatplus/checks/net/protocollib/DebugAdapter.java diff --git a/NCPCompatProtocolLib/src/main/java/fr/neatmonster/nocheatplus/checks/net/protocollib/DebugAdapter.java b/NCPCompatProtocolLib/src/main/java/fr/neatmonster/nocheatplus/checks/net/protocollib/DebugAdapter.java new file mode 100644 index 00000000..2698496c --- /dev/null +++ b/NCPCompatProtocolLib/src/main/java/fr/neatmonster/nocheatplus/checks/net/protocollib/DebugAdapter.java @@ -0,0 +1,30 @@ +package fr.neatmonster.nocheatplus.checks.net.protocollib; + +import org.bukkit.entity.Player; +import org.bukkit.plugin.Plugin; + +import com.comphenix.protocol.PacketType; +import com.comphenix.protocol.events.ListenerPriority; +import com.comphenix.protocol.events.PacketEvent; + +import fr.neatmonster.nocheatplus.NCPAPIProvider; +import fr.neatmonster.nocheatplus.logging.Streams; + +public class DebugAdapter extends BaseAdapter { + + public DebugAdapter(Plugin plugin) { + super(plugin, ListenerPriority.LOW, new PacketType[] { + PacketType.Play.Client.BLOCK_PLACE, + PacketType.Play.Client.BLOCK_DIG, + }); + } + + @Override + public void onPacketReceiving(PacketEvent event) { + final Player player = event.getPlayer(); + if (dataFactory.getData(player).debug) { + NCPAPIProvider.getNoCheatPlusAPI().getLogManager().debug(Streams.TRACE_FILE, player.getName() + " packet: " + event.getPacketType()); + } + } + +} diff --git a/NCPCompatProtocolLib/src/main/java/fr/neatmonster/nocheatplus/checks/net/protocollib/ProtocolLibComponent.java b/NCPCompatProtocolLib/src/main/java/fr/neatmonster/nocheatplus/checks/net/protocollib/ProtocolLibComponent.java index df72f4a0..f02ca89a 100644 --- a/NCPCompatProtocolLib/src/main/java/fr/neatmonster/nocheatplus/checks/net/protocollib/ProtocolLibComponent.java +++ b/NCPCompatProtocolLib/src/main/java/fr/neatmonster/nocheatplus/checks/net/protocollib/ProtocolLibComponent.java @@ -40,7 +40,10 @@ public class ProtocolLibComponent implements DisableListener, INotifyReload { private void register(Plugin plugin) { StaticLog.logInfo("Adding packet level hooks for ProtocolLib (MC " + ProtocolLibrary.getProtocolManager().getMinecraftVersion().getVersion() + ")..."); - // Register Classes having a constructor with Plugin as argument. + if (ConfigManager.isTrueForAnyConfig(ConfPaths.NET + ConfPaths.SUB_DEBUG) || ConfigManager.isTrueForAnyConfig(ConfPaths.CHECKS_DEBUG) ) { + // (Debug logging. Only activates if debug is set for checks or checks.net, not on the fly.) + register("fr.neatmonster.nocheatplus.checks.net.protocollib.DebugAdapter", plugin); + } if (ConfigManager.isTrueForAnyConfig(ConfPaths.NET_FLYINGFREQUENCY_ACTIVE)) { // (Also sets lastKeepAliveTime, if enabled.) register("fr.neatmonster.nocheatplus.checks.net.protocollib.FlyingFrequency", plugin); diff --git a/NCPCore/src/main/java/fr/neatmonster/nocheatplus/checks/blockbreak/BlockBreakListener.java b/NCPCore/src/main/java/fr/neatmonster/nocheatplus/checks/blockbreak/BlockBreakListener.java index 8bfc0db0..5986874a 100644 --- a/NCPCore/src/main/java/fr/neatmonster/nocheatplus/checks/blockbreak/BlockBreakListener.java +++ b/NCPCore/src/main/java/fr/neatmonster/nocheatplus/checks/blockbreak/BlockBreakListener.java @@ -21,6 +21,7 @@ import fr.neatmonster.nocheatplus.checks.CheckType; import fr.neatmonster.nocheatplus.checks.inventory.Items; import fr.neatmonster.nocheatplus.compat.AlmostBoolean; import fr.neatmonster.nocheatplus.hooks.NCPExemptionManager; +import fr.neatmonster.nocheatplus.logging.Streams; import fr.neatmonster.nocheatplus.permissions.Permissions; import fr.neatmonster.nocheatplus.stats.Counters; import fr.neatmonster.nocheatplus.utilities.BlockProperties; @@ -38,7 +39,7 @@ public class BlockBreakListener extends CheckListener { /** The fast break check (per block breaking speed). */ private final FastBreak fastBreak = addCheck(new FastBreak()); - + /** The frequency check (number of blocks broken) */ private final Frequency frequency = addCheck(new Frequency()); @@ -47,17 +48,17 @@ public class BlockBreakListener extends CheckListener { /** The reach check. */ private final Reach reach = addCheck(new Reach()); - + /** The wrong block check. */ private final WrongBlock wrongBlock = addCheck(new WrongBlock()); - + private AlmostBoolean isInstaBreak = AlmostBoolean.NO; - + private final Counters counters = NCPAPIProvider.getNoCheatPlusAPI().getGenericInstance(Counters.class); private final int idCancelDIllegalItem = counters.registerKey("illegalitem"); - + public BlockBreakListener(){ - super(CheckType.BLOCKBREAK); + super(CheckType.BLOCKBREAK); } /** @@ -68,23 +69,23 @@ public class BlockBreakListener extends CheckListener { */ @EventHandler(ignoreCancelled = false, priority = EventPriority.LOWEST) public void onBlockBreak(final BlockBreakEvent event) { - + final Player player = event.getPlayer(); - + // Illegal enchantments hotfix check. if (Items.checkIllegalEnchantments(player, player.getItemInHand())) { - event.setCancelled(true); - counters.addPrimaryThread(idCancelDIllegalItem, 1); + event.setCancelled(true); + counters.addPrimaryThread(idCancelDIllegalItem, 1); } - - // Cancelled events only leads to resetting insta break. - if (event.isCancelled()) { - isInstaBreak = AlmostBoolean.NO; - return; - } - - // TODO: maybe invalidate instaBreak on some occasions. - + + // Cancelled events only leads to resetting insta break. + if (event.isCancelled()) { + isInstaBreak = AlmostBoolean.NO; + return; + } + + // TODO: maybe invalidate instaBreak on some occasions. + final Block block = event.getBlock(); @@ -92,43 +93,43 @@ public class BlockBreakListener extends CheckListener { // Do the actual checks, if still needed. It's a good idea to make computationally cheap checks first, because // it may save us from doing the computationally expensive checks. - + final BlockBreakConfig cc = BlockBreakConfig.getConfig(player); final BlockBreakData data = BlockBreakData.getData(player); final long now = System.currentTimeMillis(); - + final GameMode gameMode = player.getGameMode(); - + // Has the player broken a block that was not damaged before? if (wrongBlock.isEnabled(player) && wrongBlock.check(player, block, cc, data, isInstaBreak)) { - cancelled = true; + cancelled = true; } // Has the player broken more blocks per second than allowed? if (!cancelled && frequency.isEnabled(player) && frequency.check(player, cc, data)) { - cancelled = true; + cancelled = true; } - + // Has the player broken blocks faster than possible? if (!cancelled && gameMode != GameMode.CREATIVE && fastBreak.isEnabled(player) && fastBreak.check(player, block, isInstaBreak, cc, data)) { - cancelled = true; + cancelled = true; } // Did the arm of the player move before breaking this block? if (!cancelled && noSwing.isEnabled(player) && noSwing.check(player, data)) { - cancelled = true; + cancelled = true; } // Is the block really in reach distance? if (!cancelled && reach.isEnabled(player) && reach.check(player, block, data)) { - cancelled = true; + cancelled = true; } // Did the player look at the block at all? if (!cancelled && direction.isEnabled(player) && direction.check(player, block, data)) { - cancelled = true; + cancelled = true; } - + // Destroying liquid blocks. if (!cancelled && BlockProperties.isLiquid(block.getType()) && !player.hasPermission(Permissions.BLOCKBREAK_BREAK_LIQUID) && !NCPExemptionManager.isExempted(player, CheckType.BLOCKBREAK_BREAK)){ cancelled = true; @@ -136,28 +137,32 @@ public class BlockBreakListener extends CheckListener { // On cancel... if (cancelled) { - event.setCancelled(cancelled); - // Reset damage position: - // TODO: Review this (!), check if set at all !? - data.clickedX = block.getX(); - data.clickedY = block.getY(); - data.clickedZ = block.getZ(); + event.setCancelled(cancelled); + // Reset damage position: + // TODO: Review this (!), check if set at all !? + data.clickedX = block.getX(); + data.clickedY = block.getY(); + data.clickedZ = block.getZ(); } else{ - // Invalidate last damage position: -// data.clickedX = Integer.MAX_VALUE; + // Invalidate last damage position: + // data.clickedX = Integer.MAX_VALUE; + // Debug log (only if not cancelled, to avoid spam). + if (data.debug) { + NCPAPIProvider.getNoCheatPlusAPI().getLogManager().debug(Streams.TRACE_FILE, player.getName() + " block break(" + block.getType() + "): " + block.getX() + ", " + block.getY() + ", " + block.getZ()); + } } - + if (isInstaBreak.decideOptimistically()) { - data.wasInstaBreak = now; + data.wasInstaBreak = now; } else { - data.wasInstaBreak = 0; + data.wasInstaBreak = 0; } - + // Adjust data. data.fastBreakBreakTime = now; -// data.fastBreakfirstDamage = now; + // data.fastBreakfirstDamage = now; isInstaBreak = AlmostBoolean.NO; } @@ -172,7 +177,7 @@ public class BlockBreakListener extends CheckListener { priority = EventPriority.MONITOR) public void onPlayerAnimation(final PlayerAnimationEvent event) { // Just set a flag to true when the arm was swung. -// NCPAPIProvider.getNoCheatPlusAPI().getLogManager().debug(Streams.TRACE_FILE, "Animation"); + // NCPAPIProvider.getNoCheatPlusAPI().getLogManager().debug(Streams.TRACE_FILE, "Animation"); BlockBreakData.getData(event.getPlayer()).noSwingArmSwung = true; } @@ -186,85 +191,85 @@ public class BlockBreakListener extends CheckListener { @EventHandler( ignoreCancelled = false, priority = EventPriority.LOWEST) public void onPlayerInteract(final PlayerInteractEvent event) { -// NCPAPIProvider.getNoCheatPlusAPI().getLogManager().debug(Streams.TRACE_FILE, "Interact("+event.isCancelled()+"): " + event.getClickedBlock()); - // The following is to set the "first damage time" for a block. - - // Return if it is not left clicking a block. - // (Allows right click to be ignored.) - isInstaBreak = AlmostBoolean.NO; + // NCPAPIProvider.getNoCheatPlusAPI().getLogManager().debug(Streams.TRACE_FILE, "Interact("+event.isCancelled()+"): " + event.getClickedBlock()); + // The following is to set the "first damage time" for a block. + + // Return if it is not left clicking a block. + // (Allows right click to be ignored.) + isInstaBreak = AlmostBoolean.NO; if (event.getAction() != Action.LEFT_CLICK_BLOCK) { - return; + return; } checkBlockDamage(event.getPlayer(), event.getClickedBlock(), event); } - + @EventHandler(ignoreCancelled = false, priority = EventPriority.LOWEST) public void onBlockDamageLowest(final BlockDamageEvent event) { - if (event.getInstaBreak()) { - // Indicate that this might have been set by CB/MC. - isInstaBreak = AlmostBoolean.MAYBE; - } + if (event.getInstaBreak()) { + // Indicate that this might have been set by CB/MC. + isInstaBreak = AlmostBoolean.MAYBE; + } } - + @EventHandler(ignoreCancelled = false, priority = EventPriority.MONITOR) public void onBlockDamage(final BlockDamageEvent event) { - if (!event.isCancelled() && event.getInstaBreak()) { - // Keep MAYBE. - if (isInstaBreak != AlmostBoolean.MAYBE) { - isInstaBreak = AlmostBoolean.YES; - } - } - else { - isInstaBreak = AlmostBoolean.NO; - } - checkBlockDamage(event.getPlayer(), event.getBlock(), event); + if (!event.isCancelled() && event.getInstaBreak()) { + // Keep MAYBE. + if (isInstaBreak != AlmostBoolean.MAYBE) { + isInstaBreak = AlmostBoolean.YES; + } + } + else { + isInstaBreak = AlmostBoolean.NO; + } + checkBlockDamage(event.getPlayer(), event.getBlock(), event); } - + private void checkBlockDamage(final Player player, final Block block, final Cancellable event){ - final long now = System.currentTimeMillis(); + final long now = System.currentTimeMillis(); final BlockBreakData data = BlockBreakData.getData(player); - -// if (event.isCancelled()){ -// // Reset the time, to avoid certain kinds of cheating. => WHICH ? -// data.fastBreakfirstDamage = now; -// data.clickedX = Integer.MAX_VALUE; // Should be enough to reset that one. -// return; -// } - + + // if (event.isCancelled()){ + // // Reset the time, to avoid certain kinds of cheating. => WHICH ? + // data.fastBreakfirstDamage = now; + // data.clickedX = Integer.MAX_VALUE; // Should be enough to reset that one. + // return; + // } + // Do not care about null blocks. if (block == null) { - return; + return; } - + final int tick = TickTask.getTick(); // Skip if already set to the same block without breaking within one tick difference. final ItemStack stack = player.getItemInHand(); final Material tool = stack == null ? null: stack.getType(); if (data.toolChanged(tool)) { - // Update. + // Update. } else if (tick < data.clickedTick || now < data.fastBreakfirstDamage || now < data.fastBreakBreakTime) { - // Time/tick ran backwards: Update. - // Tick running backwards should not happen in the main thread unless for reload. A plugin could reset it (not intended). + // Time/tick ran backwards: Update. + // Tick running backwards should not happen in the main thread unless for reload. A plugin could reset it (not intended). } else if (data.fastBreakBreakTime < data.fastBreakfirstDamage && data.clickedX == block.getX() && data.clickedZ == block.getZ() && data.clickedY == block.getY()){ - // Preserve first damage time. - if (tick - data.clickedTick <= 1 ) { - return; - } + // Preserve first damage time. + if (tick - data.clickedTick <= 1 ) { + return; + } } // (Always set, the interact event only fires once: the first time.) // Only record first damage: data.setClickedBlock(block, tick, now, tool); } - + @EventHandler(ignoreCancelled = false, priority = EventPriority.MONITOR) public void onItemHeld(final PlayerItemHeldEvent event) { - // Reset clicked block. - // TODO: Not for 1.5.2 and before? - final Player player = event.getPlayer(); - final BlockBreakData data = BlockBreakData.getData(player); - if (data.toolChanged(player.getInventory().getItem(event.getNewSlot()))) { - data.resetClickedBlock(); - } + // Reset clicked block. + // TODO: Not for 1.5.2 and before? + final Player player = event.getPlayer(); + final BlockBreakData data = BlockBreakData.getData(player); + if (data.toolChanged(player.getInventory().getItem(event.getNewSlot()))) { + data.resetClickedBlock(); + } } - + } diff --git a/NCPCore/src/main/java/fr/neatmonster/nocheatplus/checks/blockplace/BlockPlaceListener.java b/NCPCore/src/main/java/fr/neatmonster/nocheatplus/checks/blockplace/BlockPlaceListener.java index 1b31642f..92b583ad 100644 --- a/NCPCore/src/main/java/fr/neatmonster/nocheatplus/checks/blockplace/BlockPlaceListener.java +++ b/NCPCore/src/main/java/fr/neatmonster/nocheatplus/checks/blockplace/BlockPlaceListener.java @@ -24,6 +24,7 @@ import fr.neatmonster.nocheatplus.checks.combined.CombinedConfig; import fr.neatmonster.nocheatplus.checks.combined.Improbable; import fr.neatmonster.nocheatplus.checks.moving.MovingConfig; import fr.neatmonster.nocheatplus.compat.BridgeMisc; +import fr.neatmonster.nocheatplus.logging.Streams; import fr.neatmonster.nocheatplus.permissions.Permissions; import fr.neatmonster.nocheatplus.stats.Counters; import fr.neatmonster.nocheatplus.utilities.BlockProperties; @@ -101,8 +102,10 @@ public class BlockPlaceListener extends CheckListener { final Block block = event.getBlockPlaced(); final Block blockAgainst = event.getBlockAgainst(); // Skip any null blocks. - if (block == null || blockAgainst == null) + if (block == null || blockAgainst == null) { return; + } + // TODO: What if same block? // TODO: Revise material use (not block.get... ?) //final Material mat = block.getType(); @@ -154,6 +157,11 @@ public class BlockPlaceListener extends CheckListener { // If one of the checks requested to cancel the event, do so. if (cancelled) { event.setCancelled(cancelled); + } else { + // Debug log (only if not cancelled, to avoid spam). + if (data.debug) { + NCPAPIProvider.getNoCheatPlusAPI().getLogManager().debug(Streams.TRACE_FILE, player.getName() + " block place(" + placedMat + "): " + block.getX() + ", " + block.getY() + ", " + block.getZ()); + } } // Cleanup // Reminder(currently unused): useLoc.setWorld(null);