mirror of
https://github.com/darksaid98/AntiCheatReloaded.git
synced 2026-12-02 14:38:45 +00:00
Get enterprise syncing to a working state
This commit is contained in:
1 parent
d021c58099
commit
25fe36d21e
11 files changed
+182
-99
No files matched your search
@@ -66,6 +66,8 @@ public class AntiCheat extends JavaPlugin {
|
|||||||
setupCommands();
|
setupCommands();
|
||||||
setupUpdater();
|
setupUpdater();
|
||||||
setupProtocol();
|
setupProtocol();
|
||||||
|
// Enterprise must come before levels
|
||||||
|
setupEnterprise();
|
||||||
restoreLevels();
|
restoreLevels();
|
||||||
// Check if NoCheatPlus is installed
|
// Check if NoCheatPlus is installed
|
||||||
Bukkit.getScheduler().runTaskLater(this, new Runnable() {
|
Bukkit.getScheduler().runTaskLater(this, new Runnable() {
|
||||||
@@ -78,15 +80,15 @@ public class AntiCheat extends JavaPlugin {
|
|||||||
}
|
}
|
||||||
}, 40L);
|
}, 40L);
|
||||||
// End tests
|
// End tests
|
||||||
if (verbose) {
|
verboseLog("Finished loading.");
|
||||||
getLogger().log(Level.INFO, "Finished loading.");
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void onDisable() {
|
public void onDisable() {
|
||||||
for (User user : manager.getUserManager().getUsers()) {
|
if (!config.shouldSyncUsers()) {
|
||||||
config.getLevels().saveLevel(user.getName(), user.getLevel());
|
for (User user : manager.getUserManager().getUsers()) {
|
||||||
|
config.getLevels().saveLevel(user.getName(), user.getLevel());
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
AnticheatManager.close();
|
AnticheatManager.close();
|
||||||
@@ -98,9 +100,7 @@ public class AntiCheat extends JavaPlugin {
|
|||||||
if (Bukkit.getPluginManager().getPlugin("ProtocolLib") != null) {
|
if (Bukkit.getPluginManager().getPlugin("ProtocolLib") != null) {
|
||||||
protocolLib = true;
|
protocolLib = true;
|
||||||
packetManager = new PacketManager(ProtocolLibrary.getProtocolManager(), this, manager);
|
packetManager = new PacketManager(ProtocolLibrary.getProtocolManager(), this, manager);
|
||||||
if (verbose) {
|
verboseLog("Hooked into ProtocolLib");
|
||||||
getLogger().log(Level.INFO, "Hooked into ProtocolLib");
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -125,9 +125,7 @@ public class AntiCheat extends JavaPlugin {
|
|||||||
}
|
}
|
||||||
}, XRAY_TIME, XRAY_TIME);
|
}, XRAY_TIME, XRAY_TIME);
|
||||||
|
|
||||||
if (verbose) {
|
verboseLog("Scheduled the XRay checker.");
|
||||||
getLogger().log(Level.INFO, "Scheduled the XRay checker.");
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -135,45 +133,37 @@ public class AntiCheat extends JavaPlugin {
|
|||||||
private void setupEvents() {
|
private void setupEvents() {
|
||||||
for (Listener listener : eventList) {
|
for (Listener listener : eventList) {
|
||||||
getServer().getPluginManager().registerEvents(listener, this);
|
getServer().getPluginManager().registerEvents(listener, this);
|
||||||
if (verbose) {
|
verboseLog("Registered events for ".concat(listener.toString().split("@")[0].split(".anticheat.")[1]));
|
||||||
getLogger().log(Level.INFO, "Registered events for ".concat(listener.toString().split("@")[0].split(".anticheat.")[1]));
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
private void setupCommands() {
|
private void setupCommands() {
|
||||||
getCommand("anticheat").setExecutor(new CommandHandler());
|
getCommand("anticheat").setExecutor(new CommandHandler());
|
||||||
if (verbose) {
|
verboseLog("Registered commands.");
|
||||||
getLogger().log(Level.INFO, "Registered commands.");
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
private void setupUpdater() {
|
private void setupUpdater() {
|
||||||
if (config.getConfig().autoUpdate.getValue()) {
|
if (config.getConfig().autoUpdate.getValue()) {
|
||||||
if (verbose) {
|
verboseLog("Checking for a new update...");
|
||||||
getLogger().log(Level.INFO, "Checking for a new update...");
|
|
||||||
}
|
|
||||||
Updater updater = new Updater(this, "anticheat", this.getFile(), Updater.UpdateType.DEFAULT, false);
|
Updater updater = new Updater(this, "anticheat", this.getFile(), Updater.UpdateType.DEFAULT, false);
|
||||||
update = updater.getResult() != Updater.UpdateResult.NO_UPDATE;
|
update = updater.getResult() != Updater.UpdateResult.NO_UPDATE;
|
||||||
if (verbose) {
|
verboseLog("Update available: " + update);
|
||||||
getLogger().log(Level.INFO, "Update available: " + update);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
private void setupConfig() {
|
private void setupConfig() {
|
||||||
config = manager.getConfiguration();
|
config = manager.getConfiguration();
|
||||||
verbose = config.getConfig().verboseStartup.getValue();
|
verbose = config.getConfig().verboseStartup.getValue();
|
||||||
if (verbose) {
|
verboseLog("Setup the config.");
|
||||||
getLogger().log(Level.INFO, "Setup the config.");
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
private void setupEnterprise() {
|
private void setupEnterprise() {
|
||||||
if(config.getConfig().enterprise.getValue()) {
|
if(config.getConfig().enterprise.getValue()) {
|
||||||
config.getEnterprise().database.connect();
|
config.getEnterprise().database.connect();
|
||||||
if (verbose) {
|
verboseLog("Connected to the database.");
|
||||||
getLogger().log(Level.INFO, "Connected to the database.");
|
|
||||||
|
if(config.getEnterprise().loggingEnabled.getValue()) {
|
||||||
|
config.getEnterprise().database.cleanEvents();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -181,16 +171,15 @@ public class AntiCheat extends JavaPlugin {
|
|||||||
private void restoreLevels() {
|
private void restoreLevels() {
|
||||||
for (Player player : getServer().getOnlinePlayers()) {
|
for (Player player : getServer().getOnlinePlayers()) {
|
||||||
String name = player.getName();
|
String name = player.getName();
|
||||||
if(!config.getConfig().enterprise.getValue()) {
|
if (!config.shouldSyncUsers()) {
|
||||||
manager.getUserManager().addUser(new User(player.getName(), config.getLevels().getLevel(name)));
|
manager.getUserManager().addUser(new User(player.getName(), config.getLevels().getLevel(name)));
|
||||||
} else {
|
} else {
|
||||||
User user = new User(player.getName());
|
User user = new User(player.getName());
|
||||||
|
user.setIsWaitingOnLevelSync(true);
|
||||||
config.getEnterprise().database.syncFrom(user);
|
config.getEnterprise().database.syncFrom(user);
|
||||||
manager.getUserManager().addUser(user);
|
manager.getUserManager().addUser(user);
|
||||||
}
|
}
|
||||||
if (verbose) {
|
verboseLog("Data for " + player.getName() + " loaded");
|
||||||
getLogger().log(Level.INFO, "Data for " + player.getName() + " re-applied from flatfile");
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -237,4 +226,8 @@ public class AntiCheat extends JavaPlugin {
|
|||||||
}
|
}
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public void verboseLog(final String string) {
|
||||||
|
getLogger().info(string);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
@@ -352,18 +352,21 @@ public class CommandHandler implements CommandExecutor {
|
|||||||
public void sendPlayerReport(CommandSender cs, List<CheckType> types, User user, int page) {
|
public void sendPlayerReport(CommandSender cs, List<CheckType> types, User user, int page) {
|
||||||
String name = user.getName();
|
String name = user.getName();
|
||||||
int pages = (int) Math.ceil(((float) types.size()) / 6);
|
int pages = (int) Math.ceil(((float) types.size()) / 6);
|
||||||
String levelString = ChatColor.GREEN+"Low";
|
|
||||||
for(Level level : CONFIG.getEvents().levels) {
|
Level level = CONFIG.getEvents().getUserLevel(user.getLevel());
|
||||||
if(level.getValue() == user.getLevel()) {
|
String levelString = ChatColor.GREEN + "Low";
|
||||||
levelString = level.getColor()+level.getName();
|
|
||||||
}
|
if(level != null) {
|
||||||
|
levelString = level.getColor() + level.getName();
|
||||||
}
|
}
|
||||||
|
levelString += " (" + user.getLevel() + ")";
|
||||||
|
|
||||||
if (page <= pages && page > 0) {
|
if (page <= pages && page > 0) {
|
||||||
cs.sendMessage("--------------------[" + GREEN + "REPORT[" + page + "/" + pages + "]" + WHITE + "]---------------------");
|
cs.sendMessage("--------------------[" + GREEN + "REPORT[" + page + "/" + pages + "]" + WHITE + "]---------------------");
|
||||||
cs.sendMessage(GRAY + "Player: " + WHITE + name);
|
cs.sendMessage(GRAY + "Player: " + WHITE + name);
|
||||||
cs.sendMessage(GRAY + "Level: " + levelString);
|
cs.sendMessage(GRAY + "Level: " + levelString);
|
||||||
for (int x = 0; x < 6; x++) {
|
for (int x = 0; x < 6; x++) {
|
||||||
int index = ((page - 1) * 5) + (x + ((page - 1) * 1));
|
int index = ((page - 1) * 5) + (x + ((page - 1)));
|
||||||
if (index < types.size()) {
|
if (index < types.size()) {
|
||||||
int use = types.get(index).getUses(name);
|
int use = types.get(index).getUses(name);
|
||||||
ChatColor color = WHITE;
|
ChatColor color = WHITE;
|
||||||
@@ -391,7 +394,7 @@ public class CommandHandler implements CommandExecutor {
|
|||||||
|
|
||||||
public void handleReload(CommandSender cs) {
|
public void handleReload(CommandSender cs) {
|
||||||
if (hasPermission(cs, Permission.SYSTEM_RELOAD)) {
|
if (hasPermission(cs, Permission.SYSTEM_RELOAD)) {
|
||||||
CONFIG.reload();
|
CONFIG.load();
|
||||||
cs.sendMessage(GREEN + "AntiCheat configuration reloaded.");
|
cs.sendMessage(GREEN + "AntiCheat configuration reloaded.");
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -47,7 +47,9 @@ public class Configuration {
|
|||||||
|
|
||||||
magic = new Magic(plugin, this);
|
magic = new Magic(plugin, this);
|
||||||
|
|
||||||
load();
|
for(ConfigurationFile file : new ConfigurationFile[]{config, events, lang, enterprise, levels, magic}) {
|
||||||
|
checkReload(file);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public void load() {
|
public void load() {
|
||||||
@@ -73,11 +75,6 @@ public class Configuration {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public void reload() {
|
|
||||||
save();
|
|
||||||
load();
|
|
||||||
}
|
|
||||||
|
|
||||||
public Config getConfig() {
|
public Config getConfig() {
|
||||||
return config;
|
return config;
|
||||||
}
|
}
|
||||||
@@ -101,4 +98,12 @@ public class Configuration {
|
|||||||
public Magic getMagic() {
|
public Magic getMagic() {
|
||||||
return magic;
|
return magic;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public boolean shouldSyncEvents() {
|
||||||
|
return getConfig().enterprise.getValue() && getEnterprise().loggingEnabled.getValue();
|
||||||
|
}
|
||||||
|
|
||||||
|
public boolean shouldSyncUsers() {
|
||||||
|
return getConfig().enterprise.getValue() && getEnterprise().usersEnabled.getValue();
|
||||||
|
}
|
||||||
}
|
}
|
||||||
@@ -149,6 +149,8 @@ public class ConfigurationFile {
|
|||||||
} else if(loadDefault) {
|
} else if(loadDefault) {
|
||||||
getConfigFile().set(path, getDefaultConfigFile().get(path));
|
getConfigFile().set(path, getDefaultConfigFile().get(path));
|
||||||
value = getDefaultConfigFile().get(path);
|
value = getDefaultConfigFile().get(path);
|
||||||
|
getConfigFile().set(path, value);
|
||||||
|
save();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -162,6 +164,7 @@ public class ConfigurationFile {
|
|||||||
|
|
||||||
public T setValue(T value) {
|
public T setValue(T value) {
|
||||||
getConfigFile().set(path, value);
|
getConfigFile().set(path, value);
|
||||||
|
save();
|
||||||
return value;
|
return value;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -72,14 +72,10 @@ public class Enterprise extends ConfigurationFile {
|
|||||||
databasePassword.getValue(),
|
databasePassword.getValue(),
|
||||||
databasePrefix.getValue(),
|
databasePrefix.getValue(),
|
||||||
databaseSchema.getValue(),
|
databaseSchema.getValue(),
|
||||||
|
serverName.getValue(),
|
||||||
loggingInterval.getValue(),
|
loggingInterval.getValue(),
|
||||||
usersInterval.getValue()
|
usersInterval.getValue(),
|
||||||
|
loggingLife.getValue()
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
public void logEvent(User user, CheckType type) {
|
|
||||||
if(loggingEnabled.getValue()) {
|
|
||||||
database.logEvent(serverName.getValue(), user, type);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
@@ -25,6 +25,8 @@ import net.h31ix.anticheat.util.Level;
|
|||||||
import net.h31ix.anticheat.util.rule.Rule;
|
import net.h31ix.anticheat.util.rule.Rule;
|
||||||
|
|
||||||
import java.util.ArrayList;
|
import java.util.ArrayList;
|
||||||
|
import java.util.Collections;
|
||||||
|
import java.util.Comparator;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
|
|
||||||
public class Events extends ConfigurationFile {
|
public class Events extends ConfigurationFile {
|
||||||
@@ -50,7 +52,7 @@ public class Events extends ConfigurationFile {
|
|||||||
this.levels = new ArrayList<Level>();
|
this.levels = new ArrayList<Level>();
|
||||||
for(String string : levels.getValue()) {
|
for(String string : levels.getValue()) {
|
||||||
Level level = Level.load(string);
|
Level level = Level.load(string);
|
||||||
if(level == null) {
|
if(level == null || level.getValue() < 0) {
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -94,5 +96,26 @@ public class Events extends ConfigurationFile {
|
|||||||
AntiCheat.getPlugin().getLogger().warning("Couldn't load rule '"+string+"' from config. Improper format used.");
|
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;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -53,7 +53,9 @@ public class EventListener implements Listener {
|
|||||||
// Ignore plugins that are creating NPCs with no names (why the hell)
|
// Ignore plugins that are creating NPCs with no names (why the hell)
|
||||||
if (user != null && user.getName() != null) {
|
if (user != null && user.getName() != null) {
|
||||||
type.logUse(user);
|
type.logUse(user);
|
||||||
CONFIG.getEnterprise().logEvent(user, type);
|
if (CONFIG.shouldSyncEvents()) {
|
||||||
|
CONFIG.getEnterprise().database.logEvent(user, type);
|
||||||
|
}
|
||||||
if (CONFIG.getConfig().fileLogLevel.getValue() == 2 && type.getUses(user.getName()) % 10 == 0) {
|
if (CONFIG.getConfig().fileLogLevel.getValue() == 2 && type.getUses(user.getName()) % 10 == 0) {
|
||||||
AntiCheat.getManager().fileLog(user.getName() + " has triggered multiple " + type + " checks.");
|
AntiCheat.getManager().fileLog(user.getName() + " has triggered multiple " + type + " checks.");
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -138,6 +138,8 @@ public class PlayerListener extends EventListener {
|
|||||||
@EventHandler(ignoreCancelled = true)
|
@EventHandler(ignoreCancelled = true)
|
||||||
public void onPlayerChat(AsyncPlayerChatEvent event) {
|
public void onPlayerChat(AsyncPlayerChatEvent event) {
|
||||||
Player player = event.getPlayer();
|
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()) {
|
if (getCheckManager().willCheck(player, CheckType.SPAM) && getConfig().getConfig().blockChatSpam.getValue()) {
|
||||||
CheckResult result = getBackend().checkSpam(player, event.getMessage());
|
CheckResult result = getBackend().checkSpam(player, event.getMessage());
|
||||||
if (result.failed()) {
|
if (result.failed()) {
|
||||||
@@ -166,6 +168,9 @@ public class PlayerListener extends EventListener {
|
|||||||
getBackend().garbageClean(event.getPlayer());
|
getBackend().garbageClean(event.getPlayer());
|
||||||
|
|
||||||
AntiCheat.getManager().addEvent(event.getEventName(), event.getHandlers().getRegisteredListeners());
|
AntiCheat.getManager().addEvent(event.getEventName(), event.getHandlers().getRegisteredListeners());
|
||||||
|
if (getConfig().shouldSyncUsers()) {
|
||||||
|
getConfig().getEnterprise().database.syncTo(getUserManager().getUser(event.getPlayer().getName()));
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@EventHandler
|
@EventHandler
|
||||||
@@ -261,7 +266,7 @@ public class PlayerListener extends EventListener {
|
|||||||
|
|
||||||
getBackend().logJoin(player);
|
getBackend().logJoin(player);
|
||||||
|
|
||||||
if (getConfig().getConfig().enterprise.getValue()) {
|
if (!getConfig().shouldSyncUsers()) {
|
||||||
if (getUserManager().getUser(player.getName()) == null) {
|
if (getUserManager().getUser(player.getName()) == null) {
|
||||||
getUserManager().addUser(new User(player.getName()));
|
getUserManager().addUser(new User(player.getName()));
|
||||||
} else {
|
} else {
|
||||||
@@ -269,6 +274,7 @@ public class PlayerListener extends EventListener {
|
|||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
User user = new User(player.getName());
|
User user = new User(player.getName());
|
||||||
|
user.setIsWaitingOnLevelSync(true);
|
||||||
getConfig().getEnterprise().database.syncFrom(user);
|
getConfig().getEnterprise().database.syncFrom(user);
|
||||||
getUserManager().addUser(user);
|
getUserManager().addUser(user);
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -176,8 +176,6 @@ public class AnticheatManager {
|
|||||||
public static void close() {
|
public static void close() {
|
||||||
fileHandler.close();
|
fileHandler.close();
|
||||||
|
|
||||||
configuration.save();
|
|
||||||
|
|
||||||
if(configuration.getConfig().enterprise.getValue()) {
|
if(configuration.getConfig().enterprise.getValue()) {
|
||||||
configuration.getEnterprise().database.shutdown();
|
configuration.getEnterprise().database.shutdown();
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -42,6 +42,8 @@ public class User {
|
|||||||
private String [] messages = new String[2];
|
private String [] messages = new String[2];
|
||||||
private Long [] messageTimes = new Long[2];
|
private Long [] messageTimes = new Long[2];
|
||||||
|
|
||||||
|
private boolean isWaitingOnLevelSync;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Initialize an AntiCheat user
|
* Initialize an AntiCheat user
|
||||||
*
|
*
|
||||||
@@ -60,7 +62,8 @@ public class User {
|
|||||||
public User(String name, int level) {
|
public User(String name, int level) {
|
||||||
this.name = name;
|
this.name = name;
|
||||||
this.level = level;
|
this.level = level;
|
||||||
this.id = getPlayer().getEntityId();
|
|
||||||
|
this.id = getPlayer().isOnline() ? getPlayer().getEntityId() : -1;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -111,23 +114,26 @@ public class User {
|
|||||||
// Prevent silent mode from increasing the level way too fast
|
// Prevent silent mode from increasing the level way too fast
|
||||||
return false;
|
return false;
|
||||||
} else {
|
} else {
|
||||||
level++;
|
if (level < config.getEvents().highestLevel) {
|
||||||
|
level++;
|
||||||
|
|
||||||
// Check levels
|
// Check levels
|
||||||
for(Level l : getLevels()) {
|
for(Level l : getLevels()) {
|
||||||
if(l.getValue() == level) {
|
if(l.getValue() == level) {
|
||||||
AntiCheat.getManager().getUserManager().alert(this, l, type);
|
AntiCheat.getManager().getUserManager().alert(this, l, type);
|
||||||
if(l.getValue() == config.getEvents().highestLevel) {
|
if(l.getValue() == config.getEvents().highestLevel) {
|
||||||
level = l.getValue() - 10;
|
level = l.getValue() - 10;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
|
||||||
|
|
||||||
// Execute rules
|
// Execute rules
|
||||||
for(Rule rule : config.getEvents().rules) {
|
for(Rule rule : config.getEvents().rules) {
|
||||||
rule.check(this, type);
|
rule.check(this, type);
|
||||||
|
}
|
||||||
|
return true;
|
||||||
}
|
}
|
||||||
return true;
|
return false;
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
return false;
|
return false;
|
||||||
@@ -147,9 +153,15 @@ public class User {
|
|||||||
* @return true if the level was valid and set properly
|
* @return true if the level was valid and set properly
|
||||||
*/
|
*/
|
||||||
public boolean setLevel(int level) {
|
public boolean setLevel(int level) {
|
||||||
if (level > 0 && level <= config.getEvents().highestLevel) {
|
isWaitingOnLevelSync = false;
|
||||||
this.level = level;
|
if (level >= 0) {
|
||||||
return true;
|
if (level <= config.getEvents().highestLevel) {
|
||||||
|
this.level = level;
|
||||||
|
return true;
|
||||||
|
} else {
|
||||||
|
this.level = config.getEvents().highestLevel;
|
||||||
|
return false;
|
||||||
|
}
|
||||||
} else {
|
} else {
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
@@ -310,4 +322,12 @@ public class User {
|
|||||||
private boolean silentMode() {
|
private boolean silentMode() {
|
||||||
return config.getConfig().silentMode.getValue();
|
return config.getConfig().silentMode.getValue();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public void setIsWaitingOnLevelSync(boolean b) {
|
||||||
|
this.isWaitingOnLevelSync = b;
|
||||||
|
}
|
||||||
|
|
||||||
|
public boolean isWaitingOnLevelSync() {
|
||||||
|
return isWaitingOnLevelSync;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
@@ -31,9 +31,10 @@ public class Database {
|
|||||||
private static final String EVENTS_TABLE = "events";
|
private static final String EVENTS_TABLE = "events";
|
||||||
private static final String USERS_TABLE = "users";
|
private static final String USERS_TABLE = "users";
|
||||||
|
|
||||||
private static final String SQL_LOG_EVENT = "INSERT INTO ? (server_name, user, check_type) VALUES (?, ?, ?)";
|
private String sqlLogEvent;
|
||||||
private static final String SQL_SYNC_FROM = "SELECT level FROM ? WHERE user = ?";
|
private String sqlSyncTo;
|
||||||
private static final String SQL_SYNC_TO = "INSERT INTO ? (user, level) VALUES (?, ?) ON DUPLICATE KEY UPDATE level = ?";
|
private String sqlSyncFrom;
|
||||||
|
private String sqlCleanEvents;
|
||||||
|
|
||||||
public enum DatabaseType {
|
public enum DatabaseType {
|
||||||
MySQL,
|
MySQL,
|
||||||
@@ -46,10 +47,13 @@ public class Database {
|
|||||||
private String password;
|
private String password;
|
||||||
private String prefix;
|
private String prefix;
|
||||||
private String schema;
|
private String schema;
|
||||||
|
private String serverName;
|
||||||
|
|
||||||
private int eventInterval;
|
private int eventInterval;
|
||||||
private int userInterval;
|
private int userInterval;
|
||||||
|
|
||||||
|
private int eventLife;
|
||||||
|
|
||||||
private Connection connection;
|
private Connection connection;
|
||||||
|
|
||||||
private PreparedStatement eventBatch;
|
private PreparedStatement eventBatch;
|
||||||
@@ -58,7 +62,7 @@ public class Database {
|
|||||||
private BukkitTask eventTask;
|
private BukkitTask eventTask;
|
||||||
private BukkitTask userTask;
|
private BukkitTask userTask;
|
||||||
|
|
||||||
public Database(DatabaseType type, String hostname, int port, String username, String password, String prefix, String schema, int eventInterval, int userInterval) {
|
public Database(DatabaseType type, String hostname, int port, String username, String password, String prefix, String schema, String serverName, int eventInterval, int userInterval, int eventLife) {
|
||||||
this.type = type;
|
this.type = type;
|
||||||
this.hostname = hostname;
|
this.hostname = hostname;
|
||||||
this.port = port;
|
this.port = port;
|
||||||
@@ -66,9 +70,17 @@ public class Database {
|
|||||||
this.password = password;
|
this.password = password;
|
||||||
this.prefix = prefix;
|
this.prefix = prefix;
|
||||||
this.schema = schema;
|
this.schema = schema;
|
||||||
|
this.serverName = serverName;
|
||||||
|
|
||||||
this.eventInterval = eventInterval;
|
this.eventInterval = eventInterval;
|
||||||
this.userInterval = userInterval;
|
this.userInterval = userInterval;
|
||||||
|
|
||||||
|
this.eventLife = eventLife;
|
||||||
|
|
||||||
|
sqlLogEvent = "INSERT INTO " + prefix + EVENTS_TABLE + " (server, user, check_type) VALUES (?, ?, ?)";
|
||||||
|
sqlSyncTo = "INSERT INTO " + prefix + USERS_TABLE + " (user, level) 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)";
|
||||||
}
|
}
|
||||||
|
|
||||||
public DatabaseType getType() {
|
public DatabaseType getType() {
|
||||||
@@ -100,12 +112,12 @@ public class Database {
|
|||||||
}
|
}
|
||||||
|
|
||||||
public void connect() {
|
public void connect() {
|
||||||
String url = type.toString().toLowerCase() + "://" + hostname + ":" + port + "/" + schema;
|
String url = "jdbc:" + type.toString().toLowerCase() + "://" + hostname + ":" + port + "/" + schema;
|
||||||
|
|
||||||
try {
|
try {
|
||||||
connection = DriverManager.getConnection(url, username, password);
|
connection = DriverManager.getConnection(url, username, password);
|
||||||
eventBatch = connection.prepareStatement(SQL_LOG_EVENT);
|
eventBatch = connection.prepareStatement(sqlLogEvent);
|
||||||
syncBatch = connection.prepareStatement(SQL_SYNC_TO);
|
syncBatch = connection.prepareStatement(sqlSyncTo);
|
||||||
|
|
||||||
connection.setAutoCommit(false);
|
connection.setAutoCommit(false);
|
||||||
|
|
||||||
@@ -114,7 +126,7 @@ public class Database {
|
|||||||
public void run() {
|
public void run() {
|
||||||
flushEvents();
|
flushEvents();
|
||||||
}
|
}
|
||||||
}, eventInterval*60*20, eventInterval*60*20);
|
}, eventInterval* 60 * 20, eventInterval * 60 * 20);
|
||||||
|
|
||||||
userTask = Bukkit.getScheduler().runTaskTimerAsynchronously(AntiCheat.getPlugin(), new Runnable() {
|
userTask = Bukkit.getScheduler().runTaskTimerAsynchronously(AntiCheat.getPlugin(), new Runnable() {
|
||||||
@Override
|
@Override
|
||||||
@@ -149,12 +161,11 @@ public class Database {
|
|||||||
return connection;
|
return connection;
|
||||||
}
|
}
|
||||||
|
|
||||||
public void logEvent(String serverName, User user, CheckType checkType) {
|
public void logEvent(User user, CheckType checkType) {
|
||||||
try {
|
try {
|
||||||
eventBatch.setString(1, EVENTS_TABLE);
|
eventBatch.setString(1, serverName);
|
||||||
eventBatch.setString(2, serverName);
|
eventBatch.setString(2, user.getName());
|
||||||
eventBatch.setString(3, user.getName());
|
eventBatch.setString(3, checkType.toString());
|
||||||
eventBatch.setString(4, checkType.toString());
|
|
||||||
|
|
||||||
eventBatch.addBatch();
|
eventBatch.addBatch();
|
||||||
} catch (SQLException e) {
|
} catch (SQLException e) {
|
||||||
@@ -163,15 +174,18 @@ public class Database {
|
|||||||
}
|
}
|
||||||
|
|
||||||
public void syncTo(User user) {
|
public void syncTo(User user) {
|
||||||
try {
|
if (!user.isWaitingOnLevelSync()) {
|
||||||
syncBatch.setString(1, USERS_TABLE);
|
System.out.println("Syncing to "+user.getName()+" value "+user.getLevel());
|
||||||
syncBatch.setString(2, user.getName());
|
try {
|
||||||
syncBatch.setInt(3, user.getLevel());
|
syncBatch.setString(1, user.getName());
|
||||||
syncBatch.setInt(4, user.getLevel());
|
syncBatch.setInt(2, user.getLevel());
|
||||||
|
syncBatch.setInt(3, user.getLevel());
|
||||||
|
syncBatch.setString(4, serverName);
|
||||||
|
|
||||||
syncBatch.addBatch();
|
syncBatch.addBatch();
|
||||||
} catch (SQLException e) {
|
} catch (SQLException e) {
|
||||||
e.printStackTrace();
|
e.printStackTrace();
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -180,12 +194,12 @@ public class Database {
|
|||||||
@Override
|
@Override
|
||||||
public void run() {
|
public void run() {
|
||||||
try {
|
try {
|
||||||
PreparedStatement statement = connection.prepareStatement(SQL_SYNC_FROM);
|
PreparedStatement statement = connection.prepareStatement(sqlSyncFrom);
|
||||||
statement.setString(1, USERS_TABLE);
|
statement.setString(1, user.getName());
|
||||||
statement.setString(2, user.getName());
|
|
||||||
|
|
||||||
ResultSet set = statement.executeQuery();
|
ResultSet set = statement.executeQuery();
|
||||||
while (set.next()) {
|
while (set.next()) {
|
||||||
|
System.out.println("Added level for " + user.getName()+":" +set.getInt("level"));
|
||||||
user.setLevel(set.getInt("level"));
|
user.setLevel(set.getInt("level"));
|
||||||
}
|
}
|
||||||
} catch (SQLException e) {
|
} catch (SQLException e) {
|
||||||
@@ -200,20 +214,40 @@ public class Database {
|
|||||||
eventBatch.executeBatch();
|
eventBatch.executeBatch();
|
||||||
connection.commit();
|
connection.commit();
|
||||||
|
|
||||||
eventBatch = connection.prepareStatement(SQL_LOG_EVENT);
|
eventBatch = connection.prepareStatement(sqlLogEvent);
|
||||||
} catch (SQLException e) {
|
} catch (SQLException e) {
|
||||||
e.printStackTrace();
|
e.printStackTrace();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public void flushSyncs() {
|
public void flushSyncs() {
|
||||||
|
System.out.println("Flushing syncs");
|
||||||
try {
|
try {
|
||||||
syncBatch.executeBatch();
|
syncBatch.executeBatch();
|
||||||
connection.commit();
|
connection.commit();
|
||||||
|
|
||||||
syncBatch = connection.prepareStatement(SQL_SYNC_TO);
|
syncBatch = connection.prepareStatement(sqlSyncTo);
|
||||||
} catch (SQLException e) {
|
} catch (SQLException e) {
|
||||||
e.printStackTrace();
|
e.printStackTrace();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public void cleanEvents() {
|
||||||
|
Bukkit.getScheduler().runTaskAsynchronously(AntiCheat.getPlugin(), new Runnable(){
|
||||||
|
@Override
|
||||||
|
public void run() {
|
||||||
|
try {
|
||||||
|
PreparedStatement statement = connection.prepareStatement(sqlCleanEvents);
|
||||||
|
statement.setInt(1, eventLife);
|
||||||
|
|
||||||
|
statement.executeUpdate();
|
||||||
|
|
||||||
|
connection.commit();
|
||||||
|
AntiCheat.getPlugin().verboseLog("Cleaned " + statement.getUpdateCount() + " old events from the database");
|
||||||
|
} catch (SQLException e) {
|
||||||
|
e.printStackTrace();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
});
|
||||||
|
}
|
||||||
}
|
}
|
||||||
Reference in new issue
Block a user