diff --git a/src/cc/co/evenprime/bukkit/nocheat/NoCheat.java b/src/cc/co/evenprime/bukkit/nocheat/NoCheat.java index 668482d8..573f6135 100644 --- a/src/cc/co/evenprime/bukkit/nocheat/NoCheat.java +++ b/src/cc/co/evenprime/bukkit/nocheat/NoCheat.java @@ -1,26 +1,24 @@ package cc.co.evenprime.bukkit.nocheat; import java.util.ArrayList; -import java.util.Collections; -import java.util.LinkedList; import java.util.List; import org.bukkit.World; import org.bukkit.command.Command; import org.bukkit.command.CommandSender; import org.bukkit.entity.Player; -import org.bukkit.permissions.Permission; import org.bukkit.plugin.PluginDescriptionFile; import org.bukkit.plugin.java.JavaPlugin; import cc.co.evenprime.bukkit.nocheat.actions.ActionManager; +import cc.co.evenprime.bukkit.nocheat.command.CommandHandler; import cc.co.evenprime.bukkit.nocheat.config.ConfigurationManager; -import cc.co.evenprime.bukkit.nocheat.config.Permissions; import cc.co.evenprime.bukkit.nocheat.config.cache.ConfigurationCache; import cc.co.evenprime.bukkit.nocheat.config.util.ActionList; import cc.co.evenprime.bukkit.nocheat.data.BaseData; import cc.co.evenprime.bukkit.nocheat.data.DataManager; import cc.co.evenprime.bukkit.nocheat.data.ExecutionHistory; +import cc.co.evenprime.bukkit.nocheat.debug.ActiveCheckPrinter; import cc.co.evenprime.bukkit.nocheat.debug.LagMeasureTask; import cc.co.evenprime.bukkit.nocheat.debug.Performance; import cc.co.evenprime.bukkit.nocheat.debug.PerformanceManager; @@ -113,14 +111,18 @@ public class NoCheat extends JavaPlugin { } // Then print a list of active checks per world - printActiveChecks(); + ActiveCheckPrinter.printActiveChecks(this, eventManagers); // Tell the server admin that we finished loading NoCheat now log.logToConsole(LogLevel.LOW, "[NoCheat] version [" + this.getDescription().getVersion() + "] is enabled."); } public ConfigurationCache getConfig(Player player) { - return conf.getConfigurationCacheForWorld(player.getWorld().getName()); + return getConfig(player.getWorld()); + } + + public ConfigurationCache getConfig(World world) { + return conf.getConfigurationCacheForWorld(world.getName()); } public void log(LogLevel level, String message, ConfigurationCache cc) { @@ -155,152 +157,9 @@ public class NoCheat extends JavaPlugin { data.cleanDataMap(); } - /** - * Print the list of active checks to the console, on a per world basis - */ - private void printActiveChecks() { - - boolean introPrinted = false; - String intro = "[NoCheat] Active Checks: "; - - // Print active checks for NoCheat, if needed. - for(World world : this.getServer().getWorlds()) { - - StringBuilder line = new StringBuilder(" ").append(world.getName()).append(": "); - - int length = line.length(); - - ConfigurationCache cc = this.conf.getConfigurationCacheForWorld(world.getName()); - - if(!cc.debug.showchecks) - continue; - - for(EventManager em : eventManagers) { - if(em.getActiveChecks(cc).size() == 0) - continue; - - for(String active : em.getActiveChecks(cc)) { - line.append(active).append(' '); - } - - if(!introPrinted) { - log.logToConsole(LogLevel.LOW, intro); - introPrinted = true; - } - - log.logToConsole(LogLevel.LOW, line.toString()); - - line = new StringBuilder(length); - - for(int i = 0; i < length; i++) { - line.append(' '); - } - } - - } - } - @Override public boolean onCommand(CommandSender sender, Command command, String label, String[] args) { - - // Not our command - if(!command.getName().equalsIgnoreCase("nocheat") || args.length == 0) - return false; - - if(args[0].equalsIgnoreCase("permlist") && args.length >= 2) { - // permlist command was used - return handlePermlistCommand(sender, args); - - } else if(args[0].equalsIgnoreCase("reload")) { - // reload command was used - return handleReloadCommand(sender); - } - - else if(args[0].equalsIgnoreCase("performance")) { - // performance command was used - return handlePerformanceCommand(sender); - } - - return false; - } - - private boolean handlePermlistCommand(CommandSender sender, String[] args) { - // Does the sender have permission to use it? - if(sender instanceof Player && !sender.hasPermission(Permissions.ADMIN_PERMLIST)) { - return false; - } - - // Get the player by name - Player player = this.getServer().getPlayerExact(args[1]); - if(player == null) { - sender.sendMessage("Unknown player: " + args[1]); - return true; - } - - // Should permissions be filtered by prefix? - String prefix = ""; - if(args.length == 3) { - prefix = args[2]; - } - - // Make a copy to allow sorting - List perms = new LinkedList(this.getDescription().getPermissions()); - Collections.reverse(perms); - - sender.sendMessage("Player " + player.getName() + " has the permission(s):"); - - for(Permission permission : perms) { - if(permission.getName().startsWith(prefix)) { - sender.sendMessage(permission.getName() + ": " + player.hasPermission(permission)); - } - } - return true; - } - - private boolean handleReloadCommand(CommandSender sender) { - // Does the sender have permission? - if(sender instanceof Player && !sender.hasPermission(Permissions.ADMIN_RELOAD)) { - return false; - } - - sender.sendMessage("[NoCheat] Reloading configuration"); - - this.conf.cleanup(); - this.conf = new ConfigurationManager(this.getDataFolder().getPath()); - this.data.clearCriticalData(); - - sender.sendMessage("[NoCheat] Configuration reloaded"); - - return true; - } - - private boolean handlePerformanceCommand(CommandSender sender) { - // Does the sender have permission? - if(sender instanceof Player && !sender.hasPermission(Permissions.ADMIN_PERFORMANCE)) { - return false; - } - - sender.sendMessage("[NoCheat] Retrieving performance statistics"); - - long totalTime = 0; - - for(Type type : Type.values()) { - Performance p = this.getPerformance(type); - - long total = p.getTotalTime(); - totalTime += total; - - StringBuilder string = new StringBuilder("").append(type.toString()); - string.append(": total ").append(Performance.toString(total)); - string.append(", relative ").append(Performance.toString(p.getRelativeTime())); - string.append(" over ").append(p.getCounter()).append(" events."); - - sender.sendMessage(string.toString()); - } - - sender.sendMessage("Total time spent: " + Performance.toString(totalTime) + " " + Performance.toString(totalTime)); - - return true; + return CommandHandler.handleCommand(this, sender, command, label, args); } public int getIngameSeconds() { @@ -328,4 +187,16 @@ public class NoCheat extends JavaPlugin { return false; } + public void logToConsole(LogLevel low, String message) { + if(log != null) { + log.logToConsole(low, message); + } + + } + + public void reloadConfig() { + conf.cleanup(); + this.conf = new ConfigurationManager(this.getDataFolder().getPath()); + this.data.clearCriticalData(); + } } diff --git a/src/cc/co/evenprime/bukkit/nocheat/command/CommandHandler.java b/src/cc/co/evenprime/bukkit/nocheat/command/CommandHandler.java new file mode 100644 index 00000000..327ec087 --- /dev/null +++ b/src/cc/co/evenprime/bukkit/nocheat/command/CommandHandler.java @@ -0,0 +1,118 @@ +package cc.co.evenprime.bukkit.nocheat.command; + +import java.util.Collections; +import java.util.LinkedList; +import java.util.List; + +import org.bukkit.command.Command; +import org.bukkit.command.CommandSender; +import org.bukkit.entity.Player; +import org.bukkit.permissions.Permission; + +import cc.co.evenprime.bukkit.nocheat.NoCheat; +import cc.co.evenprime.bukkit.nocheat.config.Permissions; +import cc.co.evenprime.bukkit.nocheat.debug.Performance; +import cc.co.evenprime.bukkit.nocheat.debug.PerformanceManager.Type; + +public class CommandHandler { + + private CommandHandler() {} + + public static boolean handleCommand(NoCheat plugin, CommandSender sender, Command command, String label, String[] args) { + + // Not our command + if(!command.getName().equalsIgnoreCase("nocheat") || args.length == 0) + return false; + + if(args[0].equalsIgnoreCase("permlist") && args.length >= 2) { + // permlist command was used + return handlePermlistCommand(plugin, sender, args); + + } else if(args[0].equalsIgnoreCase("reload")) { + // reload command was used + return handleReloadCommand(plugin, sender); + } + + else if(args[0].equalsIgnoreCase("performance")) { + // performance command was used + return handlePerformanceCommand(plugin, sender); + } + + return false; + } + + private static boolean handlePermlistCommand(NoCheat plugin, CommandSender sender, String[] args) { + // Does the sender have permission to use it? + if(sender instanceof Player && !sender.hasPermission(Permissions.ADMIN_PERMLIST)) { + return false; + } + + // Get the player by name + Player player = plugin.getServer().getPlayerExact(args[1]); + if(player == null) { + sender.sendMessage("Unknown player: " + args[1]); + return true; + } + + // Should permissions be filtered by prefix? + String prefix = ""; + if(args.length == 3) { + prefix = args[2]; + } + + // Make a copy to allow sorting + List perms = new LinkedList(plugin.getDescription().getPermissions()); + Collections.reverse(perms); + + sender.sendMessage("Player " + player.getName() + " has the permission(s):"); + + for(Permission permission : perms) { + if(permission.getName().startsWith(prefix)) { + sender.sendMessage(permission.getName() + ": " + player.hasPermission(permission)); + } + } + return true; + } + + private static boolean handleReloadCommand(NoCheat plugin, CommandSender sender) { + // Does the sender have permission? + if(sender instanceof Player && !sender.hasPermission(Permissions.ADMIN_RELOAD)) { + return false; + } + + sender.sendMessage("[NoCheat] Reloading configuration"); + plugin.reloadConfig(); + sender.sendMessage("[NoCheat] Configuration reloaded"); + + return true; + } + + private static boolean handlePerformanceCommand(NoCheat plugin, CommandSender sender) { + // Does the sender have permission? + if(sender instanceof Player && !sender.hasPermission(Permissions.ADMIN_PERFORMANCE)) { + return false; + } + + sender.sendMessage("[NoCheat] Retrieving performance statistics"); + + long totalTime = 0; + + for(Type type : Type.values()) { + Performance p = plugin.getPerformance(type); + + long total = p.getTotalTime(); + totalTime += total; + + StringBuilder string = new StringBuilder("").append(type.toString()); + string.append(": total ").append(Performance.toString(total)); + string.append(", relative ").append(Performance.toString(p.getRelativeTime())); + string.append(" over ").append(p.getCounter()).append(" events."); + + sender.sendMessage(string.toString()); + } + + sender.sendMessage("Total time spent: " + Performance.toString(totalTime) + " " + Performance.toString(totalTime)); + + return true; + } +} diff --git a/src/cc/co/evenprime/bukkit/nocheat/config/FlatFileConfiguration.java b/src/cc/co/evenprime/bukkit/nocheat/config/FlatFileConfiguration.java index 8f059f85..39215589 100644 --- a/src/cc/co/evenprime/bukkit/nocheat/config/FlatFileConfiguration.java +++ b/src/cc/co/evenprime/bukkit/nocheat/config/FlatFileConfiguration.java @@ -97,7 +97,6 @@ public class FlatFileConfiguration extends Configuration { return al; } - private OptionNode getOptionNodeForString(OptionNode root, String key) { String parts[] = key.split("\\.", 2); @@ -180,17 +179,21 @@ public class FlatFileConfiguration extends Configuration { saveRecursive(w, o); } - + return; + } else { + saveLeaf(w, node); } + } + + private void saveLeaf(BufferedWriter w, OptionNode node) throws IOException { // Save a leaf node, if it's really stored here Object object = this.get(node); if(object == null) { return; } - // Get the full id of the node String id = node.getName(); OptionNode i = node; @@ -220,7 +223,7 @@ public class FlatFileConfiguration extends Configuration { } } - private void saveActionList(BufferedWriter w, String id, ActionList actionList) throws IOException { + private void saveActionList(BufferedWriter w, String id, ActionList actionList) throws IOException { for(Integer treshold : actionList.getTresholds()) { StringBuilder s = new StringBuilder(""); for(Action s2 : actionList.getActions(treshold)) { diff --git a/src/cc/co/evenprime/bukkit/nocheat/debug/ActiveCheckPrinter.java b/src/cc/co/evenprime/bukkit/nocheat/debug/ActiveCheckPrinter.java new file mode 100644 index 00000000..b4719198 --- /dev/null +++ b/src/cc/co/evenprime/bukkit/nocheat/debug/ActiveCheckPrinter.java @@ -0,0 +1,56 @@ +package cc.co.evenprime.bukkit.nocheat.debug; + +import java.util.List; + +import org.bukkit.World; + +import cc.co.evenprime.bukkit.nocheat.NoCheat; +import cc.co.evenprime.bukkit.nocheat.config.cache.ConfigurationCache; +import cc.co.evenprime.bukkit.nocheat.events.EventManager; +import cc.co.evenprime.bukkit.nocheat.log.LogLevel; + + +public class ActiveCheckPrinter { + + public static void printActiveChecks(NoCheat plugin, List eventManagers) { + + boolean introPrinted = false; + String intro = "[NoCheat] Active Checks: "; + + // Print active checks for NoCheat, if needed. + for(World world : plugin.getServer().getWorlds()) { + + StringBuilder line = new StringBuilder(" ").append(world.getName()).append(": "); + + int length = line.length(); + + ConfigurationCache cc = plugin.getConfig(world); + + if(!cc.debug.showchecks) + continue; + + for(EventManager em : eventManagers) { + if(em.getActiveChecks(cc).size() == 0) + continue; + + for(String active : em.getActiveChecks(cc)) { + line.append(active).append(' '); + } + + if(!introPrinted) { + plugin.logToConsole(LogLevel.LOW, intro); + introPrinted = true; + } + + plugin.logToConsole(LogLevel.LOW, line.toString()); + + line = new StringBuilder(length); + + for(int i = 0; i < length; i++) { + line.append(' '); + } + } + + } + } +} diff --git a/src/cc/co/evenprime/bukkit/nocheat/events/PlayerMoveEventManager.java b/src/cc/co/evenprime/bukkit/nocheat/events/PlayerMoveEventManager.java index 3e18eb7b..4ba42916 100644 --- a/src/cc/co/evenprime/bukkit/nocheat/events/PlayerMoveEventManager.java +++ b/src/cc/co/evenprime/bukkit/nocheat/events/PlayerMoveEventManager.java @@ -144,18 +144,24 @@ public class PlayerMoveEventManager extends PlayerListener implements EventManag public List getActiveChecks(ConfigurationCache cc) { LinkedList s = new LinkedList(); - if(cc.moving.check && cc.moving.runflyCheck && !cc.moving.allowFlying) - s.add("moving.runfly"); - if(cc.moving.check && cc.moving.runflyCheck && cc.moving.allowFlying) - s.add("moving.flying"); - if(cc.moving.check && cc.moving.runflyCheck && !cc.moving.allowFlying && cc.moving.swimmingCheck) - s.add("moving.swimming"); - if(cc.moving.check && cc.moving.runflyCheck && !cc.moving.allowFlying && cc.moving.sneakingCheck) - s.add("moving.sneaking"); - if(cc.moving.check && cc.moving.runflyCheck && !cc.moving.allowFlying && cc.moving.nofallCheck) - s.add("moving.nofall"); - if(cc.moving.check && cc.moving.morePacketsCheck) - s.add("moving.morepackets"); + if(cc.moving.check) { + if(cc.moving.runflyCheck) { + + if(!cc.moving.allowFlying) { + s.add("moving.runfly"); + if(cc.moving.swimmingCheck) + s.add("moving.swimming"); + if(cc.moving.sneakingCheck) + s.add("moving.sneaking"); + if(cc.moving.nofallCheck) + s.add("moving.nofall"); + } else + s.add("moving.flying"); + + } + if(cc.moving.morePacketsCheck) + s.add("moving.morepackets"); + } return s; }