From ef3bc33748242667fb9ce60e8a30d0538e2ac8c6 Mon Sep 17 00:00:00 2001 From: Evenprime Date: Sat, 19 Feb 2011 14:50:05 +0100 Subject: [PATCH] Minor change to onGround Check + more config options Instead of using the built-in Math.floor I now use my own, because I want to treat a very special case different to see if it fixes people "freezing" on borders of blocks. Now you can change the limit(s) for the speedhack-check yourself. --- .../bukkit/nocheat/NoCheatConfiguration.java | 66 ++++++++++++++----- .../bukkit/nocheat/NoCheatPlugin.java | 8 +-- .../nocheat/NoCheatPluginPlayerListener.java | 61 ++++++++++------- 3 files changed, 88 insertions(+), 47 deletions(-) diff --git a/src/cc/co/evenprime/bukkit/nocheat/NoCheatConfiguration.java b/src/cc/co/evenprime/bukkit/nocheat/NoCheatConfiguration.java index 470d1bd3..6fd4dab9 100644 --- a/src/cc/co/evenprime/bukkit/nocheat/NoCheatConfiguration.java +++ b/src/cc/co/evenprime/bukkit/nocheat/NoCheatConfiguration.java @@ -8,7 +8,6 @@ import java.util.logging.ConsoleHandler; import java.util.logging.FileHandler; import java.util.logging.Level; import java.util.logging.Logger; -import java.util.logging.SimpleFormatter; import org.bukkit.util.config.Configuration; @@ -17,6 +16,16 @@ public class NoCheatConfiguration { public static final String loggerName = "cc.co.evenprime.bukkit.nocheat"; public static final Logger logger = Logger.getLogger(loggerName); + public static boolean speedhackActive = true; + public static boolean movingActive = true; + public static int speedhackInterval = 2000; + public static int speedhackLow = 60; + public static int speedhackMed = 90; + public static int speedhackHigh = 120; + + private static ConsoleHandler ch = null; + private static FileHandler fh = null; + private NoCheatConfiguration() {} public static void config(File configurationFile) { @@ -30,23 +39,34 @@ public class NoCheatConfiguration { logger.setLevel(Level.INFO); logger.setUseParentHandlers(false); - ConsoleHandler ch = new ConsoleHandler(); - - ch.setLevel(stringToLevel(c.getString("logging.logtoconsole"))); - ch.setFormatter(Logger.getLogger("Minecraft").getHandlers()[0].getFormatter()); - logger.addHandler(ch); - - FileHandler fh = null; - try { - fh = new FileHandler(c.getString("logging.filename"), true); - fh.setLevel(stringToLevel(c.getString("logging.logtofile"))); - fh.setFormatter(Logger.getLogger("Minecraft").getHandlers()[0].getFormatter()); - logger.addHandler(fh); - - } catch (Exception e) { - // TODO Auto-generated catch block - e.printStackTrace(); + if(ch == null) { + ch = new ConsoleHandler(); + + ch.setLevel(stringToLevel(c.getString("logging.logtoconsole"))); + ch.setFormatter(Logger.getLogger("Minecraft").getHandlers()[0].getFormatter()); + logger.addHandler(ch); } + + if(fh == null) { + try { + fh = new FileHandler(c.getString("logging.filename"), true); + fh.setLevel(stringToLevel(c.getString("logging.logtofile"))); + fh.setFormatter(Logger.getLogger("Minecraft").getHandlers()[0].getFormatter()); + logger.addHandler(fh); + + } catch (Exception e) { + // TODO Auto-generated catch block + e.printStackTrace(); + } + } + + speedhackActive = c.getBoolean("active.speedhack", true); + movingActive = c.getBoolean("active.moving", true); + + speedhackInterval = c.getInt("speedhack.interval", 2000); + speedhackLow = c.getInt("speedhack.limits.low", 60); + speedhackMed = c.getInt("speedhack.limits.med", 90); + speedhackHigh = c.getInt("speedhack.limits.high", 120); } private static Level stringToLevel(String string) { @@ -67,10 +87,22 @@ public class NoCheatConfiguration { f.createNewFile(); BufferedWriter w = new BufferedWriter(new FileWriter(f)); + w.write("# Logging: potential log levels are info, warn, severe, off"); w.newLine(); w.write("logging:"); w.newLine(); w.write(" filename: plugins/NoCheat/nocheat.log"); w.newLine(); w.write(" logtofile: info"); w.newLine(); w.write(" logtoconsole: severe"); w.newLine(); + w.write("# Checks that are activated (true or false)"); w.newLine(); + w.write("active:"); w.newLine(); + w.write(" speedhack: true"); w.newLine(); + w.write(" moving: true"); w.newLine(); + w.write("# Speedhack: interval in milliseconds, limits are events in that interval") ;w.newLine(); + w.write("speedhack:"); w.newLine(); + w.write(" interval: 2000"); w.newLine(); + w.write(" limits:"); w.newLine(); + w.write(" low: 60"); w.newLine(); + w.write(" med: 90"); w.newLine(); + w.write(" high: 120"); w.newLine(); w.flush(); w.close(); } catch (IOException e) { // TODO Auto-generated catch block diff --git a/src/cc/co/evenprime/bukkit/nocheat/NoCheatPlugin.java b/src/cc/co/evenprime/bukkit/nocheat/NoCheatPlugin.java index 98dfa30d..db54e77a 100644 --- a/src/cc/co/evenprime/bukkit/nocheat/NoCheatPlugin.java +++ b/src/cc/co/evenprime/bukkit/nocheat/NoCheatPlugin.java @@ -37,7 +37,7 @@ public class NoCheatPlugin extends JavaPlugin { playerListener = new NoCheatPluginPlayerListener(this); vehicleListener = new NoCheatPluginVehicleListener(this, playerListener); - setupConfig(); + log = NoCheatConfiguration.logger; } @@ -58,6 +58,7 @@ public class NoCheatPlugin extends JavaPlugin { Logger.getLogger("Minecraft").info( "NoCheat version " + pdfFile.getVersion() + " is enabled!" ); setupPermissions(); + setupConfig(); } public void setupPermissions() { @@ -76,9 +77,6 @@ public class NoCheatPlugin extends JavaPlugin { public void setupConfig() { NoCheatConfiguration.config(new File("plugins/NoCheat/nocheat.yml")); - - // Test config - Logger l = Logger.getLogger(NoCheatConfiguration.loggerName); - + } } \ No newline at end of file diff --git a/src/cc/co/evenprime/bukkit/nocheat/NoCheatPluginPlayerListener.java b/src/cc/co/evenprime/bukkit/nocheat/NoCheatPluginPlayerListener.java index a237a125..ba044fc5 100644 --- a/src/cc/co/evenprime/bukkit/nocheat/NoCheatPluginPlayerListener.java +++ b/src/cc/co/evenprime/bukkit/nocheat/NoCheatPluginPlayerListener.java @@ -151,9 +151,6 @@ public class NoCheatPluginPlayerListener extends PlayerListener { private static double maxX = 0.5D; private static double maxZ = 0.5D; - private static final long timeFrameForSpeedHackCheck = 2000; - private static final long eventLimitForSpeedHackCheck = 60; - // Store data between Events private static Map playerData = new HashMap(); @@ -208,27 +205,31 @@ public class NoCheatPluginPlayerListener extends PlayerListener { // Get the time of the server long time = System.currentTimeMillis(); - boolean allowSpeedhack = false; + boolean ignoreSpeedhackCheck = false; + if(!NoCheatConfiguration.speedhackActive) ignoreSpeedhackCheck = true; if(NoCheatPlugin.Permissions != null && NoCheatPlugin.Permissions.has(event.getPlayer(), "nocheat.speedhack")) { - allowSpeedhack = true; + ignoreSpeedhackCheck = true; } else if(NoCheatPlugin.Permissions == null && event.getPlayer().isOp() ) { - allowSpeedhack = true; + ignoreSpeedhackCheck = true; } - if(!allowSpeedhack){ + if(!ignoreSpeedhackCheck){ // Is it time for a speedhack check now? - if(time > timeFrameForSpeedHackCheck + data.lastSpeedHackCheck ) { + if(time > NoCheatConfiguration.speedhackInterval + data.lastSpeedHackCheck ) { // Yes - int limit = (int)((eventLimitForSpeedHackCheck * (time - data.lastSpeedHackCheck)) / timeFrameForSpeedHackCheck); - - if(data.eventsSinceLastSpeedHackCheck > limit) { - // Probably someone is speedhacking here! Better log that - NoCheatPlugin.log.info("NoCheatPlugin: "+event.getPlayer().getDisplayName()+" probably uses a speedhack. He sent "+ data.eventsSinceLastSpeedHackCheck + " events, but only "+limit+ " were allowed in the timeframe!"); - } + int limitLow = (int)((NoCheatConfiguration.speedhackLow * (time - data.lastSpeedHackCheck)) / NoCheatConfiguration.speedhackInterval); + int limitMed = (int)((NoCheatConfiguration.speedhackMed * (time - data.lastSpeedHackCheck)) / NoCheatConfiguration.speedhackInterval); + int limitHigh = (int)((NoCheatConfiguration.speedhackHigh * (time - data.lastSpeedHackCheck)) / NoCheatConfiguration.speedhackInterval); + if(data.eventsSinceLastSpeedHackCheck > limitHigh) + NoCheatPlugin.log.severe("NoCheatPlugin: "+event.getPlayer().getName()+" sent "+ data.eventsSinceLastSpeedHackCheck + " move events, but only "+limitLow+ " were allowed. Speedhack?"); + else if(data.eventsSinceLastSpeedHackCheck > limitMed) + NoCheatPlugin.log.warning("NoCheatPlugin: "+event.getPlayer().getName()+" sent "+ data.eventsSinceLastSpeedHackCheck + " move events, but only "+limitLow+ " were allowed. Speedhack?"); + else if(data.eventsSinceLastSpeedHackCheck > limitLow) + NoCheatPlugin.log.info("NoCheatPlugin: "+event.getPlayer().getName()+" sent "+ data.eventsSinceLastSpeedHackCheck + " move events, but only "+limitLow+ " were allowed. Speedhack?"); // Reset values for next check data.eventsSinceLastSpeedHackCheck = 0; data.lastSpeedHackCheck = time; @@ -237,15 +238,16 @@ public class NoCheatPluginPlayerListener extends PlayerListener { data.eventsSinceLastSpeedHackCheck++; } - boolean allowMoving = false; - if(NoCheatPlugin.Permissions != null && NoCheatPlugin.Permissions.has(event.getPlayer(), "nocheat.moving")) { - allowMoving = true; + boolean ignoreMovingCheck = false; + if(!NoCheatConfiguration.movingActive) ignoreMovingCheck = true; + else if(NoCheatPlugin.Permissions != null && NoCheatPlugin.Permissions.has(event.getPlayer(), "nocheat.moving")) { + ignoreMovingCheck = true; } else if(NoCheatPlugin.Permissions == null && event.getPlayer().isOp() ) { - allowMoving = true; + ignoreMovingCheck = true; } - if(!allowMoving){ + if(!ignoreMovingCheck){ // First check the distance the player has moved horizontally // TODO: Make this check much more precise if(!event.isCancelled()) { @@ -262,8 +264,8 @@ public class NoCheatPluginPlayerListener extends PlayerListener { // pre-calculate boundary values that are needed multiple times in the following checks // the array each contains [lowerX, higherX, Y, lowerZ, higherZ] - int fromValues[] = {(int)Math.floor(from.getX() - 0.3D), (int)Math.floor(from.getX() + 0.3D), from.getBlockY(), (int)Math.floor(from.getZ() - 0.3D),(int)Math.floor(from.getZ() + 0.3D) }; - int toValues[] = {(int)Math.floor(to.getX() - 0.3D), (int)Math.floor(to.getX() + 0.3D), to.getBlockY(), (int)Math.floor(to.getZ() - 0.3D),(int)Math.floor(to.getZ() + 0.3D) }; + int fromValues[] = {floor_double(from.getX() - 0.3D), (int)Math.floor(from.getX() + 0.3D), from.getBlockY(), floor_double(from.getZ() - 0.3D),(int)Math.floor(from.getZ() + 0.3D) }; + int toValues[] = {floor_double(to.getX() - 0.3D), (int)Math.floor(to.getX() + 0.3D), to.getBlockY(), floor_double(to.getZ() - 0.3D), (int)Math.floor(to.getZ() + 0.3D) }; // compare locations to the world to guess if the player is standing on the ground, a half-block or next to a ladder boolean onGroundFrom = playerIsOnGround(from.getWorld(), fromValues, from); @@ -313,10 +315,13 @@ public class NoCheatPluginPlayerListener extends PlayerListener { } // Player is moving through air (during jumping, falling) else { + // May also be at the very edge of a platform (I seem to not be able to reliably tell if that's the case if(!(to.getY() - from.getY() < jumpingPhases[data.phase])) { event.setCancelled(true); } - else data.phase++; // Enter next phase of the flight + else { + data.phase++; // Enter next phase of the flight + } } // do a security check on the jumping phase, such that we don't get @@ -331,8 +336,8 @@ public class NoCheatPluginPlayerListener extends PlayerListener { data.violations++; // Log the violation - NoCheatPlugin.log.info("NoCheatPlugin: "+event.getPlayer().getDisplayName()+" begins violating constraints. Total Violations: "+data.violations); - NoCheatPlugin.log.info("NoCheatPlugin: He tried to go from " + String.format("%.5f,%.5f,%.5f to %.5f,%.5f,%.5f", from.getX(), from.getY(), from.getZ(), to.getX(), to.getY(), to.getZ())); + NoCheatPlugin.log.info("NoCheatPlugin: "+event.getPlayer().getName()+" begins violating constraints. Total Violations: "+data.violations); + NoCheatPlugin.log.info("NoCheatPlugin: Moving from " + String.format("(%.5f, %.5f, %.5f) to (%.5f, %.5f, %.5f)", from.getX(), from.getY(), from.getZ(), to.getX(), to.getY(), to.getZ())); data.lastWasInvalid = true; @@ -355,7 +360,7 @@ public class NoCheatPluginPlayerListener extends PlayerListener { } else if(!event.isCancelled() && data.lastWasInvalid) { data.lastWasInvalid = false; - NoCheatPlugin.log.info("NoCheatPlugin: "+event.getPlayer().getDisplayName()+" stopped violating constraints. Total Violations: "+data.violations); + NoCheatPlugin.log.info("NoCheatPlugin: "+event.getPlayer().getName()+" stopped violating constraints. Total Violations: "+data.violations); } } } @@ -409,4 +414,10 @@ public class NoCheatPluginPlayerListener extends PlayerListener { else return false; } + + public static int floor_double(double d) + { + int i = (int)d; + return d > (double)i ? i : i - 1; + } }