diff --git a/src/cc/co/evenprime/bukkit/nocheat/data/BaseData.java b/src/cc/co/evenprime/bukkit/nocheat/data/BaseData.java index 3125f5bd..a34b024b 100644 --- a/src/cc/co/evenprime/bukkit/nocheat/data/BaseData.java +++ b/src/cc/co/evenprime/bukkit/nocheat/data/BaseData.java @@ -9,9 +9,7 @@ public class BaseData extends Data { public final FightData fight; public final TimedData timed; - private final Data[] data; // for convenience - - protected long lastUsedTime; + private final Data[] data; // for convenience public boolean armswung; @@ -26,7 +24,6 @@ public class BaseData extends Data { data = new Data[] {this.blockbreak, this.blockplace, this.chat, this.moving, this.fight, this.timed}; - this.lastUsedTime = System.currentTimeMillis(); } public void clearCriticalData() { @@ -35,7 +32,4 @@ public class BaseData extends Data { } } - public boolean shouldBeRemoved(long currentTimeInMilliseconds) { - return lastUsedTime + 60000L < currentTimeInMilliseconds; - } } diff --git a/src/cc/co/evenprime/bukkit/nocheat/data/PlayerManager.java b/src/cc/co/evenprime/bukkit/nocheat/data/PlayerManager.java index 243fbf27..6276ce68 100644 --- a/src/cc/co/evenprime/bukkit/nocheat/data/PlayerManager.java +++ b/src/cc/co/evenprime/bukkit/nocheat/data/PlayerManager.java @@ -17,13 +17,11 @@ import cc.co.evenprime.bukkit.nocheat.player.NoCheatPlayerImpl; public class PlayerManager { // Store data between Events - private final Map players; - private final NoCheat plugin; - private final List removals; + private final Map players; + private final NoCheat plugin; public PlayerManager(NoCheat plugin) { - this.players = new HashMap(); - this.removals = new ArrayList(5); + this.players = new HashMap(); this.plugin = plugin; } @@ -33,14 +31,17 @@ public class PlayerManager { */ public NoCheatPlayer getPlayer(String playerName) { - NoCheatPlayer p = this.players.get(playerName); + NoCheatPlayerImpl p = this.players.get(playerName); if(p == null) { - // TODO: Differentiate which player"type" should be created, e.g. based on bukkit version + // 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.setLastUsedTime(System.currentTimeMillis()); + return p; } @@ -54,31 +55,6 @@ public class PlayerManager { } } - /** - * check if some data hasn't been used for a while and remove it - * - */ - public void cleanDataMap() { - synchronized(removals) { - long time = System.currentTimeMillis(); - try { - for(Entry p : this.players.entrySet()) { - if(p.getValue().getData().shouldBeRemoved(time)) { - removals.add(p.getKey()); - } - } - - for(String p : removals) { - this.players.remove(p); - } - - removals.clear(); - } catch(Exception e) { - e.printStackTrace(); - } - } - } - public void clearCriticalData(String playerName) { NoCheatPlayer p = this.players.get(playerName); if(p != null) { @@ -86,4 +62,19 @@ public class PlayerManager { } } + public void cleanDataMap() { + long time = System.currentTimeMillis(); + List removals = new ArrayList(5); + + for(Entry e : this.players.entrySet()) { + if(e.getValue().shouldBeRemoved(time)) { + removals.add(e.getKey()); + } + } + + for(String key : removals) { + this.players.remove(key); + } + } + } diff --git a/src/cc/co/evenprime/bukkit/nocheat/events/EventManager.java b/src/cc/co/evenprime/bukkit/nocheat/events/EventManager.java index b0e02c14..2d217e2a 100644 --- a/src/cc/co/evenprime/bukkit/nocheat/events/EventManager.java +++ b/src/cc/co/evenprime/bukkit/nocheat/events/EventManager.java @@ -39,7 +39,7 @@ import cc.co.evenprime.bukkit.nocheat.debug.Performance; */ public abstract class EventManager { - protected NoCheat plugin; + protected final NoCheat plugin; private static class BlockL extends BlockListener { diff --git a/src/cc/co/evenprime/bukkit/nocheat/player/NoCheatPlayerImpl.java b/src/cc/co/evenprime/bukkit/nocheat/player/NoCheatPlayerImpl.java index 5668d522..c76eeb48 100644 --- a/src/cc/co/evenprime/bukkit/nocheat/player/NoCheatPlayerImpl.java +++ b/src/cc/co/evenprime/bukkit/nocheat/player/NoCheatPlayerImpl.java @@ -18,10 +18,14 @@ public class NoCheatPlayerImpl implements NoCheatPlayer { 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); this.plugin = plugin; this.data = data; + + this.lastUsedTime = System.currentTimeMillis(); } public boolean hasPermission(String permission) { @@ -57,14 +61,20 @@ public class NoCheatPlayerImpl implements NoCheatPlayer { } public float getSpeedAmplifier() { - EntityPlayer ep = ((CraftPlayer)player).getHandle(); + EntityPlayer ep = ((CraftPlayer) player).getHandle(); if(ep.hasEffect(MobEffectList.FASTER_MOVEMENT)) { // Taken directly from Minecraft code, should work return 1.0F + 0.2F * ep.getEffect(MobEffectList.FASTER_MOVEMENT).getAmplifier() + 1; - } - else { + } else { return 1.0F; } } + + public void setLastUsedTime(long currentTimeInMilliseconds) { + this.lastUsedTime = System.currentTimeMillis(); + } + public boolean shouldBeRemoved(long currentTimeInMilliseconds) { + return lastUsedTime + 60000L < currentTimeInMilliseconds; + } }