From e853eac486f8b850c03b9d828fe2b0a0b924e8c9 Mon Sep 17 00:00:00 2001 From: Evenprime Date: Tue, 15 Nov 2011 17:05:06 +0100 Subject: [PATCH] Fix two common mistakes by other plugins (cancelling move events and cancelling toggle sprint events) --- .../bukkit/nocheat/config/Configuration.java | 3 ++- .../nocheat/config/DefaultConfiguration.java | 1 + .../bukkit/nocheat/config/Explainations.java | 1 + .../bukkit/nocheat/config/cache/CCDebug.java | 2 ++ .../bukkit/nocheat/events/EventManager.java | 19 +++++++++++++++++++ .../events/PlayerChatEventManager.java | 2 +- .../events/PlayerTeleportEventManager.java | 19 ++++++++++++++++++- 7 files changed, 44 insertions(+), 3 deletions(-) diff --git a/src/cc/co/evenprime/bukkit/nocheat/config/Configuration.java b/src/cc/co/evenprime/bukkit/nocheat/config/Configuration.java index aeff669e..170adc4e 100644 --- a/src/cc/co/evenprime/bukkit/nocheat/config/Configuration.java +++ b/src/cc/co/evenprime/bukkit/nocheat/config/Configuration.java @@ -30,7 +30,8 @@ public abstract class Configuration { private final static OptionNode DEBUG = new OptionNode("debug", ROOT, DataType.PARENT); public final static OptionNode DEBUG_SHOWACTIVECHECKS = new OptionNode("showactivechecks", DEBUG, DataType.BOOLEAN); - + public static final OptionNode DEBUG_COMPATIBILITY = new OptionNode("compatibility", DEBUG, DataType.BOOLEAN); + private final static OptionNode MOVING = new OptionNode("moving", ROOT, DataType.PARENT); public final static OptionNode MOVING_CHECK = new OptionNode("check", MOVING, DataType.BOOLEAN); public final static OptionNode MOVING_IDENTIFYCREATIVEMODE = new OptionNode("identifycreativemode", MOVING, DataType.BOOLEAN); diff --git a/src/cc/co/evenprime/bukkit/nocheat/config/DefaultConfiguration.java b/src/cc/co/evenprime/bukkit/nocheat/config/DefaultConfiguration.java index 9080a981..8aec3281 100644 --- a/src/cc/co/evenprime/bukkit/nocheat/config/DefaultConfiguration.java +++ b/src/cc/co/evenprime/bukkit/nocheat/config/DefaultConfiguration.java @@ -32,6 +32,7 @@ public class DefaultConfiguration extends Configuration { /*** DEBUG ***/ { setValue(DEBUG_SHOWACTIVECHECKS, false); + setValue(DEBUG_COMPATIBILITY, true); } /*** MOVING ***/ diff --git a/src/cc/co/evenprime/bukkit/nocheat/config/Explainations.java b/src/cc/co/evenprime/bukkit/nocheat/config/Explainations.java index fcbcc9a2..46a6f33f 100644 --- a/src/cc/co/evenprime/bukkit/nocheat/config/Explainations.java +++ b/src/cc/co/evenprime/bukkit/nocheat/config/Explainations.java @@ -25,6 +25,7 @@ public class Explainations { set(Configuration.LOGGING_CHATLEVEL, "What log-level need messages to have to get displayed in the ingame chat. Values are:\n low: all messages\n med: med and high messages only\n high: high messages only\n off: no messages at all."); set(Configuration.DEBUG_SHOWACTIVECHECKS, "Print to the console an overview of all checks that are enabled when NoCheat gets loaded."); + set(Configuration.DEBUG_COMPATIBILITY, "Do some voodoo to fix common mistakes of other plugins which interfere with NoCheat."); set(Configuration.MOVING_CHECK, "If true, do various checks on PlayerMove events."); set(Configuration.MOVING_IDENTIFYCREATIVEMODE, "If true, NoCheat will automatically identify if players are in creative mode and will allow them to fly, avoid fall damage etc."); diff --git a/src/cc/co/evenprime/bukkit/nocheat/config/cache/CCDebug.java b/src/cc/co/evenprime/bukkit/nocheat/config/cache/CCDebug.java index e0518dc5..a2e4063f 100644 --- a/src/cc/co/evenprime/bukkit/nocheat/config/cache/CCDebug.java +++ b/src/cc/co/evenprime/bukkit/nocheat/config/cache/CCDebug.java @@ -5,10 +5,12 @@ import cc.co.evenprime.bukkit.nocheat.config.Configuration; public class CCDebug { public final boolean showchecks; + public final boolean overrideIdiocy; public CCDebug(Configuration data) { showchecks = data.getBoolean(Configuration.DEBUG_SHOWACTIVECHECKS); + overrideIdiocy = data.getBoolean(Configuration.DEBUG_COMPATIBILITY); } } diff --git a/src/cc/co/evenprime/bukkit/nocheat/events/EventManager.java b/src/cc/co/evenprime/bukkit/nocheat/events/EventManager.java index 2d217e2a..083f7251 100644 --- a/src/cc/co/evenprime/bukkit/nocheat/events/EventManager.java +++ b/src/cc/co/evenprime/bukkit/nocheat/events/EventManager.java @@ -24,6 +24,7 @@ import org.bukkit.event.player.PlayerMoveEvent; import org.bukkit.event.player.PlayerPortalEvent; import org.bukkit.event.player.PlayerRespawnEvent; import org.bukkit.event.player.PlayerTeleportEvent; +import org.bukkit.event.player.PlayerToggleSprintEvent; import org.bukkit.event.player.PlayerVelocityEvent; import cc.co.evenprime.bukkit.nocheat.NoCheat; @@ -224,6 +225,20 @@ public abstract class EventManager { m.handlePlayerAnimationEvent(event, priority); } } + + @Override + public void onPlayerToggleSprint(final PlayerToggleSprintEvent event) { + if(ignoreCancelledEvents && event.isCancelled()) + return; + + if(measureTime != null && measureTime.isEnabled()) { + final long startTime = System.nanoTime(); + m.handlePlayerToggleSprintEvent(event, priority); + measureTime.addTime(System.nanoTime() - startTime); + } else { + m.handlePlayerToggleSprintEvent(event, priority); + } + } } private static class EntityL extends EntityListener { @@ -358,4 +373,8 @@ public abstract class EventManager { protected void handleEntityDamageByEntityEvent(final EntityDamageByEntityEvent event, final Priority priority) { handleEvent(event, priority); } + + protected void handlePlayerToggleSprintEvent(PlayerToggleSprintEvent event, Priority priority) { + handleEvent(event, priority); + } } diff --git a/src/cc/co/evenprime/bukkit/nocheat/events/PlayerChatEventManager.java b/src/cc/co/evenprime/bukkit/nocheat/events/PlayerChatEventManager.java index 3f95466a..8f42226a 100644 --- a/src/cc/co/evenprime/bukkit/nocheat/events/PlayerChatEventManager.java +++ b/src/cc/co/evenprime/bukkit/nocheat/events/PlayerChatEventManager.java @@ -36,7 +36,7 @@ public class PlayerChatEventManager extends EventManager { @Override protected void handlePlayerCommandPreprocessEvent(final PlayerCommandPreprocessEvent event, final Priority priority) { - handleEvent((PlayerChatEvent) event, priority); + handlePlayerChatEvent((PlayerChatEvent) event, priority); } @Override diff --git a/src/cc/co/evenprime/bukkit/nocheat/events/PlayerTeleportEventManager.java b/src/cc/co/evenprime/bukkit/nocheat/events/PlayerTeleportEventManager.java index b9257f72..dded269f 100644 --- a/src/cc/co/evenprime/bukkit/nocheat/events/PlayerTeleportEventManager.java +++ b/src/cc/co/evenprime/bukkit/nocheat/events/PlayerTeleportEventManager.java @@ -7,6 +7,7 @@ import org.bukkit.event.player.PlayerMoveEvent; import org.bukkit.event.player.PlayerPortalEvent; import org.bukkit.event.player.PlayerRespawnEvent; import org.bukkit.event.player.PlayerTeleportEvent; +import org.bukkit.event.player.PlayerToggleSprintEvent; import cc.co.evenprime.bukkit.nocheat.NoCheat; import cc.co.evenprime.bukkit.nocheat.data.MovingData; @@ -22,7 +23,8 @@ public class PlayerTeleportEventManager extends EventManager { super(plugin); - registerListener(Event.Type.PLAYER_MOVE, Priority.Monitor, false, null); + registerListener(Event.Type.PLAYER_MOVE, Priority.Highest, false, null); + registerListener(Event.Type.PLAYER_TOGGLE_SPRINT, Priority.Highest, false, null); registerListener(Event.Type.PLAYER_TELEPORT, Priority.Monitor, true, null); registerListener(Event.Type.PLAYER_TELEPORT, Priority.Highest, false, null); registerListener(Event.Type.PLAYER_PORTAL, Priority.Monitor, true, null); @@ -63,6 +65,21 @@ public class PlayerTeleportEventManager extends EventManager { return; handleTeleportation(event.getPlayer()); + + // Fix a common mistake that other developers make (cancelling move + // events is crazy, rather set the target location to the from location + if(plugin.getPlayer(event.getPlayer().getName()).getConfiguration().debug.overrideIdiocy) { + event.setCancelled(false); + event.setTo(event.getFrom().clone()); + } + } + + @Override + protected void handlePlayerToggleSprintEvent(final PlayerToggleSprintEvent event, final Priority priority) { + if(event.isCancelled() && event.isSprinting()) { + if(plugin.getPlayer(event.getPlayer().getName()).getConfiguration().debug.overrideIdiocy) + event.setCancelled(false); + } } private void handleTeleportation(final Player player) {