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()) {
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();

View File

@@ -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);
}
}

View File

@@ -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();
}
}