Made /wg report look better.

This commit is contained in:
sk89q 2011-04-09 02:25:47 -07:00
parent 1e203b58c3
commit a5a5f7e24a
2 changed files with 264 additions and 87 deletions

View File

@ -31,7 +31,6 @@
import java.util.Map; import java.util.Map;
import org.bukkit.Server; import org.bukkit.Server;
import org.bukkit.World; import org.bukkit.World;
import org.bukkit.command.CommandSender;
import org.bukkit.entity.Entity; import org.bukkit.entity.Entity;
import org.bukkit.plugin.Plugin; import org.bukkit.plugin.Plugin;
import com.sk89q.worldguard.protection.GlobalRegionManager; import com.sk89q.worldguard.protection.GlobalRegionManager;
@ -40,50 +39,71 @@
import com.sk89q.worldguard.protection.flags.StateFlag; import com.sk89q.worldguard.protection.flags.StateFlag;
import com.sk89q.worldguard.protection.managers.RegionManager; import com.sk89q.worldguard.protection.managers.RegionManager;
import com.sk89q.worldguard.protection.regions.ProtectedRegion; import com.sk89q.worldguard.protection.regions.ProtectedRegion;
import com.sk89q.worldguard.util.LogListBlock;
public class ReportWriter { public class ReportWriter {
private static final SimpleDateFormat dateFmt = private static final SimpleDateFormat dateFmt =
new SimpleDateFormat("yyyy-MM-dd kk:mm Z"); new SimpleDateFormat("yyyy-MM-dd kk:mm Z");
private WorldGuardPlugin plugin;
private Date date = new Date(); private Date date = new Date();
private StringBuilder output = new StringBuilder(); private StringBuilder output = new StringBuilder();
public ReportWriter(WorldGuardPlugin plugin) { public ReportWriter(WorldGuardPlugin plugin) {
this.plugin = plugin; appendReportHeader(plugin);
appendHeader(plugin);
appendServerInformation(plugin.getServer()); appendServerInformation(plugin.getServer());
appendPluginInformation(plugin.getServer().getPluginManager().getPlugins()); appendPluginInformation(plugin.getServer().getPluginManager().getPlugins());
appendWorldInformation(plugin.getServer().getWorlds()); appendWorldInformation(plugin.getServer().getWorlds());
appendGlobalConfiguration(plugin.getGlobalConfiguration()); appendGlobalConfiguration(plugin.getGlobalConfiguration());
appendWorldConfigurations(plugin, plugin.getServer().getWorlds(), appendWorldConfigurations(plugin, plugin.getServer().getWorlds(),
plugin.getGlobalRegionManager(), plugin.getGlobalConfiguration()); plugin.getGlobalRegionManager(), plugin.getGlobalConfiguration());
appendRule(); appendln("-------------");
appendln("END OF REPORT"); appendln("END OF REPORT");
appendln(); 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(text);
output.append("\r\n"); 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(String.format(text, args));
output.append("\r\n"); output.append("\r\n");
} }
private void appendln() { protected void append(LogListBlock log) {
output.append(log.toString());
}
protected void appendln() {
output.append("\r\n"); output.append("\r\n");
} }
private void appendRule() { protected void appendHeader(String text) {
output.append("--------------------------------------------------\r\n"); 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("WorldGuard Configuration Report");
appendln("Generated " + dateFmt.format(date)); appendln("Generated " + dateFmt.format(date));
appendln(); appendln();
@ -92,76 +112,104 @@ private void appendHeader(WorldGuardPlugin plugin) {
} }
private void appendGlobalConfiguration(ConfigurationManager config) { private void appendGlobalConfiguration(ConfigurationManager config) {
appendRule(); appendHeader("Global Configuration");
appendln("GLOBAL CONFIGURATION:");
appendRule(); LogListBlock log = new LogListBlock();
appendln(); LogListBlock entitiesLog = log.putChild("Entities");
LogListBlock configLog = log.putChild("Configuration");
Class<? extends ConfigurationManager> cls = config.getClass(); Class<? extends ConfigurationManager> cls = config.getClass();
for (Field field : cls.getFields()) { for (Field field : cls.getFields()) {
try { try {
Object val = field.get(config); Object val = field.get(config);
appendln("%-30s: %s", field.getName(), String.valueOf(val)); configLog.put(field.getName(), val);
} catch (IllegalArgumentException e) { } catch (IllegalArgumentException e) {
e.printStackTrace(); e.printStackTrace();
} catch (IllegalAccessException e) { } catch (IllegalAccessException e) {
} }
} }
append(log);
appendln(); appendln();
} }
private void appendServerInformation(Server server) { private void appendServerInformation(Server server) {
appendRule(); appendHeader("Server Information");
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());
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(); appendln();
} }
private void appendPluginInformation(Plugin[] plugins) { private void appendPluginInformation(Plugin[] plugins) {
appendRule(); appendHeader("Plugins");
appendln("PLUGINS (%d)", plugins.length);
appendRule(); LogListBlock log = new LogListBlock();
appendln();
for (Plugin plugin : plugins) { for (Plugin plugin : plugins) {
appendln("%s %s <%s>:", plugin.getDescription().getName(), log.put(plugin.getDescription().getName(), plugin.getDescription().getVersion());
plugin.getDescription().getVersion(),
plugin.getDescription().getWebsite());
appendln(" %-15s: %s", "Entry point", plugin.getDescription().getMain());
appendln(" %-15s: %s", "Data folder", plugin.getDataFolder().getAbsoluteFile());
} }
append(log);
appendln(); 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<World> worlds) { private void appendWorldInformation(List<World> worlds) {
appendRule(); appendHeader("Worlds");
appendln("WORLDS (%d)", worlds.size());
appendRule(); LogListBlock log = new LogListBlock();
appendln();
int i = 0; int i = 0;
for (World world : worlds) { for (World world : worlds) {
int loadedChunkCount = world.getLoadedChunks().length; int loadedChunkCount = world.getLoadedChunks().length;
appendln("%d. %s:", i, world.getName()); LogListBlock worldLog = log.putChild(world.getName() + " (" + i + ")");
appendln(" Information:"); LogListBlock infoLog = worldLog.putChild("Information");
appendln(" %-15s: %s", "ID", world.getId()); LogListBlock entitiesLog = worldLog.putChild("Entities");
appendln(" %-15s: %s", "Environment", world.getEnvironment().toString());
appendln(" %-15s: %d", "Player #", world.getPlayers().size()); infoLog.put("ID", world.getId());
appendln(" %-15s: %d", "Entity #", world.getEntities().size()); infoLog.put("Environment", world.getEnvironment().toString());
appendln(" %-15s: %d", "Loaded chunk #", loadedChunkCount); infoLog.put("Player count", world.getPlayers().size());
appendln(" Entities:"); 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<Class<? extends Entity>, Integer> entityCounts = Map<Class<? extends Entity>, Integer> entityCounts =
new HashMap<Class<? extends Entity>, Integer>(); new HashMap<Class<? extends Entity>, Integer>();
@ -180,83 +228,84 @@ private void appendWorldInformation(List<World> worlds) {
// Print entities // Print entities
for (Map.Entry<Class<? extends Entity>, Integer> entry for (Map.Entry<Class<? extends Entity>, Integer> entry
: entityCounts.entrySet()) { : entityCounts.entrySet()) {
appendln(" %-25s: %d [%f]", entry.getKey().getSimpleName(), entitiesLog.put(entry.getKey().getSimpleName(),
entry.getValue(), (float) (entry.getValue() / (double) loadedChunkCount)); "%d [%f]",
entry.getValue(),
(float) (entry.getValue() / (double) loadedChunkCount));
} }
i++; i++;
} }
append(log);
appendln(); appendln();
} }
private void appendWorldConfigurations(WorldGuardPlugin plugin, List<World> worlds, private void appendWorldConfigurations(WorldGuardPlugin plugin, List<World> worlds,
GlobalRegionManager regionMgr, ConfigurationManager mgr) { GlobalRegionManager regionMgr, ConfigurationManager mgr) {
appendRule(); appendHeader("World Configurations");
appendln("WORLD CONFIGURATIONS");
appendRule(); LogListBlock log = new LogListBlock();
appendln();
int i = 0; int i = 0;
for (World world : worlds) { 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); WorldConfiguration config = mgr.get(world);
appendln(" Configuration:");
appendln(" %-30s: %s", "File",
(new File(plugin.getDataFolder(), "worlds/"
+ world.getName() + "/config.yml")).getAbsoluteFile());
Class<? extends WorldConfiguration> cls = config.getClass(); Class<? extends WorldConfiguration> cls = config.getClass();
for (Field field : cls.getFields()) { for (Field field : cls.getFields()) {
try { try {
Object val = field.get(config); Object val = field.get(config);
appendln(" %-30s: %s", field.getName(), String.valueOf(val)); configLog.put(field.getName(), String.valueOf(val));
} catch (IllegalArgumentException e) { } catch (IllegalArgumentException e) {
e.printStackTrace(); e.printStackTrace();
} catch (IllegalAccessException e) { } catch (IllegalAccessException e) {
} }
} }
appendln(" Blacklist:");
appendln(" %-15s: %s", "File",
(new File(plugin.getDataFolder(), "worlds/"
+ world.getName() + "/blacklist.txt")).getAbsoluteFile());
if (config.getBlacklist() == null) { if (config.getBlacklist() == null) {
appendln(" DISABLED / NO ENTRIES"); blacklistLog.put("State", "DISABLED");
} else { } else {
appendln(" %-15s: %s", "Number of items", blacklistLog.put("State", "Enabled");
blacklistLog.put("Number of items",
config.getBlacklist().getItemCount()); config.getBlacklist().getItemCount());
appendln(" %-15s: %s", "Is whitelist", blacklistLog.put("Is whitelist",
config.getBlacklist().isWhitelist() ? "YES" : "NO"); 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); RegionManager worldRegions = regionMgr.get(world);
appendln(" %-15s: %s", "Type",
worldRegions.getClass().getCanonicalName()); regionsLog.put("Type", worldRegions.getClass().getCanonicalName());
appendln(" %-15s: %s", "Number of regions", regionsLog.put("Number of regions", worldRegions.getRegions().size());
worldRegions.getRegions().size()); LogListBlock globalRegionLog = regionsLog.putChild("Global region");
appendln(" Global region:");
ProtectedRegion globalRegion = worldRegions.getRegion("__global__"); ProtectedRegion globalRegion = worldRegions.getRegion("__global__");
if (globalRegion == null) { if (globalRegion == null) {
appendln(" UNDEFINED"); globalRegionLog.put("Status", "UNDEFINED");
} else { } else {
appendln(" %-20s: %s", "Type",
globalRegion.getClass().getCanonicalName());
for (Flag<?> flag : DefaultFlag.getFlags()) { for (Flag<?> flag : DefaultFlag.getFlags()) {
if (flag instanceof StateFlag) { if (flag instanceof StateFlag) {
appendln(" %-20s: %s", flag.getName(), globalRegionLog.put(flag.getName(),
globalRegion.getFlag(flag)); globalRegion.getFlag(flag));
} }
} }
} }
} }
append(log);
appendln(); appendln();
} }

View File

@ -0,0 +1,128 @@
// $Id$
/*
* WorldGuard
* Copyright (C) 2010 sk89q <http://www.sk89q.com>
*
* 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 <http://www.gnu.org/licenses/>.
*/
package com.sk89q.worldguard.util;
import java.util.Map;
import java.util.LinkedHashMap;
public class LogListBlock {
private LinkedHashMap <String, Object> items
= new LinkedHashMap<String, Object>();
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<String, Object> 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("");
}
}