Skip to content

Commit

Permalink
improved database and added signlogging
Browse files Browse the repository at this point in the history
  • Loading branch information
DiddiZ committed Mar 11, 2011
1 parent 9dc9624 commit 5dcecdc
Show file tree
Hide file tree
Showing 9 changed files with 421 additions and 422 deletions.
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
/.classpath
/.project
/.settings
/bin
/bin
/release
32 changes: 12 additions & 20 deletions src/de/diddiz/LogBlock/AreaBlockSearch.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
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;

Expand All @@ -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();
Expand All @@ -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);
Expand All @@ -51,25 +48,22 @@ public void run() {
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)
Expand All @@ -79,13 +73,11 @@ else if (rs.getInt("replaced") == 0)
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('_', ' ');
}
Expand Down
66 changes: 28 additions & 38 deletions src/de/diddiz/LogBlock/AreaStats.java
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
import java.util.HashSet;
import java.util.logging.Level;

import org.bukkit.ChatColor;
import org.bukkit.entity.Player;

public class AreaStats implements Runnable
Expand All @@ -17,57 +18,50 @@ 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<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 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);
ps.setInt(4, player.getLocation().getBlockX()+size);
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);
ps.setInt(4, player.getLocation().getBlockX()+size);
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)
Expand All @@ -77,27 +71,23 @@ public void run()
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));
}
}
}
}
88 changes: 88 additions & 0 deletions src/de/diddiz/LogBlock/BlockStats.java
Original file line number Diff line number Diff line change
@@ -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('_', ' ');
}
}
8 changes: 8 additions & 0 deletions src/de/diddiz/LogBlock/Config.java
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down Expand Up @@ -52,10 +54,14 @@ static boolean Load(Configuration 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()){
Expand All @@ -75,8 +81,10 @@ static boolean Load(Configuration 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;
}
Expand Down
Loading

0 comments on commit 5dcecdc

Please sign in to comment.