diff --git a/src/main/java/net/h31ix/anticheat/AntiCheat.java b/src/main/java/net/h31ix/anticheat/AntiCheat.java index 158a8fd..f37d5a7 100644 --- a/src/main/java/net/h31ix/anticheat/AntiCheat.java +++ b/src/main/java/net/h31ix/anticheat/AntiCheat.java @@ -41,6 +41,7 @@ import org.bukkit.plugin.java.JavaPlugin; public class AntiCheat extends JavaPlugin { private static AnticheatManager manager; + private static AntiCheat plugin; private static List eventList = new ArrayList(); private static boolean update = false; private static Configuration config; @@ -52,6 +53,7 @@ public class AntiCheat extends JavaPlugin { @Override public void onEnable() { + plugin = this; manager = new AnticheatManager(this, getLogger()); eventList.add(new PlayerListener()); eventList.add(new BlockListener()); @@ -85,11 +87,8 @@ public class AntiCheat extends JavaPlugin { @Override public void onDisable() { - if (!config.shouldSyncUsers()) { - for (User user : manager.getUserManager().getUsers()) { - config.getLevels().saveLevel(user.getName(), user.getLevel()); - } - } + verboseLog("Saving user levels..."); + config.getLevels().saveLevelsFromUsers(getManager().getUserManager().getUsers()); AnticheatManager.close(); getServer().getScheduler().cancelTasks(this); @@ -159,9 +158,6 @@ public class AntiCheat extends JavaPlugin { private void setupEnterprise() { if(config.getConfig().enterprise.getValue()) { - config.getEnterprise().database.connect(); - verboseLog("Connected to the database."); - if(config.getEnterprise().loggingEnabled.getValue()) { config.getEnterprise().database.cleanEvents(); } @@ -171,20 +167,18 @@ public class AntiCheat extends JavaPlugin { private void restoreLevels() { for (Player player : getServer().getOnlinePlayers()) { String name = player.getName(); - if (!config.shouldSyncUsers()) { - manager.getUserManager().addUser(new User(player.getName(), config.getLevels().getLevel(name))); - } else { - User user = new User(player.getName()); - user.setIsWaitingOnLevelSync(true); - config.getEnterprise().database.syncFrom(user); - manager.getUserManager().addUser(user); - } - verboseLog("Data for " + player.getName() + " loaded"); + + User user = new User(name); + user.setIsWaitingOnLevelSync(true); + config.getLevels().loadLevelToUser(user); + + manager.getUserManager().addUser(user); + verboseLog("Data for " + name + " loaded"); } } public static AntiCheat getPlugin() { - return manager.getPlugin(); + return plugin; } public static AnticheatManager getManager() { @@ -228,6 +222,8 @@ public class AntiCheat extends JavaPlugin { } public void verboseLog(final String string) { - getLogger().info(string); + if (verbose) { + getLogger().info(string); + } } } diff --git a/src/main/java/net/h31ix/anticheat/CommandHandler.java b/src/main/java/net/h31ix/anticheat/CommandHandler.java index 4789393..e828716 100644 --- a/src/main/java/net/h31ix/anticheat/CommandHandler.java +++ b/src/main/java/net/h31ix/anticheat/CommandHandler.java @@ -317,7 +317,7 @@ public class CommandHandler implements CommandExecutor { User user = AntiCheat.getManager().getUserManager().getUser(args[1]); boolean cont = false; if (user == null) { - if ((user = AntiCheat.getManager().getUserManager().loadUserFromFile(args[1])) == null) { + if ((user = AntiCheat.getManager().getUserManager().getUser(args[1])) == null) { cs.sendMessage(RED + "Player: " + WHITE + args[1] + RED + " not found."); } else { cont = true; @@ -353,7 +353,7 @@ public class CommandHandler implements CommandExecutor { String name = user.getName(); int pages = (int) Math.ceil(((float) types.size()) / 6); - Level level = CONFIG.getEvents().getUserLevel(user.getLevel()); + Level level = user.getNamedLevel(); String levelString = ChatColor.GREEN + "Low"; if(level != null) { @@ -403,7 +403,7 @@ public class CommandHandler implements CommandExecutor { public boolean onCommand(CommandSender cs, Command cmd, String alias, String[] args) { if (args.length == 3) { if (args[0].equalsIgnoreCase("report")) { - for(Level level : CONFIG.getEvents().levels) { + for(Level level : CONFIG.getEvents().getLevels()) { if(args[1].equalsIgnoreCase(level.getName())) { handleReport(cs, args); return true; diff --git a/src/main/java/net/h31ix/anticheat/config/Configuration.java b/src/main/java/net/h31ix/anticheat/config/Configuration.java index 7b47d63..10133d1 100644 --- a/src/main/java/net/h31ix/anticheat/config/Configuration.java +++ b/src/main/java/net/h31ix/anticheat/config/Configuration.java @@ -21,51 +21,82 @@ package net.h31ix.anticheat.config; import net.h31ix.anticheat.AntiCheat; import net.h31ix.anticheat.config.files.*; import net.h31ix.anticheat.config.files.Magic; +import net.h31ix.anticheat.config.holders.mysql.MySQLEventsHolder; +import net.h31ix.anticheat.config.holders.mysql.MySQLLevelsHolder; +import net.h31ix.anticheat.config.holders.mysql.MySQLRulesHolder; +import net.h31ix.anticheat.config.holders.yaml.YamlLevelsHolder; +import net.h31ix.anticheat.config.holders.yaml.YamlEventsHolder; +import net.h31ix.anticheat.config.holders.yaml.YamlRulesHolder; +import net.h31ix.anticheat.config.providers.Events; +import net.h31ix.anticheat.config.providers.Levels; +import net.h31ix.anticheat.config.providers.Rules; + +import java.util.ArrayList; public class Configuration { - private final AntiCheat plugin; - private Config config; - private Events events; - private Lang lang; private Enterprise enterprise; - - private Levels levels; - + private Lang lang; private Magic magic; + private Events events; + private Levels levels; + private Rules rules; + + private ArrayList flatfiles; + private ArrayList dbfiles; + public Configuration(AntiCheat plugin) { - this.plugin = plugin; config = new Config(plugin, this); - events = new Events(plugin, this); - lang = new Lang(plugin, this); enterprise = new Enterprise(plugin, this); - - levels = new Levels(plugin, this); - + lang = new Lang(plugin, this); magic = new Magic(plugin, this); - for(ConfigurationFile file : new ConfigurationFile[]{config, events, lang, enterprise, levels, magic}) { + flatfiles = new ArrayList(){{ add(config); add(enterprise); add(lang); add(magic); }}; + dbfiles = new ArrayList(); + + // The following values can be configuration from a database, or from flatfile. + if(config.enterprise.getValue() && enterprise.configEvents.getValue()) { + events = new MySQLEventsHolder(this); + dbfiles.add((MySQLEventsHolder)events); + } else { + events = new YamlEventsHolder(plugin, this); + flatfiles.add((YamlEventsHolder)events); + } + + if(config.enterprise.getValue() && enterprise.configRules.getValue()) { + rules = new MySQLRulesHolder(this); + dbfiles.add((MySQLRulesHolder)rules); + } else { + rules = new YamlRulesHolder(plugin, this); + flatfiles.add((YamlRulesHolder)rules); + } + + if(config.enterprise.getValue() && enterprise.configEvents.getValue()) { + levels = new MySQLLevelsHolder(this); + dbfiles.add((MySQLLevelsHolder)levels); + } else { + levels = new YamlLevelsHolder(plugin, this); + flatfiles.add((YamlLevelsHolder)levels); + } + // End + + for(ConfigurationFile file : flatfiles) { + file.save(); checkReload(file); } } public void load() { - for(ConfigurationFile file : new ConfigurationFile[]{config, events, lang, enterprise, levels, magic}) { + for(ConfigurationFile file : flatfiles) { file.load(); checkReload(file); } - } - - public void save() { - config.save(); - events.save(); - lang.save(); - enterprise.save(); - levels.save(); - magic.save(); + for(ConfigurationTable table : dbfiles) { + table.load(); + } } private void checkReload(ConfigurationFile file) { @@ -83,6 +114,10 @@ public class Configuration { return events; } + public Rules getRules() { + return rules; + } + public Lang getLang() { return lang; } @@ -98,12 +133,4 @@ public class Configuration { public Magic getMagic() { return magic; } - - public boolean shouldSyncEvents() { - return getConfig().enterprise.getValue() && getEnterprise().loggingEnabled.getValue(); - } - - public boolean shouldSyncUsers() { - return getConfig().enterprise.getValue() && getEnterprise().usersEnabled.getValue(); - } } diff --git a/src/main/java/net/h31ix/anticheat/config/ConfigurationTable.java b/src/main/java/net/h31ix/anticheat/config/ConfigurationTable.java new file mode 100644 index 0000000..3973be9 --- /dev/null +++ b/src/main/java/net/h31ix/anticheat/config/ConfigurationTable.java @@ -0,0 +1,68 @@ +/* + * 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 . + */ + +package net.h31ix.anticheat.config; + +import net.h31ix.anticheat.util.enterprise.Database; + +import java.sql.Connection; + +public class ConfigurationTable { + + private final String table; + private final Configuration config; + + private Database database; + private String prefix; + private String serverName; + + public ConfigurationTable(Configuration config, String table) { + this.config = config; + this.table = table; + + load(); + } + + public void load() { + this.database = config.getEnterprise().database; + this.prefix = database.getPrefix(); + this.serverName = config.getEnterprise().serverName.getValue(); + + open(); + } + + public void open() { + // Nothing to do + } + + public Database getDatabase() { + return database; + } + + public String getFullTable() { + return prefix + table; + } + + public String getServerName() { + return serverName; + } + + public Connection getConnection() { + return database.getConnection(); + } +} diff --git a/src/main/java/net/h31ix/anticheat/config/files/Enterprise.java b/src/main/java/net/h31ix/anticheat/config/files/Enterprise.java index f209a8c..b8c1ebe 100644 --- a/src/main/java/net/h31ix/anticheat/config/files/Enterprise.java +++ b/src/main/java/net/h31ix/anticheat/config/files/Enterprise.java @@ -21,8 +21,6 @@ package net.h31ix.anticheat.config.files; import net.h31ix.anticheat.AntiCheat; import net.h31ix.anticheat.config.Configuration; import net.h31ix.anticheat.config.ConfigurationFile; -import net.h31ix.anticheat.manage.CheckType; -import net.h31ix.anticheat.manage.User; import net.h31ix.anticheat.util.enterprise.Database; public class Enterprise extends ConfigurationFile { @@ -35,7 +33,10 @@ public class Enterprise extends ConfigurationFile { public ConfigValue loggingLife; public ConfigValue loggingInterval; - public ConfigValue usersEnabled; + public ConfigValue syncUsers; + + public ConfigValue configEvents; + public ConfigValue configRules; public Database database; @@ -51,7 +52,10 @@ public class Enterprise extends ConfigurationFile { loggingLife = new ConfigValue("logging.life"); loggingInterval = new ConfigValue("logging.interval"); - usersEnabled = new ConfigValue("users.enable"); + syncUsers = new ConfigValue("sync.users"); + + configEvents = new ConfigValue("config.events"); + configRules = new ConfigValue("config.rules"); ConfigValue databaseType = new ConfigValue("database.type"); ConfigValue databaseHostname = new ConfigValue("database.hostname"); @@ -74,5 +78,7 @@ public class Enterprise extends ConfigurationFile { loggingInterval.getValue(), loggingLife.getValue() ); + + database.connect(); } } diff --git a/src/main/java/net/h31ix/anticheat/config/files/Events.java b/src/main/java/net/h31ix/anticheat/config/files/Events.java deleted file mode 100644 index a558562..0000000 --- a/src/main/java/net/h31ix/anticheat/config/files/Events.java +++ /dev/null @@ -1,121 +0,0 @@ -/* - * 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 . - */ - -package net.h31ix.anticheat.config.files; - -import net.h31ix.anticheat.AntiCheat; -import net.h31ix.anticheat.config.Configuration; -import net.h31ix.anticheat.config.ConfigurationFile; -import net.h31ix.anticheat.util.Level; -import net.h31ix.anticheat.util.rule.Rule; - -import java.util.ArrayList; -import java.util.Collections; -import java.util.Comparator; -import java.util.List; - -public class Events extends ConfigurationFile { - - public static final String FILENAME = "events.yml"; - - public List levels; - public List rules; - - public int highestLevel; - - public Events(AntiCheat plugin, Configuration config) { - super(plugin, config, FILENAME); - } - - @Override - public void open() { - ConfigValue> levels = new ConfigValue>("levels"); - ConfigValue> actions = new ConfigValue>("actions"); - ConfigValue> rules = new ConfigValue>("rules"); - - // Convert levels list to Levels - this.levels = new ArrayList(); - for(String string : levels.getValue()) { - Level level = Level.load(string); - if(level == null || level.getValue() < 0) { - continue; - } - - this.levels.add(level); - highestLevel = level.getValue() > highestLevel ? level.getValue() : highestLevel; - } - - // Convert actions list and add to corresponding Levels - for(String string : actions.getValue()) { - 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 format used."); - break; - } - for(Level level : this.levels) { - if(level.getName().equalsIgnoreCase(name)) { - level.addAction(action); - } - } - } - - // Convert rules list to Rules - this.rules = new ArrayList(); - List tempRules = rules.getValue(); - for(int i=0;i() { - public int compare(Level l1, Level l2) { - if (l1.getValue() == l2.getValue()) { - return 0; - } else if (l1.getValue() < l2.getValue()) { - return -1; - } - return 1; - } - }); - } - - public Level getUserLevel(int value) { - for(int i=0;i= levels.get(i).getValue() && value < levels.get(i+1).getValue()) return levels.get(i); - } - return null; - } -} diff --git a/src/main/java/net/h31ix/anticheat/config/holders/mysql/MySQLEventsHolder.java b/src/main/java/net/h31ix/anticheat/config/holders/mysql/MySQLEventsHolder.java new file mode 100644 index 0000000..db32571 --- /dev/null +++ b/src/main/java/net/h31ix/anticheat/config/holders/mysql/MySQLEventsHolder.java @@ -0,0 +1,87 @@ +/* + * 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 . + */ + +package net.h31ix.anticheat.config.holders.mysql; + +import net.h31ix.anticheat.config.Configuration; +import net.h31ix.anticheat.config.ConfigurationTable; +import net.h31ix.anticheat.config.providers.Events; +import net.h31ix.anticheat.util.Level; + +import java.sql.ResultSet; +import java.sql.SQLException; +import java.util.ArrayList; +import java.util.Arrays; +import java.util.List; + +public class MySQLEventsHolder extends ConfigurationTable implements Events { + + private static final String TABLE = "events"; + + private List levels; + private int highestLevel; + + public MySQLEventsHolder(Configuration config) { + super(config, TABLE); + } + + @Override + public void open() { + highestLevel = 0; + levels = new ArrayList(); + + String sqlCreate= "CREATE TABLE IF NOT EXISTS " + getFullTable() + "(" + + " `id` INT NOT NULL," + + " `name` VARCHAR(45) NOT NULL," + + " `level` INT NOT NULL," + + " `color` VARCHAR(45) NOT NULL," + + " `actions` VARCHAR(45) NOT NULL," + + " PRIMARY KEY (`id`));"; + String sqlLoad = "SELECT * FROM " + getFullTable(); + + try { + getConnection().prepareStatement(sqlCreate).executeUpdate(); + getConnection().commit(); + + ResultSet set = getConnection().prepareStatement(sqlLoad).executeQuery(); + while(set.next()) { + String name = set.getString("name"); + int level = set.getInt("level"); + String color = set.getString("color"); + List actions = Arrays.asList(set.getString("actions").split(",")); + + levels.add(new Level(name, level, color, actions)); + if(level > highestLevel) { + highestLevel = level; + } + } + } catch (SQLException e) { + e.printStackTrace(); + } + } + + @Override + public List getLevels() { + return levels; + } + + @Override + public int getHighestLevel() { + return highestLevel; + } +} diff --git a/src/main/java/net/h31ix/anticheat/config/holders/mysql/MySQLLevelsHolder.java b/src/main/java/net/h31ix/anticheat/config/holders/mysql/MySQLLevelsHolder.java new file mode 100644 index 0000000..5b59aa9 --- /dev/null +++ b/src/main/java/net/h31ix/anticheat/config/holders/mysql/MySQLLevelsHolder.java @@ -0,0 +1,152 @@ +/* + * 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 . + */ + +package net.h31ix.anticheat.config.holders.mysql; + +import net.h31ix.anticheat.AntiCheat; +import net.h31ix.anticheat.config.Configuration; +import net.h31ix.anticheat.config.ConfigurationTable; +import net.h31ix.anticheat.config.providers.Levels; +import net.h31ix.anticheat.manage.User; +import org.bukkit.Bukkit; + +import java.sql.PreparedStatement; +import java.sql.ResultSet; +import java.sql.SQLException; +import java.util.List; + +public class MySQLLevelsHolder extends ConfigurationTable implements Levels { + + private static final String TABLE = "levels"; + + private String sqlSave; + private String sqlLoad; + + public MySQLLevelsHolder(Configuration config) { + super(config, TABLE); + } + + @Override + public void open() { + sqlSave = "INSERT INTO " + getFullTable() + + " (user, level, last_update_server) " + + "VALUES (?, ?, ?) " + + "ON DUPLICATE KEY UPDATE " + + "level = IF(last_update = ?, ?, level+?), " + + "last_update=CURRENT_TIMESTAMP, " + + "last_update_server=?"; + + sqlLoad = "SELECT level, last_update FROM " + getFullTable() + " " + + "WHERE user = ?"; + + String sqlCreate = "CREATE TABLE IF NOT EXISTS " + getFullTable() + "(" + + " `user` VARCHAR(45) NOT NULL," + + " `level` INT NOT NULL," + + " `last_update` TIMESTAMP NOT NULL DEFAULT NOW()," + + " `last_update_server` VARCHAR(45) NOT NULL," + + " PRIMARY KEY (`user`));"; + + try { + getConnection().prepareStatement(sqlCreate).executeUpdate(); + getConnection().commit(); + } catch (SQLException e) { + e.printStackTrace(); + } + } + + @Override + public void saveLevelFromUser(final User user) { + Bukkit.getScheduler().runTaskAsynchronously(AntiCheat.getPlugin(), new Runnable(){ + @Override + public void run() { + if (!user.isWaitingOnLevelSync()) { + AntiCheat.debugLog("Syncing to "+user.getName()+" value "+user.getLevel()); + try { + PreparedStatement statement = getConnection().prepareStatement(sqlSave); + statement.setString(1, user.getName()); + statement.setInt(2, user.getLevel()); + statement.setString(3, getServerName()); + statement.setTimestamp(4, user.getLevelSyncTimestamp()); + statement.setInt(5, user.getLevel()); + statement.setInt(6, user.getLevel()); + statement.setString(7, getServerName()); + + statement.executeUpdate(); + } catch (SQLException e) { + e.printStackTrace(); + } + } + AntiCheat.getManager().getUserManager().remove(user); + } + }); + } + + @Override + public void loadLevelToUser(final User user) { + Bukkit.getScheduler().runTaskAsynchronously(AntiCheat.getPlugin(), new Runnable(){ + @Override + public void run() { + try { + PreparedStatement statement = getConnection().prepareStatement(sqlLoad); + statement.setString(1, user.getName()); + + ResultSet set = statement.executeQuery(); + + boolean has = false; + while(set.next()) { + has = true; + AntiCheat.debugLog("Syncing from "+user.getName()+" value "+set.getInt("level")); + user.setLevel(set.getInt("level")); + user.setLevelSyncTimestamp(set.getTimestamp("last_update")); + } + if(!has) user.setLevel(0); + } catch (SQLException e) { + e.printStackTrace(); + } + } + }); + } + + @Override + public void saveLevelsFromUsers(List users) { + try { + PreparedStatement statement = getConnection().prepareStatement(sqlSave); + for(User user : users) { + if (!user.isWaitingOnLevelSync()) { + try { + statement.setString(1, user.getName()); + statement.setInt(2, user.getLevel()); + statement.setString(3, getServerName()); + statement.setTimestamp(4, user.getLevelSyncTimestamp()); + statement.setInt(5, user.getLevel()); + statement.setInt(6, user.getLevel()); + statement.setString(7, getServerName()); + + statement.addBatch(); + } catch (SQLException e) { + e.printStackTrace(); + } + } + } + statement.executeBatch(); + getConnection().commit(); + } catch (SQLException e) { + e.printStackTrace(); + } + } +} diff --git a/src/main/java/net/h31ix/anticheat/config/holders/mysql/MySQLRulesHolder.java b/src/main/java/net/h31ix/anticheat/config/holders/mysql/MySQLRulesHolder.java new file mode 100644 index 0000000..f7380d8 --- /dev/null +++ b/src/main/java/net/h31ix/anticheat/config/holders/mysql/MySQLRulesHolder.java @@ -0,0 +1,74 @@ +/* + * 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 . + */ + +package net.h31ix.anticheat.config.holders.mysql; + +import net.h31ix.anticheat.AntiCheat; +import net.h31ix.anticheat.config.Configuration; +import net.h31ix.anticheat.config.ConfigurationTable; +import net.h31ix.anticheat.config.providers.Rules; +import net.h31ix.anticheat.util.rule.Rule; + +import java.sql.ResultSet; +import java.sql.SQLException; +import java.util.ArrayList; +import java.util.List; + +public class MySQLRulesHolder extends ConfigurationTable implements Rules { + + private static final String TABLE = "rules"; + + private List rules; + + public MySQLRulesHolder(Configuration config) { + super(config, TABLE); + } + + @Override + public void open() { + String sqlCreate = "CREATE TABLE IF NOT EXISTS " + getFullTable() + "(" + + " `id` INT NOT NULL AUTO_INCREMENT," + + " `rule` VARCHAR(45) NOT NULL," + + " PRIMARY KEY (`id`));"; + + String sqlLoad = "SELECT * FROM " + getFullTable(); + + try { + getConnection().prepareStatement(sqlCreate).executeUpdate(); + getConnection().commit(); + + this.rules = new ArrayList(); + ResultSet set = getConnection().prepareStatement(sqlLoad).executeQuery(); + while(set.next()) { + String string = set.getString("rule"); + Rule rule = Rule.load(string); + if(rule != null) { + this.rules.add(rule); + } else { + AntiCheat.getPlugin().getLogger().warning("Couldn't load rule '"+string+"' from the database. Improper format used."); + } + } + } catch (SQLException e) { + e.printStackTrace(); + } + } + + public List getRules() { + return rules; + } +} diff --git a/src/main/java/net/h31ix/anticheat/config/holders/yaml/YamlEventsHolder.java b/src/main/java/net/h31ix/anticheat/config/holders/yaml/YamlEventsHolder.java new file mode 100644 index 0000000..65a930d --- /dev/null +++ b/src/main/java/net/h31ix/anticheat/config/holders/yaml/YamlEventsHolder.java @@ -0,0 +1,84 @@ +/* + * 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 . + */ + +package net.h31ix.anticheat.config.holders.yaml; + +import net.h31ix.anticheat.AntiCheat; +import net.h31ix.anticheat.config.Configuration; +import net.h31ix.anticheat.config.ConfigurationFile; +import net.h31ix.anticheat.config.providers.Events; +import net.h31ix.anticheat.config.providers.Rules; +import net.h31ix.anticheat.util.Level; +import net.h31ix.anticheat.util.rule.Rule; + +import java.util.ArrayList; +import java.util.Collections; +import java.util.Comparator; +import java.util.List; + +public class YamlEventsHolder extends ConfigurationFile implements Events { + + public static final String FILENAME = "events.yml"; + + private List levels; + + private int highestLevel; + + public YamlEventsHolder(AntiCheat plugin, Configuration config) { + super(plugin, config, FILENAME); + } + + @Override + public List getLevels() { + return levels; + } + + @Override + public int getHighestLevel() { + return highestLevel; + } + + @Override + public void open() { + ConfigValue> levels = new ConfigValue>("events"); + + // Convert levels list to Levels + this.levels = new ArrayList(); + for(String string : levels.getValue()) { + Level level = Level.load(string); + if(level == null || level.getValue() < 0) { + continue; + } + + this.levels.add(level); + highestLevel = level.getValue() > highestLevel ? level.getValue() : highestLevel; + } + + // Sort levels + Collections.sort(this.levels, new Comparator() { + public int compare(Level l1, Level l2) { + if (l1.getValue() == l2.getValue()) { + return 0; + } else if (l1.getValue() < l2.getValue()) { + return -1; + } + return 1; + } + }); + } +} diff --git a/src/main/java/net/h31ix/anticheat/config/files/Levels.java b/src/main/java/net/h31ix/anticheat/config/holders/yaml/YamlLevelsHolder.java similarity index 59% rename from src/main/java/net/h31ix/anticheat/config/files/Levels.java rename to src/main/java/net/h31ix/anticheat/config/holders/yaml/YamlLevelsHolder.java index 11ec012..0de6fd3 100644 --- a/src/main/java/net/h31ix/anticheat/config/files/Levels.java +++ b/src/main/java/net/h31ix/anticheat/config/holders/yaml/YamlLevelsHolder.java @@ -16,30 +16,52 @@ * along with this program. If not, see . */ -package net.h31ix.anticheat.config.files; +package net.h31ix.anticheat.config.holders.yaml; import net.h31ix.anticheat.AntiCheat; import net.h31ix.anticheat.config.Configuration; import net.h31ix.anticheat.config.ConfigurationFile; +import net.h31ix.anticheat.config.providers.Levels; +import net.h31ix.anticheat.manage.User; -public class Levels extends ConfigurationFile { +import java.util.List; + +public class YamlLevelsHolder extends ConfigurationFile implements Levels { public static final String FILENAME = "data/levels.yml"; - public Levels(AntiCheat plugin, Configuration config) { + public YamlLevelsHolder(AntiCheat plugin, Configuration config) { super(plugin, config, FILENAME, false); } - public int getLevel(String name) { + @Override + public void loadLevelToUser(User user) { + user.setLevel(getLevel(user.getName())); + } + + @Override + public void saveLevelFromUser(User user) { + saveLevel(user.getName(), user.getLevel()); + AntiCheat.getManager().getUserManager().remove(user); + } + + @Override + public void saveLevelsFromUsers(List users) { + for(User user : users) { + saveLevelFromUser(user); + } + } + + private int getLevel(String name) { ConfigValue level = new ConfigValue(name, false); if(level.hasValue()) { return level.getValue(); } else { - return -1; + return 0; } } - public void saveLevel(String name, int level) { + private void saveLevel(String name, int level) { new ConfigValue(name, false).setValue(new Integer(level)); } } diff --git a/src/main/java/net/h31ix/anticheat/config/holders/yaml/YamlRulesHolder.java b/src/main/java/net/h31ix/anticheat/config/holders/yaml/YamlRulesHolder.java new file mode 100644 index 0000000..cc1923c --- /dev/null +++ b/src/main/java/net/h31ix/anticheat/config/holders/yaml/YamlRulesHolder.java @@ -0,0 +1,72 @@ +/* + * 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 . + */ + +package net.h31ix.anticheat.config.holders.yaml; + +import net.h31ix.anticheat.AntiCheat; +import net.h31ix.anticheat.config.Configuration; +import net.h31ix.anticheat.config.ConfigurationFile; +import net.h31ix.anticheat.config.providers.Events; +import net.h31ix.anticheat.config.providers.Rules; +import net.h31ix.anticheat.util.Level; +import net.h31ix.anticheat.util.rule.Rule; + +import java.util.ArrayList; +import java.util.Collections; +import java.util.Comparator; +import java.util.List; + +public class YamlRulesHolder extends ConfigurationFile implements Rules { + + public static final String FILENAME = "events.yml"; + + private List rules; + + public YamlRulesHolder(AntiCheat plugin, Configuration config) { + super(plugin, config, FILENAME); + } + + @Override + public List getRules() { + return rules; + } + + @Override + public void open() { + ConfigValue> rules = new ConfigValue>("rules"); + + // Convert rules list to Rules + this.rules = new ArrayList(); + List tempRules = rules.getValue(); + for(int i=0;i. + */ + +package net.h31ix.anticheat.config.providers; + +import net.h31ix.anticheat.util.Level; + +import java.util.List; + +public interface Events { + + public List getLevels(); + + public int getHighestLevel(); +} diff --git a/src/main/java/net/h31ix/anticheat/config/providers/Levels.java b/src/main/java/net/h31ix/anticheat/config/providers/Levels.java new file mode 100644 index 0000000..6401151 --- /dev/null +++ b/src/main/java/net/h31ix/anticheat/config/providers/Levels.java @@ -0,0 +1,32 @@ +/* + * 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 . + */ + +package net.h31ix.anticheat.config.providers; + +import net.h31ix.anticheat.manage.User; + +import java.util.List; + +public interface Levels { + + public void loadLevelToUser(User user); + + public void saveLevelFromUser(User user); + + public void saveLevelsFromUsers(List users); +} diff --git a/src/main/java/net/h31ix/anticheat/config/providers/Rules.java b/src/main/java/net/h31ix/anticheat/config/providers/Rules.java new file mode 100644 index 0000000..6ec8f15 --- /dev/null +++ b/src/main/java/net/h31ix/anticheat/config/providers/Rules.java @@ -0,0 +1,29 @@ +/* + * 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 . + */ + + +package net.h31ix.anticheat.config.providers; + +import net.h31ix.anticheat.util.rule.Rule; + +import java.util.List; + +public interface Rules { + + public List getRules(); +} diff --git a/src/main/java/net/h31ix/anticheat/event/EventListener.java b/src/main/java/net/h31ix/anticheat/event/EventListener.java index 3fe5947..06f7468 100644 --- a/src/main/java/net/h31ix/anticheat/event/EventListener.java +++ b/src/main/java/net/h31ix/anticheat/event/EventListener.java @@ -53,7 +53,7 @@ public class EventListener implements Listener { // Ignore plugins that are creating NPCs with no names (why the hell) if (user != null && user.getName() != null) { type.logUse(user); - if (CONFIG.shouldSyncEvents()) { + if (CONFIG.getConfig().enterprise.getValue() && CONFIG.getEnterprise().loggingEnabled.getValue()) { CONFIG.getEnterprise().database.logEvent(user, type); } if (CONFIG.getConfig().fileLogLevel.getValue() == 2 && type.getUses(user.getName()) % 10 == 0) { diff --git a/src/main/java/net/h31ix/anticheat/event/PlayerListener.java b/src/main/java/net/h31ix/anticheat/event/PlayerListener.java index aa04262..2ca0550 100644 --- a/src/main/java/net/h31ix/anticheat/event/PlayerListener.java +++ b/src/main/java/net/h31ix/anticheat/event/PlayerListener.java @@ -138,8 +138,7 @@ public class PlayerListener extends EventListener { @EventHandler(ignoreCancelled = true) public void onPlayerChat(AsyncPlayerChatEvent event) { Player player = event.getPlayer(); - log("asdf", player, CheckType.SPRINT); - getUserManager().getUser(player.getName()).increaseLevel(CheckType.SPRINT); + if (getCheckManager().willCheck(player, CheckType.SPAM) && getConfig().getConfig().blockChatSpam.getValue()) { CheckResult result = getBackend().checkSpam(player, event.getMessage()); if (result.failed()) { @@ -167,10 +166,11 @@ public class PlayerListener extends EventListener { public void onPlayerQuit(PlayerQuitEvent event) { getBackend().garbageClean(event.getPlayer()); + User user = getUserManager().getUser(event.getPlayer().getName()); + + getConfig().getLevels().saveLevelFromUser(user); + AntiCheat.getManager().addEvent(event.getEventName(), event.getHandlers().getRegisteredListeners()); - if (getConfig().shouldSyncUsers()) { - getConfig().getEnterprise().database.syncTo(getUserManager().getUser(event.getPlayer().getName())); - } } @EventHandler @@ -266,18 +266,11 @@ public class PlayerListener extends EventListener { getBackend().logJoin(player); - if (!getConfig().shouldSyncUsers()) { - if (getUserManager().getUser(player.getName()) == null) { - getUserManager().addUser(new User(player.getName())); - } else { - getUserManager().addUserFromFile(player.getName()); - } - } else { - User user = new User(player.getName()); - user.setIsWaitingOnLevelSync(true); - getConfig().getEnterprise().database.syncFrom(user); - getUserManager().addUser(user); - } + User user = new User(player.getName()); + user.setIsWaitingOnLevelSync(true); + getConfig().getLevels().loadLevelToUser(user); + getUserManager().addUser(user); + if (player.hasMetadata(Utilities.SPY_METADATA)) { for (Player p : player.getServer().getOnlinePlayers()) { if (!Permission.SYSTEM_SPY.get(p)) { diff --git a/src/main/java/net/h31ix/anticheat/manage/Backend.java b/src/main/java/net/h31ix/anticheat/manage/Backend.java index 559adb0..cffafcd 100644 --- a/src/main/java/net/h31ix/anticheat/manage/Backend.java +++ b/src/main/java/net/h31ix/anticheat/manage/Backend.java @@ -114,10 +114,7 @@ public class Backend { public void garbageClean(Player player) { String pN = player.getName(); - User user = manager.getUserManager().getUser(pN); - if (user != null) { - manager.getUserManager().remove(user); - } + blocksDropped.remove(pN); blockTime.remove(pN); movingExempt.remove(pN); diff --git a/src/main/java/net/h31ix/anticheat/manage/User.java b/src/main/java/net/h31ix/anticheat/manage/User.java index 1c9d623..e87a96d 100644 --- a/src/main/java/net/h31ix/anticheat/manage/User.java +++ b/src/main/java/net/h31ix/anticheat/manage/User.java @@ -29,6 +29,7 @@ import org.bukkit.entity.Player; import org.bukkit.inventory.Inventory; import org.bukkit.inventory.ItemStack; +import java.sql.Timestamp; import java.util.*; public class User { @@ -43,6 +44,7 @@ public class User { private Long [] messageTimes = new Long[2]; private boolean isWaitingOnLevelSync; + private Timestamp levelSyncTimestamp; /** * Initialize an AntiCheat user @@ -50,19 +52,7 @@ public class User { * @param name Player's name */ public User(String name) { - this(name, 0); - } - - /** - * Initialize an AntiCheat user with a predefined level - * - * @param name Player's name - * @param level Player's level - */ - public User(String name, int level) { this.name = name; - this.level = level; - this.id = getPlayer().isOnline() ? getPlayer().getEntityId() : -1; } @@ -102,6 +92,16 @@ public class User { return level; } + public Level getNamedLevel() { + List levels = config.getEvents().getLevels(); + for (int i=0;i= levels.get(i).getValue() && level < levels.get(i+1).getValue()) return levels.get(i); + } + return null; + } + /** * Increase the player's level (from failing a check) * @@ -114,21 +114,21 @@ public class User { // Prevent silent mode from increasing the level way too fast return false; } else { - if (level < config.getEvents().highestLevel) { + if (level < config.getEvents().getHighestLevel()) { level++; // Check levels for(Level l : getLevels()) { if(l.getValue() == level) { AntiCheat.getManager().getUserManager().alert(this, l, type); - if(l.getValue() == config.getEvents().highestLevel) { + if(l.getValue() == config.getEvents().getHighestLevel()) { level = l.getValue() - 10; } } } // Execute rules - for(Rule rule : config.getEvents().rules) { + for(Rule rule : config.getRules().getRules()) { rule.check(this, type); } return true; @@ -155,11 +155,11 @@ public class User { public boolean setLevel(int level) { isWaitingOnLevelSync = false; if (level >= 0) { - if (level <= config.getEvents().highestLevel) { + if (level <= config.getEvents().getHighestLevel()) { this.level = level; return true; } else { - this.level = config.getEvents().highestLevel; + this.level = config.getEvents().getHighestLevel(); return false; } } else { @@ -316,7 +316,7 @@ public class User { } private List getLevels() { - return config.getEvents().levels; + return config.getEvents().getLevels(); } private boolean silentMode() { @@ -330,4 +330,12 @@ public class User { public boolean isWaitingOnLevelSync() { return isWaitingOnLevelSync; } + + public void setLevelSyncTimestamp(Timestamp timestamp) { + levelSyncTimestamp = timestamp; + } + + public Timestamp getLevelSyncTimestamp() { + return levelSyncTimestamp; + } } diff --git a/src/main/java/net/h31ix/anticheat/manage/UserManager.java b/src/main/java/net/h31ix/anticheat/manage/UserManager.java index b716fff..0bed3a9 100644 --- a/src/main/java/net/h31ix/anticheat/manage/UserManager.java +++ b/src/main/java/net/h31ix/anticheat/manage/UserManager.java @@ -59,7 +59,10 @@ public class UserManager { } } // Otherwise try to load them - return loadUserFromFile(name); + User user = new User(name); + user.setIsWaitingOnLevelSync(true); + config.getLevels().loadLevelToUser(user); + return user; } /** @@ -86,10 +89,18 @@ public class UserManager { * @param user User to remove */ public void remove(User user) { - config.getLevels().saveLevel(user.getName(), user.getLevel()); users.remove(user); } + /** + * Save a user's level + * + * @param user User to save + */ + public void saveLevel(User user) { + config.getLevels().saveLevelFromUser(user); + } + /** * Get a user's level, or 0 if the player isn't found * @@ -129,37 +140,6 @@ public class UserManager { } } - /** - * Load a user into the list from flatfile - * - * @param name Name of the user - * @return true if found in the file - */ - public boolean addUserFromFile(String name) { - User user = loadUserFromFile(name); - if (user != null) { - users.add(user); - return true; - } else { - return false; - } - } - - /** - * Get a user from flatfile - * - * @param name Name of the user - * @return the user if found, null otherwise - */ - public User loadUserFromFile(String name) { - int level = config.getLevels().getLevel(name); - if (level == -1) { - return null; - } else { - return new User(name, level); - } - } - /** * Fire an alert * diff --git a/src/main/java/net/h31ix/anticheat/util/Level.java b/src/main/java/net/h31ix/anticheat/util/Level.java index e2d1dcf..279ffb6 100644 --- a/src/main/java/net/h31ix/anticheat/util/Level.java +++ b/src/main/java/net/h31ix/anticheat/util/Level.java @@ -19,6 +19,7 @@ package net.h31ix.anticheat.util; import java.util.ArrayList; +import java.util.Arrays; import java.util.List; import net.h31ix.anticheat.AntiCheat; @@ -29,36 +30,23 @@ public class Level { private String name; private int value; private ChatColor color = ChatColor.RED; - private boolean customColor = false; private List actions = new ArrayList(); - public Level(String name, int value) { + public Level(String name, int value, String color, List actions) { + this.name = name; this.value = value; - } + this.actions = actions; - 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; } @@ -81,18 +69,19 @@ public class Level { public static Level load(String string) { try { - string = Utilities.removeWhitespace(string); - String name = string.split(":")[0]; - int level = Integer.parseInt(string.split(":")[1]); - if(string.split(":").length == 3) { + if(string.split(":").length == 4) { + string = Utilities.removeWhitespace(string); + String name = string.split(":")[0]; + int level = Integer.parseInt(string.split(":")[1]); String color = string.split(":")[2]; - return new Level(name, level, color); + List actions = Arrays.asList(string.split(":")[3].split(",")); + return new Level(name, level, color, actions); } else { - return new Level(name, level); + throw new Exception(); } } catch (Exception ex) { AntiCheat.getPlugin().getLogger().warning("An event was initialized 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("The proper format is: 'name : threshold : color : action' such as 'High : 50 : RED : KICK'"); AntiCheat.getPlugin().getLogger().warning("This event will NOT run. ("+ex.getMessage()+")"); return null; } @@ -100,6 +89,6 @@ public class Level { @Override public String toString() { - return name + " : " + value + (customColor ? " : "+color.name() : ""); + return name + " : " + value + " : "+ color.name() + " : " + Utilities.listToCommaString(actions); } } diff --git a/src/main/java/net/h31ix/anticheat/util/Utilities.java b/src/main/java/net/h31ix/anticheat/util/Utilities.java index 82d16fa..26d0fa2 100644 --- a/src/main/java/net/h31ix/anticheat/util/Utilities.java +++ b/src/main/java/net/h31ix/anticheat/util/Utilities.java @@ -456,6 +456,24 @@ public final class Utilities { i.add(string); return i; } + + /** + * Create a comma-delimited string from a list + * + * @param list the list to parse + * @return the list in a string format + */ + public static String listToCommaString(List list) { + StringBuilder b = new StringBuilder(); + for(int i=0;i