Code refactoring + cleaner seperation between speedhack and moving

check + only register listeners for activated checks
This commit is contained in:
Evenprime 2011-04-25 21:53:27 +02:00
parent cce01cca1a
commit d9b73cbb67
16 changed files with 183 additions and 132 deletions

View File

@ -8,11 +8,9 @@ import org.bukkit.Bukkit;
import org.bukkit.command.Command;
import org.bukkit.command.CommandSender;
import org.bukkit.entity.Player;
import org.bukkit.event.Event.Priority;
import org.bukkit.event.Event;
import org.bukkit.plugin.PluginDescriptionFile;
import org.bukkit.plugin.java.JavaPlugin;
import org.bukkit.plugin.PluginManager;
import cc.co.evenprime.bukkit.nocheat.actions.Action;
import cc.co.evenprime.bukkit.nocheat.checks.AirbuildCheck;
@ -20,13 +18,6 @@ import cc.co.evenprime.bukkit.nocheat.checks.BedteleportCheck;
import cc.co.evenprime.bukkit.nocheat.checks.ItemdupeCheck;
import cc.co.evenprime.bukkit.nocheat.checks.MovingCheck;
import cc.co.evenprime.bukkit.nocheat.checks.SpeedhackCheck;
import cc.co.evenprime.bukkit.nocheat.listeners.AirbuildListener;
import cc.co.evenprime.bukkit.nocheat.listeners.BedteleportListener;
import cc.co.evenprime.bukkit.nocheat.listeners.MovingListener;
import cc.co.evenprime.bukkit.nocheat.listeners.MovingMonitor;
import cc.co.evenprime.bukkit.nocheat.listeners.MovingEntityListener;
import cc.co.evenprime.bukkit.nocheat.listeners.ItemdupeListener;
import cc.co.evenprime.bukkit.nocheat.listeners.SpeedhackListener;
import com.ensifera.animosity.craftirc.CraftIRC;
import com.nijikokun.bukkit.Permissions.Permissions;
@ -130,33 +121,9 @@ public class NoCheat extends JavaPlugin {
// parse the nocheat.yml config file
setupConfig();
// Create our listeners and feed them with neccessary information
PluginManager pm = getServer().getPluginManager();
// Register listeners for moving check
pm.registerEvent(Event.Type.PLAYER_MOVE, new MovingListener(movingCheck), Priority.Lowest, this);
pm.registerEvent(Event.Type.PLAYER_TELEPORT, new MovingMonitor(movingCheck), Priority.Monitor, this);
pm.registerEvent(Event.Type.PLAYER_INTERACT, new MovingMonitor(movingCheck), Priority.Monitor, this);
pm.registerEvent(Event.Type.PLAYER_MOVE, new MovingMonitor(movingCheck), Priority.Monitor, this);
pm.registerEvent(Event.Type.PLAYER_RESPAWN, new MovingMonitor(movingCheck), Priority.Monitor, this);
pm.registerEvent(Event.Type.ENTITY_DAMAGE, new MovingEntityListener(movingCheck), Priority.Monitor, this);
pm.registerEvent(Event.Type.PLAYER_PICKUP_ITEM, new ItemdupeListener(itemdupeCheck), Priority.Lowest, this);
pm.registerEvent(Event.Type.PLAYER_INTERACT, new ItemdupeListener(itemdupeCheck), Priority.Lowest, this);
// Register listeners for speedhack check
pm.registerEvent(Event.Type.PLAYER_MOVE, new SpeedhackListener(speedhackCheck), Priority.High, this);
// Register listeners for airbuild check
pm.registerEvent(Event.Type.BLOCK_PLACE, new AirbuildListener(airbuildCheck), Priority.Low, this);
// Register listeners for bedteleport check
pm.registerEvent(Event.Type.PLAYER_TELEPORT, new BedteleportListener(bedteleportCheck), Priority.Lowest, this);
PluginDescriptionFile pdfFile = this.getDescription();
// Get, if available, the Permissions and irc plugin
setupPermissions();
setupIRC();

View File

@ -118,11 +118,11 @@ public class NoCheatConfiguration {
plugin.airbuildCheck.actions[1] = stringToActions(c.getString("airbuild.action.med"), plugin.airbuildCheck.actions[1]);
plugin.airbuildCheck.actions[2] = stringToActions(c.getString("airbuild.action.high"), plugin.airbuildCheck.actions[2]);
plugin.speedhackCheck.setActive(c.getBoolean("active.speedhack", plugin.speedhackCheck.isActive()));
plugin.movingCheck.setActive(c.getBoolean("active.moving", plugin.movingCheck.isActive()));
plugin.airbuildCheck.setActive(c.getBoolean("active.airbuild", plugin.airbuildCheck.isActive()));
plugin.bedteleportCheck.setActive(c.getBoolean("active.bedteleport", plugin.bedteleportCheck.isActive()));
plugin.itemdupeCheck.setActive(c.getBoolean("active.itemdupe", plugin.itemdupeCheck.isActive()));
plugin.speedhackCheck.setActive(c.getBoolean("active.speedhack", true));
plugin.movingCheck.setActive(c.getBoolean("active.moving", true));
plugin.airbuildCheck.setActive(c.getBoolean("active.airbuild", false));
plugin.bedteleportCheck.setActive(c.getBoolean("active.bedteleport", true));
plugin.itemdupeCheck.setActive(c.getBoolean("active.itemdupe", false));
}
public void cleanup() {
@ -241,11 +241,11 @@ public class NoCheatConfiguration {
w.write(" logtoirctag: "+ircTag); w.newLine();
w.write("# Checks and Bugfixes that are activated (true or false)"); w.newLine();
w.write("active:"); w.newLine();
w.write(" speedhack: "+plugin.speedhackCheck.isActive()); w.newLine();
w.write(" moving: "+plugin.movingCheck.isActive()); w.newLine();
w.write(" airbuild: "+plugin.airbuildCheck.isActive()); w.newLine();
w.write(" bedteleport: "+plugin.bedteleportCheck.isActive()); w.newLine();
w.write(" itemdupe: "+plugin.itemdupeCheck.isActive()); w.newLine();
w.write(" speedhack: true"); w.newLine();
w.write(" moving: true"); w.newLine();
w.write(" airbuild: false"); w.newLine();
w.write(" bedteleport: true"); w.newLine();
w.write(" itemdupe: false"); w.newLine();
w.write("# Speedhack specific options"); w.newLine();
w.write("speedhack:"); w.newLine();
w.write(" logmessage: \"" + plugin.speedhackCheck.logMessage+"\""); w.newLine();

View File

@ -1,9 +1,13 @@
package cc.co.evenprime.bukkit.nocheat.checks;
import org.bukkit.Bukkit;
import org.bukkit.Location;
import org.bukkit.Material;
import org.bukkit.entity.Player;
import org.bukkit.event.Event;
import org.bukkit.event.Event.Priority;
import org.bukkit.event.block.BlockPlaceEvent;
import org.bukkit.plugin.PluginManager;
import cc.co.evenprime.bukkit.nocheat.NoCheatData;
import cc.co.evenprime.bukkit.nocheat.NoCheat;
@ -11,6 +15,7 @@ import cc.co.evenprime.bukkit.nocheat.actions.Action;
import cc.co.evenprime.bukkit.nocheat.actions.CancelAction;
import cc.co.evenprime.bukkit.nocheat.actions.CustomAction;
import cc.co.evenprime.bukkit.nocheat.actions.LogAction;
import cc.co.evenprime.bukkit.nocheat.listeners.AirbuildBlockListener;
/**
@ -30,15 +35,13 @@ public class AirbuildCheck extends Check {
public final int limits[] = { 1, 3, 10 };
public AirbuildCheck(NoCheat plugin) {
super(plugin);
setActive(false);
super(plugin, "airbuild", NoCheatData.PERMISSION_AIRBUILD);
}
public void check(BlockPlaceEvent event) {
// Should we check at all?
if(plugin.hasPermission(event.getPlayer(), NoCheatData.PERMISSION_AIRBUILD))
return;
if(hasPermission(event.getPlayer())) return;
// Are all 6 sides "air-blocks" -> cancel the event
if(event.getBlockAgainst().getType() == Material.AIR) {
@ -120,7 +123,11 @@ public class AirbuildCheck extends Check {
}
@Override
public String getName() {
return "airbuild";
protected void registerListeners() {
PluginManager pm = Bukkit.getServer().getPluginManager();
// Register listeners for airbuild check
pm.registerEvent(Event.Type.BLOCK_PLACE, new AirbuildBlockListener(this), Priority.Low, plugin);
}
}

View File

@ -1,9 +1,14 @@
package cc.co.evenprime.bukkit.nocheat.checks;
import org.bukkit.Bukkit;
import org.bukkit.event.Event;
import org.bukkit.event.Event.Priority;
import org.bukkit.event.player.PlayerMoveEvent;
import org.bukkit.plugin.PluginManager;
import cc.co.evenprime.bukkit.nocheat.NoCheat;
import cc.co.evenprime.bukkit.nocheat.NoCheatData;
import cc.co.evenprime.bukkit.nocheat.listeners.BedteleportPlayerListener;
/**
*
@ -14,14 +19,13 @@ import cc.co.evenprime.bukkit.nocheat.NoCheatData;
public class BedteleportCheck extends Check {
public BedteleportCheck(NoCheat plugin) {
super(plugin);
setActive(true);
super(plugin, "bedteleport", NoCheatData.PERMISSION_BEDTELEPORT);
}
public void check(PlayerMoveEvent event) {
// Should we check at all?
if(plugin.hasPermission(event.getPlayer(), NoCheatData.PERMISSION_BEDTELEPORT))
if(hasPermission(event.getPlayer()))
return;
if(event.getPlayer().isSleeping())
@ -29,7 +33,11 @@ public class BedteleportCheck extends Check {
}
@Override
public String getName() {
return "bedteleport";
protected void registerListeners() {
PluginManager pm = Bukkit.getServer().getPluginManager();
// Register listeners for bedteleport check
pm.registerEvent(Event.Type.PLAYER_TELEPORT, new BedteleportPlayerListener(this), Priority.Lowest, plugin);
}
}

View File

@ -1,5 +1,8 @@
package cc.co.evenprime.bukkit.nocheat.checks;
import org.bukkit.entity.Player;
import cc.co.evenprime.bukkit.nocheat.NoCheat;
/**
@ -9,20 +12,42 @@ import cc.co.evenprime.bukkit.nocheat.NoCheat;
*/
public abstract class Check {
public Check(NoCheat plugin) {
this.plugin = plugin;
}
private boolean active = true;
private boolean active = false;
private boolean listenersRegistered = false;
private int permission;
private String name;
protected NoCheat plugin;
public Check(NoCheat plugin, String name, int permission) {
this.plugin = plugin;
this.permission = permission;
this.name = name;
}
protected boolean hasPermission(Player player) {
// Should we check at all?
return !active || plugin.hasPermission(player, permission);
}
protected abstract void registerListeners();
public boolean isActive() {
return active;
}
public void setActive(boolean a) {
active = a;
public void setActive(boolean active) {
synchronized(this) {
if(active && !listenersRegistered) {
listenersRegistered = true;
registerListeners();
}
}
// There is no way to unregister listeners ...
this.active = active;
}
public abstract String getName();
public String getName() {
return name;
}
}

View File

@ -2,26 +2,30 @@ package cc.co.evenprime.bukkit.nocheat.checks;
import java.util.logging.Level;
import org.bukkit.Bukkit;
import org.bukkit.entity.Item;
import org.bukkit.event.Event;
import org.bukkit.event.Listener;
import org.bukkit.event.Event.Priority;
import org.bukkit.event.player.PlayerInteractEvent;
import org.bukkit.event.player.PlayerPickupItemEvent;
import org.bukkit.inventory.ItemStack;
import org.bukkit.plugin.PluginManager;
import cc.co.evenprime.bukkit.nocheat.NoCheat;
import cc.co.evenprime.bukkit.nocheat.NoCheatData;
import cc.co.evenprime.bukkit.nocheat.listeners.ItemdupePlayerListener;
public class ItemdupeCheck extends Check {
public ItemdupeCheck(NoCheat plugin){
super(plugin);
this.setActive(false);
super(plugin, "itemdupe", NoCheatData.PERMISSION_ITEMDUPE);
}
public void check(PlayerPickupItemEvent event) {
// Should we check at all?
if(plugin.hasPermission(event.getPlayer(), NoCheatData.PERMISSION_ITEMDUPE))
return;
if(hasPermission(event.getPlayer())) return;
Item i = event.getItem();
if(i != null) {
@ -49,9 +53,16 @@ public class ItemdupeCheck extends Check {
}
@Override
public String getName() {
protected void registerListeners() {
PluginManager pm = Bukkit.getServer().getPluginManager();
// Register listeners for itemdupe check
Listener itemdupePlayerListener = new ItemdupePlayerListener(this);
// Register listeners for itemdupe check
pm.registerEvent(Event.Type.PLAYER_PICKUP_ITEM, itemdupePlayerListener, Priority.Lowest, plugin);
pm.registerEvent(Event.Type.PLAYER_INTERACT, itemdupePlayerListener, Priority.Lowest, plugin);
return "itemdupe";
}
}

View File

@ -2,13 +2,18 @@ package cc.co.evenprime.bukkit.nocheat.checks;
import java.util.logging.Level;
import org.bukkit.Bukkit;
import org.bukkit.Location;
import org.bukkit.Material;
import org.bukkit.World;
import org.bukkit.entity.Player;
import org.bukkit.event.Event;
import org.bukkit.event.Listener;
import org.bukkit.event.Event.Priority;
import org.bukkit.event.player.PlayerMoveEvent;
import org.bukkit.event.player.PlayerRespawnEvent;
import org.bukkit.event.player.PlayerTeleportEvent;
import org.bukkit.plugin.PluginManager;
import org.bukkit.util.Vector;
import cc.co.evenprime.bukkit.nocheat.NoCheatData;
@ -17,6 +22,9 @@ import cc.co.evenprime.bukkit.nocheat.actions.Action;
import cc.co.evenprime.bukkit.nocheat.actions.CancelAction;
import cc.co.evenprime.bukkit.nocheat.actions.CustomAction;
import cc.co.evenprime.bukkit.nocheat.actions.LogAction;
import cc.co.evenprime.bukkit.nocheat.listeners.MovingEntityListener;
import cc.co.evenprime.bukkit.nocheat.listeners.MovingPlayerListener;
import cc.co.evenprime.bukkit.nocheat.listeners.MovingPlayerMonitor;
/**
* Check if the player should be allowed to make that move, e.g. is he allowed to jump here or move that far in one step
@ -27,8 +35,7 @@ import cc.co.evenprime.bukkit.nocheat.actions.LogAction;
public class MovingCheck extends Check {
public MovingCheck(NoCheat plugin) {
super(plugin);
setActive(true);
super(plugin, "moving", NoCheatData.PERMISSION_MOVING);
}
// How many move events can a player have in air before he is expected to lose altitude (or land somewhere)
@ -173,7 +180,7 @@ public class MovingCheck extends Check {
final Player player = event.getPlayer();
// Should we check at all
if(plugin.hasPermission(player, NoCheatData.PERMISSION_MOVING)) {
if(hasPermission(player)) {
statisticElapsedTimeNano += System.nanoTime() - startTime;
statisticTotalEvents++;
return;
@ -628,15 +635,23 @@ public class MovingCheck extends Check {
private void resetData(NoCheatData data, Location l) {
// If it wasn't our plugin that ordered the teleport, forget (almost) all our information and start from scratch
data.speedhackSetBackPoint = l;
data.movingSetBackPoint = l;
data.speedhackEventsSinceLastCheck = 0;
data.movingJumpPhase = 0;
data.movingTeleportTo = null;
}
@Override
public String getName() {
return "moving";
protected void registerListeners() {
PluginManager pm = Bukkit.getServer().getPluginManager();
Listener movingPlayerMonitor = new MovingPlayerMonitor(this);
// Register listeners for moving check
pm.registerEvent(Event.Type.PLAYER_MOVE, new MovingPlayerListener(this), Priority.Lowest, plugin);
pm.registerEvent(Event.Type.PLAYER_INTERACT, movingPlayerMonitor, Priority.Monitor, plugin);
pm.registerEvent(Event.Type.PLAYER_MOVE, movingPlayerMonitor, Priority.Monitor, plugin);
pm.registerEvent(Event.Type.PLAYER_RESPAWN, movingPlayerMonitor, Priority.Monitor, plugin);
pm.registerEvent(Event.Type.ENTITY_DAMAGE, new MovingEntityListener(this), Priority.Monitor, plugin);
pm.registerEvent(Event.Type.PLAYER_TELEPORT, new MovingPlayerMonitor(this), Priority.Monitor, plugin);
}
}

View File

@ -1,6 +1,12 @@
package cc.co.evenprime.bukkit.nocheat.checks;
import org.bukkit.Bukkit;
import org.bukkit.event.Event;
import org.bukkit.event.Event.Priority;
import org.bukkit.event.Listener;
import org.bukkit.event.player.PlayerMoveEvent;
import org.bukkit.event.player.PlayerTeleportEvent;
import org.bukkit.plugin.PluginManager;
import cc.co.evenprime.bukkit.nocheat.NoCheatData;
import cc.co.evenprime.bukkit.nocheat.NoCheat;
@ -8,6 +14,7 @@ import cc.co.evenprime.bukkit.nocheat.actions.Action;
import cc.co.evenprime.bukkit.nocheat.actions.CancelAction;
import cc.co.evenprime.bukkit.nocheat.actions.CustomAction;
import cc.co.evenprime.bukkit.nocheat.actions.LogAction;
import cc.co.evenprime.bukkit.nocheat.listeners.SpeedhackPlayerListener;
/**
* Log if a player sends to many move events in a specific time frame, usually the result of tinkering with the system clock
@ -18,8 +25,7 @@ import cc.co.evenprime.bukkit.nocheat.actions.LogAction;
public class SpeedhackCheck extends Check {
public SpeedhackCheck(NoCheat plugin) {
super(plugin);
setActive(true);
super(plugin, "speedhack", NoCheatData.PERMISSION_SPEEDHACK);
}
private static final long interval = 1000;
@ -39,8 +45,7 @@ public class SpeedhackCheck extends Check {
public void check(PlayerMoveEvent event) {
// Should we check at all?
if(plugin.hasPermission(event.getPlayer(), NoCheatData.PERMISSION_SPEEDHACK))
return;
if(hasPermission(event.getPlayer())) return;
// Get the player-specific data
NoCheatData data = NoCheatData.getPlayerData(event.getPlayer());
@ -130,7 +135,21 @@ public class SpeedhackCheck extends Check {
}
@Override
public String getName() {
return "speedhack";
protected void registerListeners() {
PluginManager pm = Bukkit.getServer().getPluginManager();
Listener speedhackPlayerListener = new SpeedhackPlayerListener(this);
// Register listeners for speedhack check
pm.registerEvent(Event.Type.PLAYER_MOVE, speedhackPlayerListener, Priority.High, plugin);
pm.registerEvent(Event.Type.PLAYER_TELEPORT, speedhackPlayerListener, Priority.Monitor, plugin);
}
public void teleported(PlayerTeleportEvent event) {
NoCheatData data = NoCheatData.getPlayerData(event.getPlayer());
data.speedhackSetBackPoint = event.getTo();
data.speedhackEventsSinceLastCheck = 0;
}
}

View File

@ -11,17 +11,16 @@ import cc.co.evenprime.bukkit.nocheat.checks.AirbuildCheck;
* @author Evenprime
*
*/
public class AirbuildListener extends BlockListener {
public class AirbuildBlockListener extends BlockListener {
private AirbuildCheck check;
public AirbuildListener(AirbuildCheck check) {
public AirbuildBlockListener(AirbuildCheck check) {
this.check = check;
}
@Override
public void onBlockPlace(BlockPlaceEvent event) {
if(!event.isCancelled() && check.isActive())
check.check(event);
if(!event.isCancelled()) check.check(event);
}
}

View File

@ -10,19 +10,17 @@ import cc.co.evenprime.bukkit.nocheat.checks.BedteleportCheck;
* @author Evenprime
*
*/
public class BedteleportListener extends PlayerListener {
public class BedteleportPlayerListener extends PlayerListener {
private BedteleportCheck check;
public BedteleportListener(BedteleportCheck check) {
public BedteleportPlayerListener(BedteleportCheck check) {
this.check = check;
}
@Override
public void onPlayerTeleport(PlayerTeleportEvent event) {
if(!event.isCancelled() && check.isActive()) {
check.check(event);
}
if(!event.isCancelled()) check.check(event);
}
}

View File

@ -6,24 +6,23 @@ import org.bukkit.event.player.PlayerPickupItemEvent;
import cc.co.evenprime.bukkit.nocheat.checks.ItemdupeCheck;
public class ItemdupeListener extends PlayerListener {
public class ItemdupePlayerListener extends PlayerListener {
ItemdupeCheck check;
public ItemdupeListener(ItemdupeCheck itemdupeCheck) {
public ItemdupePlayerListener(ItemdupeCheck itemdupeCheck) {
check = itemdupeCheck;
}
@Override
public void onPlayerPickupItem(PlayerPickupItemEvent event) {
if(check.isActive()) check.check(event);
if(!event.isCancelled()) check.check(event);
}
@Override
public void onPlayerInteract(PlayerInteractEvent event) {
if(check.isActive()) check.check(event);
if(!event.isCancelled()) check.check(event);
}
}

View File

@ -15,7 +15,6 @@ public class MovingEntityListener extends EntityListener {
this.check = check;
}
@Override
public void onEntityDamage(EntityDamageEvent event) {
if(event.getEntity() instanceof Player) {

View File

@ -12,18 +12,16 @@ import cc.co.evenprime.bukkit.nocheat.checks.MovingCheck;
* @author Evenprime
*/
public class MovingListener extends PlayerListener {
public class MovingPlayerListener extends PlayerListener {
private MovingCheck check;
public MovingListener(MovingCheck check) {
public MovingPlayerListener(MovingCheck check) {
this.check = check;
}
@Override
public void onPlayerMove(PlayerMoveEvent event) {
if(!event.isCancelled() && check.isActive())
check.check(event);
if(!event.isCancelled()) check.check(event);
}
}

View File

@ -14,11 +14,11 @@ import cc.co.evenprime.bukkit.nocheat.checks.MovingCheck;
* @author Evenprime
*
*/
public class MovingMonitor extends PlayerListener {
public class MovingPlayerMonitor extends PlayerListener {
private MovingCheck check;
public MovingMonitor(MovingCheck check) {
public MovingPlayerMonitor(MovingCheck check) {
this.check = check;
}

View File

@ -1,27 +0,0 @@
package cc.co.evenprime.bukkit.nocheat.listeners;
import org.bukkit.event.player.PlayerListener;
import org.bukkit.event.player.PlayerMoveEvent;
import cc.co.evenprime.bukkit.nocheat.checks.SpeedhackCheck;
/**
*
* @author Evenprime
*
*/
public class SpeedhackListener extends PlayerListener {
private SpeedhackCheck check;
public SpeedhackListener(SpeedhackCheck check) {
this.check = check;
}
@Override
public void onPlayerMove(PlayerMoveEvent event) {
if(!event.isCancelled() && check.isActive())
check.check(event);
}
}

View File

@ -0,0 +1,33 @@
package cc.co.evenprime.bukkit.nocheat.listeners;
import org.bukkit.event.player.PlayerListener;
import org.bukkit.event.player.PlayerMoveEvent;
import org.bukkit.event.player.PlayerTeleportEvent;
import cc.co.evenprime.bukkit.nocheat.checks.SpeedhackCheck;
/**
*
* @author Evenprime
*
*/
public class SpeedhackPlayerListener extends PlayerListener {
private SpeedhackCheck check;
public SpeedhackPlayerListener(SpeedhackCheck check) {
this.check = check;
}
@Override
public void onPlayerMove(PlayerMoveEvent event) {
if(!event.isCancelled()) check.check(event);
}
@Override
public void onPlayerTeleport(PlayerTeleportEvent event) {
if(!event.isCancelled()) check.teleported(event);
}
}