Another round of code cleaning, simplification

Fixed nocheat reload command to give feedback and clear critical data
Remove stored data when a player disconnects
This commit is contained in:
Evenprime 2011-10-10 19:27:45 +02:00
parent 4309c70a39
commit 747e5bcdce
37 changed files with 330 additions and 296 deletions

View File

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

View File

@ -21,6 +21,7 @@ import cc.co.evenprime.bukkit.nocheat.events.BlockPlaceEventManager;
import cc.co.evenprime.bukkit.nocheat.events.BlockBreakEventManager;
import cc.co.evenprime.bukkit.nocheat.events.EventManager;
import cc.co.evenprime.bukkit.nocheat.events.PlayerChatEventManager;
import cc.co.evenprime.bukkit.nocheat.events.PlayerQuitEventManager;
import cc.co.evenprime.bukkit.nocheat.events.PlayerMoveEventManager;
import cc.co.evenprime.bukkit.nocheat.events.PlayerTeleportEventManager;
import cc.co.evenprime.bukkit.nocheat.log.LogLevel;
@ -37,17 +38,17 @@ import cc.co.evenprime.bukkit.nocheat.log.LogManager;
*/
public class NoCheat extends JavaPlugin {
private ConfigurationManager conf;
private LogManager log;
private DataManager data;
private ConfigurationManager conf;
private LogManager log;
private DataManager data;
private List<EventManager> eventManagers = new LinkedList<EventManager>();
private final List<EventManager> eventManagers = new LinkedList<EventManager>();
private int taskId = -1;
private int ingameseconds = 0;
private long lastIngamesecondTime = 0L;
private long lastIngamesecondDuration = 0L;
private boolean skipCheck = false;
private int taskId = -1;
private int ingameseconds = 0;
private long lastIngamesecondTime = 0L;
private long lastIngamesecondDuration = 0L;
private boolean skipCheck = false;
public NoCheat() {
@ -75,7 +76,7 @@ public class NoCheat extends JavaPlugin {
log.logToConsole(LogLevel.LOW, "[NoCheat] This version is for CB #1240. It may break at any time and for any other version.");
this.data = new DataManager();
// parse the nocheat.yml config file
this.conf = new ConfigurationManager(this.getDataFolder().getPath());
@ -84,13 +85,13 @@ public class NoCheat extends JavaPlugin {
eventManagers.add(new PlayerChatEventManager(this));
eventManagers.add(new BlockBreakEventManager(this));
eventManagers.add(new BlockPlaceEventManager(this));
eventManagers.add(new PlayerQuitEventManager(this));
PluginDescriptionFile pdfFile = this.getDescription();
if(taskId == -1) {
taskId = this.getServer().getScheduler().scheduleSyncRepeatingTask(this, new Runnable() {
@Override
public void run() {
// If the previous second took to long, skip checks during
@ -224,10 +225,9 @@ public class NoCheat extends JavaPlugin {
sender.sendMessage("[NoCheat] Reloading configuration");
this.conf.cleanup();
DataManager newData = new DataManager();
this.conf = new ConfigurationManager(this.getDataFolder().getPath());
this.data = newData;
this.data.resetAllCriticalData();
sender.sendMessage("[NoCheat] Configuration loaded");
return true;

View File

@ -22,16 +22,11 @@ public class ActionManager {
this.actions.put(action.name.toLowerCase(), action);
}
public Action getAction(String actionName) {
return this.actions.get(actionName.toLowerCase());
}
public Action[] getActions(String[] actionNames) {
Action[] result = new Action[actionNames.length];
for(int i = 0; i < actionNames.length; i++) {
result[i] = getAction(actionNames[i]);
result[i] = this.actions.get(actionNames[i].toLowerCase());
}
return result;

View File

@ -96,10 +96,10 @@ public class ConsoleCommandSender implements CommandSender {
server.dispatchCommand(this, command);
} catch(Exception e) {
// TODO: Better error handling
System.out.println("[NoCheat] failed to execute the command '"+command + "', please check if everything is setup correct.");
}
}
@Override
public String getName() {
return "NoCheat";
}

View File

@ -20,14 +20,14 @@ public class ActionHistory {
private int totalEntries = 0;
private long lastClearedTime = 0;
public ExecutionHistoryEntry(int monitoredTimeFrame) {
private ExecutionHistoryEntry(int monitoredTimeFrame) {
this.executionTimes = new int[monitoredTimeFrame];
}
/**
* Remember an execution at the specific time
*/
public void addCounter(long time) {
private void addCounter(long time) {
// clear out now outdated values from the array
if(time - lastClearedTime > 0) {
// Clear the next few fields of the array

View File

@ -10,39 +10,39 @@ import cc.co.evenprime.bukkit.nocheat.data.LogData;
public abstract class ActionWithParameters extends Action {
protected enum WildCard {
PLAYER("player"), LOCATION("location"), WORLD("world"), VIOLATIONS("violations"), MOVEDISTANCE("movedistance"), REACHDISTANCE("reachdistance"), FALLDISTANCE("falldistance"), LOCATION_TO("locationto"), CHECK("check"), PACKETS("packets"), TEXT("text"), PLACE_LOCATION("placelocation"), PLACE_AGAINST("placeagainst"), BLOCK_TYPE("blocktype");
private final String s;
private WildCard(String s) {
this.s = s;
}
private static final WildCard get(String s) {
for(WildCard c : WildCard.values()) {
if(c.s.equals(s)) {
return c;
}
}
return null;
}
private enum WildCard {
PLAYER("player"), LOCATION("location"), WORLD("world"), VIOLATIONS("violations"), MOVEDISTANCE("movedistance"), REACHDISTANCE("reachdistance"), FALLDISTANCE("falldistance"), LOCATION_TO("locationto"), CHECK("check"), PACKETS("packets"), TEXT("text"), PLACE_LOCATION("placelocation"), PLACE_AGAINST("placeagainst"), BLOCK_TYPE("blocktype");
private final String s;
private WildCard(String s) {
this.s = s;
}
protected final ArrayList<Object> messageParts;
private static final WildCard get(String s) {
for(WildCard c : WildCard.values()) {
if(c.s.equals(s)) {
return c;
}
}
return null;
}
}
private final ArrayList<Object> messageParts;
public ActionWithParameters(String name, int delay, int repeat, String message) {
super(name, delay, repeat);
messageParts = new ArrayList<Object>();
parseMessage(message);
}
protected void parseMessage(String message) {
private void parseMessage(String message) {
String parts[] = message.split("\\[", 2);
// No opening braces left
if(parts.length != 2) {
messageParts.add(message);
@ -50,7 +50,7 @@ public abstract class ActionWithParameters extends Action {
// Found an opening brace
else {
String parts2[] = parts[1].split("\\]", 2);
// Found no matching closing brace
if(parts2.length != 2) {
messageParts.add(message);
@ -58,12 +58,12 @@ public abstract class ActionWithParameters extends Action {
// Found a matching closing brace
else {
WildCard w = WildCard.get(parts2[0]);
if(w != null) {
// Found an existing wildcard inbetween the braces
messageParts.add(parts[0]);
messageParts.add(w);
// Go further down recursive
parseMessage(parts2[1]);
} else {
@ -82,7 +82,7 @@ public abstract class ActionWithParameters extends Action {
public String getMessage(LogData data) {
StringBuilder log = new StringBuilder(100); // Should be big enough most
// of the time
for(Object part : messageParts) {
if(part instanceof String) {
log.append((String) part);
@ -90,7 +90,7 @@ public abstract class ActionWithParameters extends Action {
log.append(getParameter((WildCard) part, data));
}
}
return log.toString();
}
@ -100,20 +100,20 @@ public abstract class ActionWithParameters extends Action {
switch (wildcard) {
case PLAYER:
return data.player.getName();
case CHECK:
return data.check;
case LOCATION:
Location l = data.player.getLocation();
return String.format(Locale.US, "%.2f,%.2f,%.2f", l.getX(), l.getY(), l.getZ());
case WORLD:
return data.player.getWorld().getName();
case VIOLATIONS:
return String.format(Locale.US, "%.2f", data.violationLevel);
return String.format(Locale.US, "%d", data.violationLevel);
case MOVEDISTANCE:
Location l2 = data.player.getLocation();
Location t = data.toLocation;
@ -124,20 +124,20 @@ public abstract class ActionWithParameters extends Action {
}
case REACHDISTANCE:
return String.format(Locale.US, "%.2f", data.reachdistance);
case FALLDISTANCE:
return String.format(Locale.US, "%.2f", data.falldistance);
case LOCATION_TO:
Location to = data.toLocation;
return String.format(Locale.US, "%.2f,%.2f,%.2f", to.getX(), to.getY(), to.getZ());
case PACKETS:
return String.valueOf(data.packets);
case TEXT:
return data.text;
case PLACE_LOCATION:
Block block = data.placed;
if(block != null) {
@ -145,17 +145,21 @@ public abstract class ActionWithParameters extends Action {
} else {
return "null";
}
case PLACE_AGAINST:
Block blocka = data.placedAgainst;
if(blocka == null) {
return "null";
}
return String.format(Locale.US, "%d %d %d", blocka.getX(), blocka.getY(), blocka.getZ());
case BLOCK_TYPE:
return data.placedMaterial.toString();
Block blockb = data.placed;
if(blockb == null) {
return "null";
}
return blockb.getType().toString();
default:
return "Evenprime was lazy and forgot to define " + wildcard + ".";
}

View File

@ -50,7 +50,7 @@ public class DirectionCheck {
data.directionViolationLevel += 1;
// Prepare some event-specific values for logging and custom actions
LogData ldata = plugin.getDataManager().getLogData(player);
LogData ldata = plugin.getDataManager().getData( player).log;
ldata.check = "blockbreak.direction";
cancel = action.executeActions(player, cc.blockbreak.directionActions, (int) data.directionViolationLevel, ldata, cc);

View File

@ -37,7 +37,7 @@ public class ReachCheck {
// Increment violation counter
data.reachViolationLevel += 1;
LogData ldata = plugin.getDataManager().getLogData(player);
LogData ldata = plugin.getDataManager().getData(player).log;
ldata.check = "blockbreak.reach";
ldata.reachdistance = distance;

View File

@ -38,7 +38,7 @@ public class OnLiquidCheck {
// all ok
} else {
data.onliquidViolationLevel += 1;
LogData ldata = plugin.getDataManager().getLogData(player);
LogData ldata = plugin.getDataManager().getData(player).log;
ldata.check = "blockplace.onliquid";
ldata.placed = blockPlaced;
ldata.placedAgainst = blockPlacedAgainst;
@ -51,7 +51,7 @@ public class OnLiquidCheck {
return cancel;
}
public boolean isSolid(Block block) {
private boolean isSolid(Block block) {
Material m = block.getType();
return !(m == Material.AIR) || (m == Material.WATER) || (m == Material.STATIONARY_WATER) || (m == Material.LAVA) || (m == Material.STATIONARY_LAVA);
}

View File

@ -47,7 +47,7 @@ public class ReachCheck {
data.reachViolationLevel += 1;
// Prepare some event-specific values for logging and custom actions
LogData ldata = plugin.getDataManager().getLogData(player);
LogData ldata = plugin.getDataManager().getData(player).log;
ldata.check = "blockplace.reach";
ldata.reachdistance = distance;

View File

@ -46,7 +46,7 @@ public class ChatCheck {
// Prepare some event-specific values for logging and custom
// actions
LogData ldata = plugin.getDataManager().getLogData(player);
LogData ldata = plugin.getDataManager().getData(player).log;
ldata.check = "chat.spam";
ldata.text = message;

View File

@ -83,7 +83,7 @@ public class FlyingCheck {
data.runflyViolationLevel += result;
// Prepare some event-specific values for logging and custom actions
LogData ldata = plugin.getDataManager().getLogData(player);
LogData ldata = plugin.getDataManager().getData(player).log;
ldata.toLocation = to;
ldata.check = "flying/toofast";

View File

@ -25,8 +25,8 @@ public class MorePacketsCheck {
private final ActionExecutor action;
private final int packetsPerTimeframe = 22;
private final int bufferLimit = 30;
private final static int packetsPerTimeframe = 22;
private final static int bufferLimit = 30;
private final NoCheat plugin;
public MorePacketsCheck(NoCheat plugin) {
@ -122,7 +122,7 @@ public class MorePacketsCheck {
if(!plugin.skipCheck() && packetsAboveLimit > 0) {
data.morePacketsViolationLevel += packetsAboveLimit;
LogData ldata = plugin.getDataManager().getLogData(player);
LogData ldata = plugin.getDataManager().getData(player).log;
// Packets above limit
ldata.packets = data.morePacketsCounter - limit;
ldata.check = "moving/morepackets";

View File

@ -15,8 +15,8 @@ import org.bukkit.World;
*/
public class MovingEventHelper {
private final double magic = 0.45D;
private final double magic2 = 0.55D;
private final static double magic = 0.45D;
private final static double magic2 = 0.55D;
// Block types that may need to be treated specially
private static final int NONSOLID = 1; // 0x00000001
@ -159,7 +159,7 @@ public class MovingEventHelper {
return (value & LIQUID) == LIQUID;
}
final boolean isNonSolid(int value) {
private final boolean isNonSolid(int value) {
return((value & NONSOLID) == NONSOLID);
}

View File

@ -56,7 +56,7 @@ public class NoFallCheck {
data.nofallViolationLevel += difference;
// Prepare some event-specific values for logging and custom actions
LogData ldata = plugin.getDataManager().getLogData(player);
LogData ldata = plugin.getDataManager().getData(player).log;
ldata.falldistance = data.fallDistance;
ldata.check = "moving/nofall";

View File

@ -97,8 +97,6 @@ public class RunFlyCheck {
* This is a workaround for people placing blocks below them causing false positives
* with the move check(s).
*
* TODO: Check if still needed, maybe this got fixed in 1.8.1
*
* @param player
* @param data
* @param blockPlaced

View File

@ -77,7 +77,7 @@ public class RunningCheck {
data.runflyViolationLevel += result;
// Prepare some event-specific values for logging and custom actions
LogData ldata = plugin.getDataManager().getLogData(player);
LogData ldata = plugin.getDataManager().getData(player).log;
ldata.toLocation = to;
if(resultHoriz > 0 && resultVert > 0)
ldata.check = "runfly/both";

View File

@ -21,21 +21,21 @@ public abstract class Configuration {
public final static OptionNode ROOT = new OptionNode(null, null, DataType.PARENT);
public final static OptionNode LOGGING = new OptionNode("logging", ROOT, DataType.PARENT);
private final static OptionNode LOGGING = new OptionNode("logging", ROOT, DataType.PARENT);
public final static OptionNode LOGGING_ACTIVE = new OptionNode("active", LOGGING, DataType.BOOLEAN);
public final static OptionNode LOGGING_FILENAME = new OptionNode("filename", LOGGING, DataType.STRING);
public final static OptionNode LOGGING_FILELEVEL = new OptionNode("filelevel", LOGGING, DataType.LOGLEVEL);
public final static OptionNode LOGGING_CONSOLELEVEL = new OptionNode("consolelevel", LOGGING, DataType.LOGLEVEL);
public final static OptionNode LOGGING_CHATLEVEL = new OptionNode("chatlevel", LOGGING, DataType.LOGLEVEL);
public final static OptionNode DEBUG = new OptionNode("debug", ROOT, DataType.PARENT);
private final static OptionNode DEBUG = new OptionNode("debug", ROOT, DataType.PARENT);
public final static OptionNode DEBUG_SHOWACTIVECHECKS = new OptionNode("showactivechecks", DEBUG, DataType.BOOLEAN);
public final static OptionNode MOVING = new OptionNode("moving", ROOT, DataType.PARENT);
private final static OptionNode MOVING = new OptionNode("moving", ROOT, DataType.PARENT);
public final static OptionNode MOVING_CHECK = new OptionNode("check", MOVING, DataType.BOOLEAN);
public final static OptionNode MOVING_IDENTIFYCREATIVEMODE = new OptionNode("identifycreativemode", MOVING, DataType.BOOLEAN);
public final static OptionNode MOVING_RUNFLY = new OptionNode("runfly", MOVING, DataType.PARENT);
private final static OptionNode MOVING_RUNFLY = new OptionNode("runfly", MOVING, DataType.PARENT);
public final static OptionNode MOVING_RUNFLY_CHECK = new OptionNode("check", MOVING_RUNFLY, DataType.BOOLEAN);
public final static OptionNode MOVING_RUNFLY_WALKINGSPEEDLIMIT = new OptionNode("alkingspeedlimit", MOVING_RUNFLY, DataType.INTEGER);
public final static OptionNode MOVING_RUNFLY_SPRINTINGSPEEDLIMIT = new OptionNode("sprintingspeedlimit", MOVING_RUNFLY, DataType.INTEGER);
@ -53,39 +53,39 @@ public abstract class Configuration {
public final static OptionNode MOVING_RUNFLY_FLYINGSPEEDLIMITHORIZONTAL = new OptionNode("flyingspeedlimithorizontal", MOVING_RUNFLY, DataType.INTEGER);
public final static OptionNode MOVING_RUNFLY_FLYINGACTIONS = new OptionNode("flyingactions", MOVING_RUNFLY, DataType.ACTIONLIST);
public final static OptionNode MOVING_MOREPACKETS = new OptionNode("morepackets", MOVING, DataType.PARENT);
private final static OptionNode MOVING_MOREPACKETS = new OptionNode("morepackets", MOVING, DataType.PARENT);
public final static OptionNode MOVING_MOREPACKETS_CHECK = new OptionNode("check", MOVING_MOREPACKETS, DataType.BOOLEAN);
public final static OptionNode MOVING_MOREPACKETS_ACTIONS = new OptionNode("actions", MOVING_MOREPACKETS, DataType.ACTIONLIST);
public final static OptionNode BLOCKBREAK = new OptionNode("blockbreak", ROOT, DataType.PARENT);
private final static OptionNode BLOCKBREAK = new OptionNode("blockbreak", ROOT, DataType.PARENT);
public final static OptionNode BLOCKBREAK_CHECK = new OptionNode("check", BLOCKBREAK, DataType.BOOLEAN);
public final static OptionNode BLOCKBREAK_REACH = new OptionNode("reach", BLOCKBREAK, DataType.PARENT);
private final static OptionNode BLOCKBREAK_REACH = new OptionNode("reach", BLOCKBREAK, DataType.PARENT);
public final static OptionNode BLOCKBREAK_REACH_CHECK = new OptionNode("check", BLOCKBREAK_REACH, DataType.BOOLEAN);
public final static OptionNode BLOCKBREAK_REACH_LIMIT = new OptionNode("limit", BLOCKBREAK_REACH, DataType.INTEGER);
public final static OptionNode BLOCKBREAK_REACH_ACTIONS = new OptionNode("actions", BLOCKBREAK_REACH, DataType.ACTIONLIST);
public final static OptionNode BLOCKBREAK_DIRECTION = new OptionNode("direction", BLOCKBREAK, DataType.PARENT);
private final static OptionNode BLOCKBREAK_DIRECTION = new OptionNode("direction", BLOCKBREAK, DataType.PARENT);
public final static OptionNode BLOCKBREAK_DIRECTION_CHECK = new OptionNode("check", BLOCKBREAK_DIRECTION, DataType.BOOLEAN);
public static final OptionNode BLOCKBREAK_DIRECTION_CHECKINSTABREAKBLOCKS = new OptionNode("checkinstabreakblocks", BLOCKBREAK_DIRECTION, DataType.BOOLEAN);
public final static OptionNode BLOCKBREAK_DIRECTION_ACTIONS = new OptionNode("actions", BLOCKBREAK_DIRECTION, DataType.ACTIONLIST);
public final static OptionNode BLOCKPLACE = new OptionNode("blockplace", ROOT, DataType.PARENT);
private final static OptionNode BLOCKPLACE = new OptionNode("blockplace", ROOT, DataType.PARENT);
public final static OptionNode BLOCKPLACE_CHECK = new OptionNode("check", BLOCKPLACE, DataType.BOOLEAN);
public final static OptionNode BLOCKPLACE_REACH = new OptionNode("reach", BLOCKPLACE, DataType.PARENT);
private final static OptionNode BLOCKPLACE_REACH = new OptionNode("reach", BLOCKPLACE, DataType.PARENT);
public final static OptionNode BLOCKPLACE_REACH_CHECK = new OptionNode("check", BLOCKPLACE_REACH, DataType.BOOLEAN);
public final static OptionNode BLOCKPLACE_REACH_LIMIT = new OptionNode("limit", BLOCKPLACE_REACH, DataType.INTEGER);
public final static OptionNode BLOCKPLACE_REACH_ACTIONS = new OptionNode("actions", BLOCKPLACE_REACH, DataType.ACTIONLIST);
public final static OptionNode BLOCKPLACE_ONLIQUID = new OptionNode("onliquid", BLOCKPLACE, DataType.PARENT);
private final static OptionNode BLOCKPLACE_ONLIQUID = new OptionNode("onliquid", BLOCKPLACE, DataType.PARENT);
public final static OptionNode BLOCKPLACE_ONLIQUID_CHECK = new OptionNode("check", BLOCKPLACE_ONLIQUID, DataType.BOOLEAN);
public final static OptionNode BLOCKPLACE_ONLIQUID_ACTIONS = new OptionNode("actions", BLOCKPLACE_ONLIQUID, DataType.ACTIONLIST);
public final static OptionNode CHAT = new OptionNode("chat", ROOT, DataType.PARENT);
private final static OptionNode CHAT = new OptionNode("chat", ROOT, DataType.PARENT);
public final static OptionNode CHAT_CHECK = new OptionNode("check", CHAT, DataType.BOOLEAN);
public final static OptionNode CHAT_SPAM = new OptionNode("spam", CHAT, DataType.PARENT);
private final static OptionNode CHAT_SPAM = new OptionNode("spam", CHAT, DataType.PARENT);
public final static OptionNode CHAT_SPAM_CHECK = new OptionNode("check", CHAT_SPAM, DataType.BOOLEAN);
public final static OptionNode CHAT_SPAM_TIMEFRAME = new OptionNode("timeframe", CHAT_SPAM, DataType.INTEGER);
public final static OptionNode CHAT_SPAM_LIMIT = new OptionNode("limit", CHAT_SPAM, DataType.INTEGER);

View File

@ -30,7 +30,6 @@ public class ConfigurationManager {
private final static String configFileName = "config.txt";
private final static String actionFileName = "actions.txt";
private final static String defaultActionFileName = "default_actions.txt";
private final static String descriptionsFileName = "descriptions.txt";
private final Map<String, ConfigurationCache> worldnameToConfigCacheMap = new HashMap<String, ConfigurationCache>();
@ -80,9 +79,9 @@ public class ConfigurationManager {
// Parse actions file
// MOVE TO ACTIONMANAGER PARSER OR SOMETHING
initializeActions(rootConfigFolder, actionManager);
defaultConfig = new DefaultConfiguration(actionManager);
// Setup the configuration tree
initializeConfig(rootConfigFolder, actionManager);
@ -175,14 +174,14 @@ public class ConfigurationManager {
}
public static File getGlobalConfigFile(String rootFolder) {
private static File getGlobalConfigFile(String rootFolder) {
File globalConfig = new File(rootFolder, configFileName);
return globalConfig;
}
public static Map<String, File> getWorldSpecificConfigFiles(String rootConfigFolder) {
private static Map<String, File> getWorldSpecificConfigFiles(String rootConfigFolder) {
HashMap<String, File> files = new HashMap<String, File>();
@ -206,6 +205,14 @@ public class ConfigurationManager {
FileHandler fh = fileToFileHandlerMap.get(logfile);
// this logger will be used ONLY for logging to a single log-file and
// only
// in this plugin, therefore it doesn't need any namespace
Logger l = Logger.getAnonymousLogger();
l.setLevel(Level.INFO);
// Ignore parent's settings
l.setUseParentHandlers(false);
if(fh == null) {
try {
try {
@ -221,6 +228,8 @@ public class ConfigurationManager {
fh.setFormatter(new LogFileFormatter());
fileToFileHandlerMap.put(logfile, fh);
l.addHandler(fh);
} catch(SecurityException e) {
e.printStackTrace();
} catch(IOException e) {
@ -228,15 +237,6 @@ public class ConfigurationManager {
}
}
// this logger will be used ONLY for logging to a single log-file and
// only
// in this plugin, therefore it doesn't need any namespace
Logger l = Logger.getAnonymousLogger();
l.setLevel(Level.INFO);
// Ignore parent's settings
l.setUseParentHandlers(false);
l.addHandler(fh);
return l;
}
@ -282,17 +282,4 @@ public class ConfigurationManager {
return cache;
}
}
public static File getDescriptionFile(String rootConfigFolder) {
return new File(rootConfigFolder, descriptionsFileName);
}
public static File getDefaultActionFile(String rootConfigFolder) {
return new File(rootConfigFolder, defaultActionFileName);
}
public static File getActionFile(String rootConfigFolder) {
return new File(rootConfigFolder, actionFileName);
}
}

View File

@ -221,11 +221,11 @@ public class FlatFileConfiguration extends Configuration {
private void saveActionList(BufferedWriter w, String id, ActionList actionList) throws IOException {
for(Integer treshold : actionList.getTresholds()) {
String s = "";
StringBuilder s = new StringBuilder("");
for(Action s2 : actionList.getActions(treshold)) {
s = s + " " + s2.name;
s.append(" ").append(s2.name);
}
saveValue(w, id + "." + treshold, s.trim());
saveValue(w, id + "." + treshold, s.toString().trim());
}
}
@ -238,7 +238,7 @@ public class FlatFileConfiguration extends Configuration {
w.write(id + " = " + value + "\r\n");
}
protected String removeQuotationMarks(String s) {
private String removeQuotationMarks(String s) {
s = s.trim();
@ -251,7 +251,7 @@ public class FlatFileConfiguration extends Configuration {
return s;
}
protected String addQuotationMarks(String s) {
private String addQuotationMarks(String s) {
return "\"" + s + "\"";
}

View File

@ -30,7 +30,7 @@ public class OptionNode {
this.type = type;
}
public void addChild(OptionNode node) {
private void addChild(OptionNode node) {
if(this.type != DataType.PARENT) {
throw new IllegalArgumentException("Can't a child to a leaf node.");
}

View File

@ -0,0 +1,37 @@
package cc.co.evenprime.bukkit.nocheat.data;
import org.bukkit.entity.Player;
public class BaseData {
public final BlockBreakData blockbreak;
public final BlockPlaceData blockplace;
public final ChatData chat;
public final LogData log;
public final MovingData moving;
private final Data[] data;
public BaseData() {
this.blockbreak = new BlockBreakData();
this.blockplace = new BlockPlaceData();
this.chat = new ChatData();
this.log = new LogData();
this.moving = new MovingData();
data = new Data[] { this.blockbreak, this.blockplace, this.chat, this.log, this.moving };
}
public void clearCriticalData() {
for(Data d : data) {
d.clearCriticalData();
}
}
public void initialize(Player player) {
for(Data d : data) {
d.initialize(player);
}
}
}

View File

@ -8,11 +8,11 @@ import org.bukkit.Location;
* @author Evenprime
*
*/
public class BlockBreakData {
public class BlockBreakData extends Data {
public double reachViolationLevel = 0.0D;
public double directionViolationLevel = 0.0D;
public double reachViolationLevel = 0.0D;
public double directionViolationLevel = 0.0D;
public Location instaBrokeBlockLocation = null;
}

View File

@ -3,10 +3,10 @@ package cc.co.evenprime.bukkit.nocheat.data;
/**
*
* @author Evenprime
*
*
*/
public class BlockPlaceData {
public class BlockPlaceData extends Data {
public double onliquidViolationLevel = 0.0D;
public double reachViolationLevel = 0.0D;
public double reachViolationLevel = 0.0D;
}

View File

@ -3,11 +3,11 @@ package cc.co.evenprime.bukkit.nocheat.data;
/**
*
* @author Evenprime
*
*
*/
public class ChatData {
public class ChatData extends Data {
public int messageCount = 0;
public int spamLasttime = 0;
}

View File

@ -0,0 +1,21 @@
package cc.co.evenprime.bukkit.nocheat.data;
import org.bukkit.entity.Player;
/**
*
* Every class that is extending this has to implement an empty Constructor()
*
* @author Evenprime
*
*/
public abstract class Data {
public void initialize(Player player) {
}
public void clearCriticalData() {
}
}

View File

@ -7,100 +7,55 @@ import org.bukkit.entity.Player;
/**
* Provide secure access to player-specific data objects for various checks or
* check groups
*
* @author Evenprime
*
* check groups.
*/
public class DataManager {
// Store data between Events
private final Map<Player, MovingData> movingData = new HashMap<Player, MovingData>();
private final Map<Player, BlockBreakData> blockbreakData = new HashMap<Player, BlockBreakData>();
private final Map<Player, BlockPlaceData> blockPlaceData = new HashMap<Player, BlockPlaceData>();
private final Map<Player, ChatData> chatData = new HashMap<Player, ChatData>();
private final Map<Player, LogData> logData = new HashMap<Player, LogData>();
private final Map<Player, BaseData> map;
public DataManager() {
this.map = new HashMap<Player, BaseData>();
}
public MovingData getMovingData(Player player) {
/**
* Get a data object of the specified class. If none is stored yet, create
* one.
*/
public BaseData getData(Player player) {
MovingData data;
// intentionally not thread-safe, because bukkit events are handled
// in sequence anyway, so zero chance of two move events of the same
// player being handled at the same time
data = movingData.get(player);
if(data == null) {
data = new MovingData();
movingData.put(player, data);
}
return data;
}
public BlockBreakData getBlockBreakData(Player player) {
BlockBreakData data;
// intentionally not thread-safe, because bukkit events are handled
// in sequence anyway, so zero chance of two move events of the same
// player being handled at the same time
data = blockbreakData.get(player);
if(data == null) {
data = new BlockBreakData();
blockbreakData.put(player, data);
}
return data;
}
public BlockPlaceData getBlockPlaceData(Player player) {
BlockPlaceData data;
BaseData data = this.map.get(player);
// intentionally not thread-safe, because bukkit events are handled
// in sequence anyway, so zero chance of two events of the same
// player being handled at the same time
data = blockPlaceData.get(player);
// And if it still happens by accident, it's no real loss anyway, as
// losing data of one instance doesn't really hurt at all
if(data == null) {
data = new BlockPlaceData();
blockPlaceData.put(player, data);
data = new BaseData();
data.initialize(player);
this.map.put(player, data);
}
return data;
}
public ChatData getChatData(Player player) {
ChatData data;
// intentionally not thread-safe, because bukkit events are handled
// in sequence anyway, so zero chance of two events of the same
// player being handled at the same time
// And if it still happens by accident, it's no real loss anyway
data = chatData.get(player);
if(data == null) {
data = new ChatData();
chatData.put(player, data);
}
return data;
/**
* Remove all data Objects of a specific player
*
* @param player
*/
public void removeDataForPlayer(Player player) {
this.map.remove(player);
}
public LogData getLogData(Player player) {
LogData data;
// intentionally not thread-safe, because bukkit events are handled
// in sequence anyway, so zero chance of two events of the same
// player being handled at the same time
// And if it still happens by accident, it's no real loss anyway
data = logData.get(player);
if(data == null) {
data = new LogData(player);
logData.put(player, data);
/**
* Reset data that may cause problems after e.g. changing the config
*
*/
public void resetAllCriticalData() {
for(BaseData b : this.map.values()) {
b.clearCriticalData();
}
return data;
}
}

View File

@ -1,32 +1,28 @@
package cc.co.evenprime.bukkit.nocheat.data;
import org.bukkit.Location;
import org.bukkit.Material;
import org.bukkit.block.Block;
import org.bukkit.entity.Player;
/**
* Everything that could be relevant for logging or consolecommand actions
*
* @author Evenprime
*
*/
public class LogData {
public class LogData extends Data {
// The player never changes
public final Player player;
public String check;
public int violationLevel;
public Player player;
public String check;
public int violationLevel;
public Location toLocation;
public int packets;
public String text;
public Material placedMaterial;
public Block placed;
public Block placedAgainst;
public double reachdistance;
public float falldistance;
public LogData(Player player) {
public int packets;
public String text;
public Block placed;
public Block placedAgainst;
public double reachdistance;
public float falldistance;
public void initialize(Player player) {
this.player = player;
check = "";
toLocation = player.getLocation();
}
}

View File

@ -1,41 +1,59 @@
package cc.co.evenprime.bukkit.nocheat.data;
import org.bukkit.Location;
import org.bukkit.entity.Player;
/**
* Playerspecific data for the moving check group
*
* @author Evenprime
*
* Player specific data for the moving check group
*/
public class MovingData {
public class MovingData extends Data {
public int jumpPhase = 0;
public int jumpPhase;
public Location runflySetBackPoint = null;
public Location runflySetBackPoint;
public double runflyViolationLevel = 0.0D;
public double runflyViolationLevel;
public double vertFreedom = 0.0D;
public double vertVelocity = 0.0D;
public int vertVelocityCounter = 0;
public double horizFreedom = 0.0D;
public int horizVelocityCounter = 0;
public double vertFreedom;
public double vertVelocity;
public int vertVelocityCounter;
public double horizFreedom;
public int horizVelocityCounter;
public double nofallViolationLevel = 0.0D;
public float fallDistance = 0.0F;
public float lastAddedFallDistance = 0.0F;
public double nofallViolationLevel;
public float fallDistance;
public float lastAddedFallDistance;
public double horizontalBuffer;
public int bunnyhopdelay = 0;
public int bunnyhopdelay;
public int morePacketsCounter;
public int morePacketsBuffer = 50;
public int morePacketsBuffer;
public Location morePacketsSetbackPoint;
public double morePacketsViolationLevel = 0;
public double morePacketsViolationLevel;
public Location teleportTo;
public int lastElapsedIngameSeconds = 0;
public int lastElapsedIngameSeconds;
@Override
public void initialize(Player player) {
runflySetBackPoint = player.getLocation();
morePacketsBuffer = 50;
morePacketsSetbackPoint = player.getLocation();
}
@Override
public void clearCriticalData() {
teleportTo = null;
jumpPhase = 0;
runflySetBackPoint = null;
fallDistance = 0;
lastAddedFallDistance = 0;
bunnyhopdelay = 0;
morePacketsBuffer = 50;
morePacketsSetbackPoint = null;
lastElapsedIngameSeconds = 0;
morePacketsCounter = 0;
}
}

View File

@ -27,8 +27,8 @@ import cc.co.evenprime.bukkit.nocheat.data.BlockBreakData;
*/
public class BlockBreakEventManager extends BlockListener implements EventManager {
private final BlockBreakCheck blockBreakCheck;
private final NoCheat plugin;
private final BlockBreakCheck blockBreakCheck;
private final NoCheat plugin;
public BlockBreakEventManager(NoCheat plugin) {
@ -57,7 +57,7 @@ public class BlockBreakEventManager extends BlockListener implements EventManage
boolean cancel = false;
// Get the player-specific stored data that applies here
final BlockBreakData data = plugin.getDataManager().getBlockBreakData(player);
final BlockBreakData data = plugin.getDataManager().getData(player).blockbreak;
cancel = blockBreakCheck.check(player, event.getBlock(), data, cc);
@ -77,14 +77,13 @@ public class BlockBreakEventManager extends BlockListener implements EventManage
final Player player = event.getPlayer();
// Get the player-specific stored data that applies here
final BlockBreakData data = plugin.getDataManager().getBlockBreakData(player);
final BlockBreakData data = plugin.getDataManager().getData(player).blockbreak;
// Remember this location. We ignore block breaks in the block-break
// direction check that are insta-breaks
data.instaBrokeBlockLocation = event.getBlock().getLocation();
}
@Override
public List<String> getActiveChecks(ConfigurationCache cc) {
LinkedList<String> s = new LinkedList<String>();
@ -96,10 +95,9 @@ public class BlockBreakEventManager extends BlockListener implements EventManage
return s;
}
@Override
public List<String> getInactiveChecks(ConfigurationCache cc) {
LinkedList<String> s = new LinkedList<String>();
if(!(cc.blockbreak.check && cc.blockbreak.directionCheck))
s.add("blockbreak.direction");
if(!(cc.blockbreak.check && cc.blockbreak.reachCheck))

View File

@ -33,7 +33,7 @@ public class BlockPlaceEventManager extends BlockListener implements EventManage
public BlockPlaceEventManager(NoCheat p) {
this.plugin = p;
this.movingCheck = new RunFlyCheck(plugin);
this.blockPlaceCheck = new BlockPlaceCheck(plugin);
@ -49,7 +49,7 @@ public class BlockPlaceEventManager extends BlockListener implements EventManage
if(!event.isCancelled()) {
final Player player = event.getPlayer();
// Get the player-specific stored data that applies here
movingCheck.blockPlaced(player, plugin.getDataManager().getMovingData(player), event.getBlockPlaced());
movingCheck.blockPlaced(player, plugin.getDataManager().getData(player).moving, event.getBlockPlaced());
}
}
}, Priority.Monitor, plugin);
@ -67,7 +67,7 @@ public class BlockPlaceEventManager extends BlockListener implements EventManage
// Find out if checks need to be done for that player
if(cc.blockplace.check && !player.hasPermission(Permissions.BLOCKPLACE)) {
cancel = blockPlaceCheck.check(player, event.getBlockPlaced(), event.getBlockAgainst(), plugin.getDataManager().getBlockPlaceData(player), cc);
cancel = blockPlaceCheck.check(player, event.getBlockPlaced(), event.getBlockAgainst(), plugin.getDataManager().getData(player).blockplace, cc);
}
if(cancel) {
@ -76,7 +76,6 @@ public class BlockPlaceEventManager extends BlockListener implements EventManage
}
}
@Override
public List<String> getActiveChecks(ConfigurationCache cc) {
LinkedList<String> s = new LinkedList<String>();
@ -88,7 +87,6 @@ public class BlockPlaceEventManager extends BlockListener implements EventManage
return s;
}
@Override
public List<String> getInactiveChecks(ConfigurationCache cc) {
LinkedList<String> s = new LinkedList<String>();

View File

@ -8,5 +8,6 @@ import cc.co.evenprime.bukkit.nocheat.config.cache.ConfigurationCache;
public interface EventManager {
public List<String> getActiveChecks(ConfigurationCache cc);
public List<String> getInactiveChecks(ConfigurationCache cc);
}

View File

@ -16,7 +16,6 @@ import cc.co.evenprime.bukkit.nocheat.NoCheat;
import cc.co.evenprime.bukkit.nocheat.checks.chat.ChatCheck;
import cc.co.evenprime.bukkit.nocheat.config.Permissions;
import cc.co.evenprime.bukkit.nocheat.config.cache.ConfigurationCache;
import cc.co.evenprime.bukkit.nocheat.data.ChatData;
/**
*
@ -54,10 +53,7 @@ public class PlayerChatEventManager extends PlayerListener implements EventManag
boolean cancel = false;
// Get the player-specific stored data that applies here
final ChatData data = plugin.getDataManager().getChatData(player);
cancel = chatCheck.check(player, event.getMessage(), data, cc);
cancel = chatCheck.check(player, event.getMessage(), plugin.getDataManager().getData(player).chat, cc);
if(cancel) {
event.setCancelled(true);
@ -70,7 +66,6 @@ public class PlayerChatEventManager extends PlayerListener implements EventManag
onPlayerChat(event);
}
@Override
public List<String> getActiveChecks(ConfigurationCache cc) {
LinkedList<String> s = new LinkedList<String>();
@ -79,7 +74,6 @@ public class PlayerChatEventManager extends PlayerListener implements EventManag
return s;
}
@Override
public List<String> getInactiveChecks(ConfigurationCache cc) {
LinkedList<String> s = new LinkedList<String>();

View File

@ -60,7 +60,7 @@ public class PlayerMoveEventManager extends PlayerListener implements EventManag
if(cc.moving.check && !player.hasPermission(Permissions.MOVE)) {
// Get the player-specific stored data that applies here
final MovingData data = plugin.getDataManager().getMovingData(player);
final MovingData data = plugin.getDataManager().getData(player).moving;
// Get some data that's needed from this event, to avoid passing the
// event itself on to the checks (and risk to
@ -96,7 +96,7 @@ public class PlayerMoveEventManager extends PlayerListener implements EventManag
if(!event.isCancelled()) {
Player player = event.getPlayer();
MovingData mdata = plugin.getDataManager().getMovingData(player);
MovingData mdata = plugin.getDataManager().getData(player).moving;
Vector v = event.getVelocity();
@ -106,7 +106,7 @@ public class PlayerMoveEventManager extends PlayerListener implements EventManag
mdata.vertFreedom += mdata.vertVelocity;
}
mdata.vertVelocityCounter = 50;
mdata.vertVelocityCounter = 50;
newVal = Math.sqrt(Math.pow(v.getX(), 2) + Math.pow(v.getZ(), 2));
if(newVal > 0.0D) {
@ -116,7 +116,6 @@ public class PlayerMoveEventManager extends PlayerListener implements EventManag
}
}
@Override
public List<String> getActiveChecks(ConfigurationCache cc) {
LinkedList<String> s = new LinkedList<String>();
@ -136,7 +135,6 @@ public class PlayerMoveEventManager extends PlayerListener implements EventManag
return s;
}
@Override
public List<String> getInactiveChecks(ConfigurationCache cc) {
LinkedList<String> s = new LinkedList<String>();

View File

@ -0,0 +1,42 @@
package cc.co.evenprime.bukkit.nocheat.events;
import java.util.Collections;
import java.util.List;
import org.bukkit.Bukkit;
import org.bukkit.event.Event;
import org.bukkit.event.Event.Priority;
import org.bukkit.event.player.PlayerListener;
import org.bukkit.event.player.PlayerQuitEvent;
import org.bukkit.plugin.PluginManager;
import cc.co.evenprime.bukkit.nocheat.NoCheat;
import cc.co.evenprime.bukkit.nocheat.config.cache.ConfigurationCache;
public class PlayerQuitEventManager extends PlayerListener implements EventManager {
private final NoCheat plugin;
public PlayerQuitEventManager(NoCheat plugin) {
this.plugin = plugin;
PluginManager pm = Bukkit.getServer().getPluginManager();
pm.registerEvent(Event.Type.PLAYER_QUIT, this, Priority.Monitor, plugin);
}
@Override
public void onPlayerQuit(PlayerQuitEvent event) {
// Get rid of the players stored data when disconnecting him
plugin.getDataManager().removeDataForPlayer(event.getPlayer());
}
public List<String> getActiveChecks(ConfigurationCache cc) {
return Collections.emptyList();
}
public List<String> getInactiveChecks(ConfigurationCache cc) {
return Collections.emptyList();
}
}

View File

@ -29,7 +29,7 @@ import cc.co.evenprime.bukkit.nocheat.data.MovingData;
public class PlayerTeleportEventManager extends PlayerListener implements EventManager {
private final NoCheat plugin;
public PlayerTeleportEventManager(NoCheat p) {
this.plugin = p;
@ -48,9 +48,9 @@ public class PlayerTeleportEventManager extends PlayerListener implements EventM
@Override
public void onPlayerTeleport(PlayerTeleportEvent event) {
final MovingData data2 = plugin.getDataManager().getMovingData(event.getPlayer());
final MovingData data = plugin.getDataManager().getData(event.getPlayer()).moving;
if(event.isCancelled()) {
if(data2.teleportTo != null && data2.teleportTo.equals(event.getTo())) {
if(data.teleportTo != null && data.teleportTo.equals(event.getTo())) {
event.setCancelled(false);
}
}
@ -60,13 +60,15 @@ public class PlayerTeleportEventManager extends PlayerListener implements EventM
@Override
public void onPlayerTeleport(PlayerTeleportEvent event) {
// Intentionally not check for cancelled state, may help with problems with other plugins...
handleTeleportation(event.getPlayer(), event.getTo());
if(!event.isCancelled()) {
handleTeleportation(event.getPlayer(), event.getTo());
}
}
public void onPlayerPortal(PlayerPortalEvent event) {
// Intentionally not check for cancelled state, may help with problems with other plugins...
handleTeleportation(event.getPlayer(), event.getTo());
if(!event.isCancelled()) {
handleTeleportation(event.getPlayer(), event.getTo());
}
}
public void onPlayerRespawn(PlayerRespawnEvent event) {
@ -79,26 +81,16 @@ public class PlayerTeleportEventManager extends PlayerListener implements EventM
handleTeleportation(event.getPlayer(), event.getFrom());
}
}
private void handleTeleportation(Player player, Location newLocation) {
/********* Moving check ************/
final MovingData data = plugin.getDataManager().getMovingData(player);
data.runflySetBackPoint = null;
data.morePacketsCounter = 0;
data.morePacketsSetbackPoint = null;
data.jumpPhase = 0;
data.fallDistance = 0F;
data.lastAddedFallDistance = 0F;
plugin.getDataManager().getData(player).clearCriticalData();
}
@Override
public List<String> getActiveChecks(ConfigurationCache cc) {
return Collections.emptyList();
}
@Override
public List<String> getInactiveChecks(ConfigurationCache cc) {
return Collections.emptyList();
}

View File

@ -66,7 +66,7 @@ public class LogManager {
* @param level
* @param message
*/
public void logToChat(LogLevel level, String message) {
private void logToChat(LogLevel level, String message) {
for(Player player : Bukkit.getServer().getOnlinePlayers()) {
if(player.hasPermission(Permissions.ADMIN_CHATLOG)) {
player.sendMessage(message);
@ -81,7 +81,7 @@ public class LogManager {
* @param message
* @param fileLogger
*/
public void logToFile(LogLevel level, String message, Logger fileLogger) {
private void logToFile(LogLevel level, String message, Logger fileLogger) {
fileLogger.log(level.level, message);
}
}