NoCheatPlus/src/cc/co/evenprime/bukkit/nocheat/NoCheatConfiguration.java
Evenprime b821292bf2 Readded "event.setCancelled(true);" for PLAYER_MOVE events, now that I
know that this was not the reason for disappearing players. I wished
cancelling move events would actually do what one would expect.
2011-03-13 15:57:19 +01:00

210 lines
7.7 KiB
Java

package cc.co.evenprime.bukkit.nocheat;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.util.logging.ConsoleHandler;
import java.util.logging.FileHandler;
import java.util.logging.Level;
import java.util.logging.Logger;
import org.bukkit.util.config.Configuration;
/**
* Central location for everything that's described in the configuration file
*
* @author Evenprime
*
*/
public class NoCheatConfiguration {
// Our personal logger
public static final String loggerName = "cc.co.evenprime.bukkit.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 = 5;
// 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 airbuildAction = "";
// The log level above which players with the permission nocheat.notify will get informed about violations
public static Level chatLevel = Level.OFF;
public static Level ircLevel = Level.OFF;
public static String ircTag = "";
// Our two log outputs, the console and a file
private static ConsoleHandler ch = null;
private static FileHandler fh = null;
private NoCheatConfiguration() {}
/**
* Read the configuration file and assign either standard values or whatever is declared in the file
* @param configurationFile
*/
public static void config(File configurationFile) {
if(!configurationFile.exists()) {
createStandardConfigFile(configurationFile);
}
Configuration c = new Configuration(configurationFile);
c.load();
logger.setLevel(Level.INFO);
logger.setUseParentHandlers(false);
if(ch == null) {
ch = new ConsoleHandler();
ch.setLevel(stringToLevel(c.getString("logging.logtoconsole")));
ch.setFormatter(Logger.getLogger("Minecraft").getHandlers()[0].getFormatter());
logger.addHandler(ch);
}
if(fh == null) {
try {
fh = new FileHandler(c.getString("logging.filename"), true);
fh.setLevel(stringToLevel(c.getString("logging.logtofile")));
fh.setFormatter(Logger.getLogger("Minecraft").getHandlers()[0].getFormatter());
logger.addHandler(fh);
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
chatLevel = stringToLevel(c.getString("logging.logtonotify")); // deprecated, will be deleted eventually
chatLevel = stringToLevel(c.getString("logging.logtochat"));
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);
speedhackLimitLow = c.getInt("speedhack.limits.low", 30);
speedhackLimitMed = c.getInt("speedhack.limits.med", 45);
speedhackLimitHigh = c.getInt("speedhack.limits.high", 60);
movingFreeMoves = c.getInt("moving.freemoves", 5);
movingActionMinor = c.getString("moving.action.low", "loglow reset");
movingActionNormal = c.getString("moving.action.med", "logmed reset");
movingActionHeavy = c.getString("moving.action.high", "loghigh reset");
speedhackActionMinor = c.getString("speedhack.action.low", "loglow");
speedhackActionNormal = c.getString("speedhack.action.med", "logmed");
speedhackActionHeavy = c.getString("speedhack.action.high", "loghigh");
airbuildAction = c.getString("airbuild.action", "logmed 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;
}
/**
* Convert a string into a log level
* @param string
* @return
*/
private static Level stringToLevel(String string) {
if(string == null) {
return Level.OFF;
}
if(string.trim().equals("info") || string.trim().equals("low")) return Level.INFO;
if(string.trim().equals("warn") || string.trim().equals("med")) return Level.WARNING;
if(string.trim().equals("severe")|| string.trim().equals("high")) return Level.SEVERE;
return Level.OFF;
}
/**
* Standard configuration file for people who haven't got one yet
* @param f
*/
private static void createStandardConfigFile(File f) {
try {
f.getParentFile().mkdirs();
f.createNewFile();
BufferedWriter w = new BufferedWriter(new FileWriter(f));
w.write("# Logging: potential log levels are low (info), med (warn), high (severe), off"); w.newLine();
w.write("logging:"); w.newLine();
w.write(" filename: plugins/NoCheat/nocheat.log"); w.newLine();
w.write(" logtofile: low"); w.newLine();
w.write(" logtoconsole: high"); w.newLine();
w.write(" logtochat: med"); w.newLine();
w.write(" logtoirc: med"); w.newLine();
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 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("# 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("# 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: 5"); 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("# Airbuild specific options"); w.newLine();
w.write("airbuild:"); w.newLine();
w.write("# Airbuild Action, one or more of 'loglow logmed loghigh deny'"); w.newLine();
w.write(" action: logmed deny"); w.newLine();
w.write("# Bedteleport specific options (none exist yet)"); w.newLine();
w.write("bedteleport:"); w.newLine();
w.flush(); w.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}