Better configuration (again) - also implemented new Chat section in config, as well as new definable chat warning messages & actions.

This commit is contained in:
h31ix committed 2012-10-17 22:24:46 -04:00
1 parent 6d611b905f
commit f8c067f566
7 files changed
+216 -62

No files matched your search

@@ -18,12 +18,9 @@
package net.h31ix.anticheat.util.yaml;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.HashMap;
import java.util.Map;
import java.util.logging.Level;
@@ -38,11 +35,39 @@ import org.yaml.snakeyaml.representer.Representer;
public class CommentedConfiguration extends YamlConfiguration
{
private Map<Integer, String> comments = new HashMap<Integer, String>();
private Map<String, String> comments = new HashMap<String, String>();
private final DumperOptions yamlOptions = new DumperOptions();
private final Representer yamlRepresenter = new YamlRepresenter();
private final Yaml yaml = new Yaml(new YamlConstructor(), yamlRepresenter, yamlOptions);
private String lastLine = null;
public CommentedConfiguration()
{
// Val 1 = line comment should be under
// Val 2 = comment
comments.put("XRay:", " # Should AntiCheat use calculations to try and find xrayers?");
comments.put("Log xray stats:", " # Should a warning be sent to admins when a person is found that could be xraying?");
comments.put("Alert when xray is found:", " # Should players in creative mode be tracked for possible xray usage?");
comments.put("Chat:", " # Should AntiCheat block players spamming messages in chat?");
comments.put("Block chat spam:", " # Should AntiCheat block players spamming commands in chat?");
comments.put("Block command spam:", " # Valid actions = NONE,KICK,BAN,COMMAND[command]");
comments.put("Ban Action:", "# Events occur when a player's hack level changes. You may configure reactions to these events here.");
comments.put("# Events occur when a player's hack level changes. You may configure reactions to these events here.", "# Valid events = NONE,WARN,KICK,BAN,COMMAND[command]");
comments.put("# Valid events = NONE,WARN,KICK,BAN,COMMAND[command]", "# Use commands like so: COMMAND[ban &player 30] or COMMAND[kick &player hacking] or COMMAND[jail &player 10]");
comments.put("# Use commands like so: COMMAND[ban &player 30] or COMMAND[kick &player hacking] or COMMAND[jail &player 10]", "# &player will be replaced with player's name, and &world will be replaced with the player's world");
comments.put("Level High:", " # How many warnings should the player get before entering medium?");
comments.put("Medium threshold:", " # How many warnings should the player get before entering high?");
comments.put("System:", " # Turning auto-update off is a _BAD_ idea. You will no longer be protected by the latest hacks/cheats if you do so, and will have to update manually.");
comments.put("# Turning auto-update off is a _BAD_ idea. You will no longer be protected by the latest hacks/cheats if you do so, and will have to update manually.", " # Should AntiCheat log ALL failed checks to console?");
comments.put("Log to console:", " # Should AntiCheat log to files?");
comments.put("# Should AntiCheat log to files?", " # 0 = off, 1 = log only when an event takes place, 2 = more detailed logs, 3 = most detailed logs");
comments.put("File log level:", " # Should AntiCheat display extra debug information when starting?");
comments.put("Verbose startup:", " # If silent mode is on, players will not be stopped when they try to hack,");
comments.put("# If silent mode is on, players will not be stopped when they try to hack,", " # and AntiCheat will do everything possible to keep them unaware of their rising hack level.");
comments.put("# and AntiCheat will do everything possible to keep them unaware of their rising hack level.", " # However, alerts will still be sent to console and to admins online, and events will still take place.");
comments.put("Silent mode:", " # Should ops be exempt from all checks?");
}
@Override
public String saveToString() {
@@ -56,46 +81,54 @@ public class CommentedConfiguration extends YamlConfiguration
dump = "";
} else {
StringBuilder builder = new StringBuilder();
int i = 0;
for(String string : dump.split("\n")) {
i++;
while(comments.containsKey(i)) {
builder.append(comments.get(i));
String [] lines = dump.split("\n");
int i =0;
while(i < lines.length) {
if(containsLine(lines[i])) {
builder.append(lines[i]);
builder.append("\n");
builder.append(getComment(lines[i]));
builder.append("\n");
lastLine = getComment(getComment(lines[i]));
i++;
} else if(containsLine(lastLine)) {
builder.append(getComment(lastLine));
builder.append("\n");
lastLine = getComment(lastLine);
} else {
builder.append(lines[i]);
builder.append("\n");
i++;
}
builder.append(string);
builder.append("\n");
}
dump = builder.toString();
dump = "# AntiCheat configuration file\n# Please report any bugs: http://dev.bukkit.org/server-mods/anticheat/\n"+builder.toString();
}
return dump;
}
@Override
public void load(InputStream stream) throws IOException, InvalidConfigurationException {
InputStreamReader reader = new InputStreamReader(stream);
StringBuilder builder = new StringBuilder();
BufferedReader input = new BufferedReader(reader);
try {
String line;
int i = 0;
while ((line = input.readLine()) != null) {
i++;
if(line.contains(COMMENT_PREFIX)) {
comments.put(i, line);
private boolean containsLine(String line)
{
if(line != null)
{
for(String string : comments.keySet()) {
if(string.replaceAll(" ", "").split(":")[0].equalsIgnoreCase(line.replaceAll(" ", "").split(":")[0])) {
return true;
}
builder.append(line);
builder.append('\n');
}
} finally {
input.close();
}
return false;
}
loadFromString(builder.toString());
private String getComment(String line)
{
for(String string : comments.keySet()) {
if(string.replaceAll(" ", "").split(":")[0].equalsIgnoreCase(line.replaceAll(" ", "").split(":")[0])) {
return comments.get(string);
}
}
return null;
}
public static CommentedConfiguration loadConfig(File file) {