From 5dcecdc08e590c168c22b6c7e1f11c234ae91474 Mon Sep 17 00:00:00 2001 From: Robin Kupper Date: Fri, 11 Mar 2011 16:54:24 +0100 Subject: [PATCH] improved database and added signlogging --- .gitignore | 3 +- src/de/diddiz/LogBlock/AreaBlockSearch.java | 32 +- src/de/diddiz/LogBlock/AreaStats.java | 66 ++- src/de/diddiz/LogBlock/BlockStats.java | 88 ++++ src/de/diddiz/LogBlock/Config.java | 8 + src/de/diddiz/LogBlock/LogBlock.java | 482 ++++++++++--------- src/de/diddiz/LogBlock/PlayerAreaStats.java | 70 ++- src/de/diddiz/LogBlock/PlayerWorldStats.java | 89 ---- src/de/diddiz/LogBlock/Rollback.java | 19 +- 9 files changed, 428 insertions(+), 429 deletions(-) create mode 100644 src/de/diddiz/LogBlock/BlockStats.java delete mode 100644 src/de/diddiz/LogBlock/PlayerWorldStats.java diff --git a/.gitignore b/.gitignore index ea7eba2..736dc24 100644 --- a/.gitignore +++ b/.gitignore @@ -1,4 +1,5 @@ /.classpath /.project /.settings -/bin \ No newline at end of file +/bin +/release \ No newline at end of file diff --git a/src/de/diddiz/LogBlock/AreaBlockSearch.java b/src/de/diddiz/LogBlock/AreaBlockSearch.java index e6bdd6a..f566243 100644 --- a/src/de/diddiz/LogBlock/AreaBlockSearch.java +++ b/src/de/diddiz/LogBlock/AreaBlockSearch.java @@ -5,7 +5,6 @@ import java.sql.PreparedStatement; import java.sql.ResultSet; import java.sql.SQLException; import java.sql.Statement; -import java.sql.Timestamp; import java.text.SimpleDateFormat; import java.util.logging.Level; @@ -22,7 +21,7 @@ public class AreaBlockSearch implements Runnable private int size; private Connection conn = null; private String table; - + AreaBlockSearch(Connection conn, Player player, int type, int size, String table) { this.player = player; this.location = player.getLocation(); @@ -31,17 +30,15 @@ public class AreaBlockSearch implements Runnable this.conn = conn; this.table = table; } - + public void run() { boolean hist = false; PreparedStatement ps = null; ResultSet rs = null; - Timestamp date; - SimpleDateFormat formatter = new SimpleDateFormat("MM-dd hh:mm:ss"); - + SimpleDateFormat formatter = new SimpleDateFormat("MM-dd HH:mm:ss"); try { conn.setAutoCommit(false); - ps = conn.prepareStatement("SELECT * FROM `" + table + "` WHERE (type = ? or replaced = ?) and y > ? and y < ? and x > ? and x < ? and z > ? and z < ? order by date desc limit 10", Statement.RETURN_GENERATED_KEYS); + ps = conn.prepareStatement("SELECT * FROM `" + table + "` INNER JOIN `players` USING (`playerid`) WHERE (type = ? or replaced = ?) and y > ? and y < ? and x > ? and x < ? and z > ? and z < ? order by date desc limit 10", Statement.RETURN_GENERATED_KEYS); ps.setInt(1, type); ps.setInt(2, type); ps.setInt(3, location.getBlockY() - size); @@ -51,25 +48,22 @@ public class AreaBlockSearch implements Runnable ps.setInt(7, location.getBlockZ() - size); ps.setInt(8, location.getBlockZ() + size); rs = ps.executeQuery(); - player.sendMessage(ChatColor.DARK_AQUA + "Block history for " + getMaterialName(type) + " within " + size + " blocks of " + location.getBlockX() + ", " + location.getBlockY() + ", " + location.getBlockZ() + ": "); - - while (rs.next()) - { - date = rs.getTimestamp("date"); - String datestr = formatter.format(date); - String msg = datestr + " " + rs.getString("player") + " (" + rs.getInt("x") + ", " + rs.getInt("y") + ", " + rs.getInt("z") + ") "; + while (rs.next()) { + String msg = formatter.format(rs.getTimestamp("date")) + " " + rs.getString("playername") + " (" + rs.getInt("x") + ", " + rs.getInt("y") + ", " + rs.getInt("z") + ") "; if (rs.getInt("type") == 0) msg = msg + "destroyed " + getMaterialName(rs.getInt("replaced")); else if (rs.getInt("replaced") == 0) msg = msg + "created " + getMaterialName(rs.getInt("type")); else msg = msg + "replaced " + getMaterialName(rs.getInt("replaced")) + " with " + getMaterialName(rs.getInt("type")); - player.sendMessage("§6" + msg); + player.sendMessage(ChatColor.GOLD + msg); hist = true; } + if (!hist) + player.sendMessage(ChatColor.DARK_AQUA + "None."); } catch (SQLException ex) { - LogBlock.log.log(Level.SEVERE, this.getClass().getName() + " SQL exception", ex); + LogBlock.log.log(Level.SEVERE, "[LogBlock AreaBlockSearch] SQL exception", ex); } finally { try { if (rs != null) @@ -79,13 +73,11 @@ public class AreaBlockSearch implements Runnable if (conn != null) conn.close(); } catch (SQLException ex) { - LogBlock.log.log(Level.SEVERE, this.getClass().getName() + " SQL exception on close", ex); + LogBlock.log.log(Level.SEVERE, "[LogBlock AreaBlockSearch] SQL exception on close", ex); } } - if (!hist) - player.sendMessage("§3None."); } - + private String getMaterialName(int type) { return Material.getMaterial(type).toString().toLowerCase().replace('_', ' '); } diff --git a/src/de/diddiz/LogBlock/AreaStats.java b/src/de/diddiz/LogBlock/AreaStats.java index ffa16c8..9485add 100644 --- a/src/de/diddiz/LogBlock/AreaStats.java +++ b/src/de/diddiz/LogBlock/AreaStats.java @@ -9,6 +9,7 @@ import java.util.HashMap; import java.util.HashSet; import java.util.logging.Level; +import org.bukkit.ChatColor; import org.bukkit.entity.Player; public class AreaStats implements Runnable @@ -17,26 +18,23 @@ public class AreaStats implements Runnable private int size; private Connection conn = null; private String table; - - AreaStats(Connection conn, Player player, int size, String table) - { + + AreaStats(Connection conn, Player player, int size, String table) { this.player = player; this.size = size; this.conn = conn; this.table = table; } - public void run() - { + + public void run() { HashSet players = new HashSet(); HashMap created = new HashMap(); HashMap destroyed = new HashMap(); - PreparedStatement ps = null; ResultSet rs = null; - try { conn.setAutoCommit(false); - ps = conn.prepareStatement("SELECT player, count(player) as num from `" + table + "` where type > 0 and y > ? and y < ? and x > ? and x < ? and z > ? and z < ? group by player order by count(player) desc limit 10", Statement.RETURN_GENERATED_KEYS); + ps = conn.prepareStatement("SELECT playername, count(playername) as num from `" + table + "` INNER JOIN `players` USING (`playerid`) where type > 0 and y > ? and y < ? and x > ? and x < ? and z > ? and z < ? group by playername order by count(playername) desc limit 10", Statement.RETURN_GENERATED_KEYS); ps.setInt(1, player.getLocation().getBlockY()-size); ps.setInt(2, player.getLocation().getBlockY()+size); ps.setInt(3, player.getLocation().getBlockX()-size); @@ -44,15 +42,13 @@ public class AreaStats implements Runnable ps.setInt(5, player.getLocation().getBlockZ()-size); ps.setInt(6, player.getLocation().getBlockZ()+size); rs = ps.executeQuery(); - while (rs.next()) - { - players.add(rs.getString("player")); - created.put(rs.getString("player"), rs.getInt("num")); + while (rs.next()) { + players.add(rs.getString("playername")); + created.put(rs.getString("playername"), rs.getInt("num")); } rs.close(); ps.close(); - - ps = conn.prepareStatement("SELECT player, count(player) as num from `" + table + "` where replaced > 0 and y > ? and y < ? and x > ? and x < ? and z > ? and z < ? group by player order by count(player) desc limit 10", Statement.RETURN_GENERATED_KEYS); + ps = conn.prepareStatement("SELECT playername, count(playername) as num from `" + table + "` INNER JOIN `players` USING (`playerid`) where replaced > 0 and y > ? and y < ? and x > ? and x < ? and z > ? and z < ? group by playername order by count(playername) desc limit 10", Statement.RETURN_GENERATED_KEYS); ps.setInt(1, player.getLocation().getBlockY()-size); ps.setInt(2, player.getLocation().getBlockY()+size); ps.setInt(3, player.getLocation().getBlockX()-size); @@ -60,14 +56,12 @@ public class AreaStats implements Runnable ps.setInt(5, player.getLocation().getBlockZ()-size); ps.setInt(6, player.getLocation().getBlockZ()+size); rs = ps.executeQuery(); - while (rs.next()) - { - players.add(rs.getString("player")); - destroyed.put(rs.getString("player"), rs.getInt("num")); + while (rs.next()) { + players.add(rs.getString("playername")); + destroyed.put(rs.getString("playername"), rs.getInt("num")); } - } catch (SQLException ex) { - LogBlock.log.log(Level.SEVERE, this.getClass().getName() + " SQL exception", ex); + LogBlock.log.log(Level.SEVERE, "[LogBlock AreaStats] SQL exception", ex); } finally { try { if (rs != null) @@ -77,27 +71,23 @@ public class AreaStats implements Runnable if (conn != null) conn.close(); } catch (SQLException ex) { - LogBlock.log.log(Level.SEVERE, this.getClass().getName() + " SQL exception on close", ex); + LogBlock.log.log(Level.SEVERE, "[LogBlock AreaStats] SQL exception on close", ex); } } - - player.sendMessage("§3Within " + size + " blocks of you: "); + player.sendMessage(ChatColor.DARK_AQUA + "Within " + size + " blocks of you: "); if (players.size() == 0) - { - player.sendMessage("§3No results found."); - return; - } - - player.sendMessage("§6" + String.format("%-6s %-6s %s", "Creat", "Destr", "Player")); - for (String p: players) - { - Integer c = created.get(p); - Integer d = destroyed.get(p); - if (c == null) - c = 0; - if (d == null) - d = 0; - player.sendMessage("§6" + String.format("%-6d %-6d %s", c, d, p)); + player.sendMessage(ChatColor.DARK_AQUA + "No results found."); + else { + player.sendMessage(ChatColor.GOLD + String.format("%-6s %-6s %s", "Creat", "Destr", "Player")); + for (String p: players) { + Integer c = created.get(p); + Integer d = destroyed.get(p); + if (c == null) + c = 0; + if (d == null) + d = 0; + player.sendMessage(ChatColor.GOLD + String.format("%-6d %-6d %s", c, d, p)); + } } } } diff --git a/src/de/diddiz/LogBlock/BlockStats.java b/src/de/diddiz/LogBlock/BlockStats.java new file mode 100644 index 0000000..1e37a48 --- /dev/null +++ b/src/de/diddiz/LogBlock/BlockStats.java @@ -0,0 +1,88 @@ +package de.diddiz.LogBlock; + +import java.sql.Connection; +import java.sql.PreparedStatement; +import java.sql.ResultSet; +import java.sql.SQLException; +import java.sql.Statement; +import java.text.SimpleDateFormat; +import java.util.logging.Level; + +import org.bukkit.ChatColor; +import org.bukkit.Material; +import org.bukkit.block.Block; +import org.bukkit.entity.Player; + +public class BlockStats implements Runnable +{ + private Player player; + private Block block; + private Connection conn; + private String table; + + BlockStats(Connection conn, Player player, Block block, String table) { + this.player = player; + this.conn = conn; + this.block = block; + this.table = table; + } + + @Override + public void run() { + if (conn == null) { + player.sendMessage(ChatColor.RED + "Failed to create database connection"); + return; + } + if (table == null) { + player.sendMessage(ChatColor.RED + "This world isn't logged"); + return; + } + boolean hist = false; + PreparedStatement ps = null; + ResultSet rs = null; + SimpleDateFormat formatter = new SimpleDateFormat("MM-dd HH:mm:ss"); + try { + conn.setAutoCommit(false); + ps = conn.prepareStatement("SELECT * FROM `" + table + "` LEFT JOIN `" + table + "-sign` USING (`id`) INNER JOIN `players` USING (`playerid`) WHERE `x` = ? AND `y` = ? AND `z` = ? ORDER BY `date` DESC", Statement.RETURN_GENERATED_KEYS); + ps.setInt(1, block.getX()); + ps.setInt(2, block.getY()); + ps.setInt(3, block.getZ()); + rs = ps.executeQuery(); + player.sendMessage(ChatColor.DARK_AQUA + "Block history (" + block.getX() + ", " + block.getY() + ", " + block.getZ() + "): "); + while (rs.next()) { + String msg = formatter.format(rs.getTimestamp("date")) + " " + rs.getString("playername") + " "; + if ((rs.getInt("type") == 63 || rs.getInt("type") == 68) && rs.getString("signtext") != null) + msg += "created " + rs.getString("signtext"); + else if (rs.getInt("type") == 54 && rs.getInt("replaced") == 54) + msg += "looked inside"; + else if (rs.getInt("type") == 0) + msg += "destroyed " + getMaterialName(rs.getInt("replaced")); + else if (rs.getInt("replaced") == 0) + msg += "created " + getMaterialName(rs.getInt("type")); + else + msg += "replaced " + getMaterialName(rs.getInt("replaced")) + " with " + getMaterialName(rs.getInt("type")); + player.sendMessage(ChatColor.GOLD + msg); + hist = true; + } + if (!hist) + player.sendMessage(ChatColor.DARK_AQUA + "None."); + } catch (SQLException ex) { + LogBlock.log.log(Level.SEVERE, "[LogBlock BlockStats] SQL exception", ex); + } finally { + try { + if (rs != null) + rs.close(); + if (ps != null) + ps.close(); + if (conn != null) + conn.close(); + } catch (SQLException ex) { + LogBlock.log.log(Level.SEVERE, "[LogBlock BlockStats] SQL exception on close", ex); + } + } + } + + private String getMaterialName(int type) { + return Material.getMaterial(type).toString().toLowerCase().replace('_', ' '); + } +} \ No newline at end of file diff --git a/src/de/diddiz/LogBlock/Config.java b/src/de/diddiz/LogBlock/Config.java index 8b8b5f1..a4c06bb 100644 --- a/src/de/diddiz/LogBlock/Config.java +++ b/src/de/diddiz/LogBlock/Config.java @@ -19,8 +19,10 @@ public class Config { static int toolID; static int toolblockID; static boolean toolblockRemove; + static boolean logSignTexts; static boolean logExplosions; static boolean logFire; + static boolean logChestAccess; static boolean usePermissions; static boolean Load(Configuration config) { @@ -52,10 +54,14 @@ public class Config { config.setProperty("toolblockID", 7); if (!keys.contains("toolblockRemove")) config.setProperty("toolblockRemove", true); + if (!keys.contains("logSignTexts")) + config.setProperty("logSignTexts", false); if (!keys.contains("logExplosions")) config.setProperty("logExplosions", false); if (!keys.contains("logFire")) config.setProperty("logFire", false); + if (!keys.contains("logChestAccess")) + config.setProperty("logChestAccess", false); if (!keys.contains("usePermissions")) config.setProperty("usePermissions", false); if (!config.save()){ @@ -75,8 +81,10 @@ public class Config { toolID = config.getInt("toolID", 270); toolblockID = config.getInt("toolblockID", 7); toolblockRemove = config.getBoolean("toolblockRemove", true); + logSignTexts = config.getBoolean("logSignTexts", false); logExplosions = config.getBoolean("logExplosions", false); logFire = config.getBoolean("logFire", false); + logChestAccess = config.getBoolean("logChestAccess", false); usePermissions = config.getBoolean("usePermissions", false); return true; } diff --git a/src/de/diddiz/LogBlock/LogBlock.java b/src/de/diddiz/LogBlock/LogBlock.java index 327fd91..27801f4 100644 --- a/src/de/diddiz/LogBlock/LogBlock.java +++ b/src/de/diddiz/LogBlock/LogBlock.java @@ -7,8 +7,6 @@ import java.sql.PreparedStatement; import java.sql.ResultSet; import java.sql.SQLException; import java.sql.Statement; -import java.sql.Timestamp; -import java.text.SimpleDateFormat; import java.util.ArrayList; import java.util.concurrent.LinkedBlockingQueue; import java.util.concurrent.TimeUnit; @@ -26,12 +24,14 @@ import org.bukkit.event.Event; import org.bukkit.event.Event.Type; import org.bukkit.event.block.BlockBreakEvent; import org.bukkit.event.block.BlockBurnEvent; +import org.bukkit.event.block.BlockInteractEvent; import org.bukkit.event.block.BlockListener; import org.bukkit.event.block.BlockPlaceEvent; import org.bukkit.event.block.BlockRightClickEvent; import org.bukkit.event.block.SignChangeEvent; import org.bukkit.event.entity.EntityExplodeEvent; import org.bukkit.event.entity.EntityListener; +import org.bukkit.event.player.PlayerEvent; import org.bukkit.event.player.PlayerItemEvent; import org.bukkit.event.player.PlayerListener; import org.bukkit.plugin.PluginManager; @@ -49,29 +49,23 @@ public class LogBlock extends JavaPlugin private ArrayList sessions = new ArrayList(); @Override - public void onEnable() - { + public void onEnable() { log = getServer().getLogger(); try { Config.Load(getConfiguration()); if (Config.usePermissions) { - if (getServer().getPluginManager().getPlugin("Permissions") != null) { - Config.usePermissions = true; + if (getServer().getPluginManager().getPlugin("Permissions") != null) log.info("[LogBlock] Permissions enabled"); - } else - log.info("[LogBlock] Permissions plugin not found. Using default permissions."); + else { + Config.usePermissions = true; + log.warning("[LogBlock] Permissions plugin not found. Using default permissions."); + } } - } catch (Exception e) { - log.log(Level.SEVERE, "[LogBlock] Exception while reading config.yml", e); - getServer().getPluginManager().disablePlugin(this); - return; - } - try { new JDCConnectionDriver(Config.dbDriver, Config.dbUrl, Config.dbUsername, Config.dbPassword); Connection conn = getConnection(); conn.close(); } catch (Exception ex) { - log.log(Level.SEVERE, "[LogBlock] Exception while creation database connection", ex); + log.log(Level.SEVERE, "[LogBlock] Exception while enabling", ex); getServer().getPluginManager().disablePlugin(this); return; } @@ -80,34 +74,35 @@ public class LogBlock extends JavaPlugin getServer().getPluginManager().disablePlugin(this); return; } - for (int i = 0; i < Config.worldNames.size(); i++) { - if (!checkTables(Config.worldTables.get(i))) { - log.log(Level.SEVERE, "[LogBlock] Errors while checking tables. They may not exist."); - getServer().getPluginManager().disablePlugin(this); - return; - } + if (!checkTables()) { + log.log(Level.SEVERE, "[LogBlock] Errors while checking tables. They may not exist."); + getServer().getPluginManager().disablePlugin(this); + return; } if (Config.keepLogDays >= 0) dropOldLogs(); - LBLBlockListener lblBlockListener = new LBLBlockListener(); + LBBlockListener lbBlockListener = new LBBlockListener(); + LBPlayerListener lbPlayerListener = new LBPlayerListener(); PluginManager pm = getServer().getPluginManager(); - pm.registerEvent(Type.PLAYER_ITEM, new LBLPlayerListener(), Event.Priority.Monitor, this); - pm.registerEvent(Type.BLOCK_RIGHTCLICKED, lblBlockListener, Event.Priority.Monitor, this); - pm.registerEvent(Type.BLOCK_PLACED, lblBlockListener, Event.Priority.Monitor, this); - pm.registerEvent(Type.BLOCK_BREAK, lblBlockListener, Event.Priority.Monitor, this); - pm.registerEvent(Type.SIGN_CHANGE, lblBlockListener, Event.Priority.Monitor, this); + pm.registerEvent(Type.PLAYER_ITEM, lbPlayerListener, Event.Priority.Monitor, this); + pm.registerEvent(Type.PLAYER_JOIN, lbPlayerListener, Event.Priority.Normal, this); + pm.registerEvent(Type.BLOCK_RIGHTCLICKED, lbBlockListener, Event.Priority.Monitor, this); + pm.registerEvent(Type.BLOCK_PLACED, lbBlockListener, Event.Priority.Monitor, this); + pm.registerEvent(Type.BLOCK_BREAK, lbBlockListener, Event.Priority.Monitor, this); + pm.registerEvent(Type.SIGN_CHANGE, lbBlockListener, Event.Priority.Monitor, this); if (Config.logFire) - pm.registerEvent(Type.BLOCK_BURN, lblBlockListener, Event.Priority.Monitor, this); + pm.registerEvent(Type.BLOCK_BURN, lbBlockListener, Event.Priority.Monitor, this); if (Config.logExplosions) - pm.registerEvent(Type.ENTITY_EXPLODE, new LBLEntityListener(), Event.Priority.Monitor, this); + pm.registerEvent(Type.ENTITY_EXPLODE, new LBEntityListener(), Event.Priority.Monitor, this); + if (Config.logChestAccess) + pm.registerEvent(Type.BLOCK_INTERACT, lbBlockListener, Event.Priority.Monitor, this); consumer = new Consumer(); new Thread(consumer).start(); log.info("Logblock v" + getDescription().getVersion() + " enabled."); } - + @Override - public void onDisable() - { + public void onDisable() { if (consumer != null) { consumer.stop(); consumer = null; @@ -123,7 +118,7 @@ public class LogBlock extends JavaPlugin Player player = (Player)sender; Connection conn = getConnection(); if (conn != null) { - String table = getTable(player.getWorld().getName()); + String table = getTable(player); if (table != null) { if (CheckPermission(player,"logblock.area")) { if (args.length == 0) { @@ -134,7 +129,7 @@ public class LogBlock extends JavaPlugin radius = Integer.parseInt(args[1]); new Thread(new AreaStats(conn, player, radius, table)).start(); } else if (args[0].equalsIgnoreCase("world")) { - new Thread(new PlayerWorldStats(conn, player, table)).start(); + new Thread(new AreaStats(conn, player, Short.MAX_VALUE, table)).start(); } else if (args[0].equalsIgnoreCase("player")) { if (args.length >= 2) { int radius = Config.defaultDist; @@ -260,33 +255,49 @@ public class LogBlock extends JavaPlugin } } - private boolean checkTables(String table) - { - Connection conn = null; - ResultSet rs = null; + private boolean checkTables() { + Connection conn = getConnection(); + Statement state = null; + if (conn == null) + return false; try { - conn = getConnection(); - if (conn == null) - return false; DatabaseMetaData dbm = conn.getMetaData(); - rs = dbm.getTables(null, null, table, null); - if (!rs.next()) { - log.log(Level.SEVERE, "[LogBlock] Crating table " + table + "."); - conn.createStatement().execute("CREATE TABLE `" + table + "` (`id` int(11) NOT NULL AUTO_INCREMENT, `date` datetime NOT NULL DEFAULT '0000-00-00 00:00:00', `player` varchar(32) NOT NULL DEFAULT '-', `replaced` int(11) NOT NULL DEFAULT '0', `type` int(11) NOT NULL DEFAULT '0', `data` TINYINT NOT NULL DEFAULT '0', `x` int(11) NOT NULL DEFAULT '0', `y` int(11) NOT NULL DEFAULT '0',`z` int(11) NOT NULL DEFAULT '0', PRIMARY KEY (`id`), KEY `coords` (`y`,`x`,`z`), KEY `type` (`type`), KEY `data` (`data`), KEY `replaced` (`replaced`), KEY `player` (`player`));"); + state = conn.createStatement(); + if (!dbm.getTables(null, null, "players", null).next()) { + log.log(Level.INFO, "[LogBlock] Crating table players."); + state.execute("CREATE TABLE `players` (`playerid` SMALLINT UNSIGNED NOT NULL AUTO_INCREMENT, `playername` varchar(32) NOT NULL DEFAULT '-', PRIMARY KEY (`playerid`), UNIQUE (`playername`))"); + if (!dbm.getTables(null, null, "players", null).next()) + return false; } - rs = dbm.getTables(null, null, table + "-extra", null); - if (!rs.next()) { - log.log(Level.SEVERE, "[LogBlock] Crating table " + table + "-extra."); - conn.createStatement().execute("CREATE TABLE `" + table + "-extra` (`id` int(11) NOT NULL, `extra` text, PRIMARY KEY (`id`));"); - return checkTables(table); + for (int i = 0; i < Config.worldNames.size(); i++) { + String table = Config.worldTables.get(i); + + if (!dbm.getTables(null, null, table, null).next()) { + log.log(Level.INFO, "[LogBlock] Crating table " + table + "."); + state.execute("CREATE TABLE `" + table + "` (`id` int(11) NOT NULL AUTO_INCREMENT, `date` datetime NOT NULL DEFAULT '0000-00-00 00:00:00', `player` varchar(32) NOT NULL DEFAULT '-', `replaced` int(11) NOT NULL DEFAULT '0', `type` int(11) NOT NULL DEFAULT '0', `data` TINYINT NOT NULL DEFAULT '0', `x` int(11) NOT NULL DEFAULT '0', `y` int(11) NOT NULL DEFAULT '0',`z` int(11) NOT NULL DEFAULT '0', PRIMARY KEY (`id`), KEY `coords` (`y`,`x`,`z`), KEY `type` (`type`), KEY `data` (`data`), KEY `replaced` (`replaced`), KEY `player` (`player`));"); + if (!dbm.getTables(null, null, table, null).next()) + return false; + } + if (!dbm.getTables(null, null, table + "-sign", null).next()) { + log.log(Level.INFO, "[LogBlock] Crating table " + table + "-sign."); + state.execute("CREATE TABLE `" + table + "-sign` (`id` int(11) NOT NULL, `signtext` TEXT, PRIMARY KEY (`id`));"); + if (!dbm.getTables(null, null, table + "-sign", null).next()) + return false; + } + if (!dbm.getTables(null, null, table + "-chest", null).next()) { + log.log(Level.INFO, "[LogBlock] Crating table " + table + "-chest."); + state.execute("CREATE TABLE `" + table + "-chest` (`id` int(11) NOT NULL, `inID` SMALLINT, `inAmount` TINYINT, `outID` SMALLINT, `outAmount` TINYINT,PRIMARY KEY (`id`));"); + if (!dbm.getTables(null, null, table + "-chest", null).next()) + return false; + } } return true; } catch (SQLException ex) { - log.log(Level.SEVERE, "[LogBlock] SQL exception", ex); + log.log(Level.SEVERE, "[LogBlock] SQL exception while checking tables", ex); } finally { try { - if (rs != null) - rs.close(); + if (state != null) + state.close(); if (conn != null) conn.close(); } catch (SQLException ex) { @@ -296,8 +307,7 @@ public class LogBlock extends JavaPlugin return false; } - private void dropOldLogs() - { + private void dropOldLogs() { Connection conn = null; Statement state = null; try { @@ -323,6 +333,14 @@ public class LogBlock extends JavaPlugin } } + 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) @@ -330,94 +348,46 @@ public class LogBlock extends JavaPlugin return Config.worldTables.get(idx); } - - private void showBlockHistory(Player player, Block b) - { - player.sendMessage("§3Block history (" + b.getX() + ", " + b.getY() + ", " + b.getZ() + "): "); - boolean hist = false; - Connection conn = null; - PreparedStatement ps = null; - ResultSet rs = null; - Timestamp date; - SimpleDateFormat formatter = new SimpleDateFormat("MM-dd HH:mm:ss"); - String table = getTable(player.getWorld().getName()); - if (table == null) - return; - try { - conn = getConnection(); - conn.setAutoCommit(false); - ps = conn.prepareStatement("SELECT * from `" + table + "` left join `" + table + "-extra` using (id) where y = ? and x = ? and z = ? order by date desc limit 10", Statement.RETURN_GENERATED_KEYS); - ps.setInt(1, b.getY()); - ps.setInt(2, b.getX()); - ps.setInt(3, b.getZ()); - rs = ps.executeQuery(); - while (rs.next()) - { - date = rs.getTimestamp("date"); - String datestr = formatter.format(date); - String msg = datestr + " " + rs.getString("player") + " "; - if (rs.getInt("type") == 0) - msg = msg + "destroyed " + Material.getMaterial(rs.getInt("replaced")).toString().toLowerCase().replace('_', ' '); - else if (rs.getInt("replaced") == 0) - { - if (rs.getInt("type") == 63) - msg = msg + "created " + rs.getString("extra"); - else - msg = msg + "created " + Material.getMaterial(rs.getInt("type")).toString().toLowerCase().replace('_', ' '); - } - else - msg = msg + "replaced " + Material.getMaterial(rs.getInt("replaced")).toString().toLowerCase().replace('_', ' ') + " with " + Material.getMaterial(rs.getInt("type")).toString().toLowerCase().replace('_', ' '); - player.sendMessage("§6" + msg); - hist = true; - } - } catch (SQLException ex) { - log.log(Level.SEVERE, "[LogBlock] SQL exception", ex); - } finally { - try { - if (rs != null) - rs.close(); - if (ps != null) - ps.close(); - if (conn != null) - conn.close(); - } catch (SQLException ex) { - log.log(Level.SEVERE, "[LogBlock] SQL exception on close", ex); - } - } - if (!hist) - player.sendMessage(ChatColor.DARK_AQUA + "None."); - } - private void queueBlock(Player player, Block block, int typeAfter) { - queueBlock(player.getName(), block, 0, typeAfter, (byte)0, null); + queueBlock(player.getName(), block, 0, typeAfter, (byte)0, null, null); } - private void queueBlock(String playerName, Block block, int typeBefore, int typeAfter, byte data, String extra) { + private void queueBlock(String playerName, Block block, int typeBefore, int typeAfter, byte data) { + queueBlock(playerName, block, typeBefore, typeAfter, data, null, null); + } + + private void queueBlock(Player player, Block block, short inType, byte inAmount, short outType, byte outAmount) { + queueBlock(player.getName(), block, 54, 54, (byte)0, null, new ChestAccess(inType, inAmount, outType, outAmount)); + } + + private void queueBlock(String playerName, Block block, int typeBefore, int typeAfter, byte data, String signtext, ChestAccess ca) { if (block == null || typeBefore < 0 || typeAfter < 0) return; - String table = getTable(block.getWorld().getName()); + String table = getTable(block); if (table == null) return; BlockRow row = new BlockRow(table, playerName, typeBefore, typeAfter, data, block.getX(), block.getY(), block.getZ()); - if (extra != null) - row.addExtra(extra); + if (signtext != null) + row.signtext = signtext; + if (ca != null) + row.ca = ca; if (!bqueue.offer(row)) - log.info("[LogBlock] failed to queue block for " + playerName); + log.info("[LogBlock] Failed to queue block for " + playerName); } - private boolean CheckPermission(Player player, String permission) { - if (Config.usePermissions) - return Permissions.Security.permission(player, permission); - else { - if (permission.equals("logblock.lookup")) - return true; - else if (permission.equals("logblock.area")) - return player.isOp(); - else if (permission.equals("logblock.rollback")) - return player.isOp(); - } - return false; - } +private boolean CheckPermission(Player player, String permission) { + if (Config.usePermissions) + return Permissions.Security.permission(player, permission); + else { + if (permission.equals("logblock.lookup")) + return true; + else if (permission.equals("logblock.area")) + return player.isOp(); + else if (permission.equals("logblock.rollback")) + return player.isOp(); + } + return false; +} static int parseTimeSpec(String timespec) { String[] split = timespec.split(" "); @@ -425,7 +395,7 @@ public class LogBlock extends JavaPlugin return 0; return parseTimeSpec(split[0], split[1]); } - + static int parseTimeSpec(String time, String unit) { int min; try { @@ -440,13 +410,13 @@ public class LogBlock extends JavaPlugin return min; } - private class LBLPlayerListener extends PlayerListener + private class LBPlayerListener extends PlayerListener { public void onPlayerItem(PlayerItemEvent event) { if (!event.isCancelled() && event.getBlockClicked() != null) { switch (event.getMaterial()) { case BUCKET: - queueBlock(event.getPlayer().getName(), event.getBlockClicked(), event.getBlockClicked().getTypeId(), 0, event.getBlockClicked().getData(), null); + queueBlock(event.getPlayer().getName(), event.getBlockClicked(), event.getBlockClicked().getTypeId(), 0, event.getBlockClicked().getData()); break; case WATER_BUCKET: queueBlock(event.getPlayer(), event.getBlockClicked().getFace(event.getBlockFace()), Material.STATIONARY_WATER.getId()); @@ -465,59 +435,88 @@ public class LogBlock extends JavaPlugin case IRON_HOE: case DIAMOND_HOE: case GOLD_HOE: - queueBlock(event.getPlayer().getName(), event.getBlockClicked(), event.getBlockClicked().getTypeId(), Material.SOIL.getId(), (byte)0, null); + queueBlock(event.getPlayer().getName(), event.getBlockClicked(), event.getBlockClicked().getTypeId(), Material.SOIL.getId(), (byte)0); break; } } } - } - private class LBLBlockListener extends BlockListener - { - public void onBlockRightClick(BlockRightClickEvent event) - { - if (event.getItemInHand().getTypeId()== Config.toolID && CheckPermission(event.getPlayer(), "logblock.lookup")) - showBlockHistory(event.getPlayer(), event.getBlock()); - } - - public void onBlockPlace(BlockPlaceEvent event) - { - if (!event.isCancelled()) { - if (event.getItemInHand().getTypeId() == Config.toolblockID && CheckPermission(event.getPlayer(), "logblock.lookup")) - { - showBlockHistory(event.getPlayer(), event.getBlockPlaced()); - if (Config.toolblockRemove) - event.setCancelled(true); - } - else - queueBlock(event.getPlayer().getName(), event.getBlockPlaced(), event.getBlockReplacedState().getTypeId(), event.getBlockPlaced().getTypeId(), event.getBlockPlaced().getData(), null); - } - } - - public void onBlockBreak(BlockBreakEvent event) - { - if (!event.isCancelled()) - queueBlock(event.getPlayer().getName(), event.getBlock(), event.getBlock().getTypeId(), 0, event.getBlock().getData(), null); - } - - public void onSignChange(SignChangeEvent event) { - if (!event.isCancelled()) - queueBlock(event.getPlayer().getName(), event.getBlock(), 0, event.getBlock().getTypeId(), event.getBlock().getData(), "sign [" + event.getLine(0) + "] [" + event.getLine(1) + "] [" + event.getLine(2) + "] [" + event.getLine(3) + "]"); - } - - public void onBlockBurn(BlockBurnEvent event) { - if (!event.isCancelled()) - queueBlock("environment", event.getBlock(), event.getBlock().getTypeId(), 0, event.getBlock().getData(), null); + public void onPlayerJoin(PlayerEvent event) { + Connection conn = getConnection(); + Statement state = null; + if (conn == null) + return; + try { + state = conn.createStatement(); + state.execute("INSERT IGNORE INTO `players` (`playername`) VALUES ('" + event.getPlayer().getName() + "');"); + } catch (SQLException ex) { + log.log(Level.SEVERE, "[LogBlock] SQL exception", ex); + } finally { + try { + if (state != null) + state.close(); + if (conn != null) + conn.close(); + } catch (SQLException ex) { + log.log(Level.SEVERE, "[LogBlock] SQL exception on close", ex); + } + } } } - private class LBLEntityListener extends EntityListener + private class LBBlockListener extends BlockListener + { + public void onBlockRightClick(BlockRightClickEvent event) { + if (event.getItemInHand().getTypeId()== Config.toolID && CheckPermission(event.getPlayer(), "logblock.lookup")) + new Thread(new BlockStats(getConnection(), event.getPlayer(), event.getBlock(), getTable(event.getBlock()))).start(); + } + + public void onBlockPlace(BlockPlaceEvent event) { + if (!event.isCancelled()) { + if (event.getItemInHand().getTypeId() == Config.toolblockID && CheckPermission(event.getPlayer(), "logblock.lookup")) { + new Thread(new BlockStats(getConnection(), event.getPlayer(), event.getBlock(), getTable(event.getBlock()))).start(); + if (Config.toolblockRemove) + event.setCancelled(true); + } else + queueBlock(event.getPlayer().getName(), event.getBlockPlaced(), event.getBlockReplacedState().getTypeId(), event.getBlockPlaced().getTypeId(), event.getBlockPlaced().getData()); + } + } + + public void onBlockBreak(BlockBreakEvent event) { + if (!event.isCancelled()) + queueBlock(event.getPlayer().getName(), event.getBlock(), event.getBlock().getTypeId(), 0, event.getBlock().getData()); + } + + public void onSignChange(SignChangeEvent event) { + if (!event.isCancelled()) + if (Config.logSignTexts) + queueBlock(event.getPlayer().getName(), event.getBlock(), 0, event.getBlock().getTypeId(), event.getBlock().getData(), "sign [" + event.getLine(0) + "] [" + event.getLine(1) + "] [" + event.getLine(2) + "] [" + event.getLine(3) + "]", null); + else + queueBlock(event.getPlayer().getName(), event.getBlock(), 0, event.getBlock().getTypeId(), event.getBlock().getData()); + } + + public void onBlockBurn(BlockBurnEvent event) { + if (!event.isCancelled()) + queueBlock("environment", event.getBlock(), event.getBlock().getTypeId(), 0, event.getBlock().getData()); + } + + public void onBlockInteract(BlockInteractEvent event) { + if (!event.isCancelled() && event.isPlayer() && event.getBlock().getType() == Material.CHEST) { + if (((Player)event.getEntity()).getItemInHand().getTypeId() == Config.toolID) + event.setCancelled(true); + else + queueBlock((Player)event.getEntity(), event.getBlock(), (short)0, (byte)0, (short)0, (byte)0); + } + } + } + + private class LBEntityListener extends EntityListener { public void onEntityExplode(EntityExplodeEvent event) { - if (!event.isCancelled()) { - for (Block block : event.blockList()) - queueBlock("environment", block, block.getTypeId(), 0, block.getData(), null); - } + if (!event.isCancelled()) { + for (Block block : event.blockList()) + queueBlock("environment", block, block.getTypeId(), 0, block.getData()); + } } } @@ -530,28 +529,28 @@ public class LogBlock extends JavaPlugin return getSession(player); } } - - private boolean isInt(String str) { - try { - Integer.parseInt(str); - return true; - } catch (NumberFormatException nfe) { - return false; - } - } - + + private boolean isInt(String str) { + try { + Integer.parseInt(str); + return true; + } catch (NumberFormatException ex) { + return false; + } + } + private class Consumer implements Runnable { private boolean stop = false; - + Consumer() { stop = false; } - + public void stop() { stop = true; } - + public void run() { PreparedStatement ps = null; Connection conn = null; @@ -568,23 +567,33 @@ public class LogBlock extends JavaPlugin b = bqueue.poll(1L, TimeUnit.SECONDS); if (b == null) continue; - ps = conn.prepareStatement("INSERT INTO `" + b.table + "` (date, player, replaced, type, data, x, y, z) VALUES (now(),?,?,?,?,?,?,?)", Statement.RETURN_GENERATED_KEYS); - ps.setString(1, b.name); - ps.setInt(2, b.replaced); - ps.setInt(3, b.type); - ps.setByte(4, b.data); - ps.setInt(5, b.x); - ps.setInt(6, b.y); - ps.setInt(7, b.z); + ps = conn.prepareStatement("INSERT INTO `" + b.table + "` (`date`, `playerid`, `replaced`, `type`, `data`, `x`, `y`, `z`) SELECT now(), `playerid`, ?, ?, ?, ?, ? , ? FROM `players` WHERE `playername` = ?", Statement.RETURN_GENERATED_KEYS); + ps.setInt(1, b.replaced); + ps.setInt(2, b.type); + ps.setByte(3, b.data); + ps.setInt(4, b.x); + ps.setInt(5, b.y); + ps.setInt(6, b.z); + ps.setString(7, b.name); ps.executeUpdate(); - if (b.extra != null) { + if (b.signtext != null) { ResultSet keys = ps.getGeneratedKeys(); keys.next(); int key = keys.getInt(1); - - ps = conn.prepareStatement("INSERT INTO `" + b.table + "-extra` (id, extra) values (?,?)"); + ps = conn.prepareStatement("INSERT INTO `" + b.table + "-sign` (`id`, `signtext`) values (?,?)"); ps.setInt(1, key); - ps.setString(2, b.extra); + ps.setString(2, b.signtext); + ps.executeUpdate(); + } else if (b.ca != null) { + ResultSet keys = ps.getGeneratedKeys(); + keys.next(); + int key = keys.getInt(1); + ps = conn.prepareStatement("INSERT INTO `" + b.table + "-chest` (`id`, `intype`, `inamount`, `outtype`, `outamount`) values (?,?,?,?,?)"); + ps.setInt(1, key); + ps.setShort(2, b.ca.inType); + ps.setByte(3, b.ca.inAmount); + ps.setShort(4, b.ca.outType); + ps.setByte(5, b.ca.outAmount); ps.executeUpdate(); } count++; @@ -608,6 +617,19 @@ public class LogBlock extends JavaPlugin } } + private class ChestAccess + { + public short inType, outType; + public byte inAmount, outAmount; + + ChestAccess(short inType, byte inAmount, short outType, byte outAmount) { + this.inType = inType; + this.inAmount = inAmount; + this.outType = outType; + this.outAmount = outAmount; + } + } + private class BlockRow { public String table; @@ -615,8 +637,9 @@ public class LogBlock extends JavaPlugin public int replaced, type; public byte data; public int x, y, z; - public String extra; - + public String signtext; + public ChestAccess ca; + BlockRow(String table, String name, int replaced, int type, byte data, int x, int y, int z) { this.table = table; this.name = name; @@ -626,41 +649,34 @@ public class LogBlock extends JavaPlugin this.x = x; this.y = y; this.z = z; - this.extra = null; - } - - public void addExtra(String extra) { - this.extra = extra; - } - - public String toString() { - return("name: " + name + " before type: " + replaced + " type: " + type + " x: " + x + " y: " + y + " z: " + z); + this.signtext = null; + this.ca = null; } } private class Session { public String user; - public Location loc1 = null, loc2 = null; + public Location loc1 = null, loc2 = null; - public Session (Player player) { - this.user = player.getName(); - } - - public boolean isloc1Set() { - if (loc1 == null) - return false; - else - return true; - } - - public boolean isloc2Set() { - if (loc2 == null) - return false; - else - return true; - } - + public Session (Player player) { + this.user = player.getName(); + } + + public boolean isloc1Set() { + if (loc1 == null) + return false; + else + return true; + } + + public boolean isloc2Set() { + if (loc2 == null) + return false; + else + return true; + } + @Override public boolean equals(Object obj) { diff --git a/src/de/diddiz/LogBlock/PlayerAreaStats.java b/src/de/diddiz/LogBlock/PlayerAreaStats.java index 4a29b01..7d5989c 100644 --- a/src/de/diddiz/LogBlock/PlayerAreaStats.java +++ b/src/de/diddiz/LogBlock/PlayerAreaStats.java @@ -9,6 +9,7 @@ import java.util.HashMap; import java.util.HashSet; import java.util.logging.Level; +import org.bukkit.ChatColor; import org.bukkit.Material; import org.bukkit.entity.Player; @@ -19,56 +20,49 @@ public class PlayerAreaStats implements Runnable private int size; private Connection conn = null; private String table; - - PlayerAreaStats(Connection conn, Player player, String name, int size, String table) - { + + PlayerAreaStats(Connection conn, Player player, String name, int size, String table) { this.player = player; this.name = name; this.size = size; this.conn = conn; this.table = table; } - public void run() - { + + public void run() { HashSet types = new HashSet(); HashMap created = new HashMap(); HashMap destroyed = new HashMap(); - PreparedStatement ps = null; ResultSet rs = null; - try { conn.setAutoCommit(false); - ps = conn.prepareStatement("SELECT type, count(type) as num from `" + table + "` where type > 0 and player = ? and y > 0 and x > ? and x < ? and z > ? and z < ? group by type order by count(replaced) desc limit 10", Statement.RETURN_GENERATED_KEYS); + ps = conn.prepareStatement("SELECT type, count(type) as num from `" + table + "` INNER JOIN `players` USING (`playerid`) where type > 0 and playername = ? and y > 0 and x > ? and x < ? and z > ? and z < ? group by type order by count(replaced) desc limit 10", Statement.RETURN_GENERATED_KEYS); ps.setString(1, name); ps.setInt(2, player.getLocation().getBlockX()-size); ps.setInt(3, player.getLocation().getBlockX()+size); ps.setInt(4, player.getLocation().getBlockZ()-size); ps.setInt(5, player.getLocation().getBlockZ()+size); rs = ps.executeQuery(); - while (rs.next()) - { - types.add(Material.getMaterial(rs.getInt("type")).toString().toLowerCase().replace('_', ' ')); - created.put(Material.getMaterial(rs.getInt("type")).toString().toLowerCase().replace('_', ' '), rs.getInt("num")); + while (rs.next()) { + types.add(getMaterialName(rs.getInt("type"))); + created.put(getMaterialName(rs.getInt("type")), rs.getInt("num")); } rs.close(); ps.close(); - - ps = conn.prepareStatement("SELECT replaced, count(replaced) as num from `" + table + "` where replaced > 0 and player = ? and y > 0 and x > ? and x < ? and z > ? and z < ? group by replaced order by count(replaced) desc limit 10", Statement.RETURN_GENERATED_KEYS); + ps = conn.prepareStatement("SELECT replaced, count(replaced) as num from `" + table + "` INNER JOIN `players` USING (`playerid`) where replaced > 0 and playername = ? and y > 0 and x > ? and x < ? and z > ? and z < ? group by replaced order by count(replaced) desc limit 10", Statement.RETURN_GENERATED_KEYS); ps.setString(1, name); ps.setInt(2, player.getLocation().getBlockX()-size); ps.setInt(3, player.getLocation().getBlockX()+size); ps.setInt(4, player.getLocation().getBlockZ()-size); ps.setInt(5, player.getLocation().getBlockZ()+size); rs = ps.executeQuery(); - while (rs.next()) - { - types.add(Material.getMaterial(rs.getInt("replaced")).toString().toLowerCase().replace('_', ' ')); - destroyed.put(Material.getMaterial(rs.getInt("replaced")).toString().toLowerCase().replace('_', ' '), rs.getInt("num")); + while (rs.next()) { + types.add(getMaterialName(rs.getInt("replaced"))); + destroyed.put(getMaterialName(rs.getInt("replaced")), rs.getInt("num")); } - } catch (SQLException ex) { - LogBlock.log.log(Level.SEVERE, this.getClass().getName() + " SQL exception", ex); + LogBlock.log.log(Level.SEVERE, "[LogBlock PlayerAreaStats] SQL exception", ex); } finally { try { if (rs != null) @@ -78,27 +72,27 @@ public class PlayerAreaStats implements Runnable if (conn != null) conn.close(); } catch (SQLException ex) { - LogBlock.log.log(Level.SEVERE, this.getClass().getName() + " SQL exception on close", ex); + LogBlock.log.log(Level.SEVERE, "[LogBlock PlayerAreaStats] SQL exception on close", ex); } } - - player.sendMessage("§3Player " + name + " within " + size + " blocks of you: "); + player.sendMessage(ChatColor.DARK_AQUA + "Player " + name + " within " + size + " blocks of you: "); if (types.size() == 0) - { - player.sendMessage("§3No results found."); - return; - } - - player.sendMessage("§6" + String.format("%-6s %-6s %s", "Creat", "Destr", "Block")); - for (String t: types) - { - Integer c = created.get(t); - Integer d = destroyed.get(t); - if (c == null) - c = 0; - if (d == null) - d = 0; - player.sendMessage("§6" + String.format("%-6d %-6d %s", c, d, t)); + player.sendMessage(ChatColor.DARK_AQUA + "No results found."); + else { + player.sendMessage(ChatColor.GOLD + String.format("%-6s %-6s %s", "Creat", "Destr", "Block")); + for (String t: types) { + Integer c = created.get(t); + Integer d = destroyed.get(t); + if (c == null) + c = 0; + if (d == null) + d = 0; + player.sendMessage(ChatColor.GOLD + String.format("%-6d %-6d %s", c, d, t)); + } } } + + private String getMaterialName(int type) { + return Material.getMaterial(type).toString().toLowerCase().replace('_', ' '); + } } diff --git a/src/de/diddiz/LogBlock/PlayerWorldStats.java b/src/de/diddiz/LogBlock/PlayerWorldStats.java deleted file mode 100644 index e4d1dfe..0000000 --- a/src/de/diddiz/LogBlock/PlayerWorldStats.java +++ /dev/null @@ -1,89 +0,0 @@ -package de.diddiz.LogBlock; - -import java.sql.Connection; -import java.sql.PreparedStatement; -import java.sql.ResultSet; -import java.sql.SQLException; -import java.sql.Statement; -import java.util.HashMap; -import java.util.HashSet; -import java.util.logging.Level; - -import org.bukkit.entity.Player; - -public class PlayerWorldStats implements Runnable -{ - private Player player; - private Connection conn = null; - private String table; - - PlayerWorldStats(Connection conn, Player player, String table) - { - this.player = player; - this.conn = conn; - this.table = table; - } - public void run() - { - HashSet players = new HashSet(); - HashMap created = new HashMap(); - HashMap destroyed = new HashMap(); - - PreparedStatement ps = null; - ResultSet rs = null; - - try { - conn.setAutoCommit(false); - ps = conn.prepareStatement("SELECT player, count(player) as num from `" + table + "` where type > 0 group by player order by count(player) desc limit 5", Statement.RETURN_GENERATED_KEYS); - rs = ps.executeQuery(); - while (rs.next()) - { - players.add(rs.getString("player")); - created.put(rs.getString("player"), rs.getInt("num")); - } - rs.close(); - ps.close(); - - ps = conn.prepareStatement("SELECT player, count(player) as num from `" + table + "` where replaced > 0 group by player order by count(player) desc limit 5", Statement.RETURN_GENERATED_KEYS); - rs = ps.executeQuery(); - while (rs.next()) - { - players.add(rs.getString("player")); - destroyed.put(rs.getString("player"), rs.getInt("num")); - } - - } catch (SQLException ex) { - LogBlock.log.log(Level.SEVERE, this.getClass().getName() + " SQL exception", ex); - } finally { - try { - if (rs != null) - rs.close(); - if (ps != null) - ps.close(); - if (conn != null) - conn.close(); - } catch (SQLException ex) { - LogBlock.log.log(Level.SEVERE, this.getClass().getName() + " SQL exception on close", ex); - } - } - - player.sendMessage("§3Within entire world:"); - if (players.size() == 0) - { - player.sendMessage("§3No results found."); - return; - } - - player.sendMessage("§6" + String.format("%-6s %-6s %s", "Creat", "Destr", "Player")); - for (String p: players) - { - Integer c = created.get(p); - Integer d = destroyed.get(p); - if (c == null) - c = 0; - if (d == null) - d = 0; - player.sendMessage("§6" + String.format("%-6d %-6d %s", c, d, p)); - } - } -} diff --git a/src/de/diddiz/LogBlock/Rollback.java b/src/de/diddiz/LogBlock/Rollback.java index d30bb7d..b54308f 100644 --- a/src/de/diddiz/LogBlock/Rollback.java +++ b/src/de/diddiz/LogBlock/Rollback.java @@ -20,13 +20,13 @@ public class Rollback implements Runnable PreparedStatement ps = null; private Player player; private Connection conn = null; - + Rollback(Player player, Connection conn, String name, int minutes, String table) { this.player = player; this.conn = conn; try { conn.setAutoCommit(false); - ps = conn.prepareStatement("SELECT type, data, replaced, x, y, z FROM `" + table + "` WHERE player = ? AND date > date_sub(now(), INTERVAL ? MINUTE) ORDER BY date DESC", Statement.RETURN_GENERATED_KEYS); + ps = conn.prepareStatement("SELECT type, data, replaced, x, y, z FROM `" + table + "` INNER JOIN `players` USING (`playerid`) WHERE playername = ? AND date > date_sub(now(), INTERVAL ? MINUTE) ORDER BY date DESC", Statement.RETURN_GENERATED_KEYS); ps.setString(1, name); ps.setInt(2, minutes); } catch (SQLException ex) { @@ -35,7 +35,7 @@ public class Rollback implements Runnable return; } } - + Rollback(Player player, Connection conn, int radius, int minutes, String table) { this.player = player; this.conn = conn; @@ -74,8 +74,7 @@ public class Rollback implements Runnable } } - public void run() - { + public void run() { ResultSet rs = null; edits.clear(); try { @@ -126,7 +125,7 @@ public class Rollback implements Runnable player.sendMessage(ChatColor.GREEN + "Rollback finished successfully"); player.sendMessage(ChatColor.GREEN + "Undid " + rolledBack + " of " + changes + " changes"); } - + private class Edit { int type, replaced; @@ -134,8 +133,7 @@ public class Rollback implements Runnable byte data; World world; - Edit(int type, int replaced, byte data, int x, int y, int z, World world) - { + Edit(int type, int replaced, byte data, int x, int y, int z, World world) { this.type = type; this.replaced = replaced; this.data = data; @@ -145,8 +143,9 @@ public class Rollback implements Runnable this.world = world; } - public boolean perform() - { + public boolean perform() { + if (type == replaced) + return false; Block block = world.getBlockAt(x, y, z); if (block.getTypeId() == type || (block.getTypeId() >= 8 && block.getTypeId() <= 11) || block.getTypeId() == 51) { if (block.setTypeId(replaced)) {