Move stuff out of NoCheat.java into seperate classes

This commit is contained in:
Evenprime 2011-10-19 18:12:35 +02:00
parent ca2da0d6b4
commit 2b4eccf4a1
5 changed files with 220 additions and 166 deletions

View File

@ -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<Permission> perms = new LinkedList<Permission>(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();
}
}

View File

@ -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<Permission> perms = new LinkedList<Permission>(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;
}
}

View File

@ -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)) {

View File

@ -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<EventManager> 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(' ');
}
}
}
}
}

View File

@ -144,18 +144,24 @@ public class PlayerMoveEventManager extends PlayerListener implements EventManag
public List<String> getActiveChecks(ConfigurationCache cc) {
LinkedList<String> s = new LinkedList<String>();
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;
}