Fixed spacing for summed lookups in console and logfiles

This commit is contained in:
Robin Kupper
2011-06-22 20:54:52 +02:00
parent 85b4fad65d
commit 1cb412715a
3 changed files with 27 additions and 20 deletions

View File

@@ -362,13 +362,13 @@ public class CommandsHandler implements CommandExecutor
if (rs.next()) { if (rs.next()) {
rs.beforeFirst(); rs.beforeFirst();
final SummarizationMode sum = params.sum; 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) 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) 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()) while (rs.next())
sender.sendMessage(ChatColor.GOLD + histformatter.format(rs, params.coords)); sender.sendMessage(ChatColor.GOLD + histformatter.format(rs));
} else } else
sender.sendMessage(ChatColor.DARK_AQUA + "No results found."); sender.sendMessage(ChatColor.DARK_AQUA + "No results found.");
} catch (final Exception ex) { } catch (final Exception ex) {
@@ -397,11 +397,11 @@ public class CommandsHandler implements CommandExecutor
file.createNewFile(); file.createNewFile();
final FileWriter writer = new FileWriter(file); final FileWriter writer = new FileWriter(file);
final String newline = System.getProperty("line.separator"); 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(); file.getParentFile().mkdirs();
int counter = 0; int counter = 0;
while (rs.next()) { while (rs.next()) {
writer.write(histformatter.format(rs, params.coords) + newline); writer.write(histformatter.format(rs) + newline);
counter++; counter++;
} }
writer.close(); writer.close();

View File

@@ -1,6 +1,7 @@
package de.diddiz.LogBlock; package de.diddiz.LogBlock;
import static de.diddiz.util.BukkitUtils.materialName; import static de.diddiz.util.BukkitUtils.materialName;
import static de.diddiz.util.Utils.spaces;
import java.sql.ResultSet; import java.sql.ResultSet;
import java.sql.SQLException; import java.sql.SQLException;
import java.text.SimpleDateFormat; import java.text.SimpleDateFormat;
@@ -10,12 +11,16 @@ public class HistoryFormatter
{ {
private final SimpleDateFormat formatter = new SimpleDateFormat("MM-dd HH:mm:ss"); private final SimpleDateFormat formatter = new SimpleDateFormat("MM-dd HH:mm:ss");
private final SummarizationMode sum; 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.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) { if (sum == SummarizationMode.NONE) {
final StringBuilder msg = new StringBuilder(formatter.format(rs.getTimestamp("date")) + " " + rs.getString("playername") + " "); final StringBuilder msg = new StringBuilder(formatter.format(rs.getTimestamp("date")) + " " + rs.getString("playername") + " ");
final int type = rs.getInt("type"); final int type = rs.getInt("type");
@@ -52,17 +57,12 @@ public class HistoryFormatter
if (coords) if (coords)
msg.append(" at " + rs.getInt("x") + ":" + rs.getInt("y") + ":" + rs.getInt("z")); msg.append(" at " + rs.getInt("x") + ":" + rs.getInt("y") + ":" + rs.getInt("z"));
return msg.toString(); return msg.toString();
} else if (sum == SummarizationMode.TYPES) }
return fillWithSpaces(rs.getInt("created")) + fillWithSpaces(rs.getInt("destroyed")) + materialName(rs.getInt("type")); String c1 = String.valueOf(rs.getInt(2)), c2 = String.valueOf(rs.getInt(3));
else c1 += spaces((int)((10 - c1.length()) / factor));
return fillWithSpaces(rs.getInt("created")) + fillWithSpaces(rs.getInt("destroyed")) + rs.getString("playername"); c2 += spaces((int)((10 - c2.length()) / factor));
} if (sum == SummarizationMode.TYPES)
return c1 + c2 + materialName(rs.getInt(1));
private static String fillWithSpaces(Integer number) { return c1 + c2 + rs.getString(1);
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();
} }
} }

View File

@@ -117,4 +117,11 @@ public class Utils
return -1; 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();
}
} }