mirror of
https://github.com/darksaid98/AntiCheatReloaded.git
synced 2026-12-01 14:38:56 +00:00
Refactor configuration system (again) to allow for SQL-based configuration files.
This commit contains breaking, unmigrated changes to events.yml. There may be code to upgrade old files to the new format in the future, but there is none as of yet. You have been warned.
This commit is contained in:
1 parent
0c6db1fed9
commit
ca4406809b
25 files changed
+852
-357
No files matched your search
@@ -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<Listener> eventList = new ArrayList<Listener>();
|
||||
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);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -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;
|
||||
|
||||
@@ -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<ConfigurationFile> flatfiles;
|
||||
private ArrayList<ConfigurationTable> 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<ConfigurationFile>(){{ add(config); add(enterprise); add(lang); add(magic); }};
|
||||
dbfiles = new ArrayList<ConfigurationTable>();
|
||||
|
||||
// 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();
|
||||
}
|
||||
}
|
||||
@@ -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 <http://www.gnu.org/licenses/>.
|
||||
*/
|
||||
|
||||
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();
|
||||
}
|
||||
}
|
||||
@@ -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<Integer> loggingLife;
|
||||
public ConfigValue<Integer> loggingInterval;
|
||||
|
||||
public ConfigValue<Boolean> usersEnabled;
|
||||
public ConfigValue<Boolean> syncUsers;
|
||||
|
||||
public ConfigValue<Boolean> configEvents;
|
||||
public ConfigValue<Boolean> configRules;
|
||||
|
||||
public Database database;
|
||||
|
||||
@@ -51,7 +52,10 @@ public class Enterprise extends ConfigurationFile {
|
||||
loggingLife = new ConfigValue<Integer>("logging.life");
|
||||
loggingInterval = new ConfigValue<Integer>("logging.interval");
|
||||
|
||||
usersEnabled = new ConfigValue<Boolean>("users.enable");
|
||||
syncUsers = new ConfigValue<Boolean>("sync.users");
|
||||
|
||||
configEvents = new ConfigValue<Boolean>("config.events");
|
||||
configRules = new ConfigValue<Boolean>("config.rules");
|
||||
|
||||
ConfigValue<String> databaseType = new ConfigValue<String>("database.type");
|
||||
ConfigValue<String> databaseHostname = new ConfigValue<String>("database.hostname");
|
||||
@@ -74,5 +78,7 @@ public class Enterprise extends ConfigurationFile {
|
||||
loggingInterval.getValue(),
|
||||
loggingLife.getValue()
|
||||
);
|
||||
|
||||
database.connect();
|
||||
}
|
||||
}
|
||||
@@ -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 <http://www.gnu.org/licenses/>.
|
||||
*/
|
||||
|
||||
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<Level> levels;
|
||||
public List<Rule> rules;
|
||||
|
||||
public int highestLevel;
|
||||
|
||||
public Events(AntiCheat plugin, Configuration config) {
|
||||
super(plugin, config, FILENAME);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void open() {
|
||||
ConfigValue<List<String>> levels = new ConfigValue<List<String>>("levels");
|
||||
ConfigValue<List<String>> actions = new ConfigValue<List<String>>("actions");
|
||||
ConfigValue<List<String>> rules = new ConfigValue<List<String>>("rules");
|
||||
|
||||
// Convert levels list to Levels
|
||||
this.levels = new ArrayList<Level>();
|
||||
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<Rule>();
|
||||
List<String> tempRules = rules.getValue();
|
||||
for(int i=0;i<tempRules.size();i++) {
|
||||
String string = tempRules.get(i);
|
||||
|
||||
if(string.equals("Check_SPIDER < 0 ? Player.KICK : null")) {
|
||||
// Default rule, won't ever run so we shouldn't load it; only used as example
|
||||
continue;
|
||||
}
|
||||
|
||||
Rule rule = Rule.load(string);
|
||||
if(rule != null) {
|
||||
this.rules.add(rule);
|
||||
} else {
|
||||
AntiCheat.getPlugin().getLogger().warning("Couldn't load rule '"+string+"' from config. Improper format used.");
|
||||
}
|
||||
}
|
||||
|
||||
// Sort levels
|
||||
Collections.sort(this.levels, new Comparator<Level>() {
|
||||
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.size();i++) {
|
||||
if (i == 0 && value < levels.get(i).getValue()) break;
|
||||
else if (i == levels.size()-1) return levels.get(i);
|
||||
else if (value >= levels.get(i).getValue() && value < levels.get(i+1).getValue()) return levels.get(i);
|
||||
}
|
||||
return null;
|
||||
}
|
||||
}
|
||||
@@ -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 <http://www.gnu.org/licenses/>.
|
||||
*/
|
||||
|
||||
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<Level> levels;
|
||||
private int highestLevel;
|
||||
|
||||
public MySQLEventsHolder(Configuration config) {
|
||||
super(config, TABLE);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void open() {
|
||||
highestLevel = 0;
|
||||
levels = new ArrayList<Level>();
|
||||
|
||||
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<String> 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<Level> getLevels() {
|
||||
return levels;
|
||||
}
|
||||
|
||||
@Override
|
||||
public int getHighestLevel() {
|
||||
return highestLevel;
|
||||
}
|
||||
}
|
||||
@@ -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 <http://www.gnu.org/licenses/>.
|
||||
*/
|
||||
|
||||
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<User> 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();
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -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 <http://www.gnu.org/licenses/>.
|
||||
*/
|
||||
|
||||
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<Rule> 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<Rule>();
|
||||
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<Rule> getRules() {
|
||||
return rules;
|
||||
}
|
||||
}
|
||||
@@ -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 <http://www.gnu.org/licenses/>.
|
||||
*/
|
||||
|
||||
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<Level> levels;
|
||||
|
||||
private int highestLevel;
|
||||
|
||||
public YamlEventsHolder(AntiCheat plugin, Configuration config) {
|
||||
super(plugin, config, FILENAME);
|
||||
}
|
||||
|
||||
@Override
|
||||
public List<Level> getLevels() {
|
||||
return levels;
|
||||
}
|
||||
|
||||
@Override
|
||||
public int getHighestLevel() {
|
||||
return highestLevel;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void open() {
|
||||
ConfigValue<List<String>> levels = new ConfigValue<List<String>>("events");
|
||||
|
||||
// Convert levels list to Levels
|
||||
this.levels = new ArrayList<Level>();
|
||||
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<Level>() {
|
||||
public int compare(Level l1, Level l2) {
|
||||
if (l1.getValue() == l2.getValue()) {
|
||||
return 0;
|
||||
} else if (l1.getValue() < l2.getValue()) {
|
||||
return -1;
|
||||
}
|
||||
return 1;
|
||||
}
|
||||
});
|
||||
}
|
||||
}
|
||||
+28
-6
@@ -16,30 +16,52 @@
|
||||
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
*/
|
||||
|
||||
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<User> users) {
|
||||
for(User user : users) {
|
||||
saveLevelFromUser(user);
|
||||
}
|
||||
}
|
||||
|
||||
private int getLevel(String name) {
|
||||
ConfigValue<Integer> level = new ConfigValue<Integer>(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<Integer>(name, false).setValue(new Integer(level));
|
||||
}
|
||||
}
|
||||
@@ -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 <http://www.gnu.org/licenses/>.
|
||||
*/
|
||||
|
||||
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<Rule> rules;
|
||||
|
||||
public YamlRulesHolder(AntiCheat plugin, Configuration config) {
|
||||
super(plugin, config, FILENAME);
|
||||
}
|
||||
|
||||
@Override
|
||||
public List<Rule> getRules() {
|
||||
return rules;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void open() {
|
||||
ConfigValue<List<String>> rules = new ConfigValue<List<String>>("rules");
|
||||
|
||||
// Convert rules list to Rules
|
||||
this.rules = new ArrayList<Rule>();
|
||||
List<String> tempRules = rules.getValue();
|
||||
for(int i=0;i<tempRules.size();i++) {
|
||||
String string = tempRules.get(i);
|
||||
|
||||
if(string.equals("Check_SPIDER < 0 ? Player.KICK : null")) {
|
||||
// Default rule, won't ever run so we shouldn't load it; only used as example
|
||||
continue;
|
||||
}
|
||||
|
||||
Rule rule = Rule.load(string);
|
||||
if(rule != null) {
|
||||
this.rules.add(rule);
|
||||
} else {
|
||||
AntiCheat.getPlugin().getLogger().warning("Couldn't load rule '"+string+"' from config. Improper format used.");
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,30 @@
|
||||
/*
|
||||
* 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.config.providers;
|
||||
|
||||
import net.h31ix.anticheat.util.Level;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
public interface Events {
|
||||
|
||||
public List<Level> getLevels();
|
||||
|
||||
public int getHighestLevel();
|
||||
}
|
||||
@@ -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 <http://www.gnu.org/licenses/>.
|
||||
*/
|
||||
|
||||
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<User> users);
|
||||
}
|
||||
@@ -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 <http://www.gnu.org/licenses/>.
|
||||
*/
|
||||
|
||||
|
||||
package net.h31ix.anticheat.config.providers;
|
||||
|
||||
import net.h31ix.anticheat.util.rule.Rule;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
public interface Rules {
|
||||
|
||||
public List<Rule> getRules();
|
||||
}
|
||||
@@ -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) {
|
||||
|
||||
@@ -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)) {
|
||||
|
||||
@@ -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);
|
||||
|
||||
@@ -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<Level> levels = config.getEvents().getLevels();
|
||||
for (int i=0;i<levels.size();i++) {
|
||||
if (i == 0 && level < levels.get(i).getValue()) break;
|
||||
else if (i == levels.size()-1) return levels.get(i);
|
||||
else if (level >= 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<Level> 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;
|
||||
}
|
||||
}
|
||||
@@ -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
|
||||
*
|
||||
|
||||
@@ -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<String> actions = new ArrayList<String>();
|
||||
|
||||
public Level(String name, int value) {
|
||||
public Level(String name, int value, String color, List<String> 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<String> 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);
|
||||
}
|
||||
}
|
||||
@@ -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<String> list) {
|
||||
StringBuilder b = new StringBuilder();
|
||||
for(int i=0;i<list.size();i++) {
|
||||
b.append(list.get(i));
|
||||
if(i < list.size()-1) {
|
||||
b.append(",");
|
||||
}
|
||||
}
|
||||
return b.toString();
|
||||
}
|
||||
|
||||
|
||||
static {
|
||||
// START INSTANT BREAK MATERIALS
|
||||
|
||||
@@ -28,16 +28,11 @@ import java.sql.*;
|
||||
|
||||
public class Database {
|
||||
|
||||
private static final String EVENTS_TABLE = "events";
|
||||
private static final String USERS_TABLE = "users";
|
||||
private static final String EVENTS_TABLE = "logs";
|
||||
|
||||
private String sqlLogEvent;
|
||||
private String sqlSyncTo;
|
||||
private String sqlSyncFrom;
|
||||
private String sqlCleanEvents;
|
||||
|
||||
private String sqlCreateEvents;
|
||||
private String sqlCreateUsers;
|
||||
|
||||
public enum DatabaseType {
|
||||
MySQL,
|
||||
@@ -76,17 +71,12 @@ public class Database {
|
||||
|
||||
this.eventLife = eventLife;
|
||||
|
||||
sqlLogEvent = "INSERT INTO " + prefix + EVENTS_TABLE + " (server, user, check_type) VALUES (?, ?, ?)";
|
||||
sqlSyncTo = "INSERT INTO " + prefix + USERS_TABLE + " (user, level, last_update_server) VALUES (?, ?, ?) ON DUPLICATE KEY UPDATE level = ?, last_update=CURRENT_TIMESTAMP, last_update_server=?";
|
||||
sqlSyncFrom = "SELECT level FROM " + prefix + USERS_TABLE + " WHERE user = ?";
|
||||
sqlCleanEvents = "DELETE FROM " + prefix + EVENTS_TABLE + " WHERE time < (CURRENT_TIMESTAMP - INTERVAL ? DAY)";
|
||||
sqlLogEvent = "INSERT INTO " + prefix + EVENTS_TABLE +
|
||||
" (server, user, check_type) " +
|
||||
"VALUES (?, ?, ?)";
|
||||
|
||||
sqlCreateUsers = "CREATE TABLE IF NOT EXISTS " + prefix + USERS_TABLE + "(" +
|
||||
" `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`));";
|
||||
sqlCleanEvents = "DELETE FROM " + prefix + EVENTS_TABLE + " " +
|
||||
"WHERE time < (CURRENT_TIMESTAMP - INTERVAL ? DAY)";
|
||||
|
||||
sqlCreateEvents = "CREATE TABLE IF NOT EXISTS " + prefix + EVENTS_TABLE + "(" +
|
||||
" `id` INT NOT NULL AUTO_INCREMENT," +
|
||||
@@ -132,6 +122,8 @@ public class Database {
|
||||
connection = DriverManager.getConnection(url, username, password);
|
||||
eventBatch = connection.prepareStatement(sqlLogEvent);
|
||||
|
||||
connection.prepareStatement(sqlCreateEvents).executeUpdate();
|
||||
|
||||
connection.setAutoCommit(false);
|
||||
|
||||
eventTask = Bukkit.getScheduler().runTaskTimerAsynchronously(AntiCheat.getPlugin(), new Runnable(){
|
||||
@@ -144,12 +136,6 @@ public class Database {
|
||||
} catch (SQLException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
try {
|
||||
connection.prepareStatement(sqlCreateUsers).executeUpdate();
|
||||
connection.prepareStatement(sqlCreateEvents).executeUpdate();
|
||||
} catch (SQLException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
|
||||
public void shutdown() {
|
||||
@@ -181,44 +167,6 @@ public class Database {
|
||||
}
|
||||
}
|
||||
|
||||
public void syncTo(User user) {
|
||||
if (!user.isWaitingOnLevelSync()) {
|
||||
AntiCheat.debugLog("Syncing to "+user.getName()+" value "+user.getLevel());
|
||||
try {
|
||||
PreparedStatement statement = connection.prepareStatement(sqlSyncTo);
|
||||
statement.setString(1, user.getName());
|
||||
statement.setInt(2, user.getLevel());
|
||||
statement.setString(3, serverName);
|
||||
statement.setInt(4, user.getLevel());
|
||||
statement.setString(5, serverName);
|
||||
|
||||
statement.executeUpdate();
|
||||
} catch (SQLException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public void syncFrom(final User user) {
|
||||
Bukkit.getScheduler().runTaskAsynchronously(AntiCheat.getPlugin(), new Runnable(){
|
||||
@Override
|
||||
public void run() {
|
||||
try {
|
||||
PreparedStatement statement = connection.prepareStatement(sqlSyncFrom);
|
||||
statement.setString(1, user.getName());
|
||||
|
||||
ResultSet set = statement.executeQuery();
|
||||
while (set.next()) {
|
||||
AntiCheat.debugLog("Syncing from "+user.getName()+" value "+set.getInt("level"));
|
||||
user.setLevel(set.getInt("level"));
|
||||
}
|
||||
} catch (SQLException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
public void flushEvents() {
|
||||
try {
|
||||
eventBatch.executeBatch();
|
||||
|
||||
Reference in new issue
Block a user