Implement new levels system.

This commit is contained in:
h31ix committed 2013-02-01 14:07:28 -05:00
1 parent 14ca51bac9
commit 52a90b9bc8
11 files changed
+307 -131

No files matched your search

@@ -22,11 +22,13 @@ import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;
import net.h31ix.anticheat.Anticheat;
import net.h31ix.anticheat.manage.AnticheatManager;
import net.h31ix.anticheat.util.yaml.CommentedConfiguration;
import org.bukkit.Bukkit;
import org.bukkit.ChatColor;
import org.bukkit.World;
import org.bukkit.configuration.file.FileConfiguration;
import org.bukkit.configuration.file.YamlConfiguration;
@@ -37,9 +39,11 @@ public class Configuration {
private File levelFile = null;
private File langFile = null;
private File magicFile = null;
private File eventsFile = null;
private CommentedConfiguration config;
private FileConfiguration level;
private FileConfiguration magic;
private FileConfiguration events;
private boolean logConsole;
private boolean logXRay;
private boolean alertXRay;
@@ -52,16 +56,14 @@ public class Configuration {
private boolean trackCreativeXRay;
private boolean eventChains;
private int fileLogLevel = 0;
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>();
private List<Level> levels = new ArrayList<Level>();
private Magic magicInstance;
private int highestLevel;
public Configuration(AnticheatManager instance) {
micromanage = instance;
@@ -69,14 +71,19 @@ public class Configuration {
levelFile = new File(micromanage.getPlugin().getDataFolder() + "/data/level.yml");
langFile = new File(micromanage.getPlugin().getDataFolder() + "/lang.yml");
magicFile = new File(micromanage.getPlugin().getDataFolder() + "/magic.yml");
load();
eventsFile = new File(micromanage.getPlugin().getDataFolder() + "/events.yml");
}
private void save() {
try {
config.save(configFile);
events.save(eventsFile);
level.save(levelFile);
magic.save(magicFile);
System.out.println("Saved");
System.out.println(config.get("Events.Level Medium"));
} catch (IOException ex) {
Logger.getLogger(Configuration.class.getName()).log(Level.SEVERE, null, ex);
Logger.getLogger(Configuration.class.getName()).severe(ex.getMessage());
}
}
@@ -128,22 +135,6 @@ public class Configuration {
return eventChains;
}
public int medThreshold() {
return medThreshold;
}
public int highThreshold() {
return highThreshold;
}
public String mediumEvent() {
return eventMed;
}
public String highEvent() {
return eventHigh;
}
public String chatActionKick() {
return chatActionKick;
}
@@ -159,12 +150,86 @@ public class Configuration {
public Language getLang() {
return language;
}
public List<Level> getLevels() {
return levels;
}
public int getHighestLevel() {
return highestLevel = -99;
}
public void updateEvents() {
// Update the old event system to events.yml
CommentedConfiguration config = CommentedConfiguration.loadConfig(configFile);
events = YamlConfiguration.loadConfiguration(Anticheat.getPlugin().getResource("events.yml"));
String medium = config.getString("Events.Level Medium");
String high = config.getString("Events.Level High");
int medValue = config.getString("Events.Medium threshold") != null ? config.getInt("Events.Medium threshold") : -1;
int highValue = config.getString("Events.High threshold") != null ? config.getInt("Events.High threshold") : -1;
System.out.println("Set event null");
config.set("Events", null);
System.out.println(config.get("Events.Level Medium"));
Level mediumLevel = new Level("Medium", medValue != -1 ? medValue : 20, ChatColor.YELLOW);
Level highLevel = new Level("High", highValue != -1 ? highValue : 50, ChatColor.RED);
Level customLevel = new Level("Custom", -1, ChatColor.GREEN);
mediumLevel.addAction(medium != null ? medium : "WARN");
highLevel.addAction(high != null ? high : "KICK");
customLevel.addAction("COMMAND[ban &player;say hello world]");
levels.add(mediumLevel);
levels.add(highLevel);
levels.add(customLevel);
writeEvents();
}
private void writeEvents() {
List<String> list = new ArrayList<String>();
List<String> list2 = new ArrayList<String>();
for(Level level : levels) {
list.add(level.toString());
for(String string : level.getActions()) {
list2.add(level.getName()+": "+string);
}
}
events.set("levels", list);
events.set("actions", list2);
save();
}
private void readEvents() {
levels = new ArrayList<Level>();
for(String string : events.getStringList("levels")) {
Level level = Level.load(string);
levels.add(level);
highestLevel = level.getValue() > highestLevel ? level.getValue() : highestLevel;
}
for(String string : events.getStringList("actions")) {
String name;
String action;
try {
name = string.split(": ")[0];
action = string.split(": ")[1];
} catch (Exception ex) {
Anticheat.getPlugin().getLogger().warning("Couldn't load action '"+string+"' from config. Improper formatting used.");
break;
}
for(Level level : levels) {
if(level.getName().equalsIgnoreCase(name)) {
level.addAction(action);
}
}
}
}
public final void load() {
micromanage.getPlugin().checkConfigs();
boolean secondLoad = config != null;
config = CommentedConfiguration.loadConfig(configFile);
level = YamlConfiguration.loadConfiguration(levelFile);
magic = YamlConfiguration.loadConfiguration(magicFile);
events = YamlConfiguration.loadConfiguration(eventsFile);
language = new Language(YamlConfiguration.loadConfiguration(langFile), langFile);
updateFolder = Bukkit.getUpdateFolder();
@@ -176,13 +241,9 @@ public class Configuration {
alertXRay = getBoolean("XRay.Alert when xray is found", false);
fileLogLevel = getInt("System.File log level", 1);
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);
eventChains = getBoolean("System.Event Chains", true);
eventMed = getString("Events.Level Medium", "WARN");
eventHigh = getString("Events.Level High", "KICK");
chatActionKick = getString("Chat.Kick Action", "KICK");
chatActionBan = getString("Chat.Ban Action", "BAN");
chatSpam = getBoolean("Chat.Block chat spam", true);
@@ -218,6 +279,10 @@ public class Configuration {
}
exemptWorlds = config.getStringList("Disable in");
if(secondLoad) {
readEvents();
}
// End pulling values from config
save();
magicInstance = new Magic(getMagic(), this, CommentedConfiguration.loadConfiguration(micromanage.getPlugin().getResource("magic.yml")));
@@ -259,7 +324,7 @@ public class Configuration {
try {
config.save(configFile);
} catch (IOException ex) {
Logger.getLogger(Configuration.class.getName()).log(Level.SEVERE, null, ex);
Logger.getLogger(Configuration.class.getName()).severe(ex.toString());
}
}
@@ -287,7 +352,7 @@ public class Configuration {
try {
level.save(levelFile);
} catch (IOException ex) {
Logger.getLogger(Configuration.class.getName()).log(Level.SEVERE, null, ex);
Logger.getLogger(Configuration.class.getName()).severe(ex.toString());
}
}
@@ -295,7 +360,7 @@ public class Configuration {
try {
newMagic.save(magicFile);
} catch (IOException ex) {
Logger.getLogger(Configuration.class.getName()).log(Level.SEVERE, null, ex);
Logger.getLogger(Configuration.class.getName()).severe(ex.toString());
}
}
@@ -29,9 +29,8 @@ import org.bukkit.ChatColor;
import org.bukkit.configuration.file.FileConfiguration;
public class Language {
private static String[] medAlert = null;
private static String[] highAlert = null;
private static String[] warning = null;
private static List<String> alert = null;
private static List<String> warning = null;
private static String banReason = null;
private static String banBroadcast = null;
private static String kickReason = null;
@@ -45,25 +44,31 @@ public class Language {
private static final ChatColor GOLD = ChatColor.GOLD;
private static final ChatColor GRAY = ChatColor.GRAY;
private FileConfiguration file;
private File f;
public Language(FileConfiguration file, File f) {
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 + "]----------------------";
for (int i = 0; i < temp.length; i++) {
medAlert[i + 1] = GRAY + temp[i];
this.f = f;
// Begin medium_alert / high_alert -> alert swap
List<String> list = file.getStringList("alert.medium_alert");
if(list != null && list.size() > 0) {
list.set(1, "&player has just entered the &level hack level.");
file.set("alert", null);
file.set("alert", list);
save();
}
medAlert[medAlert.length - 1] = GOLD + "-----------------------------------------------------";
temp = file.getList("alert.high_alert").toArray(new String[file.getList("alert.high_alert").size()]);
highAlert = new String[temp.length + 2];
highAlert[0] = GOLD + "----------------------[" + GRAY + "AntiCheat" + GOLD + "]----------------------";
for (int i = 0; i < temp.length; i++) {
highAlert[i + 1] = GRAY + temp[i];
// End swap
List<String> temp = file.getStringList("alert");
alert = new ArrayList<String>();
alert.add(GOLD + "----------------------[" + GRAY + "AntiCheat" + GOLD + "]----------------------");
for (int i = 0; i < temp.size(); i++) {
alert.add(GRAY + temp.get(i));
}
highAlert[highAlert.length - 1] = GOLD + "-----------------------------------------------------";
alert.add(GOLD + "-----------------------------------------------------");
warning = file.getList("warning.player_warning").toArray(new String[file.getList("warning.player_warning").size()]);
warning = file.getStringList("warning.player_warning");
banReason = getString("ban.ban_reason", "Banned by AntiCheat");
banBroadcast = getString("ban.ban_broadcast", "[AntiCheat] &player was banned for hacking.");
kickReason = getString("kick.kick_reason", "Kicked by AntiCheat");
@@ -73,6 +78,10 @@ public class Language {
chatBanReason = getString("chat.ban_reason", "Banned for spamming");
chatKickBroadcast = getString("chat.kick_broadcast", "[AntiCheat] &player was kicked for spamming");
chatBanBroadcast = getString("chat.ban_broadcast", "[AntiCheat] &player was banned for spamming");
save();
}
private void save() {
try {
file.save(f);
} catch (IOException ex) {
@@ -80,16 +89,12 @@ public class Language {
}
}
public List<String> getMediumAlert() {
return new ArrayList<String>(Arrays.asList(medAlert));
}
public List<String> getHighAlert() {
return new ArrayList<String>(Arrays.asList(highAlert));
public List<String> getAlert() {
return alert;
}
public List<String> getWarning() {
return new ArrayList<String>(Arrays.asList(warning));
return warning;
}
public String getBanReason() {
@@ -0,0 +1,105 @@
/*
* AntiCheat for Bukkit.
* Copyright (C) 2012-2013 AntiCheat Team | http://gravitydevelopment.net
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
package net.h31ix.anticheat.util;
import net.h31ix.anticheat.Anticheat;
import org.bukkit.ChatColor;
import java.util.ArrayList;
import java.util.List;
public class Level {
private String name;
private int value;
private ChatColor color = ChatColor.RED;
private boolean customColor = false;
private List<String> actions = new ArrayList<String>();
public Level(String name, int value) {
this.name = name;
this.value = value;
}
public Level(String name, int value, ChatColor color) {
this(name, value);
this.color = color;
customColor = true;
}
public Level(String name, int value, String color) {
this(name, value);
ChatColor c = ChatColor.valueOf(color);
if(c == null) {
Anticheat.getPlugin().getLogger().warning("Event '"+name+"' was initialized with the color '"+color+"' which is invalid.");
Anticheat.getPlugin().getLogger().warning("This event will not run properly. See http://jd.bukkit.org/apidocs/org/bukkit/ChatColor.html#enum_constant_summary for a list of valid colors");
} else {
this.color = c;
customColor = true;
}
}
public void addAction(String string) {
actions.add(string);
}
public void setValue(int value) {
this.value = value;
}
public String getName() {
return name;
}
public int getValue() {
return value;
}
public ChatColor getColor() {
return color;
}
public List<String> getActions() {
return actions;
}
public static Level load(String string) {
try {
string = string.trim();
String name = string.split(":")[0];
int level = Integer.parseInt(string.split(":")[1]);
if(string.split(":").length == 3) {
String color = string.split(":")[2];
return new Level(name, level, color);
} else {
return new Level(name, level);
}
} catch (Exception ex) {
Anticheat.getPlugin().getLogger().warning("An event was intialized with an invalid string: '"+string+"'");
Anticheat.getPlugin().getLogger().warning("The proper format is: 'name: threshold, color' such as 'high: 50, RED'");
Anticheat.getPlugin().getLogger().warning("This event will NOT run. ("+ex.getMessage()+")");
return null;
}
}
@Override
public String toString() {
return name + " : " + value + (customColor ? " : "+color.name() : "");
}
}
@@ -50,12 +50,6 @@ public class CommentedConfiguration extends YamlConfiguration {
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?");