Refactoring: seperate listeners for different checks + instancing

instead of static for almost anything
This commit is contained in:
Evenprime 2011-03-25 16:47:37 +01:00
parent 6a6cb9e247
commit 77f347e182
16 changed files with 991 additions and 918 deletions

View File

@ -3,7 +3,7 @@ name: NoCheatPlugin
author: Evenprime
main: cc.co.evenprime.bukkit.nocheat.NoCheatPlugin
version: 0.7.4
version: 0.7.4a
commands:
nocheat:

View File

@ -19,60 +19,33 @@ import org.bukkit.util.config.Configuration;
public class NoCheatConfiguration {
// Our personal logger
public static final String loggerName = "cc.co.evenprime.nocheat";
public static final Logger logger = Logger.getLogger(loggerName);
// Which checks are active
public static boolean speedhackCheckActive;
public static boolean movingCheckActive;
public static boolean airbuildCheckActive;
public static boolean bedteleportCheckActive;
// Limits for the speedhack check
public static int speedhackLimitLow;
public static int speedhackLimitMed;
public static int speedhackLimitHigh;
// How should speedhack violations be treated?
public static String speedhackActionMinor = "";
public static String speedhackActionNormal = "";
public static String speedhackActionHeavy = "";
public static int movingFreeMoves = 1;
// How should moving violations be treated?
public static String movingActionMinor = "";
public static String movingActionNormal = "";
public static String movingActionHeavy = "";
// How should airbuild violations be treated?
public static String airbuildActionLow = "";
public static String airbuildActionMed = "";
public static String airbuildActionHigh = "";
public static Runnable airbuildRunnable = null;
public static int airbuildLimitLow = 1;
public static int airbuildLimitMed = 3;
public static int airbuildLimitHigh = 10;
private final String loggerName = "cc.co.evenprime.nocheat";
public final Logger logger = Logger.getLogger(loggerName);
// The log level above which information gets logged to the specified logger
public static Level chatLevel = Level.OFF;
public static Level ircLevel = Level.OFF;
public static Level consoleLevel = Level.OFF;
public Level chatLevel = Level.OFF;
public Level ircLevel = Level.OFF;
public Level consoleLevel = Level.OFF;
public static String ircTag = "";
public String ircTag = "";
// Our log output to a file
private static FileHandler fh = null;
private FileHandler fh = null;
private NoCheatConfiguration() {}
private final NoCheatPlugin plugin;
public NoCheatConfiguration(File configurationFile, NoCheatPlugin plugin) {
this.plugin = plugin;
this.config(configurationFile);
}
/**
* Read the configuration file and assign either standard values or whatever is declared in the file
* @param configurationFile
*/
public static void config(File configurationFile) {
public void config(File configurationFile) {
if(!configurationFile.exists()) {
createStandardConfigFile(configurationFile);
@ -103,38 +76,30 @@ public class NoCheatConfiguration {
ircLevel = stringToLevel(c.getString("logging.logtoirc"));
ircTag = c.getString("logging.logtoirctag", "nocheat");
speedhackCheckActive = c.getBoolean("active.speedhack", true);
movingCheckActive = c.getBoolean("active.moving", true);
airbuildCheckActive = c.getBoolean("active.airbuild", false);
bedteleportCheckActive = c.getBoolean("active.bedteleport", true);
plugin.speedhackCheck.limitLow = c.getInt("speedhack.limits.low", plugin.speedhackCheck.limitLow);
plugin.speedhackCheck.limitMed = c.getInt("speedhack.limits.med", plugin.speedhackCheck.limitMed);
plugin.speedhackCheck.limitHigh = c.getInt("speedhack.limits.high", plugin.speedhackCheck.limitHigh);
speedhackLimitLow = c.getInt("speedhack.limits.low", 30);
speedhackLimitMed = c.getInt("speedhack.limits.med", 45);
speedhackLimitHigh = c.getInt("speedhack.limits.high", 60);
plugin.movingCheck.actionLow = c.getString("moving.action.low", plugin.movingCheck.actionLow);
plugin.movingCheck.actionMed = c.getString("moving.action.med", plugin.movingCheck.actionMed);
plugin.movingCheck.actionHigh = c.getString("moving.action.high", plugin.movingCheck.actionHigh);
movingFreeMoves = c.getInt("moving.freemoves", 1);
plugin.speedhackCheck.actionLow = c.getString("speedhack.action.low", plugin.speedhackCheck.actionLow);
plugin.speedhackCheck.actionMed = c.getString("speedhack.action.med", plugin.speedhackCheck.actionMed);
plugin.speedhackCheck.actionHigh = c.getString("speedhack.action.high", plugin.speedhackCheck.actionHigh);
movingActionMinor = c.getString("moving.action.low", "loglow reset");
movingActionNormal = c.getString("moving.action.med", "logmed reset");
movingActionHeavy = c.getString("moving.action.high", "loghigh reset");
plugin.airbuildCheck.limitLow = c.getInt("airbuild.limits.low", plugin.airbuildCheck.limitLow);
plugin.airbuildCheck.limitMed = c.getInt("airbuild.limits.med", plugin.airbuildCheck.limitMed);
plugin.airbuildCheck.limitHigh = c.getInt("airbuild.limits.high", plugin.airbuildCheck.limitHigh);
speedhackActionMinor = c.getString("speedhack.action.low", "loglow reset");
speedhackActionNormal = c.getString("speedhack.action.med", "logmed reset");
speedhackActionHeavy = c.getString("speedhack.action.high", "loghigh reset");
plugin.airbuildCheck.actionLow = c.getString("airbuild.action.low", plugin.airbuildCheck.actionLow);
plugin.airbuildCheck.actionMed = c.getString("airbuild.action.med", plugin.airbuildCheck.actionMed);
plugin.airbuildCheck.actionHigh = c.getString("airbuild.action.high", plugin.airbuildCheck.actionHigh);
airbuildLimitLow = c.getInt("airbuild.limits.low", 1);
airbuildLimitMed = c.getInt("airbuild.limits.med", 3);
airbuildLimitHigh = c.getInt("airbuild.limits.high", 10);
airbuildActionLow = c.getString("airbuild.action.low", "loglow deny");
airbuildActionMed = c.getString("airbuild.action.med", "logmed deny");
airbuildActionHigh = c.getString("airbuild.action.high", "loghigh deny");
// 1 is minimum. This is needed to smooth over some minecraft bugs like
// when a minecart gets broken while a player is inside it (which causes the player to "move"
// up 1.0D which is much more than a usual jump would allow in 1 event
if(movingFreeMoves < 1) movingFreeMoves = 1;
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()));
}
/**
@ -158,7 +123,7 @@ public class NoCheatConfiguration {
* Standard configuration file for people who haven't got one yet
* @param f
*/
private static void createStandardConfigFile(File f) {
private void createStandardConfigFile(File f) {
try {
f.getParentFile().mkdirs();
f.createNewFile();
@ -174,42 +139,40 @@ public class NoCheatConfiguration {
w.write(" logtoirctag: nocheat"); w.newLine();
w.write("# Checks and Bugfixes 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(" airbuild: false"); w.newLine();
w.write(" bedteleport: true"); 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("# Speedhack specific options"); w.newLine();
w.write("speedhack:"); w.newLine();
w.write(" limits:"); w.newLine();
w.write(" low: 30"); w.newLine();
w.write(" med: 45"); w.newLine();
w.write(" high: 60"); w.newLine();
w.write(" low: "+plugin.speedhackCheck.limitLow); w.newLine();
w.write(" med: "+plugin.speedhackCheck.limitMed); w.newLine();
w.write(" high: "+plugin.speedhackCheck.limitHigh); w.newLine();
w.write("# Speedhack Action, one or more of 'loglow logmed loghigh reset'"); w.newLine();
w.write(" action:"); w.newLine();
w.write(" low: loglow reset"); w.newLine();
w.write(" med: logmed reset"); w.newLine();
w.write(" high: loghigh reset"); w.newLine();
w.write(" low: "+plugin.speedhackCheck.actionLow); w.newLine();
w.write(" med: "+plugin.speedhackCheck.actionMed); w.newLine();
w.write(" high: "+plugin.speedhackCheck.actionHigh); w.newLine();
w.write("# Moving specific options") ; w.newLine();
w.write("moving:"); w.newLine();
w.write("# After how many minor violations should the plugin react (minimum 1)"); w.newLine();
w.write(" freemoves: 1"); w.newLine();
w.write("# Moving Action, one or more of 'loglow logmed loghigh reset'"); w.newLine();
w.write(" action:"); w.newLine();
w.write(" low: loglow reset"); w.newLine();
w.write(" med: logmed reset"); w.newLine();
w.write(" high: loghigh reset"); w.newLine();
w.write(" low: "+plugin.movingCheck.actionLow); w.newLine();
w.write(" med: "+plugin.movingCheck.actionMed); w.newLine();
w.write(" high: "+plugin.movingCheck.actionHigh); w.newLine();
w.write("# Airbuild specific options"); w.newLine();
w.write("airbuild:"); w.newLine();
w.write("# How many blocks per second are placed by the player in midair (determines log level)"); w.newLine();
w.write(" limits:"); w.newLine();
w.write(" low: 1"); w.newLine();
w.write(" med: 3"); w.newLine();
w.write(" high: 10"); w.newLine();
w.write(" low: "+plugin.airbuildCheck.limitLow); w.newLine();
w.write(" med: "+plugin.airbuildCheck.limitMed); w.newLine();
w.write(" high: "+plugin.airbuildCheck.limitHigh); w.newLine();
w.write("# Airbuild Action, one or more of 'loglow logmed loghigh deny'"); w.newLine();
w.write(" action:"); w.newLine();
w.write(" low: loglow deny"); w.newLine();
w.write(" med: logmed deny"); w.newLine();
w.write(" high: loghigh deny"); w.newLine();
w.write(" low: "+plugin.airbuildCheck.actionLow); w.newLine();
w.write(" med: "+plugin.airbuildCheck.actionMed); w.newLine();
w.write(" high: "+plugin.airbuildCheck.actionHigh); w.newLine();
w.write("# Bedteleport specific options (none exist yet)"); w.newLine();
w.write("bedteleport:"); w.newLine();

View File

@ -15,9 +15,15 @@ import org.bukkit.plugin.PluginDescriptionFile;
import org.bukkit.plugin.java.JavaPlugin;
import org.bukkit.plugin.PluginManager;
import cc.co.evenprime.bukkit.nocheat.listeners.NoCheatBlockListener;
import cc.co.evenprime.bukkit.nocheat.listeners.NoCheatPlayerListener;
import cc.co.evenprime.bukkit.nocheat.listeners.NoCheatPlayerListenerMonitor;
import cc.co.evenprime.bukkit.nocheat.checks.AirbuildCheck;
import cc.co.evenprime.bukkit.nocheat.checks.BedteleportCheck;
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.SpeedhackListener;
import com.ensifera.animosity.craftirc.CraftIRC;
import com.nijikokun.bukkit.Permissions.Permissions;
@ -28,34 +34,36 @@ import org.bukkit.plugin.Plugin;
*
* NoCheatPlugin
*
* Check various player events for their plausibilty and log/deny them based on configuration
* Check various player events for their plausibility and log/deny them based on configuration
*
* @author Evenprime
*/
public class NoCheatPlugin extends JavaPlugin {
// Various listeners needed for different Checks
private NoCheatPlayerListener playerListener;
private NoCheatPlayerListenerMonitor playerListenerMonitor;
private NoCheatBlockListener blockListener;
public final MovingCheck movingCheck;
public final BedteleportCheck bedteleportCheck;
public final SpeedhackCheck speedhackCheck;
public final AirbuildCheck airbuildCheck;
// My main logger
private static Logger consoleLogger;
private static Logger fileLogger;
private NoCheatConfiguration config;
public static NoCheatPlugin p;
// Permissions 2.x, if available
private PermissionHandler permissions;
// Permissions 2.0, if available
public static PermissionHandler Permissions = null;
// CraftIRC 2.0, if available
public static CraftIRC Irc = null;
// CraftIRC 2.x, if available
private CraftIRC irc;
// Store data between Events
public static Map<Player, NoCheatData> playerData = new HashMap<Player, NoCheatData>();
private final Map<Player, NoCheatData> playerData = new HashMap<Player, NoCheatData>();
public NoCheatPlugin() {
p = this;
movingCheck = new MovingCheck(this);
bedteleportCheck = new BedteleportCheck(this);
speedhackCheck = new SpeedhackCheck(this);
airbuildCheck = new AirbuildCheck(this);
// parse the nocheat.yml config file
setupConfig();
}
/**
@ -66,7 +74,7 @@ public class NoCheatPlugin extends JavaPlugin {
* @param p
* @return
*/
public static NoCheatData getPlayerData(Player p) {
public NoCheatData getPlayerData(Player p) {
NoCheatData data = null;
if((data = playerData.get(p)) == null ) {
@ -94,7 +102,7 @@ public class NoCheatPlugin extends JavaPlugin {
}
if(args.length == 0) {
sender.sendMessage("NC: Using "+ ((Permissions == null) ? "isOp()" : "Permissions") + ". Activated checks/bugfixes: " + getActiveChecksAsString());
sender.sendMessage("NC: Using "+ ((permissions == null) ? "isOp()" : "Permissions") + ". Activated checks/bugfixes: " + getActiveChecksAsString());
return true;
}
else if(args.length == 1 && args[0] != null && args[0].trim().equals("-p")) {
@ -129,32 +137,35 @@ public class NoCheatPlugin extends JavaPlugin {
public void onDisable() {
PluginDescriptionFile pdfFile = this.getDescription();
Logger.getLogger("Minecraft").info( "[NoCheatPlugin] version [" + pdfFile.getVersion() + "] is disabled.");
}
public void onEnable() {
// Create our listeners and feed them with neccessary information
playerListener = new NoCheatPlayerListener();
playerListenerMonitor = new NoCheatPlayerListenerMonitor();
blockListener = new NoCheatBlockListener();
fileLogger = NoCheatConfiguration.logger;
consoleLogger = Logger.getLogger("Minecraft");
PluginManager pm = getServer().getPluginManager();
pm.registerEvent(Event.Type.PLAYER_MOVE, playerListener, Priority.Lowest, this); // used for speedhack and moving checks
pm.registerEvent(Event.Type.PLAYER_QUIT, playerListener, Priority.Monitor, this); // used to delete old data of users
pm.registerEvent(Event.Type.BLOCK_PLACED, blockListener, Priority.Low, this); // used for airbuild check
pm.registerEvent(Event.Type.PLAYER_TELEPORT, playerListener, Priority.Lowest, this); // used for teleportfrombed check
pm.registerEvent(Event.Type.PLAYER_TELEPORT, playerListenerMonitor, Priority.Monitor, this); // used for moving, speedhack check
PluginDescriptionFile pdfFile = this.getDescription();
// parse the nocheat.yml config file
setupConfig();
// 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);
// 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_PLACED, 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();
@ -165,7 +176,7 @@ public class NoCheatPlugin extends JavaPlugin {
/**
* Get, if available, a reference to the Permissions-plugin
*/
public void setupPermissions() {
private void setupPermissions() {
PermissionHandler p = null;
Plugin test = this.getServer().getPluginManager().getPlugin("Permissions");
@ -183,13 +194,13 @@ public class NoCheatPlugin extends JavaPlugin {
Logger.getLogger("Minecraft").warning("[NoCheatPlugin] version [" + pdfFile.getVersion() + "] couldn't find Permissions plugin. Fallback to 'isOp()' equals 'nocheat.*'");
}
Permissions = p;
permissions = p;
}
/**
* Get, if available, a reference to the Permissions-plugin
*/
public void setupIRC() {
private void setupIRC() {
CraftIRC p = null;
Plugin test = this.getServer().getPluginManager().getPlugin("CraftIRC");
@ -203,45 +214,46 @@ public class NoCheatPlugin extends JavaPlugin {
Logger.getLogger("Minecraft").warning("[NoCheatPlugin] version [" + pdfFile.getVersion() + "] couldn't find CrafTIRC plugin. Disabling logging to IRC.");
}
Irc = p;
irc = p;
}
/**
* Log a violation message to all locations declared in the config file
* @param message
*/
private static void log(Level l, String message) {
private void log(Level l, String message) {
if(l != null) {
logToChat(l, message);
logToIRC(l, message);
logToConsole(l, message);
fileLogger.log(l, message);
config.logger.log(l, message);
}
}
private static void logToChat(Level l, String message) {
if(NoCheatConfiguration.chatLevel.intValue() <= l.intValue()) {
for(Player player : p.getServer().getOnlinePlayers()) {
private void logToChat(Level l, String message) {
if(config.chatLevel.intValue() <= l.intValue()) {
for(Player player : getServer().getOnlinePlayers()) {
if(hasPermission(player, "nocheat.notify")) {
player.sendMessage("["+l.getName()+"] " + message);
}
}
}
}
private static void logToIRC(Level l, String message) {
if(Irc != null && NoCheatConfiguration.ircLevel.intValue() <= l.intValue()) {
Irc.sendMessageToTag("["+l.getName()+"] " + message , NoCheatConfiguration.ircTag);
private void logToIRC(Level l, String message) {
if(irc != null && config.ircLevel.intValue() <= l.intValue()) {
irc.sendMessageToTag("["+l.getName()+"] " + message , config.ircTag);
}
}
private static void logToConsole(Level l, String message) {
if( NoCheatConfiguration.consoleLevel.intValue() <= l.intValue()) {
consoleLogger.log(l, message);
private void logToConsole(Level l, String message) {
if( config.consoleLevel.intValue() <= l.intValue()) {
Logger.getLogger("Minecraft").log(l, message);
}
}
public static void logAction(String actions, String message) {
public void logAction(String actions, String message) {
if(actions == null) return;
// LOGGING IF NEEDED AND WHERE NEEDED
@ -258,19 +270,19 @@ public class NoCheatPlugin extends JavaPlugin {
}
if(logLevel != null) {
NoCheatPlugin.log(logLevel, "NC: "+message);
log(logLevel, "NC: "+message);
}
}
public static boolean hasPermission(Player player, String permission) {
public boolean hasPermission(Player player, String permission) {
if(player == null || permission == null) {
return false;
}
if(NoCheatPlugin.Permissions != null && NoCheatPlugin.Permissions.has(player, permission))
if(permissions != null && permissions.has(player, permission))
return true;
else if(NoCheatPlugin.Permissions == null && player.isOp())
else if(permissions == null && player.isOp())
return true;
else
return false;
@ -280,24 +292,26 @@ public class NoCheatPlugin extends JavaPlugin {
* Read the config file
*/
private void setupConfig() {
NoCheatConfiguration.config(new File("plugins/NoCheat/nocheat.yml"));
if(this.config == null)
this.config = new NoCheatConfiguration(new File("plugins/NoCheat/nocheat.yml"), this);
else
this.config.config(new File("plugins/NoCheat/nocheat.yml"));
}
private String getActiveChecksAsString() {
return (NoCheatConfiguration.movingCheckActive ? "moving ": "") +
(NoCheatConfiguration.speedhackCheckActive ? "speedhack " : "") +
(NoCheatConfiguration.airbuildCheckActive ? "airbuild " : "") +
(NoCheatConfiguration.bedteleportCheckActive ? "bedteleport " : "");
return (movingCheck.isActive() ? movingCheck.getName() + " " : "") +
(speedhackCheck.isActive() ? speedhackCheck.getName() + " " : "") +
(airbuildCheck.isActive() ? airbuildCheck.getName() + " " : "") +
(bedteleportCheck.isActive() ? bedteleportCheck.getName() + " " : "");
}
private String getPermissionsForPlayerAsString(Player p) {
return (!NoCheatConfiguration.movingCheckActive ? "moving* ": (hasPermission(p, "nocheat.moving") ? "moving " : "") +
(!NoCheatConfiguration.speedhackCheckActive ? "speedhack* " : (hasPermission(p, "nocheat.speedhack") ? "speedhack " : "")) +
(!NoCheatConfiguration.airbuildCheckActive ? "airbuild* " : (hasPermission(p, "nocheat.airbuild") ? "airbuild " : "")) +
(!NoCheatConfiguration.bedteleportCheckActive ? "bedteleport* " : (hasPermission(p, "nocheat.bedteleport") ? "bedteleport " : "")) +
return (!movingCheck.isActive() ? movingCheck.getName() + "* " : (hasPermission(p, "nocheat.moving") ? movingCheck.getName() + " " : "") +
(!speedhackCheck.isActive() ? speedhackCheck.getName() + "* " : (hasPermission(p, "nocheat.speedhack") ? speedhackCheck.getName() + " " : "")) +
(!airbuildCheck.isActive() ? airbuildCheck.getName() + "* " : (hasPermission(p, "nocheat.airbuild") ? airbuildCheck.getName() + " " : "")) +
(!bedteleportCheck.isActive() ? bedteleportCheck.getName() + "* " : (hasPermission(p, "nocheat.bedteleport") ? bedteleportCheck.getName() + " " : "")) +
(hasPermission(p, "nocheat.notify") ? "notify " : ""));
}
}

View File

@ -5,7 +5,6 @@ import org.bukkit.Material;
import org.bukkit.entity.Player;
import org.bukkit.event.block.BlockPlaceEvent;
import cc.co.evenprime.bukkit.nocheat.NoCheatConfiguration;
import cc.co.evenprime.bukkit.nocheat.NoCheatData;
import cc.co.evenprime.bukkit.nocheat.NoCheatPlugin;
@ -16,18 +15,31 @@ import cc.co.evenprime.bukkit.nocheat.NoCheatPlugin;
* @author Evenprime
*
*/
public class AirbuildCheck {
public class AirbuildCheck extends Check {
// How should airbuild violations be treated?
public String actionLow = "loglow deny";
public String actionMed = "logmed deny";
public String actionHigh = "loghigh deny";
public static void check(BlockPlaceEvent event) {
public int limitLow = 1;
public int limitMed = 3;
public int limitHigh = 10;
public AirbuildCheck(NoCheatPlugin plugin) {
super(plugin);
setActive(false);
}
public void check(BlockPlaceEvent event) {
// Should we check at all?
if(NoCheatPlugin.hasPermission(event.getPlayer(), "nocheat.airbuild"))
if(plugin.hasPermission(event.getPlayer(), "nocheat.airbuild"))
return;
// Are all 6 sides "air-blocks" -> cancel the event
if(event.getBlockAgainst().getType() == Material.AIR) {
final NoCheatData data = NoCheatPlugin.getPlayerData(event.getPlayer());
final NoCheatData data = plugin.getPlayerData(event.getPlayer());
final Player p = event.getPlayer();
if(data.airbuildRunnable == null) {
@ -42,30 +54,30 @@ public class AirbuildCheck {
};
// Give a summary in 50 ticks ~ 1 second
NoCheatPlugin.p.getServer().getScheduler().scheduleAsyncDelayedTask(NoCheatPlugin.p, data.airbuildRunnable, 50);
plugin.getServer().getScheduler().scheduleAsyncDelayedTask(plugin, data.airbuildRunnable, 50);
}
data.airbuildPerSecond++;
boolean log = false;
// Only explicitly log certain "milestones"
if(data.airbuildPerSecond >= NoCheatConfiguration.airbuildLimitHigh) {
if(data.airbuildPerSecond == NoCheatConfiguration.airbuildLimitHigh) {
if(data.airbuildPerSecond >= limitHigh) {
if(data.airbuildPerSecond == limitHigh) {
log = true;
}
action(NoCheatConfiguration.airbuildActionHigh, event, log);
action(actionHigh, event, log);
}
else if(data.airbuildPerSecond >= NoCheatConfiguration.airbuildLimitMed) {
if(data.airbuildPerSecond == NoCheatConfiguration.airbuildLimitMed) {
else if(data.airbuildPerSecond >= limitMed) {
if(data.airbuildPerSecond == limitMed) {
log = true;
}
action(NoCheatConfiguration.airbuildActionMed, event, log);
action(actionMed, event, log);
}
else if(data.airbuildPerSecond >= NoCheatConfiguration.airbuildLimitLow) {
if(data.airbuildPerSecond == NoCheatConfiguration.airbuildLimitLow) {
else if(data.airbuildPerSecond >= limitLow) {
if(data.airbuildPerSecond == limitLow) {
log = true;
}
action(NoCheatConfiguration.airbuildActionLow, event, log);
action(actionLow, event, log);
}
else
{
@ -74,12 +86,12 @@ public class AirbuildCheck {
}
}
private static void action(String action, BlockPlaceEvent event, boolean log) {
private void action(String action, BlockPlaceEvent event, boolean log) {
// LOG IF NEEDED
if(log && action.contains("log")) {
Location l = event.getBlockPlaced().getLocation();
NoCheatPlugin.logAction(action, "Airbuild violation: "+event.getPlayer().getName()+" tried to place block " + event.getBlockPlaced().getType() + " in the air at " + l.getBlockX() + "," + l.getBlockY() +"," + l.getBlockZ());
plugin.logAction(action, "Airbuild violation: "+event.getPlayer().getName()+" tried to place block " + event.getBlockPlaced().getType() + " in the air at " + l.getBlockX() + "," + l.getBlockY() +"," + l.getBlockZ());
}
// DENY IF NEEDED
@ -88,21 +100,26 @@ public class AirbuildCheck {
}
}
private static void summary(Player player, NoCheatData data) {
private void summary(Player player, NoCheatData data) {
String logLine = "Airbuild violation summary: " +player.getName() + " total events per second: " + data.airbuildPerSecond;
// Give a summary according to the highest violation level we encountered in that second
if(data.airbuildPerSecond >= NoCheatConfiguration.airbuildLimitHigh) {
NoCheatPlugin.logAction(NoCheatConfiguration.airbuildActionHigh, logLine);
if(data.airbuildPerSecond >= limitHigh) {
plugin.logAction(actionHigh, logLine);
}
else if(data.airbuildPerSecond >= NoCheatConfiguration.airbuildLimitMed) {
NoCheatPlugin.logAction(NoCheatConfiguration.airbuildActionMed, logLine);
else if(data.airbuildPerSecond >= limitMed) {
plugin.logAction(actionMed, logLine);
}
else if(data.airbuildPerSecond >= NoCheatConfiguration.airbuildLimitLow) {
NoCheatPlugin.logAction(NoCheatConfiguration.airbuildActionLow, logLine);
else if(data.airbuildPerSecond >= limitLow) {
plugin.logAction(actionLow, logLine);
}
data.airbuildPerSecond = 0;
}
@Override
public String getName() {
return "airbuild";
}
}

View File

@ -5,13 +5,17 @@ import org.bukkit.event.player.PlayerMoveEvent;
import cc.co.evenprime.bukkit.nocheat.NoCheatPlugin;
public class BedteleportCheck {
public class BedteleportCheck extends Check {
public BedteleportCheck(NoCheatPlugin plugin) {
super(plugin);
setActive(true);
}
public static void check(PlayerMoveEvent event) {
public void check(PlayerMoveEvent event) {
// Should we check at all?
if(NoCheatPlugin.hasPermission(event.getPlayer(), "nocheat.bedteleport"))
if(plugin.hasPermission(event.getPlayer(), "nocheat.bedteleport"))
return;
if(event.getFrom().getWorld().getBlockTypeIdAt(event.getFrom()) == Material.BED_BLOCK.getId()) {
@ -21,4 +25,9 @@ public class BedteleportCheck {
event.setCancelled(true);
}
}
@Override
public String getName() {
return "bedteleport";
}
}

View File

@ -0,0 +1,24 @@
package cc.co.evenprime.bukkit.nocheat.checks;
import cc.co.evenprime.bukkit.nocheat.NoCheatPlugin;
public abstract class Check {
public Check(NoCheatPlugin plugin) {
this.plugin = plugin;
}
private boolean active = true;
protected NoCheatPlugin plugin;
public boolean isActive() {
return active;
}
public void setActive(boolean a) {
active = a;
}
public abstract String getName();
}

View File

@ -8,7 +8,6 @@ import org.bukkit.Material;
import org.bukkit.World;
import org.bukkit.event.player.PlayerMoveEvent;
import cc.co.evenprime.bukkit.nocheat.NoCheatConfiguration;
import cc.co.evenprime.bukkit.nocheat.NoCheatData;
import cc.co.evenprime.bukkit.nocheat.NoCheatPlugin;
@ -18,10 +17,15 @@ import cc.co.evenprime.bukkit.nocheat.NoCheatPlugin;
* @author Evenprime
*
*/
public class MovingCheck {
public class MovingCheck extends Check {
public MovingCheck(NoCheatPlugin plugin) {
super(plugin);
setActive(true);
}
// How many move events can a player have in air before he is expected to lose altitude (or land somewhere)
static final int jumpingLimit = 3;
static final int jumpingLimit = 4;
// How high may a player get compared to his last location with ground contact
static final double jumpingHeightLimit = 1.3D;
@ -34,6 +38,11 @@ public class MovingCheck {
public static double movingDistanceMed = 2.0D;
public static double movingDistanceHigh = 5.0D;
// How should moving violations be treated?
public String actionLow = "loglow reset";
public String actionMed = "logmed reset";
public String actionHigh = "loghigh reset";
final static double magic = 0.30000001192092896D;
final static double magic2 = 0.69999998807907103D;
@ -135,14 +144,15 @@ public class MovingCheck {
types[Material.CAKE_BLOCK.getId()]= BlockType.UNKNOWN;
}
public static void check(final PlayerMoveEvent event) {
public void check(final PlayerMoveEvent event) {
// Should we check at all
if(NoCheatPlugin.hasPermission(event.getPlayer(), "nocheat.moving"))
if(plugin.hasPermission(event.getPlayer(), "nocheat.moving"))
return;
// Get the player-specific data
NoCheatData data = NoCheatPlugin.getPlayerData(event.getPlayer());
NoCheatData data = plugin.getPlayerData(event.getPlayer());
// Notice to myself: How world changes with e.g. command /world work:
// 1. TeleportEvent from the players current position to another position in the _same_ world
@ -186,7 +196,7 @@ public class MovingCheck {
}
// The server believes the player should be moving up, so we ignore this event
if(event.getPlayer().getVelocity().getY() > 0) {
if(event.getPlayer().getVelocity().getY() >= 0) {
data.movingSetBackPoint = null;
data.speedhackSetBackPoint = null;
return;
@ -345,20 +355,9 @@ public class MovingCheck {
// Find out with what actions to treat the violation(s)
if(Level.INFO.equals(vl)) {
if(data.movingMinorViolationsInARow > 0) log = false;
data.movingMinorViolationsInARow++;
actions = NoCheatConfiguration.movingActionMinor;
// React only after the freebee illegal moves have all been used
if(data.movingMinorViolationsInARow <= NoCheatConfiguration.movingFreeMoves) {
vl = null;
actions = null;
}
if(data.movingMinorViolationsInARow > NoCheatConfiguration.movingFreeMoves+1) {
log = false;
}
actions = actionLow;
// after a set number of minor violations a normal violation gets thrown
if(data.movingMinorViolationsInARow % 40 == 0) {
@ -370,31 +369,46 @@ public class MovingCheck {
if(Level.WARNING.equals(vl)) {
if(data.movingNormalViolationsInARow > 0) log = false;
data.movingNormalViolationsInARow++;
actions = NoCheatConfiguration.movingActionNormal;
actions = actionMed;
}
if(Level.SEVERE.equals(vl)) {
if(data.movingHeavyViolationsInARow > 0) log = false;
data.movingHeavyViolationsInARow++;
actions = NoCheatConfiguration.movingActionHeavy;
actions = actionHigh;
}
action(event, actions, log);
}
}
public void teleported(PlayerMoveEvent event) {
NoCheatData data = plugin.getPlayerData(event.getPlayer());
if(data.reset) { // My plugin requested this teleport, so we don't do anything
data.reset = false;
}
else {
if(!event.isCancelled()) {
// If it wasn't our plugin that ordered the teleport, forget all our information and start from scratch at the new location
data.speedhackSetBackPoint = event.getTo().clone();
data.movingSetBackPoint = event.getTo().clone();
data.movingJumpPhase = 0;
}
}
}
/**
* Perform actions that were specified by the admin
* @param event
* @param actions
*/
private static void action(PlayerMoveEvent event, String actions, boolean log) {
private void action(PlayerMoveEvent event, String actions, boolean log) {
if(actions == null) return;
// LOGGING IF NEEDED
if(log && actions.contains("log")) {
NoCheatPlugin.logAction(actions, "Moving violation: "+event.getPlayer().getName()+" from " + String.format("%s (%.5f, %.5f, %.5f) to %s (%.5f, %.5f, %.5f)", event.getFrom().getWorld().getName(), event.getFrom().getX(), event.getFrom().getY(), event.getFrom().getZ(), event.getTo().getWorld().getName(), event.getTo().getX(), event.getTo().getY(), event.getTo().getZ()));
plugin.logAction(actions, "Moving violation: "+event.getPlayer().getName()+" from " + String.format("%s (%.5f, %.5f, %.5f) to %s (%.5f, %.5f, %.5f)", event.getFrom().getWorld().getName(), event.getFrom().getX(), event.getFrom().getY(), event.getFrom().getZ(), event.getTo().getWorld().getName(), event.getTo().getX(), event.getTo().getY(), event.getTo().getZ()));
}
// RESET IF NEEDED
@ -405,7 +419,7 @@ public class MovingCheck {
protected static void legitimateMove(NoCheatData data, PlayerMoveEvent event) {
protected void legitimateMove(NoCheatData data, PlayerMoveEvent event) {
// Give some logging about violations if the player hasn't done any for at least two seconds
if(data.movingLastViolationTime != 0 && data.movingLastViolationTime + 2000L < System.currentTimeMillis()) {
@ -414,15 +428,15 @@ public class MovingCheck {
// Give some additional logs about now ending violations
if(data.movingHeavyViolationsInARow > 0) {
NoCheatPlugin.logAction(NoCheatConfiguration.movingActionHeavy, "Moving violation ended: "+event.getPlayer().getName() + " total Events: "+ data.movingHeavyViolationsInARow);
plugin.logAction(actionHigh, "Moving violation ended: "+event.getPlayer().getName() + " total Events: "+ data.movingHeavyViolationsInARow);
data.movingHeavyViolationsInARow = 0;
}
if(data.movingNormalViolationsInARow > 0) {
NoCheatPlugin.logAction(NoCheatConfiguration.movingActionNormal, "Moving violation ended: "+event.getPlayer().getName()+ " total Events: "+ data.movingNormalViolationsInARow);
plugin.logAction(actionMed, "Moving violation ended: "+event.getPlayer().getName()+ " total Events: "+ data.movingNormalViolationsInARow);
data.movingNormalViolationsInARow = 0;
}
if(data.movingMinorViolationsInARow > NoCheatConfiguration.movingFreeMoves) {
NoCheatPlugin.logAction(NoCheatConfiguration.movingActionMinor, "Moving violation ended: "+event.getPlayer().getName()+ " total Events: "+ data.movingMinorViolationsInARow);
if(data.movingMinorViolationsInARow > 0) {
plugin.logAction(actionLow, "Moving violation ended: "+event.getPlayer().getName()+ " total Events: "+ data.movingMinorViolationsInARow);
data.movingMinorViolationsInARow = 0;
}
@ -442,9 +456,9 @@ public class MovingCheck {
* @param data
* @param event
*/
private static void resetPlayer(PlayerMoveEvent event) {
private void resetPlayer(PlayerMoveEvent event) {
NoCheatData data = NoCheatPlugin.getPlayerData(event.getPlayer());
NoCheatData data = plugin.getPlayerData(event.getPlayer());
// Reset the jumpphase. We choose the setback-point such that it should be
// on solid ground, but in case it isn't (maybe the ground is gone now) we
@ -533,14 +547,7 @@ public class MovingCheck {
return false;
}
public static int floor_double(double d)
{
int i = (int)d;
return d > (double)i ? i : i - 1;
}
public static int lowerBorder(double d1) {
private static int lowerBorder(double d1) {
double floor = Math.floor(d1);
double d4 = floor + magic;
@ -552,7 +559,7 @@ public class MovingCheck {
return (int) (floor - d4);
}
public static int upperBorder(double d1) {
private static int upperBorder(double d1) {
double floor = Math.floor(d1);
double d4 = floor + magic2;
@ -563,4 +570,9 @@ public class MovingCheck {
return (int) (floor - d4);
}
@Override
public String getName() {
return "moving";
}
}

View File

@ -3,7 +3,6 @@ package cc.co.evenprime.bukkit.nocheat.checks;
import org.bukkit.Location;
import org.bukkit.event.player.PlayerMoveEvent;
import cc.co.evenprime.bukkit.nocheat.NoCheatConfiguration;
import cc.co.evenprime.bukkit.nocheat.NoCheatData;
import cc.co.evenprime.bukkit.nocheat.NoCheatPlugin;
@ -13,20 +12,34 @@ import cc.co.evenprime.bukkit.nocheat.NoCheatPlugin;
* @author Evenprime
*
*/
public class SpeedhackCheck {
public class SpeedhackCheck extends Check {
public SpeedhackCheck(NoCheatPlugin plugin) {
super(plugin);
setActive(true);
}
private static final long interval = 1000;
private static final int violationsLimit = 3;
public static void check(PlayerMoveEvent event) {
// Limits for the speedhack check
public int limitLow = 30;
public int limitMed = 45;
public int limitHigh = 60;
// How should speedhack violations be treated?
public String actionLow = "loglow reset";
public String actionMed = "logmed reset";
public String actionHigh = "loghigh reset";
public void check(PlayerMoveEvent event) {
// Should we check at all?
if(NoCheatPlugin.hasPermission(event.getPlayer(), "nocheat.speedhack"))
if(plugin.hasPermission(event.getPlayer(), "nocheat.speedhack"))
return;
// Get the player-specific data
NoCheatData data = NoCheatPlugin.getPlayerData(event.getPlayer());
NoCheatData data = plugin.getPlayerData(event.getPlayer());
// Get the time of the server
long time = System.currentTimeMillis();
@ -37,14 +50,14 @@ public class SpeedhackCheck {
// TODO: Needs some better handling for server lag
String action = null;
int limitLow = (int)((NoCheatConfiguration.speedhackLimitLow * (time - data.speedhackLastCheck)) / interval);
int limitMed = (int)((NoCheatConfiguration.speedhackLimitMed * (time - data.speedhackLastCheck)) / interval);
int limitHigh = (int)((NoCheatConfiguration.speedhackLimitHigh * (time - data.speedhackLastCheck)) / interval);
int low = (int)((limitLow * (time - data.speedhackLastCheck)) / interval);
int med = (int)((limitMed * (time - data.speedhackLastCheck)) / interval);
int high = (int)((limitHigh * (time - data.speedhackLastCheck)) / interval);
if(data.speedhackEventsSinceLastCheck > limitHigh) action = NoCheatConfiguration.speedhackActionHeavy;
else if(data.speedhackEventsSinceLastCheck > limitMed) action = NoCheatConfiguration.speedhackActionNormal;
else if(data.speedhackEventsSinceLastCheck > limitLow) action = NoCheatConfiguration.speedhackActionMinor;
if(data.speedhackEventsSinceLastCheck > high) action = actionLow;
else if(data.speedhackEventsSinceLastCheck > med) action = actionMed;
else if(data.speedhackEventsSinceLastCheck > low) action = actionHigh;
if(action == null) {
data.speedhackSetBackPoint = event.getFrom().clone();
@ -71,12 +84,12 @@ public class SpeedhackCheck {
data.speedhackEventsSinceLastCheck++;
}
private static void action(String actions, PlayerMoveEvent event, NoCheatData data) {
private void action(String actions, PlayerMoveEvent event, NoCheatData data) {
if(actions == null) return;
// LOGGING IF NEEDED
if(actions.contains("log")) {
NoCheatPlugin.logAction(actions, event.getPlayer().getName()+" sent "+ data.speedhackEventsSinceLastCheck + " move events, but only "+NoCheatConfiguration.speedhackLimitLow+ " were allowed. Speedhack?");
plugin.logAction(actions, event.getPlayer().getName()+" sent "+ data.speedhackEventsSinceLastCheck + " move events, but only "+limitLow+ " were allowed. Speedhack?");
}
// RESET IF NEEDED
if(actions.contains("reset")) {
@ -103,4 +116,9 @@ public class SpeedhackCheck {
event.getPlayer().teleportTo(event.getFrom());
}
}
@Override
public String getName() {
return "speedhack";
}
}

View File

@ -3,7 +3,6 @@ package cc.co.evenprime.bukkit.nocheat.listeners;
import org.bukkit.event.block.BlockListener;
import org.bukkit.event.block.BlockPlaceEvent;
import cc.co.evenprime.bukkit.nocheat.NoCheatConfiguration;
import cc.co.evenprime.bukkit.nocheat.checks.AirbuildCheck;
/**
@ -12,17 +11,17 @@ import cc.co.evenprime.bukkit.nocheat.checks.AirbuildCheck;
* @author Evenprime
*
*/
public class NoCheatBlockListener extends BlockListener {
public NoCheatBlockListener() {
public class AirbuildListener extends BlockListener {
private AirbuildCheck check;
public AirbuildListener(AirbuildCheck check) {
this.check = check;
}
@Override
public void onBlockPlace(BlockPlaceEvent event) {
if(!event.isCancelled() && NoCheatConfiguration.airbuildCheckActive)
AirbuildCheck.check(event);
if(!event.isCancelled() && check.isActive())
check.check(event);
}
}

View File

@ -0,0 +1,23 @@
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.BedteleportCheck;
public class BedteleportListener extends PlayerListener {
private BedteleportCheck check;
public BedteleportListener(BedteleportCheck check) {
this.check = check;
}
@Override
public void onPlayerTeleport(PlayerMoveEvent event) {
if(!event.isCancelled() && check.isActive()) {
check.check(event);
}
}
}

View File

@ -0,0 +1,28 @@
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.MovingCheck;
/**
* Handle events for all Player related events
*
* @author Evenprime
*/
public class MovingListener extends PlayerListener {
private MovingCheck check;
public MovingListener(MovingCheck check) {
this.check = check;
}
@Override
public void onPlayerMove(PlayerMoveEvent event) {
if(!event.isCancelled() && check.isActive())
check.check(event);
}
}

View File

@ -0,0 +1,21 @@
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.MovingCheck;
public class MovingMonitor extends PlayerListener {
private MovingCheck check;
public MovingMonitor(MovingCheck check) {
this.check = check;
}
@Override
public void onPlayerTeleport(PlayerMoveEvent event) {
check.teleported(event);
}
}

View File

@ -1,49 +0,0 @@
package cc.co.evenprime.bukkit.nocheat.listeners;
import org.bukkit.event.player.PlayerEvent;
import org.bukkit.event.player.PlayerListener;
import org.bukkit.event.player.PlayerMoveEvent;
import cc.co.evenprime.bukkit.nocheat.NoCheatConfiguration;
import cc.co.evenprime.bukkit.nocheat.NoCheatPlugin;
import cc.co.evenprime.bukkit.nocheat.checks.BedteleportCheck;
import cc.co.evenprime.bukkit.nocheat.checks.MovingCheck;
import cc.co.evenprime.bukkit.nocheat.checks.SpeedhackCheck;
/**
* Handle events for all Player related events
*
* @author Evenprime
*/
public class NoCheatPlayerListener extends PlayerListener {
public NoCheatPlayerListener() { }
@Override
public void onPlayerQuit(PlayerEvent event) {
NoCheatPlugin.playerData.remove(event.getPlayer());
}
@Override
public void onPlayerMove(PlayerMoveEvent event) {
if(!event.isCancelled() && NoCheatConfiguration.speedhackCheckActive)
SpeedhackCheck.check(event);
if(!event.isCancelled() && NoCheatConfiguration.movingCheckActive)
MovingCheck.check(event);
}
@Override
public void onPlayerTeleport(PlayerMoveEvent event) {
if(!event.isCancelled() && NoCheatConfiguration.bedteleportCheckActive) {
BedteleportCheck.check(event);
}
}
}

View File

@ -1,28 +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.NoCheatData;
import cc.co.evenprime.bukkit.nocheat.NoCheatPlugin;
public class NoCheatPlayerListenerMonitor extends PlayerListener {
@Override
public void onPlayerTeleport(PlayerMoveEvent event) {
NoCheatData data = NoCheatPlugin.getPlayerData(event.getPlayer());
if(data.reset) { // My plugin requested this teleport, so we don't do anything
data.reset = false;
}
else {
if(!event.isCancelled()) {
// If it wasn't our plugin that ordered the teleport, forget all our information and start from scratch at the new location
data.speedhackSetBackPoint = event.getTo().clone();
data.movingSetBackPoint = event.getTo().clone();
data.movingJumpPhase = 0;
}
}
}
}

View File

@ -0,0 +1,22 @@
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;
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);
}
}