Merged worldNames and worldTables to a hashmap.

This commit is contained in:
Robin Kupper
2011-04-04 03:08:03 +02:00
parent b321668cde
commit 7e505d4803
4 changed files with 17 additions and 32 deletions

View File

@@ -20,7 +20,7 @@ public class ClearLog implements Runnable
Statement state = null; Statement state = null;
try { try {
state = conn.createStatement(); state = conn.createStatement();
for (String table : LogBlock.config.worldTables) { for (String table : LogBlock.config.tables.values()) {
int deleted = state.executeUpdate("DELETE FROM `" + table + "` WHERE date < date_sub(now(), INTERVAL " + LogBlock.config.keepLogDays + " DAY)"); int deleted = state.executeUpdate("DELETE FROM `" + table + "` WHERE date < date_sub(now(), INTERVAL " + LogBlock.config.keepLogDays + " DAY)");
if (deleted > 0) if (deleted > 0)
LogBlock.log.info("[LogBlock] Cleared out table " + table + ". Deleted " + deleted + " entries."); LogBlock.log.info("[LogBlock] Cleared out table " + table + ". Deleted " + deleted + " entries.");

View File

@@ -1,13 +1,13 @@
package de.diddiz.LogBlock; package de.diddiz.LogBlock;
import java.util.Arrays; import java.util.Arrays;
import java.util.HashMap;
import java.util.List; import java.util.List;
import org.bukkit.util.config.Configuration; import org.bukkit.util.config.Configuration;
public class Config { public class Config {
List<String> worldNames; HashMap<Integer, String> tables;
List<String> worldTables;
String dbDriver; String dbDriver;
String dbUrl; String dbUrl;
String dbUsername; String dbUsername;
@@ -31,7 +31,7 @@ public class Config {
String logLeavesDecayAs; String logLeavesDecayAs;
boolean usePermissions; boolean usePermissions;
Config (LogBlock logblock) { Config (LogBlock logblock) throws Exception {
Configuration config = logblock.getConfiguration(); Configuration config = logblock.getConfiguration();
config.load(); config.load();
List<String> keys = config.getKeys(null); List<String> keys = config.getKeys(null);
@@ -87,8 +87,6 @@ public class Config {
config.setProperty("usePermissions", false); config.setProperty("usePermissions", false);
if (!config.save()) if (!config.save())
LogBlock.log.severe("[LogBlock] Error while writing to config.yml"); LogBlock.log.severe("[LogBlock] Error while writing to config.yml");
worldNames = config.getStringList("worldNames", null);
worldTables = config.getStringList("worldTables", null);
dbDriver = config.getString("driver"); dbDriver = config.getString("driver");
dbUrl = config.getString("url"); dbUrl = config.getString("url");
dbUsername = config.getString("username"); dbUsername = config.getString("username");
@@ -111,5 +109,13 @@ public class Config {
logFireAs = config.getString("logFireAs"); logFireAs = config.getString("logFireAs");
logLeavesDecayAs = config.getString("logLeavesDecayAs"); logLeavesDecayAs = config.getString("logLeavesDecayAs");
usePermissions = config.getBoolean("usePermissions", false); usePermissions = config.getBoolean("usePermissions", false);
List<String> worldNames = config.getStringList("worldNames", null);
List<String> worldTables = config.getStringList("worldTables", null);
tables = new HashMap<Integer, String>();
if (worldNames == null || worldTables == null || worldNames.size() == 0 || worldNames.size() != worldTables.size())
throw new Exception("worldNames or worldTables not set porperly");
for (int i = 0; i < worldNames.size(); i++) {
tables.put(worldNames.get(i).hashCode(), worldTables.get(i));
}
} }
} }

View File

@@ -30,7 +30,7 @@ public class Consumer implements Runnable
public void queueBlock(String playerName, Block block, int typeBefore, int typeAfter, byte data, String signtext, ChestAccess ca) { public void queueBlock(String playerName, Block block, int typeBefore, int typeAfter, byte data, String signtext, ChestAccess ca) {
if (block == null || typeBefore < 0 || typeAfter < 0) if (block == null || typeBefore < 0 || typeAfter < 0)
return; return;
String table = LogBlock.config.worldTables.get(LogBlock.config.worldNames.indexOf(block.getWorld().getName())); String table = LogBlock.config.tables.get(block.getWorld().getName().hashCode());
if (table == null) if (table == null)
return; return;
if (playerName.length() > 32) if (playerName.length() > 32)

View File

@@ -80,11 +80,6 @@ public class LogBlock extends JavaPlugin
getServer().getPluginManager().disablePlugin(this); getServer().getPluginManager().disablePlugin(this);
return; return;
} }
if (config.worldNames == null || config.worldTables == null || config.worldNames.size() == 0 || config.worldNames.size() != config.worldTables.size()) {
log.log(Level.SEVERE, "[LogBlock] worldNames or worldTables not set porperly");
getServer().getPluginManager().disablePlugin(this);
return;
}
if (!checkTables()) { if (!checkTables()) {
log.log(Level.SEVERE, "[LogBlock] Errors while checking tables. They may not exist."); log.log(Level.SEVERE, "[LogBlock] Errors while checking tables. They may not exist.");
getServer().getPluginManager().disablePlugin(this); getServer().getPluginManager().disablePlugin(this);
@@ -138,7 +133,7 @@ public class LogBlock extends JavaPlugin
} }
Player player = (Player)sender; Player player = (Player)sender;
Connection conn = getConnection(); Connection conn = getConnection();
String table = getTable(player); String table = config.tables.get(player.getWorld().getName().hashCode());
if (conn == null) { if (conn == null) {
player.sendMessage(ChatColor.RED + "Can't create SQL connection."); player.sendMessage(ChatColor.RED + "Can't create SQL connection.");
return true; return true;
@@ -367,8 +362,7 @@ public class LogBlock extends JavaPlugin
return false; return false;
} }
state.execute("INSERT IGNORE INTO `lb-players` (`playername`) VALUES ('" + config.logTNTExplosionsAs + "'), ('" + config.logCreeperExplosionsAs + "'), ('" + config.logFireAs + "'), ('" + config.logLeavesDecayAs + "'), ('" + config.logFireballExplosionsAs + "'), ('Environment')"); state.execute("INSERT IGNORE INTO `lb-players` (`playername`) VALUES ('" + config.logTNTExplosionsAs + "'), ('" + config.logCreeperExplosionsAs + "'), ('" + config.logFireAs + "'), ('" + config.logLeavesDecayAs + "'), ('" + config.logFireballExplosionsAs + "'), ('Environment')");
for (int i = 0; i < config.worldNames.size(); i++) { for (String table : config.tables.values()) {
String table = config.worldTables.get(i);
if (!dbm.getTables(null, null, table, null).next()) { if (!dbm.getTables(null, null, table, null).next()) {
log.log(Level.INFO, "[LogBlock] Crating table " + table + "."); log.log(Level.INFO, "[LogBlock] Crating table " + table + ".");
state.execute("CREATE TABLE `" + table + "` (`id` INT NOT NULL AUTO_INCREMENT, `date` datetime NOT NULL DEFAULT '0000-00-00 00:00:00', `playerid` SMALLINT UNSIGNED NOT NULL DEFAULT '0', `replaced` TINYINT UNSIGNED NOT NULL DEFAULT '0', `type` TINYINT UNSIGNED NOT NULL DEFAULT '0', `data` TINYINT UNSIGNED NOT NULL DEFAULT '0', `x` SMALLINT NOT NULL DEFAULT '0', `y` TINYINT UNSIGNED NOT NULL DEFAULT '0',`z` SMALLINT NOT NULL DEFAULT '0', PRIMARY KEY (`id`), KEY `coords` (`y`,`x`,`z`), KEY `type` (`type`), KEY `data` (`data`), KEY `replaced` (`replaced`));"); state.execute("CREATE TABLE `" + table + "` (`id` INT NOT NULL AUTO_INCREMENT, `date` datetime NOT NULL DEFAULT '0000-00-00 00:00:00', `playerid` SMALLINT UNSIGNED NOT NULL DEFAULT '0', `replaced` TINYINT UNSIGNED NOT NULL DEFAULT '0', `type` TINYINT UNSIGNED NOT NULL DEFAULT '0', `data` TINYINT UNSIGNED NOT NULL DEFAULT '0', `x` SMALLINT NOT NULL DEFAULT '0', `y` TINYINT UNSIGNED NOT NULL DEFAULT '0',`z` SMALLINT NOT NULL DEFAULT '0', PRIMARY KEY (`id`), KEY `coords` (`y`,`x`,`z`), KEY `type` (`type`), KEY `data` (`data`), KEY `replaced` (`replaced`));");
@@ -404,21 +398,6 @@ public class LogBlock extends JavaPlugin
return false; return false;
} }
private String getTable (Player player) {
return getTable(player.getWorld().getName());
}
private String getTable (Block block) {
return getTable(block.getWorld().getName());
}
private String getTable (String worldName) {
int idx = config.worldNames.indexOf(worldName);
if (idx == -1)
return null;
return config.worldTables.get(idx);
}
private boolean CheckPermission(Player player, String permission) { private boolean CheckPermission(Player player, String permission) {
if (config.usePermissions) if (config.usePermissions)
return Permissions.Security.permission(player, permission); return Permissions.Security.permission(player, permission);
@@ -555,10 +534,10 @@ public class LogBlock extends JavaPlugin
public void onPlayerInteract(PlayerInteractEvent event) { public void onPlayerInteract(PlayerInteractEvent event) {
if (!event.isCancelled()) { if (!event.isCancelled()) {
if (event.getAction() == Action.RIGHT_CLICK_BLOCK && event.getMaterial().getId() == LogBlock.config.toolID && CheckPermission(event.getPlayer(), "logblock.lookup")) { if (event.getAction() == Action.RIGHT_CLICK_BLOCK && event.getMaterial().getId() == LogBlock.config.toolID && CheckPermission(event.getPlayer(), "logblock.lookup")) {
getServer().getScheduler().scheduleAsyncDelayedTask(LogBlock.this, new BlockStats(getConnection(), event.getPlayer(), event.getClickedBlock(), getTable(event.getClickedBlock()))); getServer().getScheduler().scheduleAsyncDelayedTask(LogBlock.this, new BlockStats(getConnection(), event.getPlayer(), event.getClickedBlock(), config.tables.get(event.getPlayer().getWorld().getName().hashCode())));
event.setCancelled(true); event.setCancelled(true);
} else if (event.getAction() == Action.RIGHT_CLICK_BLOCK && event.getMaterial().getId() == LogBlock.config.toolblockID && CheckPermission(event.getPlayer(), "logblock.lookup")) { } else if (event.getAction() == Action.RIGHT_CLICK_BLOCK && event.getMaterial().getId() == LogBlock.config.toolblockID && CheckPermission(event.getPlayer(), "logblock.lookup")) {
getServer().getScheduler().scheduleAsyncDelayedTask(LogBlock.this, new BlockStats(getConnection(), event.getPlayer(), event.getClickedBlock().getFace(event.getBlockFace()), getTable(event.getClickedBlock()))); getServer().getScheduler().scheduleAsyncDelayedTask(LogBlock.this, new BlockStats(getConnection(), event.getPlayer(), event.getClickedBlock().getFace(event.getBlockFace()), config.tables.get(event.getPlayer().getWorld().getName().hashCode())));
if (config.toolblockRemove) if (config.toolblockRemove)
event.setCancelled(true); event.setCancelled(true);
} }