diff --git a/src/de/diddiz/LogBlock/PlayerAreaStats.java b/src/de/diddiz/LogBlock/PlayerAreaStats.java index 83d5aa2..b74aab7 100644 --- a/src/de/diddiz/LogBlock/PlayerAreaStats.java +++ b/src/de/diddiz/LogBlock/PlayerAreaStats.java @@ -5,8 +5,6 @@ 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.ChatColor; @@ -30,36 +28,31 @@ public class PlayerAreaStats implements Runnable } 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 + "` INNER JOIN `lb-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 = conn.prepareStatement("SELECT * FROM (SELECT `type` , count(`type`) AS `created` FROM `" + table + "` INNER JOIN `lb-players` USING (`playerid`) WHERE `playername` = ? AND x > ? AND x < ? AND z > ? AND z < ? AND `type` > 0 AND `type` != `replaced` GROUP BY `type`) AS c RIGHT JOIN (SELECT `replaced` AS `type`, count(`replaced`) AS `destroyed` FROM `" + table + "` INNER JOIN `lb-players` USING (`playerid`) WHERE `playername` = ? AND x > ? AND x < ? AND z > ? AND z < ? AND `replaced` > 0 AND `type` != `replaced` GROUP BY `replaced`) AS d ON c.`type` = d.`type` ORDER BY `created` + `destroyed` DESC LIMIT 15;", Statement.NO_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); + ps.setString(6, name); + ps.setInt(7, player.getLocation().getBlockX()-size); + ps.setInt(8, player.getLocation().getBlockX()+size); + ps.setInt(9, player.getLocation().getBlockZ()-size); + ps.setInt(10, player.getLocation().getBlockZ()+size); rs = ps.executeQuery(); - 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 + "` INNER JOIN `lb-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(getMaterialName(rs.getInt("replaced"))); - destroyed.put(getMaterialName(rs.getInt("replaced")), rs.getInt("num")); + player.sendMessage(ChatColor.DARK_AQUA + "Player " + name + " within " + size + " blocks of you: "); + if (!rs.next()) + player.sendMessage(ChatColor.DARK_AQUA + "No results found."); + else { + player.sendMessage(ChatColor.GOLD + String.format("%-6s %-6s %s", "Creat", "Destr", "Block")); + rs.beforeFirst(); + while (rs.next()) { + player.sendMessage(ChatColor.GOLD + String.format("%-6d %-6d %s", rs.getInt("created"), rs.getInt("destroyed"), Material.getMaterial(rs.getInt("type")).toString().toLowerCase().replace('_', ' '))); + } } } catch (SQLException ex) { LogBlock.log.log(Level.SEVERE, "[LogBlock PlayerAreaStats] SQL exception", ex); @@ -75,24 +68,5 @@ public class PlayerAreaStats implements Runnable LogBlock.log.log(Level.SEVERE, "[LogBlock PlayerAreaStats] SQL exception on close", ex); } } - player.sendMessage(ChatColor.DARK_AQUA + "Player " + name + " within " + size + " blocks of you: "); - if (types.size() == 0) - 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('_', ' '); } }