From 1cb412715ab388d4d530368267558f3f65f5757d Mon Sep 17 00:00:00 2001 From: Robin Kupper Date: Wed, 22 Jun 2011 20:54:52 +0200 Subject: [PATCH] Fixed spacing for summed lookups in console and logfiles --- src/de/diddiz/LogBlock/CommandsHandler.java | 12 ++++----- src/de/diddiz/LogBlock/HistoryFormatter.java | 28 ++++++++++---------- src/de/diddiz/util/Utils.java | 7 +++++ 3 files changed, 27 insertions(+), 20 deletions(-) diff --git a/src/de/diddiz/LogBlock/CommandsHandler.java b/src/de/diddiz/LogBlock/CommandsHandler.java index 70c1849..c148b57 100644 --- a/src/de/diddiz/LogBlock/CommandsHandler.java +++ b/src/de/diddiz/LogBlock/CommandsHandler.java @@ -362,13 +362,13 @@ public class CommandsHandler implements CommandExecutor if (rs.next()) { rs.beforeFirst(); final SummarizationMode sum = params.sum; - final HistoryFormatter histformatter = new HistoryFormatter(sum); + final HistoryFormatter histformatter = new HistoryFormatter(sum, params.coords, (sender instanceof Player ? 2 / 3f : 1)); if (sum == SummarizationMode.TYPES) - sender.sendMessage(ChatColor.GOLD + String.format("%-6s %-6s %s", "Creat", "Destr", "Block")); + sender.sendMessage(ChatColor.GOLD + "Created - Destroyed - Block"); else if (sum == SummarizationMode.PLAYERS) - sender.sendMessage(ChatColor.GOLD + String.format("%-6s %-6s %s", "Created", "Destroyed", "Playername")); + sender.sendMessage(ChatColor.GOLD + "Created - Destroyed - Player"); while (rs.next()) - sender.sendMessage(ChatColor.GOLD + histformatter.format(rs, params.coords)); + sender.sendMessage(ChatColor.GOLD + histformatter.format(rs)); } else sender.sendMessage(ChatColor.DARK_AQUA + "No results found."); } catch (final Exception ex) { @@ -397,11 +397,11 @@ public class CommandsHandler implements CommandExecutor file.createNewFile(); final FileWriter writer = new FileWriter(file); final String newline = System.getProperty("line.separator"); - final HistoryFormatter histformatter = new HistoryFormatter(params.sum); + final HistoryFormatter histformatter = new HistoryFormatter(params.sum, params.coords, 1); file.getParentFile().mkdirs(); int counter = 0; while (rs.next()) { - writer.write(histformatter.format(rs, params.coords) + newline); + writer.write(histformatter.format(rs) + newline); counter++; } writer.close(); diff --git a/src/de/diddiz/LogBlock/HistoryFormatter.java b/src/de/diddiz/LogBlock/HistoryFormatter.java index 7b4b39b..c8bf710 100644 --- a/src/de/diddiz/LogBlock/HistoryFormatter.java +++ b/src/de/diddiz/LogBlock/HistoryFormatter.java @@ -1,6 +1,7 @@ package de.diddiz.LogBlock; import static de.diddiz.util.BukkitUtils.materialName; +import static de.diddiz.util.Utils.spaces; import java.sql.ResultSet; import java.sql.SQLException; import java.text.SimpleDateFormat; @@ -10,12 +11,16 @@ public class HistoryFormatter { private final SimpleDateFormat formatter = new SimpleDateFormat("MM-dd HH:mm:ss"); private final SummarizationMode sum; + private final boolean coords; + private final float factor; - HistoryFormatter(SummarizationMode sum) { + HistoryFormatter(SummarizationMode sum, boolean coords, float factor) { this.sum = sum; + this.coords = coords; + this.factor = factor; } - String format(ResultSet rs, boolean coords) throws SQLException { + String format(ResultSet rs) throws SQLException { if (sum == SummarizationMode.NONE) { final StringBuilder msg = new StringBuilder(formatter.format(rs.getTimestamp("date")) + " " + rs.getString("playername") + " "); final int type = rs.getInt("type"); @@ -52,17 +57,12 @@ public class HistoryFormatter if (coords) msg.append(" at " + rs.getInt("x") + ":" + rs.getInt("y") + ":" + rs.getInt("z")); return msg.toString(); - } else if (sum == SummarizationMode.TYPES) - return fillWithSpaces(rs.getInt("created")) + fillWithSpaces(rs.getInt("destroyed")) + materialName(rs.getInt("type")); - else - return fillWithSpaces(rs.getInt("created")) + fillWithSpaces(rs.getInt("destroyed")) + rs.getString("playername"); - } - - private static String fillWithSpaces(Integer number) { - final StringBuilder filled = new StringBuilder(number.toString()); - final int neededSpaces = (36 - filled.length() * 6) / 4; - for (int i = 0; i < neededSpaces; i++) - filled.append(' '); - return filled.toString(); + } + String c1 = String.valueOf(rs.getInt(2)), c2 = String.valueOf(rs.getInt(3)); + c1 += spaces((int)((10 - c1.length()) / factor)); + c2 += spaces((int)((10 - c2.length()) / factor)); + if (sum == SummarizationMode.TYPES) + return c1 + c2 + materialName(rs.getInt(1)); + return c1 + c2 + rs.getString(1); } } diff --git a/src/de/diddiz/util/Utils.java b/src/de/diddiz/util/Utils.java index 9b8194b..a8ecfb4 100644 --- a/src/de/diddiz/util/Utils.java +++ b/src/de/diddiz/util/Utils.java @@ -117,4 +117,11 @@ public class Utils return -1; } } + + public static String spaces(int count) { + final StringBuilder filled = new StringBuilder(count); + for (int i = 0; i < count; i++) + filled.append(' '); + return filled.toString(); + } }