From d502f6647208d6cd6f780a4e72d638623a4e9f4c Mon Sep 17 00:00:00 2001 From: Evenprime Date: Fri, 18 Nov 2011 02:34:33 +0100 Subject: [PATCH] Potential fix for all those "player can no longer do X" problems --- plugin.yml | 2 +- src/cc/co/evenprime/bukkit/nocheat/NoCheat.java | 4 ++-- .../bukkit/nocheat/data/PlayerManager.java | 11 +++++++---- .../nocheat/events/BlockBreakEventManager.java | 6 +++--- .../nocheat/events/BlockPlaceEventManager.java | 2 +- .../bukkit/nocheat/events/ChatEventManager.java | 2 +- .../bukkit/nocheat/events/FightEventManager.java | 4 ++-- .../bukkit/nocheat/events/MovingEventManager.java | 6 +++--- .../nocheat/events/PlayerTeleportEventManager.java | 6 +++--- .../bukkit/nocheat/events/TimedEventManager.java | 3 ++- .../bukkit/nocheat/player/NoCheatPlayerImpl.java | 14 ++++++++------ 11 files changed, 33 insertions(+), 27 deletions(-) diff --git a/plugin.yml b/plugin.yml index 98649e7a..c70c49f2 100644 --- a/plugin.yml +++ b/plugin.yml @@ -3,7 +3,7 @@ name: NoCheat author: Evenprime main: cc.co.evenprime.bukkit.nocheat.NoCheat -version: 2.16b +version: 2.16c commands: nocheat: diff --git a/src/cc/co/evenprime/bukkit/nocheat/NoCheat.java b/src/cc/co/evenprime/bukkit/nocheat/NoCheat.java index 9f21d68b..e65173ac 100644 --- a/src/cc/co/evenprime/bukkit/nocheat/NoCheat.java +++ b/src/cc/co/evenprime/bukkit/nocheat/NoCheat.java @@ -187,8 +187,8 @@ public class NoCheat extends JavaPlugin { } - public NoCheatPlayer getPlayer(String playerName) { - return players.getPlayer(playerName); + public NoCheatPlayer getPlayer(Player player) { + return players.getPlayer(player); } } diff --git a/src/cc/co/evenprime/bukkit/nocheat/data/PlayerManager.java b/src/cc/co/evenprime/bukkit/nocheat/data/PlayerManager.java index 6276ce68..02c8cad3 100644 --- a/src/cc/co/evenprime/bukkit/nocheat/data/PlayerManager.java +++ b/src/cc/co/evenprime/bukkit/nocheat/data/PlayerManager.java @@ -6,6 +6,8 @@ import java.util.List; import java.util.Map; import java.util.Map.Entry; +import org.bukkit.entity.Player; + import cc.co.evenprime.bukkit.nocheat.NoCheat; import cc.co.evenprime.bukkit.nocheat.NoCheatPlayer; import cc.co.evenprime.bukkit.nocheat.player.NoCheatPlayerImpl; @@ -29,18 +31,19 @@ public class PlayerManager { * Get a data object of the specified class. If none is stored yet, create * one. */ - public NoCheatPlayer getPlayer(String playerName) { + public NoCheatPlayer getPlayer(Player player) { - NoCheatPlayerImpl p = this.players.get(playerName); + NoCheatPlayerImpl p = this.players.get(player.getName()); if(p == null) { // TODO: Differentiate which player"type" should be created, e.g. // based on bukkit version - p = new NoCheatPlayerImpl(playerName, plugin, new BaseData()); - this.players.put(playerName, p); + p = new NoCheatPlayerImpl(player, plugin, new BaseData()); + this.players.put(player.getName(), p); } p.setLastUsedTime(System.currentTimeMillis()); + p.refresh(player); return p; } diff --git a/src/cc/co/evenprime/bukkit/nocheat/events/BlockBreakEventManager.java b/src/cc/co/evenprime/bukkit/nocheat/events/BlockBreakEventManager.java index e6e77a88..7186fdbf 100644 --- a/src/cc/co/evenprime/bukkit/nocheat/events/BlockBreakEventManager.java +++ b/src/cc/co/evenprime/bukkit/nocheat/events/BlockBreakEventManager.java @@ -51,7 +51,7 @@ public class BlockBreakEventManager extends EventManager { boolean cancelled = false; - final NoCheatPlayer player = plugin.getPlayer(event.getPlayer().getName()); + final NoCheatPlayer player = plugin.getPlayer(event.getPlayer()); final CCBlockBreak cc = player.getConfiguration().blockbreak; if(!cc.check || player.hasPermission(Permissions.BLOCKBREAK)) { @@ -81,7 +81,7 @@ public class BlockBreakEventManager extends EventManager { return; // Get the player-specific stored data that applies here - final BlockBreakData data = plugin.getPlayer(event.getPlayer().getName()).getData().blockbreak; + final BlockBreakData data = plugin.getPlayer(event.getPlayer()).getData().blockbreak; // Remember this location. We ignore block breaks in the block-break // direction check that are insta-breaks @@ -90,7 +90,7 @@ public class BlockBreakEventManager extends EventManager { @Override protected void handlePlayerAnimationEvent(final PlayerAnimationEvent event, final Priority priority) { - plugin.getPlayer(event.getPlayer().getName()).getData().blockbreak.armswung = true; + plugin.getPlayer(event.getPlayer()).getData().blockbreak.armswung = true; } public List getActiveChecks(ConfigurationCache cc) { diff --git a/src/cc/co/evenprime/bukkit/nocheat/events/BlockPlaceEventManager.java b/src/cc/co/evenprime/bukkit/nocheat/events/BlockPlaceEventManager.java index 12a752af..22658f9d 100644 --- a/src/cc/co/evenprime/bukkit/nocheat/events/BlockPlaceEventManager.java +++ b/src/cc/co/evenprime/bukkit/nocheat/events/BlockPlaceEventManager.java @@ -47,7 +47,7 @@ public class BlockPlaceEventManager extends EventManager { boolean cancelled = false; - final NoCheatPlayer player = plugin.getPlayer(event.getPlayer().getName()); + final NoCheatPlayer player = plugin.getPlayer(event.getPlayer()); final CCBlockPlace cc = player.getConfiguration().blockplace; if(!cc.check || player.hasPermission(Permissions.BLOCKPLACE)) { diff --git a/src/cc/co/evenprime/bukkit/nocheat/events/ChatEventManager.java b/src/cc/co/evenprime/bukkit/nocheat/events/ChatEventManager.java index 0338e4af..c78e5f08 100644 --- a/src/cc/co/evenprime/bukkit/nocheat/events/ChatEventManager.java +++ b/src/cc/co/evenprime/bukkit/nocheat/events/ChatEventManager.java @@ -44,7 +44,7 @@ public class ChatEventManager extends EventManager { boolean cancelled = false; - final NoCheatPlayer player = plugin.getPlayer(event.getPlayer().getName()); + final NoCheatPlayer player = plugin.getPlayer(event.getPlayer()); final CCChat cc = player.getConfiguration().chat; if(!cc.check || player.hasPermission(Permissions.CHAT)) { diff --git a/src/cc/co/evenprime/bukkit/nocheat/events/FightEventManager.java b/src/cc/co/evenprime/bukkit/nocheat/events/FightEventManager.java index 0f6d3a33..91adfb53 100644 --- a/src/cc/co/evenprime/bukkit/nocheat/events/FightEventManager.java +++ b/src/cc/co/evenprime/bukkit/nocheat/events/FightEventManager.java @@ -45,7 +45,7 @@ public class FightEventManager extends EventManager { // Our event listener already checked if that cast is valid final Player damager = (Player) event.getDamager(); - final NoCheatPlayer player = plugin.getPlayer(damager.getName()); + final NoCheatPlayer player = plugin.getPlayer(damager); final CCFight cc = player.getConfiguration().fight; if(!cc.check || player.hasPermission(Permissions.FIGHT)) { @@ -73,7 +73,7 @@ public class FightEventManager extends EventManager { @Override protected void handlePlayerAnimationEvent(final PlayerAnimationEvent event, final Priority priority) { - plugin.getPlayer(event.getPlayer().getName()).getData().fight.armswung = true; + plugin.getPlayer(event.getPlayer()).getData().fight.armswung = true; } public List getActiveChecks(ConfigurationCache cc) { diff --git a/src/cc/co/evenprime/bukkit/nocheat/events/MovingEventManager.java b/src/cc/co/evenprime/bukkit/nocheat/events/MovingEventManager.java index d62e6e75..81ec00ea 100644 --- a/src/cc/co/evenprime/bukkit/nocheat/events/MovingEventManager.java +++ b/src/cc/co/evenprime/bukkit/nocheat/events/MovingEventManager.java @@ -55,7 +55,7 @@ public class MovingEventManager extends EventManager { @Override protected void handleBlockPlaceEvent(final BlockPlaceEvent event, final Priority priority) { - final NoCheatPlayer player = plugin.getPlayer(event.getPlayer().getName()); + final NoCheatPlayer player = plugin.getPlayer(event.getPlayer()); // Get the player-specific stored data that applies here final MovingData data = player.getData().moving; @@ -91,7 +91,7 @@ public class MovingEventManager extends EventManager { } // Get the world-specific configuration that applies here - final NoCheatPlayer player = plugin.getPlayer(event.getPlayer().getName()); + final NoCheatPlayer player = plugin.getPlayer(event.getPlayer()); final CCMoving cc = player.getConfiguration().moving; if(!cc.check || player.hasPermission(Permissions.MOVING)) { @@ -147,7 +147,7 @@ public class MovingEventManager extends EventManager { @Override protected void handlePlayerVelocityEvent(final PlayerVelocityEvent event, final Priority priority) { - final MovingData data = plugin.getPlayer(event.getPlayer().getName()).getData().moving; + final MovingData data = plugin.getPlayer(event.getPlayer()).getData().moving; final Vector v = event.getVelocity(); diff --git a/src/cc/co/evenprime/bukkit/nocheat/events/PlayerTeleportEventManager.java b/src/cc/co/evenprime/bukkit/nocheat/events/PlayerTeleportEventManager.java index ede2400f..f77ab2cd 100644 --- a/src/cc/co/evenprime/bukkit/nocheat/events/PlayerTeleportEventManager.java +++ b/src/cc/co/evenprime/bukkit/nocheat/events/PlayerTeleportEventManager.java @@ -41,7 +41,7 @@ public class PlayerTeleportEventManager extends EventManager { if(!event.isCancelled()) return; - NoCheatPlayer player = plugin.getPlayer(event.getPlayer().getName()); + NoCheatPlayer player = plugin.getPlayer(event.getPlayer()); final MovingData data = player.getData().moving; if(data.teleportTo.isSet() && data.teleportTo.equals(event.getTo())) { @@ -72,7 +72,7 @@ public class PlayerTeleportEventManager extends EventManager { // 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) { + if(plugin.getPlayer(event.getPlayer()).getConfiguration().debug.overrideIdiocy) { event.setCancelled(false); event.setTo(event.getFrom().clone()); } @@ -81,7 +81,7 @@ public class PlayerTeleportEventManager extends EventManager { @Override protected void handlePlayerToggleSprintEvent(final PlayerToggleSprintEvent event, final Priority priority) { if(event.isCancelled() && event.isSprinting()) { - if(plugin.getPlayer(event.getPlayer().getName()).getConfiguration().debug.overrideIdiocy) + if(plugin.getPlayer(event.getPlayer()).getConfiguration().debug.overrideIdiocy) event.setCancelled(false); } } diff --git a/src/cc/co/evenprime/bukkit/nocheat/events/TimedEventManager.java b/src/cc/co/evenprime/bukkit/nocheat/events/TimedEventManager.java index 1fcf2085..dedc658c 100644 --- a/src/cc/co/evenprime/bukkit/nocheat/events/TimedEventManager.java +++ b/src/cc/co/evenprime/bukkit/nocheat/events/TimedEventManager.java @@ -8,6 +8,7 @@ import java.util.List; import net.minecraft.server.EntityPlayer; import org.bukkit.craftbukkit.CraftServer; +import org.bukkit.entity.Player; import cc.co.evenprime.bukkit.nocheat.NoCheat; import cc.co.evenprime.bukkit.nocheat.NoCheatPlayer; @@ -79,7 +80,7 @@ public class TimedEventManager extends EventManager { if(performanceCheck) nanoTimeStart = System.nanoTime(); - handleEvent(plugin.getPlayer(p.name)); + handleEvent(plugin.getPlayer((Player)p.getBukkitEntity())); // store performance time if(performanceCheck) diff --git a/src/cc/co/evenprime/bukkit/nocheat/player/NoCheatPlayerImpl.java b/src/cc/co/evenprime/bukkit/nocheat/player/NoCheatPlayerImpl.java index f3b15ddf..88453ff6 100644 --- a/src/cc/co/evenprime/bukkit/nocheat/player/NoCheatPlayerImpl.java +++ b/src/cc/co/evenprime/bukkit/nocheat/player/NoCheatPlayerImpl.java @@ -3,7 +3,6 @@ package cc.co.evenprime.bukkit.nocheat.player; import net.minecraft.server.EntityPlayer; import net.minecraft.server.MobEffectList; -import org.bukkit.Bukkit; import org.bukkit.GameMode; import org.bukkit.craftbukkit.entity.CraftPlayer; import org.bukkit.entity.Player; @@ -15,20 +14,23 @@ import cc.co.evenprime.bukkit.nocheat.data.BaseData; public class NoCheatPlayerImpl implements NoCheatPlayer { - private final Player player; + private Player player; private final NoCheat plugin; private final BaseData data; - private long lastUsedTime; - public NoCheatPlayerImpl(String playerName, NoCheat plugin, BaseData data) { - this.player = Bukkit.getServer().getPlayer(playerName); + public NoCheatPlayerImpl(Player player, NoCheat plugin, BaseData data) { + this.player = player; this.plugin = plugin; this.data = data; this.lastUsedTime = System.currentTimeMillis(); } + public void refresh(Player player) { + this.player = player; + } + public boolean hasPermission(String permission) { if(permission == null) { System.out.println("NoCheat: Warning, asked for null permission"); @@ -69,7 +71,7 @@ public class NoCheatPlayerImpl implements NoCheatPlayer { EntityPlayer ep = ((CraftPlayer) player).getHandle(); if(ep.hasEffect(MobEffectList.FASTER_MOVEMENT)) { // Taken directly from Minecraft code, should work - return 1.0F + 0.2F * (float)(ep.getEffect(MobEffectList.FASTER_MOVEMENT).getAmplifier() + 1); + return 1.0F + 0.2F * (float) (ep.getEffect(MobEffectList.FASTER_MOVEMENT).getAmplifier() + 1); } else { return 1.0F; }