From a47df27b307d7f05862c509045e8753751ec0b47 Mon Sep 17 00:00:00 2001 From: Olof Larsson Date: Sat, 22 Sep 2012 12:40:52 +0200 Subject: [PATCH] Logging with color and improvement to Colls. --- src/com/massivecraft/mcore4/MPlugin.java | 25 +++++++++++++++---- .../mcore4/cmd/arg/ARUniverse.java | 1 - .../mcore4/integration/Integration.java | 9 ++++--- src/com/massivecraft/mcore4/store/Colls.java | 19 ++++++++------ src/com/massivecraft/mcore4/usys/Aspect.java | 2 ++ .../massivecraft/mcore4/usys/Multiverse.java | 9 +++++-- .../usys/cmd/CmdUsysMultiverseList.java | 9 +------ .../usys/cmd/CmdUsysMultiverseShow.java | 2 +- .../mcore4/usys/cmd/CmdUsysUniverseNew.java | 2 +- src/com/massivecraft/mcore4/util/MUtil.java | 13 ++++++++++ src/com/massivecraft/mcore4/util/Txt.java | 1 + 11 files changed, 62 insertions(+), 30 deletions(-) diff --git a/src/com/massivecraft/mcore4/MPlugin.java b/src/com/massivecraft/mcore4/MPlugin.java index c35e5d04..eafc9bd5 100644 --- a/src/com/massivecraft/mcore4/MPlugin.java +++ b/src/com/massivecraft/mcore4/MPlugin.java @@ -4,6 +4,8 @@ import java.util.logging.Level; import java.util.logging.Logger; import org.bukkit.Bukkit; +import org.bukkit.ChatColor; +import org.bukkit.command.ConsoleCommandSender; import org.bukkit.event.Listener; import org.bukkit.plugin.java.JavaPlugin; @@ -37,7 +39,10 @@ public abstract class MPlugin extends JavaPlugin implements Listener public boolean preEnable() { timeEnableStart = System.currentTimeMillis(); - this.logPrefix = "["+this.getDescription().getFullName()+"] "; + + this.logPrefixColored = Txt.parse("[%s %s] ", this.getDescription().getName(), this.getDescription().getVersion()); + this.logPrefixPlain = ChatColor.stripColor(this.logPrefixColored); + log("=== ENABLE START ==="); // Create Gson @@ -54,7 +59,7 @@ public abstract class MPlugin extends JavaPlugin implements Listener public void postEnable() { - log("=== ENABLE DONE (Took "+(System.currentTimeMillis()-timeEnableStart)+"ms) ==="); + log(Txt.parse("=== ENABLE COMPLELTE (Took "+(System.currentTimeMillis()-timeEnableStart)+"ms) ===")); } // -------------------------------------------- // @@ -94,7 +99,7 @@ public abstract class MPlugin extends JavaPlugin implements Listener public void suicide() { - log("Now I suicide!"); + log(Txt.parse("Now I suicide!")); Bukkit.getPluginManager().disablePlugin(this); } @@ -109,13 +114,23 @@ public abstract class MPlugin extends JavaPlugin implements Listener // -------------------------------------------- // // LOGGING // -------------------------------------------- // - private String logPrefix = null; + private String logPrefixColored = null; + private String logPrefixPlain = null; public void log(Object... msg) { log(Level.INFO, msg); } public void log(Level level, Object... msg) { - Logger.getLogger("Minecraft").log(level, this.logPrefix + Txt.implode(msg, " ")); + String imploded = Txt.implode(msg, " "); + ConsoleCommandSender sender = Bukkit.getConsoleSender(); + if (level == Level.INFO && sender != null) + { + Bukkit.getConsoleSender().sendMessage(this.logPrefixColored + imploded); + } + else + { + Logger.getLogger("Minecraft").log(level, this.logPrefixPlain + imploded); + } } } diff --git a/src/com/massivecraft/mcore4/cmd/arg/ARUniverse.java b/src/com/massivecraft/mcore4/cmd/arg/ARUniverse.java index 53a4b2da..9604f15c 100644 --- a/src/com/massivecraft/mcore4/cmd/arg/ARUniverse.java +++ b/src/com/massivecraft/mcore4/cmd/arg/ARUniverse.java @@ -27,7 +27,6 @@ public class ARUniverse implements ArgReader result.getErrors().add("No universe \""+str+"\" exists in multiverse "+this.multiverse.getId()+"."); Collection names = new ArrayList(multiverse.getUniverses()); - names.add(Multiverse.DEFAULT); result.getErrors().add("Use "+Txt.implodeCommaAndDot(names, "%s", ", ", " or ", ".")); } diff --git a/src/com/massivecraft/mcore4/integration/Integration.java b/src/com/massivecraft/mcore4/integration/Integration.java index d3025459..8a75f88e 100644 --- a/src/com/massivecraft/mcore4/integration/Integration.java +++ b/src/com/massivecraft/mcore4/integration/Integration.java @@ -9,6 +9,7 @@ import org.bukkit.event.server.PluginEnableEvent; import org.bukkit.plugin.Plugin; import com.massivecraft.mcore4.MPlugin; +import com.massivecraft.mcore4.util.Txt; public class Integration implements Listener { @@ -49,11 +50,11 @@ public class Integration implements Listener { this.features.activate(); this.active = true; - this.ourPlugin.log("Activated integration with "+this.features.getTargetPluginName()+"."); + this.ourPlugin.log(Txt.parse("Activated integration with %s.", this.features.getTargetPluginName())); } catch (Exception e) { - this.ourPlugin.log("Failed to activate integration with "+this.features.getTargetPluginName()+"."); + this.ourPlugin.log(Txt.parse("Failed to activate integration with %s.", this.features.getTargetPluginName())); e.printStackTrace(); } } @@ -66,11 +67,11 @@ public class Integration implements Listener { this.active = false; this.features.deactivate(); - this.ourPlugin.log("Deactivated integration with "+this.features.getTargetPluginName()+"."); + this.ourPlugin.log(Txt.parse("Deactivated integration with %s.", this.features.getTargetPluginName())); } catch (Exception e) { - this.ourPlugin.log("Failed to deactivate integration with "+this.features.getTargetPluginName()+"."); + this.ourPlugin.log(Txt.parse("Failed to deactivate integration with %s.", this.features.getTargetPluginName())); e.printStackTrace(); } } diff --git a/src/com/massivecraft/mcore4/store/Colls.java b/src/com/massivecraft/mcore4/store/Colls.java index 4ed700ec..0ec3d747 100644 --- a/src/com/massivecraft/mcore4/store/Colls.java +++ b/src/com/massivecraft/mcore4/store/Colls.java @@ -1,6 +1,8 @@ package com.massivecraft.mcore4.store; +import java.util.ArrayList; import java.util.HashMap; +import java.util.List; import java.util.Map; import com.massivecraft.mcore4.usys.Aspect; @@ -9,14 +11,10 @@ import com.massivecraft.mcore4.util.MUtil; public abstract class Colls, E, L> { protected Map name2coll = new HashMap(); - public Map name2coll() { return this.name2coll; } public abstract C createColl(String name); - public abstract Aspect aspect(); public abstract String basename(); - - public abstract Db getDb(); // -------------------------------------------- // // CONSTRUCT @@ -24,12 +22,17 @@ public abstract class Colls, E, L> public void init() { - String start = this.collnameForUniverse(""); - for (String collname : this.getDb().collnames()) + this.getColls(); + } + + public List getColls() + { + List ret = new ArrayList(); + for (String universe : this.aspect().multiverse().getUniverses()) { - if ( ! collname.startsWith(start)) continue; - this.getForCollname(collname); + ret.add(this.getForUniverse(universe)); } + return ret; } // -------------------------------------------- // diff --git a/src/com/massivecraft/mcore4/usys/Aspect.java b/src/com/massivecraft/mcore4/usys/Aspect.java index c6c99339..ce0b64a8 100644 --- a/src/com/massivecraft/mcore4/usys/Aspect.java +++ b/src/com/massivecraft/mcore4/usys/Aspect.java @@ -5,6 +5,7 @@ import java.util.Arrays; import java.util.Collection; import com.massivecraft.mcore4.store.Entity; +import com.massivecraft.mcore4.xlib.gson.annotations.SerializedName; public class Aspect extends Entity { @@ -40,6 +41,7 @@ public class Aspect extends Entity // STORED FIELDS // -------------------------------------------- // + @SerializedName("mid") protected String multiverseId; public String multiverseId() { return this.multiverseId; } public void multiverseId(String val) { this.multiverseId = val; } diff --git a/src/com/massivecraft/mcore4/usys/Multiverse.java b/src/com/massivecraft/mcore4/usys/Multiverse.java index c8d888ee..cbddac6a 100644 --- a/src/com/massivecraft/mcore4/usys/Multiverse.java +++ b/src/com/massivecraft/mcore4/usys/Multiverse.java @@ -2,6 +2,7 @@ package com.massivecraft.mcore4.usys; import java.util.HashMap; import java.util.HashSet; +import java.util.LinkedHashSet; import java.util.List; import java.util.Map; import java.util.Map.Entry; @@ -66,11 +67,12 @@ public class Multiverse extends Entity public boolean containsUniverse(String worldName) { - return worldName.equals(Multiverse.DEFAULT) || this.uw.containsKey(worldName); + return this.getUniverses().contains(worldName); } public Set newUniverse(String universe) { + if (universe.equals(Multiverse.DEFAULT)) return null; Set ret = this.uw.get(universe); if (ret == null) { @@ -87,7 +89,10 @@ public class Multiverse extends Entity public Set getUniverses() { - return this.uw.keySet(); + Set ret = new LinkedHashSet(); + ret.addAll(this.uw.keySet()); + ret.add(Multiverse.DEFAULT); + return ret; } public String getUniverseForWorldName(String worldName) diff --git a/src/com/massivecraft/mcore4/usys/cmd/CmdUsysMultiverseList.java b/src/com/massivecraft/mcore4/usys/cmd/CmdUsysMultiverseList.java index ca70d22d..f96edcf6 100644 --- a/src/com/massivecraft/mcore4/usys/cmd/CmdUsysMultiverseList.java +++ b/src/com/massivecraft/mcore4/usys/cmd/CmdUsysMultiverseList.java @@ -31,14 +31,7 @@ public class CmdUsysMultiverseList extends UsysCommand for (Multiverse multiverse : MultiverseColl.i.getAll()) { - if (multiverse.getUniverses().size() > 0) - { - lines.add(""+multiverse.getId()+" has "+Txt.implodeCommaAndDot(multiverse.getUniverses(), "%s", ", ", " and ", ".")); - } - else - { - lines.add(""+multiverse.getId()+" has no universes."); - } + lines.add(""+multiverse.getId()+" has "+Txt.implodeCommaAndDot(multiverse.getUniverses(), "%s", ", ", " and ", ".")); } // Send them diff --git a/src/com/massivecraft/mcore4/usys/cmd/CmdUsysMultiverseShow.java b/src/com/massivecraft/mcore4/usys/cmd/CmdUsysMultiverseShow.java index ce9f8f95..e4b6b4b8 100644 --- a/src/com/massivecraft/mcore4/usys/cmd/CmdUsysMultiverseShow.java +++ b/src/com/massivecraft/mcore4/usys/cmd/CmdUsysMultiverseShow.java @@ -30,9 +30,9 @@ public class CmdUsysMultiverseShow extends UsysCommand for (String universe : multiverse.getUniverses()) { + if (universe.equals(Multiverse.DEFAULT)) continue; msg(""+universe+": "+Txt.implodeCommaAndDot(multiverse.getWorlds(universe), "%s", ", ", " and ", ".")); } - msg("default: for all other worlds."); msg(""); diff --git a/src/com/massivecraft/mcore4/usys/cmd/CmdUsysUniverseNew.java b/src/com/massivecraft/mcore4/usys/cmd/CmdUsysUniverseNew.java index ec83e3ea..c3797592 100644 --- a/src/com/massivecraft/mcore4/usys/cmd/CmdUsysUniverseNew.java +++ b/src/com/massivecraft/mcore4/usys/cmd/CmdUsysUniverseNew.java @@ -24,7 +24,7 @@ public class CmdUsysUniverseNew extends UsysCommand String universe = this.arg(0); - if (multiverse.containsUniverse(universe) || universe.equals(Multiverse.DEFAULT)) + if (multiverse.containsUniverse(universe)) { msg("The universe %s already exists in multiverse %s.", universe, multiverse.getId()); return; diff --git a/src/com/massivecraft/mcore4/util/MUtil.java b/src/com/massivecraft/mcore4/util/MUtil.java index 01ed220b..eff0b443 100644 --- a/src/com/massivecraft/mcore4/util/MUtil.java +++ b/src/com/massivecraft/mcore4/util/MUtil.java @@ -2,6 +2,7 @@ package com.massivecraft.mcore4.util; import java.util.ArrayList; import java.util.Arrays; +import java.util.Collection; import java.util.Comparator; import java.util.HashMap; import java.util.HashSet; @@ -148,6 +149,18 @@ public class MUtil return ret; } + // -------------------------------------------- // + // LE NICE RANDOM + // -------------------------------------------- // + + public static T random(Collection coll) + { + if (coll.size() == 0) return null; + if (coll.size() == 1) return coll.iterator().next(); + int index = MCore.random.nextInt(coll.size()); + return new ArrayList(coll).get(index); + } + // -------------------------------------------- // // SORTING // -------------------------------------------- // diff --git a/src/com/massivecraft/mcore4/util/Txt.java b/src/com/massivecraft/mcore4/util/Txt.java index edd13c54..cb0e8e9f 100644 --- a/src/com/massivecraft/mcore4/util/Txt.java +++ b/src/com/massivecraft/mcore4/util/Txt.java @@ -98,6 +98,7 @@ public class Txt parseReplacements.put("

", "\u00A73"); parseReplacements.put("", "\u00A73"); parseReplacements.put("&&", "&"); + parseReplacements.put("§§", "§"); // Color by number/char for (int i = 48; i <= 122; i++)