improved database and added signlogging

This commit is contained in:
Robin Kupper
2011-03-11 16:54:24 +01:00
parent 9dc962490f
commit 5dcecdc08e
9 changed files with 428 additions and 429 deletions

1
.gitignore vendored
View File

@@ -2,3 +2,4 @@
/.project /.project
/.settings /.settings
/bin /bin
/release

View File

@@ -5,7 +5,6 @@ import java.sql.PreparedStatement;
import java.sql.ResultSet; import java.sql.ResultSet;
import java.sql.SQLException; import java.sql.SQLException;
import java.sql.Statement; import java.sql.Statement;
import java.sql.Timestamp;
import java.text.SimpleDateFormat; import java.text.SimpleDateFormat;
import java.util.logging.Level; import java.util.logging.Level;
@@ -36,12 +35,10 @@ public class AreaBlockSearch implements Runnable
boolean hist = false; boolean hist = false;
PreparedStatement ps = null; PreparedStatement ps = null;
ResultSet rs = 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 { try {
conn.setAutoCommit(false); 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(1, type);
ps.setInt(2, type); ps.setInt(2, type);
ps.setInt(3, location.getBlockY() - size); ps.setInt(3, location.getBlockY() - size);
@@ -51,25 +48,22 @@ public class AreaBlockSearch implements Runnable
ps.setInt(7, location.getBlockZ() - size); ps.setInt(7, location.getBlockZ() - size);
ps.setInt(8, location.getBlockZ() + size); ps.setInt(8, location.getBlockZ() + size);
rs = ps.executeQuery(); rs = ps.executeQuery();
player.sendMessage(ChatColor.DARK_AQUA + "Block history for " + getMaterialName(type) + " within " + size + " blocks of " + location.getBlockX() + ", " + location.getBlockY() + ", " + location.getBlockZ() + ": "); player.sendMessage(ChatColor.DARK_AQUA + "Block history for " + getMaterialName(type) + " within " + size + " blocks of " + location.getBlockX() + ", " + location.getBlockY() + ", " + location.getBlockZ() + ": ");
while (rs.next()) {
while (rs.next()) String msg = formatter.format(rs.getTimestamp("date")) + " " + rs.getString("playername") + " (" + rs.getInt("x") + ", " + rs.getInt("y") + ", " + rs.getInt("z") + ") ";
{
date = rs.getTimestamp("date");
String datestr = formatter.format(date);
String msg = datestr + " " + rs.getString("player") + " (" + rs.getInt("x") + ", " + rs.getInt("y") + ", " + rs.getInt("z") + ") ";
if (rs.getInt("type") == 0) if (rs.getInt("type") == 0)
msg = msg + "destroyed " + getMaterialName(rs.getInt("replaced")); msg = msg + "destroyed " + getMaterialName(rs.getInt("replaced"));
else if (rs.getInt("replaced") == 0) else if (rs.getInt("replaced") == 0)
msg = msg + "created " + getMaterialName(rs.getInt("type")); msg = msg + "created " + getMaterialName(rs.getInt("type"));
else else
msg = msg + "replaced " + getMaterialName(rs.getInt("replaced")) + " with " + getMaterialName(rs.getInt("type")); msg = msg + "replaced " + getMaterialName(rs.getInt("replaced")) + " with " + getMaterialName(rs.getInt("type"));
player.sendMessage("<EFBFBD>6" + msg); player.sendMessage(ChatColor.GOLD + msg);
hist = true; hist = true;
} }
if (!hist)
player.sendMessage(ChatColor.DARK_AQUA + "None.");
} catch (SQLException ex) { } 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 { } finally {
try { try {
if (rs != null) if (rs != null)
@@ -79,11 +73,9 @@ public class AreaBlockSearch implements Runnable
if (conn != null) if (conn != null)
conn.close(); conn.close();
} catch (SQLException ex) { } 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("<EFBFBD>3None.");
} }
private String getMaterialName(int type) { private String getMaterialName(int type) {

View File

@@ -9,6 +9,7 @@ import java.util.HashMap;
import java.util.HashSet; import java.util.HashSet;
import java.util.logging.Level; import java.util.logging.Level;
import org.bukkit.ChatColor;
import org.bukkit.entity.Player; import org.bukkit.entity.Player;
public class AreaStats implements Runnable public class AreaStats implements Runnable
@@ -18,25 +19,22 @@ public class AreaStats implements Runnable
private Connection conn = null; private Connection conn = null;
private String table; 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.player = player;
this.size = size; this.size = size;
this.conn = conn; this.conn = conn;
this.table = table; this.table = table;
} }
public void run()
{ public void run() {
HashSet<String> players = new HashSet<String>(); HashSet<String> players = new HashSet<String>();
HashMap<String, Integer> created = new HashMap<String, Integer>(); HashMap<String, Integer> created = new HashMap<String, Integer>();
HashMap<String, Integer> destroyed = new HashMap<String, Integer>(); HashMap<String, Integer> destroyed = new HashMap<String, Integer>();
PreparedStatement ps = null; PreparedStatement ps = null;
ResultSet rs = null; ResultSet rs = null;
try { try {
conn.setAutoCommit(false); 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(1, player.getLocation().getBlockY()-size);
ps.setInt(2, player.getLocation().getBlockY()+size); ps.setInt(2, player.getLocation().getBlockY()+size);
ps.setInt(3, player.getLocation().getBlockX()-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(5, player.getLocation().getBlockZ()-size);
ps.setInt(6, player.getLocation().getBlockZ()+size); ps.setInt(6, player.getLocation().getBlockZ()+size);
rs = ps.executeQuery(); rs = ps.executeQuery();
while (rs.next()) while (rs.next()) {
{ players.add(rs.getString("playername"));
players.add(rs.getString("player")); created.put(rs.getString("playername"), rs.getInt("num"));
created.put(rs.getString("player"), rs.getInt("num"));
} }
rs.close(); rs.close();
ps.close(); ps.close();
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 = 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.setInt(1, player.getLocation().getBlockY()-size); ps.setInt(1, player.getLocation().getBlockY()-size);
ps.setInt(2, player.getLocation().getBlockY()+size); ps.setInt(2, player.getLocation().getBlockY()+size);
ps.setInt(3, player.getLocation().getBlockX()-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(5, player.getLocation().getBlockZ()-size);
ps.setInt(6, player.getLocation().getBlockZ()+size); ps.setInt(6, player.getLocation().getBlockZ()+size);
rs = ps.executeQuery(); rs = ps.executeQuery();
while (rs.next()) while (rs.next()) {
{ players.add(rs.getString("playername"));
players.add(rs.getString("player")); destroyed.put(rs.getString("playername"), rs.getInt("num"));
destroyed.put(rs.getString("player"), rs.getInt("num"));
} }
} catch (SQLException ex) { } 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 { } finally {
try { try {
if (rs != null) if (rs != null)
@@ -77,27 +71,23 @@ public class AreaStats implements Runnable
if (conn != null) if (conn != null)
conn.close(); conn.close();
} catch (SQLException ex) { } 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(ChatColor.DARK_AQUA + "Within " + size + " blocks of you: ");
player.sendMessage("<EFBFBD>3Within " + size + " blocks of you: ");
if (players.size() == 0) if (players.size() == 0)
{ player.sendMessage(ChatColor.DARK_AQUA + "No results found.");
player.sendMessage("<EFBFBD>3No results found."); else {
return; player.sendMessage(ChatColor.GOLD + String.format("%-6s %-6s %s", "Creat", "Destr", "Player"));
} for (String p: players) {
Integer c = created.get(p);
player.sendMessage("<EFBFBD>6" + String.format("%-6s %-6s %s", "Creat", "Destr", "Player")); Integer d = destroyed.get(p);
for (String p: players) if (c == null)
{ c = 0;
Integer c = created.get(p); if (d == null)
Integer d = destroyed.get(p); d = 0;
if (c == null) player.sendMessage(ChatColor.GOLD + String.format("%-6d %-6d %s", c, d, p));
c = 0; }
if (d == null)
d = 0;
player.sendMessage("<EFBFBD>6" + String.format("%-6d %-6d %s", c, d, p));
} }
} }
} }

View File

@@ -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('_', ' ');
}
}

View File

@@ -19,8 +19,10 @@ public class Config {
static int toolID; static int toolID;
static int toolblockID; static int toolblockID;
static boolean toolblockRemove; static boolean toolblockRemove;
static boolean logSignTexts;
static boolean logExplosions; static boolean logExplosions;
static boolean logFire; static boolean logFire;
static boolean logChestAccess;
static boolean usePermissions; static boolean usePermissions;
static boolean Load(Configuration config) { static boolean Load(Configuration config) {
@@ -52,10 +54,14 @@ public class Config {
config.setProperty("toolblockID", 7); config.setProperty("toolblockID", 7);
if (!keys.contains("toolblockRemove")) if (!keys.contains("toolblockRemove"))
config.setProperty("toolblockRemove", true); config.setProperty("toolblockRemove", true);
if (!keys.contains("logSignTexts"))
config.setProperty("logSignTexts", false);
if (!keys.contains("logExplosions")) if (!keys.contains("logExplosions"))
config.setProperty("logExplosions", false); config.setProperty("logExplosions", false);
if (!keys.contains("logFire")) if (!keys.contains("logFire"))
config.setProperty("logFire", false); config.setProperty("logFire", false);
if (!keys.contains("logChestAccess"))
config.setProperty("logChestAccess", false);
if (!keys.contains("usePermissions")) if (!keys.contains("usePermissions"))
config.setProperty("usePermissions", false); config.setProperty("usePermissions", false);
if (!config.save()){ if (!config.save()){
@@ -75,8 +81,10 @@ public class Config {
toolID = config.getInt("toolID", 270); toolID = config.getInt("toolID", 270);
toolblockID = config.getInt("toolblockID", 7); toolblockID = config.getInt("toolblockID", 7);
toolblockRemove = config.getBoolean("toolblockRemove", true); toolblockRemove = config.getBoolean("toolblockRemove", true);
logSignTexts = config.getBoolean("logSignTexts", false);
logExplosions = config.getBoolean("logExplosions", false); logExplosions = config.getBoolean("logExplosions", false);
logFire = config.getBoolean("logFire", false); logFire = config.getBoolean("logFire", false);
logChestAccess = config.getBoolean("logChestAccess", false);
usePermissions = config.getBoolean("usePermissions", false); usePermissions = config.getBoolean("usePermissions", false);
return true; return true;
} }

View File

@@ -7,8 +7,6 @@ import java.sql.PreparedStatement;
import java.sql.ResultSet; import java.sql.ResultSet;
import java.sql.SQLException; import java.sql.SQLException;
import java.sql.Statement; import java.sql.Statement;
import java.sql.Timestamp;
import java.text.SimpleDateFormat;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.concurrent.LinkedBlockingQueue; import java.util.concurrent.LinkedBlockingQueue;
import java.util.concurrent.TimeUnit; import java.util.concurrent.TimeUnit;
@@ -26,12 +24,14 @@ import org.bukkit.event.Event;
import org.bukkit.event.Event.Type; import org.bukkit.event.Event.Type;
import org.bukkit.event.block.BlockBreakEvent; import org.bukkit.event.block.BlockBreakEvent;
import org.bukkit.event.block.BlockBurnEvent; import org.bukkit.event.block.BlockBurnEvent;
import org.bukkit.event.block.BlockInteractEvent;
import org.bukkit.event.block.BlockListener; import org.bukkit.event.block.BlockListener;
import org.bukkit.event.block.BlockPlaceEvent; import org.bukkit.event.block.BlockPlaceEvent;
import org.bukkit.event.block.BlockRightClickEvent; import org.bukkit.event.block.BlockRightClickEvent;
import org.bukkit.event.block.SignChangeEvent; import org.bukkit.event.block.SignChangeEvent;
import org.bukkit.event.entity.EntityExplodeEvent; import org.bukkit.event.entity.EntityExplodeEvent;
import org.bukkit.event.entity.EntityListener; import org.bukkit.event.entity.EntityListener;
import org.bukkit.event.player.PlayerEvent;
import org.bukkit.event.player.PlayerItemEvent; import org.bukkit.event.player.PlayerItemEvent;
import org.bukkit.event.player.PlayerListener; import org.bukkit.event.player.PlayerListener;
import org.bukkit.plugin.PluginManager; import org.bukkit.plugin.PluginManager;
@@ -49,29 +49,23 @@ public class LogBlock extends JavaPlugin
private ArrayList<Session> sessions = new ArrayList<Session>(); private ArrayList<Session> sessions = new ArrayList<Session>();
@Override @Override
public void onEnable() public void onEnable() {
{
log = getServer().getLogger(); log = getServer().getLogger();
try { try {
Config.Load(getConfiguration()); Config.Load(getConfiguration());
if (Config.usePermissions) { if (Config.usePermissions) {
if (getServer().getPluginManager().getPlugin("Permissions") != null) { if (getServer().getPluginManager().getPlugin("Permissions") != null)
Config.usePermissions = true;
log.info("[LogBlock] Permissions enabled"); log.info("[LogBlock] Permissions enabled");
} else else {
log.info("[LogBlock] Permissions plugin not found. Using default permissions."); 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); new JDCConnectionDriver(Config.dbDriver, Config.dbUrl, Config.dbUsername, Config.dbPassword);
Connection conn = getConnection(); Connection conn = getConnection();
conn.close(); conn.close();
} catch (Exception ex) { } 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); getServer().getPluginManager().disablePlugin(this);
return; return;
} }
@@ -80,34 +74,35 @@ public class LogBlock extends JavaPlugin
getServer().getPluginManager().disablePlugin(this); getServer().getPluginManager().disablePlugin(this);
return; return;
} }
for (int i = 0; i < Config.worldNames.size(); i++) { if (!checkTables()) {
if (!checkTables(Config.worldTables.get(i))) { 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); return;
return;
}
} }
if (Config.keepLogDays >= 0) if (Config.keepLogDays >= 0)
dropOldLogs(); dropOldLogs();
LBLBlockListener lblBlockListener = new LBLBlockListener(); LBBlockListener lbBlockListener = new LBBlockListener();
LBPlayerListener lbPlayerListener = new LBPlayerListener();
PluginManager pm = getServer().getPluginManager(); PluginManager pm = getServer().getPluginManager();
pm.registerEvent(Type.PLAYER_ITEM, new LBLPlayerListener(), Event.Priority.Monitor, this); pm.registerEvent(Type.PLAYER_ITEM, lbPlayerListener, Event.Priority.Monitor, this);
pm.registerEvent(Type.BLOCK_RIGHTCLICKED, lblBlockListener, Event.Priority.Monitor, this); pm.registerEvent(Type.PLAYER_JOIN, lbPlayerListener, Event.Priority.Normal, this);
pm.registerEvent(Type.BLOCK_PLACED, lblBlockListener, Event.Priority.Monitor, this); pm.registerEvent(Type.BLOCK_RIGHTCLICKED, lbBlockListener, Event.Priority.Monitor, this);
pm.registerEvent(Type.BLOCK_BREAK, lblBlockListener, Event.Priority.Monitor, this); pm.registerEvent(Type.BLOCK_PLACED, lbBlockListener, Event.Priority.Monitor, this);
pm.registerEvent(Type.SIGN_CHANGE, lblBlockListener, 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) 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) 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(); consumer = new Consumer();
new Thread(consumer).start(); new Thread(consumer).start();
log.info("Logblock v" + getDescription().getVersion() + " enabled."); log.info("Logblock v" + getDescription().getVersion() + " enabled.");
} }
@Override @Override
public void onDisable() public void onDisable() {
{
if (consumer != null) { if (consumer != null) {
consumer.stop(); consumer.stop();
consumer = null; consumer = null;
@@ -123,7 +118,7 @@ public class LogBlock extends JavaPlugin
Player player = (Player)sender; Player player = (Player)sender;
Connection conn = getConnection(); Connection conn = getConnection();
if (conn != null) { if (conn != null) {
String table = getTable(player.getWorld().getName()); String table = getTable(player);
if (table != null) { if (table != null) {
if (CheckPermission(player,"logblock.area")) { if (CheckPermission(player,"logblock.area")) {
if (args.length == 0) { if (args.length == 0) {
@@ -134,7 +129,7 @@ public class LogBlock extends JavaPlugin
radius = Integer.parseInt(args[1]); radius = Integer.parseInt(args[1]);
new Thread(new AreaStats(conn, player, radius, table)).start(); new Thread(new AreaStats(conn, player, radius, table)).start();
} else if (args[0].equalsIgnoreCase("world")) { } 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")) { } else if (args[0].equalsIgnoreCase("player")) {
if (args.length >= 2) { if (args.length >= 2) {
int radius = Config.defaultDist; int radius = Config.defaultDist;
@@ -260,33 +255,49 @@ public class LogBlock extends JavaPlugin
} }
} }
private boolean checkTables(String table) private boolean checkTables() {
{ Connection conn = getConnection();
Connection conn = null; Statement state = null;
ResultSet rs = null; if (conn == null)
return false;
try { try {
conn = getConnection();
if (conn == null)
return false;
DatabaseMetaData dbm = conn.getMetaData(); DatabaseMetaData dbm = conn.getMetaData();
rs = dbm.getTables(null, null, table, null); state = conn.createStatement();
if (!rs.next()) { if (!dbm.getTables(null, null, "players", null).next()) {
log.log(Level.SEVERE, "[LogBlock] Crating table " + table + "."); log.log(Level.INFO, "[LogBlock] Crating table players.");
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.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); for (int i = 0; i < Config.worldNames.size(); i++) {
if (!rs.next()) { String table = Config.worldTables.get(i);
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`));"); if (!dbm.getTables(null, null, table, null).next()) {
return checkTables(table); 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; return true;
} catch (SQLException ex) { } catch (SQLException ex) {
log.log(Level.SEVERE, "[LogBlock] SQL exception", ex); log.log(Level.SEVERE, "[LogBlock] SQL exception while checking tables", ex);
} finally { } finally {
try { try {
if (rs != null) if (state != null)
rs.close(); state.close();
if (conn != null) if (conn != null)
conn.close(); conn.close();
} catch (SQLException ex) { } catch (SQLException ex) {
@@ -296,8 +307,7 @@ public class LogBlock extends JavaPlugin
return false; return false;
} }
private void dropOldLogs() private void dropOldLogs() {
{
Connection conn = null; Connection conn = null;
Statement state = null; Statement state = null;
try { 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) { private String getTable (String worldName) {
int idx = Config.worldNames.indexOf(worldName); int idx = Config.worldNames.indexOf(worldName);
if (idx == -1) if (idx == -1)
@@ -330,94 +348,46 @@ public class LogBlock extends JavaPlugin
return Config.worldTables.get(idx); return Config.worldTables.get(idx);
} }
private void showBlockHistory(Player player, Block b)
{
player.sendMessage("<EFBFBD>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("<EFBFBD>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) { 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) if (block == null || typeBefore < 0 || typeAfter < 0)
return; return;
String table = getTable(block.getWorld().getName()); String table = getTable(block);
if (table == null) if (table == null)
return; return;
BlockRow row = new BlockRow(table, playerName, typeBefore, typeAfter, data, block.getX(), block.getY(), block.getZ()); BlockRow row = new BlockRow(table, playerName, typeBefore, typeAfter, data, block.getX(), block.getY(), block.getZ());
if (extra != null) if (signtext != null)
row.addExtra(extra); row.signtext = signtext;
if (ca != null)
row.ca = ca;
if (!bqueue.offer(row)) 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) { 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);
else { else {
if (permission.equals("logblock.lookup")) if (permission.equals("logblock.lookup"))
return true; return true;
else if (permission.equals("logblock.area")) else if (permission.equals("logblock.area"))
return player.isOp(); return player.isOp();
else if (permission.equals("logblock.rollback")) else if (permission.equals("logblock.rollback"))
return player.isOp(); return player.isOp();
} }
return false; return false;
} }
static int parseTimeSpec(String timespec) { static int parseTimeSpec(String timespec) {
String[] split = timespec.split(" "); String[] split = timespec.split(" ");
@@ -440,13 +410,13 @@ public class LogBlock extends JavaPlugin
return min; return min;
} }
private class LBLPlayerListener extends PlayerListener private class LBPlayerListener extends PlayerListener
{ {
public void onPlayerItem(PlayerItemEvent event) { public void onPlayerItem(PlayerItemEvent event) {
if (!event.isCancelled() && event.getBlockClicked() != null) { if (!event.isCancelled() && event.getBlockClicked() != null) {
switch (event.getMaterial()) { switch (event.getMaterial()) {
case BUCKET: 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; break;
case WATER_BUCKET: case WATER_BUCKET:
queueBlock(event.getPlayer(), event.getBlockClicked().getFace(event.getBlockFace()), Material.STATIONARY_WATER.getId()); 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 IRON_HOE:
case DIAMOND_HOE: case DIAMOND_HOE:
case GOLD_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; break;
} }
} }
} }
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 LBLBlockListener extends BlockListener private class LBBlockListener extends BlockListener
{ {
public void onBlockRightClick(BlockRightClickEvent event) public void onBlockRightClick(BlockRightClickEvent event) {
{
if (event.getItemInHand().getTypeId()== Config.toolID && CheckPermission(event.getPlayer(), "logblock.lookup")) if (event.getItemInHand().getTypeId()== Config.toolID && CheckPermission(event.getPlayer(), "logblock.lookup"))
showBlockHistory(event.getPlayer(), event.getBlock()); new Thread(new BlockStats(getConnection(), event.getPlayer(), event.getBlock(), getTable(event.getBlock()))).start();
} }
public void onBlockPlace(BlockPlaceEvent event) public void onBlockPlace(BlockPlaceEvent event) {
{ if (!event.isCancelled()) {
if (!event.isCancelled()) { if (event.getItemInHand().getTypeId() == Config.toolblockID && CheckPermission(event.getPlayer(), "logblock.lookup")) {
if (event.getItemInHand().getTypeId() == Config.toolblockID && CheckPermission(event.getPlayer(), "logblock.lookup")) new Thread(new BlockStats(getConnection(), event.getPlayer(), event.getBlock(), getTable(event.getBlock()))).start();
{
showBlockHistory(event.getPlayer(), event.getBlockPlaced());
if (Config.toolblockRemove) if (Config.toolblockRemove)
event.setCancelled(true); event.setCancelled(true);
} } else
else queueBlock(event.getPlayer().getName(), event.getBlockPlaced(), event.getBlockReplacedState().getTypeId(), event.getBlockPlaced().getTypeId(), event.getBlockPlaced().getData());
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());
}
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);
} }
} }
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);
}
} }
private class LBLEntityListener extends EntityListener private class LBEntityListener extends EntityListener
{ {
public void onEntityExplode(EntityExplodeEvent event) { public void onEntityExplode(EntityExplodeEvent event) {
if (!event.isCancelled()) { if (!event.isCancelled()) {
for (Block block : event.blockList()) for (Block block : event.blockList())
queueBlock("environment", block, block.getTypeId(), 0, block.getData(), null); queueBlock("environment", block, block.getTypeId(), 0, block.getData());
} }
} }
} }
@@ -531,14 +530,14 @@ public class LogBlock extends JavaPlugin
} }
} }
private boolean isInt(String str) { private boolean isInt(String str) {
try { try {
Integer.parseInt(str); Integer.parseInt(str);
return true; return true;
} catch (NumberFormatException nfe) { } catch (NumberFormatException ex) {
return false; return false;
} }
} }
private class Consumer implements Runnable private class Consumer implements Runnable
{ {
@@ -568,23 +567,33 @@ public class LogBlock extends JavaPlugin
b = bqueue.poll(1L, TimeUnit.SECONDS); b = bqueue.poll(1L, TimeUnit.SECONDS);
if (b == null) if (b == null)
continue; continue;
ps = conn.prepareStatement("INSERT INTO `" + b.table + "` (date, player, replaced, type, data, x, y, z) VALUES (now(),?,?,?,?,?,?,?)", Statement.RETURN_GENERATED_KEYS); 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.setString(1, b.name); ps.setInt(1, b.replaced);
ps.setInt(2, b.replaced); ps.setInt(2, b.type);
ps.setInt(3, b.type); ps.setByte(3, b.data);
ps.setByte(4, b.data); ps.setInt(4, b.x);
ps.setInt(5, b.x); ps.setInt(5, b.y);
ps.setInt(6, b.y); ps.setInt(6, b.z);
ps.setInt(7, b.z); ps.setString(7, b.name);
ps.executeUpdate(); ps.executeUpdate();
if (b.extra != null) { if (b.signtext != null) {
ResultSet keys = ps.getGeneratedKeys(); ResultSet keys = ps.getGeneratedKeys();
keys.next(); keys.next();
int key = keys.getInt(1); int key = keys.getInt(1);
ps = conn.prepareStatement("INSERT INTO `" + b.table + "-sign` (`id`, `signtext`) values (?,?)");
ps = conn.prepareStatement("INSERT INTO `" + b.table + "-extra` (id, extra) values (?,?)");
ps.setInt(1, key); 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(); ps.executeUpdate();
} }
count++; 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 private class BlockRow
{ {
public String table; public String table;
@@ -615,7 +637,8 @@ public class LogBlock extends JavaPlugin
public int replaced, type; public int replaced, type;
public byte data; public byte data;
public int x, y, z; 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) { BlockRow(String table, String name, int replaced, int type, byte data, int x, int y, int z) {
this.table = table; this.table = table;
@@ -626,40 +649,33 @@ public class LogBlock extends JavaPlugin
this.x = x; this.x = x;
this.y = y; this.y = y;
this.z = z; this.z = z;
this.extra = null; this.signtext = null;
} this.ca = 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);
} }
} }
private class Session private class Session
{ {
public String user; public String user;
public Location loc1 = null, loc2 = null; public Location loc1 = null, loc2 = null;
public Session (Player player) { public Session (Player player) {
this.user = player.getName(); this.user = player.getName();
} }
public boolean isloc1Set() { public boolean isloc1Set() {
if (loc1 == null) if (loc1 == null)
return false; return false;
else else
return true; return true;
} }
public boolean isloc2Set() { public boolean isloc2Set() {
if (loc2 == null) if (loc2 == null)
return false; return false;
else else
return true; return true;
} }
@Override @Override
public boolean equals(Object obj) public boolean equals(Object obj)

View File

@@ -9,6 +9,7 @@ import java.util.HashMap;
import java.util.HashSet; import java.util.HashSet;
import java.util.logging.Level; import java.util.logging.Level;
import org.bukkit.ChatColor;
import org.bukkit.Material; import org.bukkit.Material;
import org.bukkit.entity.Player; import org.bukkit.entity.Player;
@@ -20,55 +21,48 @@ public class PlayerAreaStats implements Runnable
private Connection conn = null; private Connection conn = null;
private String table; 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.player = player;
this.name = name; this.name = name;
this.size = size; this.size = size;
this.conn = conn; this.conn = conn;
this.table = table; this.table = table;
} }
public void run()
{ public void run() {
HashSet<String> types = new HashSet<String>(); HashSet<String> types = new HashSet<String>();
HashMap<String, Integer> created = new HashMap<String, Integer>(); HashMap<String, Integer> created = new HashMap<String, Integer>();
HashMap<String, Integer> destroyed = new HashMap<String, Integer>(); HashMap<String, Integer> destroyed = new HashMap<String, Integer>();
PreparedStatement ps = null; PreparedStatement ps = null;
ResultSet rs = null; ResultSet rs = null;
try { try {
conn.setAutoCommit(false); 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.setString(1, name);
ps.setInt(2, player.getLocation().getBlockX()-size); ps.setInt(2, player.getLocation().getBlockX()-size);
ps.setInt(3, player.getLocation().getBlockX()+size); ps.setInt(3, player.getLocation().getBlockX()+size);
ps.setInt(4, player.getLocation().getBlockZ()-size); ps.setInt(4, player.getLocation().getBlockZ()-size);
ps.setInt(5, player.getLocation().getBlockZ()+size); ps.setInt(5, player.getLocation().getBlockZ()+size);
rs = ps.executeQuery(); rs = ps.executeQuery();
while (rs.next()) while (rs.next()) {
{ types.add(getMaterialName(rs.getInt("type")));
types.add(Material.getMaterial(rs.getInt("type")).toString().toLowerCase().replace('_', ' ')); created.put(getMaterialName(rs.getInt("type")), rs.getInt("num"));
created.put(Material.getMaterial(rs.getInt("type")).toString().toLowerCase().replace('_', ' '), rs.getInt("num"));
} }
rs.close(); rs.close();
ps.close(); ps.close();
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 = 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.setString(1, name); ps.setString(1, name);
ps.setInt(2, player.getLocation().getBlockX()-size); ps.setInt(2, player.getLocation().getBlockX()-size);
ps.setInt(3, player.getLocation().getBlockX()+size); ps.setInt(3, player.getLocation().getBlockX()+size);
ps.setInt(4, player.getLocation().getBlockZ()-size); ps.setInt(4, player.getLocation().getBlockZ()-size);
ps.setInt(5, player.getLocation().getBlockZ()+size); ps.setInt(5, player.getLocation().getBlockZ()+size);
rs = ps.executeQuery(); rs = ps.executeQuery();
while (rs.next()) while (rs.next()) {
{ types.add(getMaterialName(rs.getInt("replaced")));
types.add(Material.getMaterial(rs.getInt("replaced")).toString().toLowerCase().replace('_', ' ')); destroyed.put(getMaterialName(rs.getInt("replaced")), rs.getInt("num"));
destroyed.put(Material.getMaterial(rs.getInt("replaced")).toString().toLowerCase().replace('_', ' '), rs.getInt("num"));
} }
} catch (SQLException ex) { } 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 { } finally {
try { try {
if (rs != null) if (rs != null)
@@ -78,27 +72,27 @@ public class PlayerAreaStats implements Runnable
if (conn != null) if (conn != null)
conn.close(); conn.close();
} catch (SQLException ex) { } 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(ChatColor.DARK_AQUA + "Player " + name + " within " + size + " blocks of you: ");
player.sendMessage("<EFBFBD>3Player " + name + " within " + size + " blocks of you: ");
if (types.size() == 0) if (types.size() == 0)
{ player.sendMessage(ChatColor.DARK_AQUA + "No results found.");
player.sendMessage("<EFBFBD>3No results found."); else {
return; player.sendMessage(ChatColor.GOLD + String.format("%-6s %-6s %s", "Creat", "Destr", "Block"));
} for (String t: types) {
Integer c = created.get(t);
player.sendMessage("<EFBFBD>6" + String.format("%-6s %-6s %s", "Creat", "Destr", "Block")); Integer d = destroyed.get(t);
for (String t: types) if (c == null)
{ c = 0;
Integer c = created.get(t); if (d == null)
Integer d = destroyed.get(t); d = 0;
if (c == null) player.sendMessage(ChatColor.GOLD + String.format("%-6d %-6d %s", c, d, t));
c = 0; }
if (d == null)
d = 0;
player.sendMessage("<EFBFBD>6" + String.format("%-6d %-6d %s", c, d, t));
} }
} }
private String getMaterialName(int type) {
return Material.getMaterial(type).toString().toLowerCase().replace('_', ' ');
}
} }

View File

@@ -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<String> players = new HashSet<String>();
HashMap<String, Integer> created = new HashMap<String, Integer>();
HashMap<String, Integer> destroyed = new HashMap<String, Integer>();
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("<EFBFBD>3Within entire world:");
if (players.size() == 0)
{
player.sendMessage("<EFBFBD>3No results found.");
return;
}
player.sendMessage("<EFBFBD>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("<EFBFBD>6" + String.format("%-6d %-6d %s", c, d, p));
}
}
}

View File

@@ -26,7 +26,7 @@ public class Rollback implements Runnable
this.conn = conn; this.conn = conn;
try { try {
conn.setAutoCommit(false); 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.setString(1, name);
ps.setInt(2, minutes); ps.setInt(2, minutes);
} catch (SQLException ex) { } catch (SQLException ex) {
@@ -74,8 +74,7 @@ public class Rollback implements Runnable
} }
} }
public void run() public void run() {
{
ResultSet rs = null; ResultSet rs = null;
edits.clear(); edits.clear();
try { try {
@@ -134,8 +133,7 @@ public class Rollback implements Runnable
byte data; byte data;
World world; 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.type = type;
this.replaced = replaced; this.replaced = replaced;
this.data = data; this.data = data;
@@ -145,8 +143,9 @@ public class Rollback implements Runnable
this.world = world; this.world = world;
} }
public boolean perform() public boolean perform() {
{ if (type == replaced)
return false;
Block block = world.getBlockAt(x, y, z); Block block = world.getBlockAt(x, y, z);
if (block.getTypeId() == type || (block.getTypeId() >= 8 && block.getTypeId() <= 11) || block.getTypeId() == 51) { if (block.getTypeId() == type || (block.getTypeId() >= 8 && block.getTypeId() <= 11) || block.getTypeId() == 51) {
if (block.setTypeId(replaced)) { if (block.setTypeId(replaced)) {