From a5a5f7e24ae64bd5e8fc75a76b569bdc8b4eb1fc Mon Sep 17 00:00:00 2001 From: sk89q Date: Sat, 9 Apr 2011 02:25:47 -0700 Subject: [PATCH] Made /wg report look better. --- .../sk89q/worldguard/bukkit/ReportWriter.java | 223 +++++++++++------- .../sk89q/worldguard/util/LogListBlock.java | 128 ++++++++++ 2 files changed, 264 insertions(+), 87 deletions(-) create mode 100644 src/com/sk89q/worldguard/util/LogListBlock.java diff --git a/src/com/sk89q/worldguard/bukkit/ReportWriter.java b/src/com/sk89q/worldguard/bukkit/ReportWriter.java index 2c9f7363..22986b4a 100644 --- a/src/com/sk89q/worldguard/bukkit/ReportWriter.java +++ b/src/com/sk89q/worldguard/bukkit/ReportWriter.java @@ -31,7 +31,6 @@ import java.util.Map; import org.bukkit.Server; import org.bukkit.World; -import org.bukkit.command.CommandSender; import org.bukkit.entity.Entity; import org.bukkit.plugin.Plugin; import com.sk89q.worldguard.protection.GlobalRegionManager; @@ -40,50 +39,71 @@ import com.sk89q.worldguard.protection.flags.StateFlag; import com.sk89q.worldguard.protection.managers.RegionManager; import com.sk89q.worldguard.protection.regions.ProtectedRegion; +import com.sk89q.worldguard.util.LogListBlock; public class ReportWriter { private static final SimpleDateFormat dateFmt = new SimpleDateFormat("yyyy-MM-dd kk:mm Z"); - private WorldGuardPlugin plugin; private Date date = new Date(); private StringBuilder output = new StringBuilder(); public ReportWriter(WorldGuardPlugin plugin) { - this.plugin = plugin; - - appendHeader(plugin); + appendReportHeader(plugin); appendServerInformation(plugin.getServer()); appendPluginInformation(plugin.getServer().getPluginManager().getPlugins()); appendWorldInformation(plugin.getServer().getWorlds()); appendGlobalConfiguration(plugin.getGlobalConfiguration()); appendWorldConfigurations(plugin, plugin.getServer().getWorlds(), plugin.getGlobalRegionManager(), plugin.getGlobalConfiguration()); - appendRule(); + appendln("-------------"); appendln("END OF REPORT"); appendln(); } - private void appendln(String text) { + protected static String repeat(String str, int n) { + if(str == null) { + return null; + } + + StringBuilder sb = new StringBuilder(); + for (int i = 0; i < n; i++) { + sb.append(str); + } + + return sb.toString(); + } + + protected void appendln(String text) { output.append(text); output.append("\r\n"); } - private void appendln(String text, Object ... args) { + protected void appendln(String text, Object ... args) { output.append(String.format(text, args)); output.append("\r\n"); } - private void appendln() { + protected void append(LogListBlock log) { + output.append(log.toString()); + } + + protected void appendln() { output.append("\r\n"); } - private void appendRule() { - output.append("--------------------------------------------------\r\n"); + protected void appendHeader(String text) { + String rule = repeat("-", text.length()); + output.append(rule); + output.append("\r\n"); + appendln(text); + output.append(rule); + output.append("\r\n"); + appendln(); } - private void appendHeader(WorldGuardPlugin plugin) { + private void appendReportHeader(WorldGuardPlugin plugin) { appendln("WorldGuard Configuration Report"); appendln("Generated " + dateFmt.format(date)); appendln(); @@ -92,76 +112,104 @@ private void appendHeader(WorldGuardPlugin plugin) { } private void appendGlobalConfiguration(ConfigurationManager config) { - appendRule(); - appendln("GLOBAL CONFIGURATION:"); - appendRule(); - appendln(); + appendHeader("Global Configuration"); + + LogListBlock log = new LogListBlock(); + LogListBlock entitiesLog = log.putChild("Entities"); + LogListBlock configLog = log.putChild("Configuration"); Class cls = config.getClass(); for (Field field : cls.getFields()) { try { Object val = field.get(config); - appendln("%-30s: %s", field.getName(), String.valueOf(val)); + configLog.put(field.getName(), val); } catch (IllegalArgumentException e) { e.printStackTrace(); } catch (IllegalAccessException e) { } } + append(log); appendln(); } private void appendServerInformation(Server server) { - appendRule(); - appendln("SERVER:"); - appendRule(); - appendln(); - - appendln("%-15s: %s", "Server ID", server.getServerId()); - appendln("%-15s: %s", "Server name", server.getServerName()); - appendln("%-15s: %s", "Implementation", server.getVersion()); - //appendln("%-15s: %s:%d", "Address", server.getIp(), server.getPort()); - appendln("%-15s: %d/%d", "Player count", - server.getOnlinePlayers().length, server.getMaxPlayers()); + appendHeader("Server Information"); + LogListBlock log = new LogListBlock(); + + Runtime runtime = Runtime.getRuntime(); + + log.put("Java", "%s %s (%s)", + System.getProperty("java.vendor"), + System.getProperty("java.version"), + System.getProperty("java.vendor.url")); + log.put("Operating system", "%s %s (%s)", + System.getProperty("os.name"), + System.getProperty("os.version"), + System.getProperty("os.arch")); + log.put("Available processors", runtime.availableProcessors()); + log.put("Free memory", runtime.freeMemory() / 1024 / 1024 + " MB"); + log.put("Max memory", runtime.maxMemory() / 1024 / 1024 + " MB"); + log.put("Total memory", runtime.totalMemory() / 1024 / 1024 + " MB"); + log.put("Server ID", server.getServerId()); + log.put("Server name", server.getServerName()); + log.put("Implementation", server.getVersion()); + //log.put("Address", server.getIp(), server.getPort()); + log.put("Player count", "%d/%d", + server.getOnlinePlayers().length, server.getMaxPlayers()); + + append(log); appendln(); } private void appendPluginInformation(Plugin[] plugins) { - appendRule(); - appendln("PLUGINS (%d)", plugins.length); - appendRule(); - appendln(); + appendHeader("Plugins"); + + LogListBlock log = new LogListBlock(); for (Plugin plugin : plugins) { - appendln("%s %s <%s>:", plugin.getDescription().getName(), - plugin.getDescription().getVersion(), - plugin.getDescription().getWebsite()); - appendln(" %-15s: %s", "Entry point", plugin.getDescription().getMain()); - appendln(" %-15s: %s", "Data folder", plugin.getDataFolder().getAbsoluteFile()); + log.put(plugin.getDescription().getName(), plugin.getDescription().getVersion()); } - + + append(log); appendln(); + + /*appendHeader("Plugin Information"); + + log = new LogListBlock(); + + for (Plugin plugin : plugins) { + log.putChild(plugin.getDescription().getName()) + .put("Data folder", plugin.getDataFolder()) + .put("Website", plugin.getDescription().getWebsite()) + .put("Entry point", plugin.getDescription().getMain()); + } + + append(log); + appendln();*/ } private void appendWorldInformation(List worlds) { - appendRule(); - appendln("WORLDS (%d)", worlds.size()); - appendRule(); - appendln(); + appendHeader("Worlds"); + + LogListBlock log = new LogListBlock(); int i = 0; for (World world : worlds) { int loadedChunkCount = world.getLoadedChunks().length; - appendln("%d. %s:", i, world.getName()); - appendln(" Information:"); - appendln(" %-15s: %s", "ID", world.getId()); - appendln(" %-15s: %s", "Environment", world.getEnvironment().toString()); - appendln(" %-15s: %d", "Player #", world.getPlayers().size()); - appendln(" %-15s: %d", "Entity #", world.getEntities().size()); - appendln(" %-15s: %d", "Loaded chunk #", loadedChunkCount); - appendln(" Entities:"); + LogListBlock worldLog = log.putChild(world.getName() + " (" + i + ")"); + LogListBlock infoLog = worldLog.putChild("Information"); + LogListBlock entitiesLog = worldLog.putChild("Entities"); + + infoLog.put("ID", world.getId()); + infoLog.put("Environment", world.getEnvironment().toString()); + infoLog.put("Player count", world.getPlayers().size()); + infoLog.put("Entity count", world.getEntities().size()); + infoLog.put("Loaded chunk count", loadedChunkCount); + infoLog.put("Spawn location", world.getSpawnLocation()); + infoLog.put("Raw time", world.getFullTime()); Map, Integer> entityCounts = new HashMap, Integer>(); @@ -180,83 +228,84 @@ private void appendWorldInformation(List worlds) { // Print entities for (Map.Entry, Integer> entry : entityCounts.entrySet()) { - appendln(" %-25s: %d [%f]", entry.getKey().getSimpleName(), - entry.getValue(), (float) (entry.getValue() / (double) loadedChunkCount)); + entitiesLog.put(entry.getKey().getSimpleName(), + "%d [%f]", + entry.getValue(), + (float) (entry.getValue() / (double) loadedChunkCount)); } i++; } - + + append(log); appendln(); } private void appendWorldConfigurations(WorldGuardPlugin plugin, List worlds, GlobalRegionManager regionMgr, ConfigurationManager mgr) { - appendRule(); - appendln("WORLD CONFIGURATIONS"); - appendRule(); - appendln(); + appendHeader("World Configurations"); + + LogListBlock log = new LogListBlock(); int i = 0; for (World world : worlds) { - appendln("%d. %s:", i, world.getName()); + LogListBlock worldLog = log.putChild(world.getName() + " (" + i + ")"); + LogListBlock infoLog = worldLog.putChild("Information"); + LogListBlock configLog = worldLog.putChild("Configuration"); + LogListBlock blacklistLog = worldLog.putChild("Blacklist"); + LogListBlock regionsLog = worldLog.putChild("Region manager"); + + infoLog.put("Configuration file", (new File(plugin.getDataFolder(), "worlds/" + + world.getName() + "/config.yml")).getAbsoluteFile()); + + infoLog.put("Blacklist file", (new File(plugin.getDataFolder(), "worlds/" + + world.getName() + "/blacklist.txt")).getAbsoluteFile()); + infoLog.put("Regions file", (new File(plugin.getDataFolder(), "worlds/" + + world.getName() + "/regions.yml")).getAbsoluteFile()); WorldConfiguration config = mgr.get(world); - appendln(" Configuration:"); - appendln(" %-30s: %s", "File", - (new File(plugin.getDataFolder(), "worlds/" - + world.getName() + "/config.yml")).getAbsoluteFile()); - Class cls = config.getClass(); for (Field field : cls.getFields()) { try { Object val = field.get(config); - appendln(" %-30s: %s", field.getName(), String.valueOf(val)); + configLog.put(field.getName(), String.valueOf(val)); } catch (IllegalArgumentException e) { e.printStackTrace(); } catch (IllegalAccessException e) { } } - - appendln(" Blacklist:"); - appendln(" %-15s: %s", "File", - (new File(plugin.getDataFolder(), "worlds/" - + world.getName() + "/blacklist.txt")).getAbsoluteFile()); + if (config.getBlacklist() == null) { - appendln(" DISABLED / NO ENTRIES"); + blacklistLog.put("State", "DISABLED"); } else { - appendln(" %-15s: %s", "Number of items", + blacklistLog.put("State", "Enabled"); + blacklistLog.put("Number of items", config.getBlacklist().getItemCount()); - appendln(" %-15s: %s", "Is whitelist", - config.getBlacklist().isWhitelist() ? "YES" : "NO"); + blacklistLog.put("Is whitelist", + config.getBlacklist().isWhitelist()); } - appendln(" Region manager:"); - appendln(" %-15s: %s", "File", - (new File(plugin.getDataFolder(), "worlds/" - + world.getName() + "/regions.yml")).getAbsoluteFile()); RegionManager worldRegions = regionMgr.get(world); - appendln(" %-15s: %s", "Type", - worldRegions.getClass().getCanonicalName()); - appendln(" %-15s: %s", "Number of regions", - worldRegions.getRegions().size()); - appendln(" Global region:"); + + regionsLog.put("Type", worldRegions.getClass().getCanonicalName()); + regionsLog.put("Number of regions", worldRegions.getRegions().size()); + LogListBlock globalRegionLog = regionsLog.putChild("Global region"); + ProtectedRegion globalRegion = worldRegions.getRegion("__global__"); if (globalRegion == null) { - appendln(" UNDEFINED"); + globalRegionLog.put("Status", "UNDEFINED"); } else { - appendln(" %-20s: %s", "Type", - globalRegion.getClass().getCanonicalName()); for (Flag flag : DefaultFlag.getFlags()) { if (flag instanceof StateFlag) { - appendln(" %-20s: %s", flag.getName(), + globalRegionLog.put(flag.getName(), globalRegion.getFlag(flag)); } } } } - + + append(log); appendln(); } diff --git a/src/com/sk89q/worldguard/util/LogListBlock.java b/src/com/sk89q/worldguard/util/LogListBlock.java new file mode 100644 index 00000000..0707cff2 --- /dev/null +++ b/src/com/sk89q/worldguard/util/LogListBlock.java @@ -0,0 +1,128 @@ +// $Id$ +/* + * WorldGuard + * Copyright (C) 2010 sk89q + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . +*/ + +package com.sk89q.worldguard.util; + +import java.util.Map; +import java.util.LinkedHashMap; + +public class LogListBlock { + private LinkedHashMap items + = new LinkedHashMap(); + private int maxKeyLength = 0; + + private void updateKey(String key) { + if (key.length() > maxKeyLength) { + maxKeyLength = key.length(); + } + } + + public LogListBlock put(String key, String value) { + updateKey(key); + items.put(key, String.valueOf(value)); + return this; + } + + public LogListBlock put(String key, LogListBlock value) { + updateKey(key); + items.put(key, value); + return this; + } + + public LogListBlock put(String key, Object value) { + put(key, String.valueOf(value)); + return this; + } + + public LogListBlock put(String key, String value, Object ... args) { + put(key, String.format(value, args)); + return this; + } + + public LogListBlock put(String key, int value) { + put(key, String.valueOf(value)); + return this; + } + + public LogListBlock put(String key, byte value) { + put(key, String.valueOf(value)); + return this; + } + + public LogListBlock put(String key, double value) { + put(key, String.valueOf(value)); + return this; + } + + public LogListBlock put(String key, float value) { + put(key, String.valueOf(value)); + return this; + } + + public LogListBlock put(String key, short value) { + put(key, String.valueOf(value)); + return this; + } + + public LogListBlock put(String key, long value) { + put(key, String.valueOf(value)); + return this; + } + + public LogListBlock put(String key, boolean value) { + put(key, String.valueOf(value)); + return this; + } + + public LogListBlock putChild(String key) { + updateKey(key); + LogListBlock block = new LogListBlock(); + items.put(key, block); + return block; + } + + private String padKey(String key, int len) { + return String.format("%-" + len + "s", key); + } + + protected String getOutput(String prefix) { + StringBuilder out = new StringBuilder(); + for (Map.Entry entry : items.entrySet()) { + Object val = entry.getValue(); + if (val instanceof LogListBlock) { + out.append(prefix); + out.append(padKey(entry.getKey(), maxKeyLength)); + out.append(":\r\n"); + out.append(((LogListBlock) val).getOutput(prefix + " ")); + } else { + out.append(prefix); + out.append(padKey(entry.getKey(), maxKeyLength)); + out.append(": "); + out.append(val.toString()); + out.append("\r\n"); + } + } + return out.toString(); + } + + @Override + public String toString() { + return getOutput(""); + } +} \ No newline at end of file