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

@@ -23,9 +23,11 @@ import java.util.HashMap;
import java.util.List;
import java.util.Map;
import net.h31ix.anticheat.util.Distance;
import net.h31ix.anticheat.util.Language;
import net.h31ix.anticheat.util.Magic;
import net.h31ix.anticheat.util.Utilities;
import net.h31ix.anticheat.util.yaml.CommentedConfiguration;
import org.bukkit.Bukkit;
import org.bukkit.ChatColor;
import org.bukkit.GameMode;
import org.bukkit.Location;
@@ -84,11 +86,13 @@ public class Backend
private Magic magic;
private AnticheatManager micromanage = null;
private Language lang = null;
public Backend(AnticheatManager instance)
{
magic = new Magic(instance.getConfiguration().getMagic(), instance.getConfiguration(), CommentedConfiguration.loadConfiguration(instance.getPlugin().getResource("magic.yml")));
micromanage = instance;
lang = micromanage.getConfiguration().getLang();
}
public void garbageClean(Player player)
@@ -1122,7 +1126,7 @@ public class Backend
{
if (amount >= magic.CHAT_WARN_LEVEL)
{
player.sendMessage(ChatColor.RED + "Stop flooding the server or you will be kicked!");
player.sendMessage(ChatColor.RED + lang.getChatWarning());
}
if (amount >= magic.CHAT_KICK_LEVEL)
{
@@ -1139,16 +1143,30 @@ public class Backend
chatKicks.put(name, kick);
}
if (kick <= magic.CHAT_BAN_LEVEL)
String event = kick <= magic.CHAT_BAN_LEVEL ? micromanage.getConfiguration().chatActionKick() : micromanage.getConfiguration().chatActionBan();
if (event.startsWith("COMMAND["))
{
player.kickPlayer(ChatColor.RED + "Spamming, kick " + kick + "/"+magic.CHAT_BAN_LEVEL);
micromanage.getPlugin().getServer().broadcastMessage(ChatColor.RED + player.getName() + " was kicked for spamming.");
String command = event.replaceAll("COMMAND\\[", "").replaceAll("]", "").replaceAll("&player", name).replaceAll("&world", player.getWorld().getName());
Bukkit.getServer().dispatchCommand(Bukkit.getConsoleSender(), command);
}
else
else if (event.equalsIgnoreCase("KICK"))
{
player.kickPlayer(ChatColor.RED+lang.getChatKickReason());
String msg = ChatColor.RED + lang.getChatKickBroadcast().replaceAll("&player", name);
if(!msg.equals(""))
{
Bukkit.broadcastMessage(msg);
}
}
else if (event.equalsIgnoreCase("BAN"))
{
player.kickPlayer(ChatColor.RED + "Banned for spamming.");
player.setBanned(true);
micromanage.getPlugin().getServer().broadcastMessage(ChatColor.RED + player.getName() + " was banned for spamming.");
player.kickPlayer(ChatColor.RED + lang.getChatBanReason());
String msg = ChatColor.RED + lang.getChatBanBroadcast().replaceAll("&player", name);
if(!msg.equals(""))
{
Bukkit.broadcastMessage(msg);
}
}
}
}
@@ -141,7 +141,7 @@ public class UserManager
messageArray.set(i, message);
}
Utilities.alert(messageArray);
execute(user, "Medium");
execute(user, config.mediumEvent());
}
public void alertHigh(User user, CheckType type)
@@ -156,19 +156,18 @@ public class UserManager
messageArray.set(i, message);
}
Utilities.alert(messageArray);
execute(user, "High");
execute(user, config.highEvent());
}
public void execute(User user, String event)
{
final String result = config.getResult(event);
final String name = user.getName();
if (result.startsWith("COMMAND["))
if (event.startsWith("COMMAND["))
{
String command = result.replaceAll("COMMAND\\[", "").replaceAll("]", "").replaceAll("&player", name).replaceAll("&world", user.getPlayer().getWorld().getName());
String command = event.replaceAll("COMMAND\\[", "").replaceAll("]", "").replaceAll("&player", name).replaceAll("&world", user.getPlayer().getWorld().getName());
Bukkit.getServer().dispatchCommand(Bukkit.getConsoleSender(), command);
}
else if (result.equalsIgnoreCase("KICK"))
else if (event.equalsIgnoreCase("KICK"))
{
user.getPlayer().kickPlayer(RED + lang.getKickReason());
String msg = RED + lang.getKickBroadcast().replaceAll("&player", name);
@@ -177,7 +176,7 @@ public class UserManager
Bukkit.broadcastMessage(msg);
}
}
else if (result.equalsIgnoreCase("WARN"))
else if (event.equalsIgnoreCase("WARN"))
{
List<String> message = lang.getWarning();
for (String string : message)
@@ -185,7 +184,7 @@ public class UserManager
user.getPlayer().sendMessage(RED + string);
}
}
else if (result.equalsIgnoreCase("BAN"))
else if (event.equalsIgnoreCase("BAN"))
{
user.getPlayer().setBanned(true);
user.getPlayer().kickPlayer(RED + lang.getBanReason());
@@ -54,6 +54,10 @@ public class Configuration
private int medThreshold = 0;
private int highThreshold = 0;
private String updateFolder;
private String eventMed;
private String eventHigh;
private String chatActionKick;
private String chatActionBan;
private static Language language;
private List<String> exemptWorlds = new ArrayList<String>();
@@ -144,6 +148,26 @@ public class Configuration
return highThreshold;
}
public String mediumEvent()
{
return eventMed;
}
public String highEvent()
{
return eventHigh;
}
public String chatActionKick()
{
return chatActionKick;
}
public String chatActionBan()
{
return chatActionBan;
}
public int getFileLogLevel()
{
return fileLogLevel;
@@ -171,13 +195,26 @@ public class Configuration
verboseStartup = getBoolean ("System.Verbose startup", false);
alertXRay = getBoolean("XRay.Alert when xray is found", false);
fileLogLevel = getInt("System.File log level", 1);
chatSpam = getBoolean("System.Block chat spam", true);
commandSpam = getBoolean("System.Block command spam", false);
silentMode = getBoolean("System.Silent mode", false);
medThreshold = getInt("Events.Medium threshold", 20);
highThreshold = getInt("Events.High threshold", 50);
opExempt = getBoolean("System.Exempt op", false);
trackCreativeXRay = getBoolean("XRay.Track creative", true);
eventMed = getString("Events.Level Medium", "WARN");
eventMed = getString("Events.Level High", "KICK");
chatActionKick = getString("Chat.Kick Action", "KICK");
chatActionBan = getString("Chat.Ban Action", "BAN");
chatSpam = getBoolean("Chat.Block chat spam", true);
commandSpam = getBoolean("Chat.Block command spam", false);
// Cleanup old values
if(config.getString("System.Block command spam") != null)
{
config.set("System.Block command spam", null);
}
if(config.getString("System.Block chat spam") != null)
{
config.set("System.Block chat spam", null);
}
// Worlds
List<String> list = new ArrayList<String>();
@@ -237,9 +274,17 @@ public class Configuration
}
}
public String getResult(String event)
private String getString(String entry, String d)
{
return config.getString("Events.Level " + event);
if (config.getString(entry) == null)
{
config.set(entry, d);
return d;
}
else
{
return config.getString(entry);
}
}
public void setLog(boolean b)
@@ -21,7 +21,6 @@ package net.h31ix.anticheat.util;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import org.bukkit.ChatColor;
import org.bukkit.configuration.file.FileConfiguration;
@@ -34,12 +33,19 @@ public class Language
private static String banBroadcast = null;
private static String kickReason = null;
private static String kickBroadcast = null;
private static String chatWarning = null;
private static String chatKickBroadcast = null;
private static String chatKickReason = null;
private static String chatBanBroadcast = null;
private static String chatBanReason = null;
private static final ChatColor GOLD = ChatColor.GOLD;
private static final ChatColor GRAY = ChatColor.GRAY;
private FileConfiguration file;
public Language(FileConfiguration file)
{
this.file = file;
String [] temp = file.getList("alert.medium_alert").toArray(new String[file.getList("alert.medium_alert").size()]);
medAlert = new String [temp.length+2];
medAlert[0] = GOLD + "----------------------[" + GRAY + "AntiCheat" + GOLD + "]----------------------";
@@ -56,12 +62,18 @@ public class Language
highAlert[i+1] = GRAY + temp[i];
}
highAlert[highAlert.length-1] = GOLD + "-----------------------------------------------------";
warning = file.getList("warning.player_warning").toArray(new String[file.getList("warning.player_warning").size()]);
banReason = file.getString("ban.ban_reason");
banBroadcast = file.getString("ban.ban_broadcast");
kickReason = file.getString("kick.kick_reason");
kickBroadcast = file.getString("kick.kick_broadcast");
// Following values were implemented later
chatWarning = getString("chat.warning", "Stop spamming the server or you will be kicked!");
chatKickReason = getString("chat.kick_reason", "Kicked for spamming");
chatBanReason = getString("chat.kick_reason", "Banned for spamming");
chatKickBroadcast = getString("chat.kick_broadcast", "[AntiCheat] &player was kicked for spamming");
chatBanBroadcast = getString("chat.kick_broadcast", "[AntiCheat] &player was banned for spamming");
}
public List<String> getMediumAlert()
@@ -98,4 +110,42 @@ public class Language
{
return kickBroadcast;
}
public String getChatKickReason()
{
return chatKickReason;
}
public String getChatKickBroadcast()
{
return chatKickBroadcast;
}
public String getChatBanReason()
{
return chatBanReason;
}
public String getChatBanBroadcast()
{
return chatBanBroadcast;
}
public String getChatWarning()
{
return chatWarning;
}
private String getString(String entry, String d)
{
if (file.getString(entry) == null)
{
file.set(entry, d);
return d;
}
else
{
return file.getString(entry);
}
}
}
@@ -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) {
+11 -7
View File
@@ -1,15 +1,24 @@
# AntiCheat configuration file
# Please report any bugs: http://dev.bukkit.org/server-mods/anticheat/
XRay:
# Should AntiCheat use calculations to try and find xrayers?
Log xray stats: true
# Should a warning be sent to admins when a person is found that could be xraying?
Alert when xray is found: false
# Should players in creative mode be tracked for possible xray usage?
Track creative: true
Chat:
# Should AntiCheat block players spamming messages in chat?
Block chat spam: true
# Should AntiCheat block players spamming commands in chat?
Block command spam: true
# Valid actions = NONE,KICK,BAN,COMMAND[command]
Kick Action: KICK
Ban Action: BAN
# 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]
# 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 any command on your server can be used.
# &player will be replaced with player's name, and &world will be replaced with the player's world
Events:
Level Medium: WARN
Level High: KICK
@@ -18,8 +27,7 @@ Events:
# How many warnings should the player get before entering high?
High threshold: 50
System:
# Turning auto-update off is a _BAD_ idea. You will no longer be protected by the latest hacks/cheats if you do so.
# AntiCheat will never bother you about updates if this is on, but if you turn it off you will have to manually update.
# 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.
Auto update: true
# Should AntiCheat log ALL failed checks to console?
Log to console: false
@@ -28,10 +36,6 @@ System:
File log level: 1
# Should AntiCheat display extra debug information when starting?
Verbose startup: false
# Should AntiCheat block players spamming messages in chat?
Block chat spam: true
# Should AntiCheat block players spamming commands in chat?
Block command spam: true
# 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.
# However, alerts will still be sent to console and to admins online, and events will still take place.
+7 -2
View File
@@ -13,10 +13,15 @@ warning:
player_warning:
- '[AntiCheat] Hacking is not permitted.'
- '[AntiCheat] If you continue to hack, action will be taken.'
spam:
warning: 'Stop spamming the server or you will be kicked!'
kick_reason: 'Kicked for spamming'
ban_reason: 'Banned for spamming'
kick_broadcast: '[AntiCheat] &player was kicked for spamming'
ban_broadcast: '[AntiCheat] &player was banned for spamming'
ban:
ban_reason: 'Banned by AntiCheat'
ban_broadcast: '[AntiCheat] &player was banned for hacking.'
kick:
kick_reason: 'Kicked by AntiCheat'
kick_broadcast: '[AntiCheat] &player was kicked for hacking.'
kick_broadcast: '[AntiCheat] &player was kicked for hacking.'