Work on adding the database syncing. Enterprise features are still experimental.

This commit is contained in:
h31ix committed 2013-10-09 20:08:18 -04:00
1 parent b28a4b0b73
commit d021c58099
10 files changed
+248 -44

No files matched your search

@@ -169,10 +169,25 @@ public class AntiCheat extends JavaPlugin {
}
}
private void setupEnterprise() {
if(config.getConfig().enterprise.getValue()) {
config.getEnterprise().database.connect();
if (verbose) {
getLogger().log(Level.INFO, "Connected to the database.");
}
}
}
private void restoreLevels() {
for (Player player : getServer().getOnlinePlayers()) {
String name = player.getName();
manager.getUserManager().addUser(new User(player.getName(), config.getLevels().getLevel(name)));
if(!config.getConfig().enterprise.getValue()) {
manager.getUserManager().addUser(new User(player.getName(), config.getLevels().getLevel(name)));
} else {
User user = new User(player.getName());
config.getEnterprise().database.syncFrom(user);
manager.getUserManager().addUser(user);
}
if (verbose) {
getLogger().log(Level.INFO, "Data for " + player.getName() + " re-applied from flatfile");
}
@@ -21,6 +21,8 @@ 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 {
@@ -29,11 +31,12 @@ public class Enterprise extends ConfigurationFile {
public ConfigValue<String> serverName;
public ConfigValue<String> loggingEnabled;
public ConfigValue<String> loggingLife;
public ConfigValue<Boolean> loggingEnabled;
public ConfigValue<Integer> loggingLife;
public ConfigValue<Integer> loggingInterval;
public ConfigValue<String> usersEnabled;
public ConfigValue<String> usersInterval;
public ConfigValue<Boolean> usersEnabled;
public ConfigValue<Integer> usersInterval;
public Database database;
@@ -45,11 +48,12 @@ public class Enterprise extends ConfigurationFile {
public void open() {
serverName = new ConfigValue<String>("server.name");
loggingEnabled = new ConfigValue<String>("logging.enable");
loggingLife = new ConfigValue<String>("logging.life");
loggingEnabled = new ConfigValue<Boolean>("logging.enable");
loggingLife = new ConfigValue<Integer>("logging.life");
loggingInterval = new ConfigValue<Integer>("logging.interval");
usersEnabled = new ConfigValue<String>("users.enable");
usersInterval = new ConfigValue<String>("users.interval");
usersEnabled = new ConfigValue<Boolean>("users.enable");
usersInterval = new ConfigValue<Integer>("users.interval");
ConfigValue<String> databaseType = new ConfigValue<String>("database.type");
ConfigValue<String> databaseHostname = new ConfigValue<String>("database.hostname");
@@ -61,13 +65,21 @@ public class Enterprise extends ConfigurationFile {
// Convert database values to Database
database = new Database(
Database.DatabaseType.valueOf(databaseType.getValue()),
databaseHostname.getValue(),
databasePort.getValue(),
databaseUsername.getValue(),
databasePassword.getValue(),
databasePrefix.getValue(),
databaseSchema.getValue()
);
Database.DatabaseType.valueOf(databaseType.getValue()),
databaseHostname.getValue(),
databasePort.getValue(),
databaseUsername.getValue(),
databasePassword.getValue(),
databasePrefix.getValue(),
databaseSchema.getValue(),
loggingInterval.getValue(),
usersInterval.getValue()
);
}
public void logEvent(User user, CheckType type) {
if(loggingEnabled.getValue()) {
database.logEvent(serverName.getValue(), user, type);
}
}
}
@@ -40,9 +40,10 @@ import org.yaml.snakeyaml.error.YAMLException;
import org.yaml.snakeyaml.representer.Representer;
/**
* An implementation of {@link org.bukkit.configuration.file.YamlConfiguration} which preserves comments.
* An extension of {@link org.bukkit.configuration.file.YamlConfiguration} which preserves comments.
* Note that this implementation is not synchronized.
* Some overridden code has been moved to this file from its implementation.
* <br>
* Some overridden code has been moved to this file from its parent & modified.
*/
public class CommentedConfiguration extends YamlConfiguration {
private Map<Integer, String> comments = new HashMap<Integer, String>();
@@ -116,15 +117,15 @@ public class CommentedConfiguration extends YamlConfiguration {
}
int i = 0;
while(queue.size() > 0) {
if(comments.containsKey(i)) {
while (queue.size() > 0) {
if (comments.containsKey(i)) {
builder.append(comments.get(i));
// Handle subsequent comments
int b = i;
while(true) {
while (true) {
b++;
if(comments.containsKey(b)) {
if (comments.containsKey(b)) {
builder.append('\n');
builder.append(comments.get(b));
} else {
@@ -53,6 +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);
CONFIG.getEnterprise().logEvent(user, type);
if (CONFIG.getConfig().fileLogLevel.getValue() == 2 && type.getUses(user.getName()) % 10 == 0) {
AntiCheat.getManager().fileLog(user.getName() + " has triggered multiple " + type + " checks.");
}
@@ -261,10 +261,16 @@ public class PlayerListener extends EventListener {
getBackend().logJoin(player);
if (getUserManager().getUser(player.getName()) == null) {
getUserManager().addUser(new User(player.getName()));
if (getConfig().getConfig().enterprise.getValue()) {
if (getUserManager().getUser(player.getName()) == null) {
getUserManager().addUser(new User(player.getName()));
} else {
getUserManager().addUserFromFile(player.getName());
}
} else {
getUserManager().addUserFromFile(player.getName());
User user = new User(player.getName());
getConfig().getEnterprise().database.syncFrom(user);
getUserManager().addUser(user);
}
if (player.hasMetadata(Utilities.SPY_METADATA)) {
for (Player p : player.getServer().getOnlinePlayers()) {
@@ -177,5 +177,9 @@ public class AnticheatManager {
fileHandler.close();
configuration.save();
if(configuration.getConfig().enterprise.getValue()) {
configuration.getEnterprise().database.shutdown();
}
}
}
@@ -54,7 +54,9 @@ public class UserManager {
*/
public User getUser(String name) {
for (User user : users) {
if (user.getName().equalsIgnoreCase(name)) { return user; }
if (user.getName().equalsIgnoreCase(name)) {
return user;
}
}
// Otherwise try to load them
return loadUserFromFile(name);
@@ -38,11 +38,6 @@ public final class Utilities {
private static final List<Material> INSTANT_BREAK = new ArrayList<Material>();
private static final List<Material> FOOD = new ArrayList<Material>();
private static final List<Material> INTERACTABLE = new ArrayList<Material>();
private static final List<Material> AXES = new ArrayList<Material>();
private static final List<Material> PICKAXES = new ArrayList<Material>();
private static final List<Material> SHEARS = new ArrayList<Material>();
private static final List<Material> SHOVELS = new ArrayList<Material>();
private static final List<Material> ANY = new ArrayList<Material>();
private static final Map<Material, Material> COMBO = new HashMap<Material, Material>();
public static final String SPY_METADATA = "ac-spydata";
@@ -166,10 +161,8 @@ public final class Utilities {
public static boolean isHoveringOverWater(Location player, int blocks) {
for (int i = player.getBlockY(); i > player.getBlockY() - blocks; i--) {
Block newloc = (new Location(player.getWorld(), player.getBlockX(), i, player.getBlockZ())).getBlock();
if (newloc.getTypeId() != 0 && newloc.isLiquid()) {
return true;
} else if (newloc.getTypeId() != 0) {
return false;
if (newloc.getType() != Material.AIR) {
return newloc.isLiquid();
}
}
@@ -213,8 +206,16 @@ public final class Utilities {
* @return true if slab
*/
public static boolean isSlab(Block block) {
int id = block.getTypeId();
return id == 43 || id == 44 || id == 125 || id == 126;
Material type = block.getType();
switch (type) {
case STEP:
case DOUBLE_STEP:
case WOOD_STEP:
case WOOD_DOUBLE_STEP:
return true;
default:
return false;
}
}
/**
@@ -224,8 +225,21 @@ public final class Utilities {
* @return true if stair
*/
public static boolean isStair(Block block) {
int id = block.getTypeId();
return id == 53 || id == 67 || id == 108 || id == 109 || id == 114 || id == 128 || id == 134 || id == 135 || id == 136;
Material type = block.getType();
switch (type) {
case WOOD_STAIRS:
case SPRUCE_WOOD_STAIRS:
case SMOOTH_STAIRS:
case SANDSTONE_STAIRS:
case QUARTZ_STAIRS:
case JUNGLE_WOOD_STAIRS:
case NETHER_BRICK_STAIRS:
case BIRCH_WOOD_STAIRS:
case COBBLESTONE_STAIRS:
return true;
default:
return false;
}
}
/**
@@ -334,7 +348,7 @@ public final class Utilities {
* @return time in milliseconds to break
*/
public static long calcSurvivalFastBreak(ItemStack tool, Material block) {
if(isInstantBreak(block) || (tool.getType() == Material.SHEARS && block.getId() == Material.LEAVES.getId())) {
if(isInstantBreak(block) || (tool.getType() == Material.SHEARS && block == Material.LEAVES)) {
return 0;
}
double bhardness = BlockHardness.getBlockHardness(block);
@@ -18,8 +18,23 @@
package net.h31ix.anticheat.util.enterprise;
import net.h31ix.anticheat.AntiCheat;
import net.h31ix.anticheat.manage.CheckType;
import net.h31ix.anticheat.manage.User;
import org.bukkit.Bukkit;
import org.bukkit.scheduler.BukkitTask;
import java.sql.*;
public class Database {
private static final String EVENTS_TABLE = "events";
private static final String USERS_TABLE = "users";
private static final String SQL_LOG_EVENT = "INSERT INTO ? (server_name, user, check_type) VALUES (?, ?, ?)";
private static final String SQL_SYNC_FROM = "SELECT level FROM ? WHERE user = ?";
private static final String SQL_SYNC_TO = "INSERT INTO ? (user, level) VALUES (?, ?) ON DUPLICATE KEY UPDATE level = ?";
public enum DatabaseType {
MySQL,
}
@@ -32,7 +47,18 @@ public class Database {
private String prefix;
private String schema;
public Database(DatabaseType type, String hostname, int port, String username, String password, String prefix, String schema) {
private int eventInterval;
private int userInterval;
private Connection connection;
private PreparedStatement eventBatch;
private PreparedStatement syncBatch;
private BukkitTask eventTask;
private BukkitTask userTask;
public Database(DatabaseType type, String hostname, int port, String username, String password, String prefix, String schema, int eventInterval, int userInterval) {
this.type = type;
this.hostname = hostname;
this.port = port;
@@ -40,6 +66,9 @@ public class Database {
this.password = password;
this.prefix = prefix;
this.schema = schema;
this.eventInterval = eventInterval;
this.userInterval = userInterval;
}
public DatabaseType getType() {
@@ -69,4 +98,122 @@ public class Database {
public String getSchema() {
return schema;
}
public void connect() {
String url = type.toString().toLowerCase() + "://" + hostname + ":" + port + "/" + schema;
try {
connection = DriverManager.getConnection(url, username, password);
eventBatch = connection.prepareStatement(SQL_LOG_EVENT);
syncBatch = connection.prepareStatement(SQL_SYNC_TO);
connection.setAutoCommit(false);
eventTask = Bukkit.getScheduler().runTaskTimerAsynchronously(AntiCheat.getPlugin(), new Runnable(){
@Override
public void run() {
flushEvents();
}
}, eventInterval*60*20, eventInterval*60*20);
userTask = Bukkit.getScheduler().runTaskTimerAsynchronously(AntiCheat.getPlugin(), new Runnable() {
@Override
public void run() {
flushSyncs();
}
}, userInterval * 60 * 20, userInterval * 60 * 20);
} catch (SQLException e) {
e.printStackTrace();
}
//TODO: setup tables
}
public void shutdown() {
eventTask.cancel();
userTask.cancel();
flushEvents();
flushSyncs();
try {
eventBatch.close();
syncBatch.close();
connection.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
public Connection getConnection() {
return connection;
}
public void logEvent(String serverName, User user, CheckType checkType) {
try {
eventBatch.setString(1, EVENTS_TABLE);
eventBatch.setString(2, serverName);
eventBatch.setString(3, user.getName());
eventBatch.setString(4, checkType.toString());
eventBatch.addBatch();
} catch (SQLException e) {
e.printStackTrace();
}
}
public void syncTo(User user) {
try {
syncBatch.setString(1, USERS_TABLE);
syncBatch.setString(2, user.getName());
syncBatch.setInt(3, user.getLevel());
syncBatch.setInt(4, user.getLevel());
syncBatch.addBatch();
} 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(SQL_SYNC_FROM);
statement.setString(1, USERS_TABLE);
statement.setString(2, user.getName());
ResultSet set = statement.executeQuery();
while (set.next()) {
user.setLevel(set.getInt("level"));
}
} catch (SQLException e) {
e.printStackTrace();
}
}
});
}
public void flushEvents() {
try {
eventBatch.executeBatch();
connection.commit();
eventBatch = connection.prepareStatement(SQL_LOG_EVENT);
} catch (SQLException e) {
e.printStackTrace();
}
}
public void flushSyncs() {
try {
syncBatch.executeBatch();
connection.commit();
syncBatch = connection.prepareStatement(SQL_SYNC_TO);
} catch (SQLException e) {
e.printStackTrace();
}
}
}