diff --git a/src/de/diddiz/LogBlock/AreaStats.java b/src/de/diddiz/LogBlock/AreaStats.java index c9cd7d7..008e796 100644 --- a/src/de/diddiz/LogBlock/AreaStats.java +++ b/src/de/diddiz/LogBlock/AreaStats.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; @@ -27,38 +25,29 @@ public class AreaStats implements Runnable } 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 playername, count(playername) as num from `" + table + "` INNER JOIN `lb-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); + ps = conn.prepareStatement("SELECT `playername`, SUM(`created`) AS `created`, SUM(`destroyed`) AS `destroyed` FROM ((SELECT `playerid`, count(`type`) AS `created`, 0 AS `destroyed` FROM `" + table + "` WHERE `type` > 0 AND x > ? AND x < ? AND z > ? AND z < ? AND `type` != `replaced` GROUP BY `playerid`) UNION (SELECT `playerid`, 0 AS `created`, count(`replaced`) AS `destroyed` FROM `" + table + "` INNER JOIN `lb-players` USING (`playerid`) WHERE `replaced` > 0 AND x > ? AND x < ? AND z > ? AND z < ? AND `type` != `replaced` GROUP BY `playerid`)) AS t INNER JOIN `lb-players` USING (`playerid`) GROUP BY `playerid` ORDER BY SUM(`created`) + SUM(`destroyed`) DESC LIMIT 15", Statement.NO_GENERATED_KEYS); + ps.setInt(1, player.getLocation().getBlockX()-size); + ps.setInt(2, player.getLocation().getBlockX()+size); + ps.setInt(3, player.getLocation().getBlockZ()-size); + ps.setInt(4, player.getLocation().getBlockZ()+size); + ps.setInt(5, player.getLocation().getBlockX()-size); + ps.setInt(6, player.getLocation().getBlockX()+size); + ps.setInt(7, player.getLocation().getBlockZ()-size); + ps.setInt(8, player.getLocation().getBlockZ()+size); rs = ps.executeQuery(); - while (rs.next()) { - players.add(rs.getString("playername")); - created.put(rs.getString("playername"), rs.getInt("num")); - } - rs.close(); - ps.close(); - ps = conn.prepareStatement("SELECT playername, count(playername) as num from `" + table + "` INNER JOIN `lb-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("playername")); - destroyed.put(rs.getString("playername"), rs.getInt("num")); + player.sendMessage(ChatColor.DARK_AQUA + "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", "Player")); + rs.beforeFirst(); + while (rs.next()) { + player.sendMessage(ChatColor.GOLD + String.format("%-6d %-6d %s", rs.getInt("created"), rs.getInt("destroyed"), rs.getString("playername"))); + } } } catch (SQLException ex) { LogBlock.log.log(Level.SEVERE, "[LogBlock AreaStats] SQL exception", ex); @@ -74,20 +63,5 @@ public class AreaStats implements Runnable LogBlock.log.log(Level.SEVERE, "[LogBlock AreaStats] SQL exception on close", ex); } } - player.sendMessage(ChatColor.DARK_AQUA + "Within " + size + " blocks of you: "); - if (players.size() == 0) - 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)); - } - } } }