From 4e6a839d792e8605d6b442bb582c2e5168d88594 Mon Sep 17 00:00:00 2001 From: Olof Larsson Date: Tue, 2 Oct 2012 21:54:20 +0200 Subject: [PATCH] wow. getting close to stable next major version \:D/ --- .../massivecraft/mcore4/InternalListener.java | 5 +- src/com/massivecraft/mcore4/MCore.java | 2 - src/com/massivecraft/mcore4/MPlugin.java | 22 +- src/com/massivecraft/mcore4/cmd/Cmd.java | 83 --- src/com/massivecraft/mcore4/cmd/MCommand.java | 11 +- .../mcore4/cmd/arg/old/AHAspect.java | 31 -- .../mcore4/cmd/arg/old/AHBase.java | 22 - .../mcore4/cmd/arg/old/AHBoolean.java | 22 - .../mcore4/cmd/arg/old/AHByte.java | 17 - .../mcore4/cmd/arg/old/AHDate.java | 22 - .../mcore4/cmd/arg/old/AHDouble.java | 17 - .../mcore4/cmd/arg/old/AHEnvironment.java | 46 -- .../mcore4/cmd/arg/old/AHFloat.java | 17 - .../mcore4/cmd/arg/old/AHInteger.java | 17 - .../mcore4/cmd/arg/old/AHMaterial.java | 24 - .../mcore4/cmd/arg/old/AHMultiverse.java | 32 -- .../mcore4/cmd/arg/old/AHPlayer.java | 39 -- .../mcore4/cmd/arg/old/AHPlayerWrapper.java | 58 -- .../mcore4/cmd/arg/old/AHPrimitive.java | 29 - .../mcore4/cmd/arg/old/AHWorld.java | 26 - .../mcore4/cmd/arg/old/AHWorldType.java | 57 -- .../mcore4/cmd/arg/old/IArgHandler.java | 16 - .../massivecraft/mcore4/persist/Entity.java | 45 -- .../mcore4/persist/IClassManager.java | 82 --- src/com/massivecraft/mcore4/persist/One.java | 156 ------ .../massivecraft/mcore4/persist/Persist.java | 177 ------- .../mcore4/persist/PlayerEntity.java | 79 --- .../massivecraft/mcore4/persist/SaveTask.java | 35 -- .../mcore4/persist/gson/GsonClassManager.java | 496 ------------------ .../persist/gson/GsonPlayerEntityManager.java | 67 --- .../mcore4/persist/gson/JsonFileFilter.java | 26 - .../mcore4/util/BukkitCommandUtil.java | 33 ++ .../massivecraft/mcore4/util/LibLoader.java | 16 +- src/com/massivecraft/mcore4/util/MUtil.java | 32 +- src/com/massivecraft/mcore4/util/Txt.java | 2 +- 35 files changed, 78 insertions(+), 1783 deletions(-) delete mode 100644 src/com/massivecraft/mcore4/cmd/Cmd.java delete mode 100644 src/com/massivecraft/mcore4/cmd/arg/old/AHAspect.java delete mode 100644 src/com/massivecraft/mcore4/cmd/arg/old/AHBase.java delete mode 100644 src/com/massivecraft/mcore4/cmd/arg/old/AHBoolean.java delete mode 100644 src/com/massivecraft/mcore4/cmd/arg/old/AHByte.java delete mode 100644 src/com/massivecraft/mcore4/cmd/arg/old/AHDate.java delete mode 100644 src/com/massivecraft/mcore4/cmd/arg/old/AHDouble.java delete mode 100644 src/com/massivecraft/mcore4/cmd/arg/old/AHEnvironment.java delete mode 100644 src/com/massivecraft/mcore4/cmd/arg/old/AHFloat.java delete mode 100644 src/com/massivecraft/mcore4/cmd/arg/old/AHInteger.java delete mode 100644 src/com/massivecraft/mcore4/cmd/arg/old/AHMaterial.java delete mode 100644 src/com/massivecraft/mcore4/cmd/arg/old/AHMultiverse.java delete mode 100644 src/com/massivecraft/mcore4/cmd/arg/old/AHPlayer.java delete mode 100644 src/com/massivecraft/mcore4/cmd/arg/old/AHPlayerWrapper.java delete mode 100644 src/com/massivecraft/mcore4/cmd/arg/old/AHPrimitive.java delete mode 100644 src/com/massivecraft/mcore4/cmd/arg/old/AHWorld.java delete mode 100644 src/com/massivecraft/mcore4/cmd/arg/old/AHWorldType.java delete mode 100644 src/com/massivecraft/mcore4/cmd/arg/old/IArgHandler.java delete mode 100644 src/com/massivecraft/mcore4/persist/Entity.java delete mode 100644 src/com/massivecraft/mcore4/persist/IClassManager.java delete mode 100644 src/com/massivecraft/mcore4/persist/One.java delete mode 100644 src/com/massivecraft/mcore4/persist/Persist.java delete mode 100644 src/com/massivecraft/mcore4/persist/PlayerEntity.java delete mode 100644 src/com/massivecraft/mcore4/persist/SaveTask.java delete mode 100644 src/com/massivecraft/mcore4/persist/gson/GsonClassManager.java delete mode 100644 src/com/massivecraft/mcore4/persist/gson/GsonPlayerEntityManager.java delete mode 100644 src/com/massivecraft/mcore4/persist/gson/JsonFileFilter.java create mode 100644 src/com/massivecraft/mcore4/util/BukkitCommandUtil.java diff --git a/src/com/massivecraft/mcore4/InternalListener.java b/src/com/massivecraft/mcore4/InternalListener.java index 6af137da..47d0d14b 100644 --- a/src/com/massivecraft/mcore4/InternalListener.java +++ b/src/com/massivecraft/mcore4/InternalListener.java @@ -17,8 +17,6 @@ import org.bukkit.event.player.PlayerTeleportEvent; import com.massivecraft.mcore4.event.MCoreAfterPlayerRespawnEvent; import com.massivecraft.mcore4.event.MCoreAfterPlayerTeleportEvent; import com.massivecraft.mcore4.event.MCorePlayerLeaveEvent; -import com.massivecraft.mcore4.persist.IClassManager; -import com.massivecraft.mcore4.persist.Persist; import com.massivecraft.mcore4.store.Coll; import com.massivecraft.mcore4.store.PlayerColl; @@ -33,6 +31,7 @@ public class InternalListener implements Listener Bukkit.getServer().getPluginManager().registerEvents(this, this.p); } + /* @EventHandler(priority = EventPriority.LOW) public void onPlayerLogin(PlayerLoginEvent event) { @@ -47,7 +46,7 @@ public class InternalListener implements Listener manager.create(id); } } - } + }*/ // -------------------------------------------- // // AFTER EVENTS diff --git a/src/com/massivecraft/mcore4/MCore.java b/src/com/massivecraft/mcore4/MCore.java index 1ad61558..aa0fbf2d 100644 --- a/src/com/massivecraft/mcore4/MCore.java +++ b/src/com/massivecraft/mcore4/MCore.java @@ -11,7 +11,6 @@ import com.massivecraft.mcore4.adapter.InventoryAdapter; import com.massivecraft.mcore4.adapter.ItemStackAdapter; import com.massivecraft.mcore4.adapter.MongoURIAdapter; import com.massivecraft.mcore4.adapter.PSAdapter; -import com.massivecraft.mcore4.persist.Persist; import com.massivecraft.mcore4.store.Coll; import com.massivecraft.mcore4.store.Db; import com.massivecraft.mcore4.store.MStore; @@ -88,7 +87,6 @@ public class MCore extends MPlugin { // This is safe since all plugins using Persist should bukkit-depend this plugin. // Note this one must be before preEnable. dooh. - Persist.instances.clear(); Coll.instances.clear(); if ( ! preEnable()) return; diff --git a/src/com/massivecraft/mcore4/MPlugin.java b/src/com/massivecraft/mcore4/MPlugin.java index 5484d8ca..4e17f5a5 100644 --- a/src/com/massivecraft/mcore4/MPlugin.java +++ b/src/com/massivecraft/mcore4/MPlugin.java @@ -9,25 +9,15 @@ import org.bukkit.command.ConsoleCommandSender; import org.bukkit.event.Listener; import org.bukkit.plugin.java.JavaPlugin; -import com.massivecraft.mcore4.cmd.Cmd; import com.massivecraft.mcore4.integration.Integration; import com.massivecraft.mcore4.integration.IntegrationFeatures; -import com.massivecraft.mcore4.persist.One; -import com.massivecraft.mcore4.persist.Persist; import com.massivecraft.mcore4.store.Coll; -import com.massivecraft.mcore4.util.LibLoader; import com.massivecraft.mcore4.util.Txt; import com.massivecraft.mcore4.xlib.gson.Gson; import com.massivecraft.mcore4.xlib.gson.GsonBuilder; public abstract class MPlugin extends JavaPlugin implements Listener { - // Tools - public Cmd cmd; - public Persist persist; - public One one; - public LibLoader lib; - // Gson public Gson gson; @@ -48,12 +38,6 @@ public abstract class MPlugin extends JavaPlugin implements Listener // Create Gson this.gson = this.getGsonBuilder().create(); - // Create tools - this.cmd = new Cmd(); // TODO: Stop creating this asap :) - this.persist = new Persist(); // TODO: Stop creating this asap :) - this.one = new One(this); // TODO: Stop creating this asap :) - this.lib = new LibLoader(this); // TODO: Stop creating this asap :) - return true; } @@ -68,11 +52,7 @@ public abstract class MPlugin extends JavaPlugin implements Listener public void onDisable() { - // Collection shutdowns for old system. - this.persist.saveAll(); - Persist.instances.remove(this.persist); - - // Collection shutdowns for new system. + // Collection shutdowns. for (Coll coll : Coll.instances) { if (coll.getMplugin() != this) continue; diff --git a/src/com/massivecraft/mcore4/cmd/Cmd.java b/src/com/massivecraft/mcore4/cmd/Cmd.java deleted file mode 100644 index 8f77389c..00000000 --- a/src/com/massivecraft/mcore4/cmd/Cmd.java +++ /dev/null @@ -1,83 +0,0 @@ -package com.massivecraft.mcore4.cmd; - -import java.lang.reflect.Field; -import java.util.Date; -import java.util.HashMap; -import java.util.Map; - -import org.bukkit.Bukkit; -import org.bukkit.Material; -import org.bukkit.World; -import org.bukkit.World.Environment; -import org.bukkit.WorldType; -import org.bukkit.command.Command; -import org.bukkit.command.SimpleCommandMap; -import org.bukkit.craftbukkit.CraftServer; -import org.bukkit.entity.Player; - -import com.massivecraft.mcore4.cmd.arg.old.AHAspect; -import com.massivecraft.mcore4.cmd.arg.old.AHBoolean; -import com.massivecraft.mcore4.cmd.arg.old.AHByte; -import com.massivecraft.mcore4.cmd.arg.old.AHDate; -import com.massivecraft.mcore4.cmd.arg.old.AHDouble; -import com.massivecraft.mcore4.cmd.arg.old.AHEnvironment; -import com.massivecraft.mcore4.cmd.arg.old.AHFloat; -import com.massivecraft.mcore4.cmd.arg.old.AHInteger; -import com.massivecraft.mcore4.cmd.arg.old.AHMaterial; -import com.massivecraft.mcore4.cmd.arg.old.AHMultiverse; -import com.massivecraft.mcore4.cmd.arg.old.AHPlayer; -import com.massivecraft.mcore4.cmd.arg.old.AHWorld; -import com.massivecraft.mcore4.cmd.arg.old.AHWorldType; -import com.massivecraft.mcore4.cmd.arg.old.IArgHandler; -import com.massivecraft.mcore4.usys.Aspect; -import com.massivecraft.mcore4.usys.Multiverse; - -public class Cmd -{ - // TODO: The cmd will no longer need to be instantiated. - // TODO: Perhaps I should remove the cmd system soon. - - protected Map, IArgHandler> argHandlers = new HashMap, IArgHandler>(); - public Map, IArgHandler> getArgHandlers() { return this.argHandlers; } - @SuppressWarnings("unchecked") - public IArgHandler getArgHandler(Class clazz) { return (IArgHandler) this.argHandlers.get(clazz); } - public void setArgHandler(Class clazz, IArgHandler handler) { this.argHandlers.put(clazz, handler); } - - public Cmd() - { - this.setArgHandler(Aspect.class, new AHAspect()); - this.setArgHandler(Boolean.class, new AHBoolean()); - this.setArgHandler(Byte.class, new AHByte()); - this.setArgHandler(Date.class, new AHDate()); - this.setArgHandler(Double.class, new AHDouble()); - this.setArgHandler(Environment.class, new AHEnvironment()); - this.setArgHandler(Float.class, new AHFloat()); - this.setArgHandler(Integer.class, new AHInteger()); - this.setArgHandler(Material.class, new AHMaterial()); - this.setArgHandler(Multiverse.class, new AHMultiverse()); - this.setArgHandler(Player.class, new AHPlayer()); - this.setArgHandler(World.class, new AHWorld()); - this.setArgHandler(WorldType.class, new AHWorldType()); - } - - public static SimpleCommandMap getBukkitCommandMap() - { - CraftServer craftServer = (CraftServer)Bukkit.getServer(); - return craftServer.getCommandMap(); - } - - @SuppressWarnings("unchecked") - public static Map getKnownCommandsFromSimpleCommandMap(SimpleCommandMap scm) - { - try - { - Field field = SimpleCommandMap.class.getDeclaredField("knownCommands"); - field.setAccessible(true); - return (Map) field.get(scm); - } - catch (Exception e) - { - return null; - } - } -} diff --git a/src/com/massivecraft/mcore4/cmd/MCommand.java b/src/com/massivecraft/mcore4/cmd/MCommand.java index 8f1446e1..70bd0914 100644 --- a/src/com/massivecraft/mcore4/cmd/MCommand.java +++ b/src/com/massivecraft/mcore4/cmd/MCommand.java @@ -2,7 +2,6 @@ package com.massivecraft.mcore4.cmd; import java.util.*; import java.util.Map.Entry; -import java.util.logging.Level; import lombok.Getter; import lombok.Setter; @@ -16,9 +15,9 @@ import com.massivecraft.mcore4.Lang; import com.massivecraft.mcore4.MPlugin; import com.massivecraft.mcore4.cmd.arg.ArgReader; import com.massivecraft.mcore4.cmd.arg.ArgResult; -import com.massivecraft.mcore4.cmd.arg.old.IArgHandler; import com.massivecraft.mcore4.cmd.req.IReq; import com.massivecraft.mcore4.cmd.req.ReqHasPerm; +import com.massivecraft.mcore4.util.BukkitCommandUtil; import com.massivecraft.mcore4.util.Perm; import com.massivecraft.mcore4.util.Txt; @@ -142,12 +141,12 @@ public abstract class MCommand public boolean register(boolean override) { BukkitGlueCommand bgc = new BukkitGlueCommand(this); - SimpleCommandMap scm = Cmd.getBukkitCommandMap(); + SimpleCommandMap scm = BukkitCommandUtil.getBukkitCommandMap(); if (override) { // Our commands are more important than your commands :P - Map knownCommands = Cmd.getKnownCommandsFromSimpleCommandMap(scm); + Map knownCommands = BukkitCommandUtil.getKnownCommandsFromSimpleCommandMap(scm); String lowerLabel = bgc.getName().trim().toLowerCase(); knownCommands.remove(lowerLabel); } @@ -464,7 +463,7 @@ public abstract class MCommand // Argument Readers DEPRACATED TODO // -------------------------------------------- // - @Deprecated + /*@Deprecated public synchronized T argAs(int idx, Class clazz, String style, T defaultNotSet, T defaultNotFound) { if ( ! this.argIsSet(idx)) @@ -516,6 +515,6 @@ public abstract class MCommand { return this.argAs(idx, clazz, (T)null, null); } - + */ } diff --git a/src/com/massivecraft/mcore4/cmd/arg/old/AHAspect.java b/src/com/massivecraft/mcore4/cmd/arg/old/AHAspect.java deleted file mode 100644 index b6a50f7c..00000000 --- a/src/com/massivecraft/mcore4/cmd/arg/old/AHAspect.java +++ /dev/null @@ -1,31 +0,0 @@ -package com.massivecraft.mcore4.cmd.arg.old; - -import org.bukkit.command.CommandSender; - -import com.massivecraft.mcore4.MPlugin; -import com.massivecraft.mcore4.Permission; -import com.massivecraft.mcore4.usys.Aspect; -import com.massivecraft.mcore4.usys.AspectColl; -import com.massivecraft.mcore4.util.Txt; - -public class AHAspect extends AHBase -{ - @Override - public Aspect parse(String str, String style, CommandSender sender, MPlugin p) - { - this.error.clear(); - - Aspect ret = AspectColl.i.get(str); - - if (ret == null) - { - this.error.add("No aspect called \"

"+str+"\"."); - if (Permission.USYS_ASPECT_LIST.has(sender, false)) - { - this.error.add("Use "+Txt.implodeCommaAndDot(AspectColl.i.getIds(), "%s", ", ", " or ", ".")); - } - } - - return ret; - } -} diff --git a/src/com/massivecraft/mcore4/cmd/arg/old/AHBase.java b/src/com/massivecraft/mcore4/cmd/arg/old/AHBase.java deleted file mode 100644 index 95e5d3c8..00000000 --- a/src/com/massivecraft/mcore4/cmd/arg/old/AHBase.java +++ /dev/null @@ -1,22 +0,0 @@ -package com.massivecraft.mcore4.cmd.arg.old; - -import java.util.ArrayList; -import java.util.Collection; - -import org.bukkit.command.CommandSender; - -import com.massivecraft.mcore4.MPlugin; - -public abstract class AHBase implements IArgHandler -{ - public Collection error = new ArrayList(); - - @Override - public abstract T parse(String str, String style, CommandSender sender, MPlugin p); - - @Override - public Collection getErrors() - { - return this.error; - } -} diff --git a/src/com/massivecraft/mcore4/cmd/arg/old/AHBoolean.java b/src/com/massivecraft/mcore4/cmd/arg/old/AHBoolean.java deleted file mode 100644 index 50ccd9e0..00000000 --- a/src/com/massivecraft/mcore4/cmd/arg/old/AHBoolean.java +++ /dev/null @@ -1,22 +0,0 @@ -package com.massivecraft.mcore4.cmd.arg.old; - - -public class AHBoolean extends AHPrimitive -{ - @Override - protected String getPrimitiveName() - { - return "boolean"; - } - - @Override - protected Boolean unsafeConvert(String str) throws Exception - { - str = str.toLowerCase(); - if (str.startsWith("y") || str.startsWith("t") || str.startsWith("on") || str.startsWith("+") || str.startsWith("1")) - { - return true; - } - return false; - } -} diff --git a/src/com/massivecraft/mcore4/cmd/arg/old/AHByte.java b/src/com/massivecraft/mcore4/cmd/arg/old/AHByte.java deleted file mode 100644 index bf557352..00000000 --- a/src/com/massivecraft/mcore4/cmd/arg/old/AHByte.java +++ /dev/null @@ -1,17 +0,0 @@ -package com.massivecraft.mcore4.cmd.arg.old; - - -public class AHByte extends AHPrimitive -{ - @Override - protected String getPrimitiveName() - { - return "byte"; - } - - @Override - protected Byte unsafeConvert(String str) throws Exception - { - return Byte.parseByte(str); - } -} diff --git a/src/com/massivecraft/mcore4/cmd/arg/old/AHDate.java b/src/com/massivecraft/mcore4/cmd/arg/old/AHDate.java deleted file mode 100644 index 912390b9..00000000 --- a/src/com/massivecraft/mcore4/cmd/arg/old/AHDate.java +++ /dev/null @@ -1,22 +0,0 @@ -package com.massivecraft.mcore4.cmd.arg.old; - -import java.text.DateFormat; -import java.text.SimpleDateFormat; -import java.util.Date; - - -public class AHDate extends AHPrimitive -{ - protected static DateFormat df = new SimpleDateFormat("yyyy-MM-dd"); - @Override - protected String getPrimitiveName() - { - return "YYYY-MM-DD date"; - } - - @Override - protected Date unsafeConvert(String str) throws Exception - { - return df.parse(str); - } -} \ No newline at end of file diff --git a/src/com/massivecraft/mcore4/cmd/arg/old/AHDouble.java b/src/com/massivecraft/mcore4/cmd/arg/old/AHDouble.java deleted file mode 100644 index b16c446f..00000000 --- a/src/com/massivecraft/mcore4/cmd/arg/old/AHDouble.java +++ /dev/null @@ -1,17 +0,0 @@ -package com.massivecraft.mcore4.cmd.arg.old; - - -public class AHDouble extends AHPrimitive -{ - @Override - protected String getPrimitiveName() - { - return "double"; - } - - @Override - protected Double unsafeConvert(String str) throws Exception - { - return Double.parseDouble(str); - } -} diff --git a/src/com/massivecraft/mcore4/cmd/arg/old/AHEnvironment.java b/src/com/massivecraft/mcore4/cmd/arg/old/AHEnvironment.java deleted file mode 100644 index 9444a183..00000000 --- a/src/com/massivecraft/mcore4/cmd/arg/old/AHEnvironment.java +++ /dev/null @@ -1,46 +0,0 @@ -package com.massivecraft.mcore4.cmd.arg.old; - -import org.bukkit.World.Environment; -import org.bukkit.command.CommandSender; - -import com.massivecraft.mcore4.MPlugin; - -public class AHEnvironment extends AHBase -{ - @Override - public Environment parse(String str, String style, CommandSender sender, MPlugin p) - { - this.error.clear(); - - Environment ret = null; - - // "THE_END" --> "end" - str = str.toLowerCase(); - str = str.replace("_", ""); - str = str.replace("the", ""); - - if (str.startsWith("no") || str.startsWith("d")) - { - // "normal" or "default" - ret = Environment.NORMAL; - } - else if (str.startsWith("ne")) - { - // "nether" - ret = Environment.NETHER; - } - else if (str.startsWith("e")) - { - // "end" - ret = Environment.THE_END; - } - - if (ret == null) - { - this.error.add("No environment matching \"

"+str+"\"."); - this.error.add("Use normal, end or nether."); - } - - return ret; - } -} diff --git a/src/com/massivecraft/mcore4/cmd/arg/old/AHFloat.java b/src/com/massivecraft/mcore4/cmd/arg/old/AHFloat.java deleted file mode 100644 index e38f3625..00000000 --- a/src/com/massivecraft/mcore4/cmd/arg/old/AHFloat.java +++ /dev/null @@ -1,17 +0,0 @@ -package com.massivecraft.mcore4.cmd.arg.old; - - -public class AHFloat extends AHPrimitive -{ - @Override - protected String getPrimitiveName() - { - return "integer"; - } - - @Override - protected Float unsafeConvert(String str) throws Exception - { - return Float.parseFloat(str); - } -} diff --git a/src/com/massivecraft/mcore4/cmd/arg/old/AHInteger.java b/src/com/massivecraft/mcore4/cmd/arg/old/AHInteger.java deleted file mode 100644 index 22784984..00000000 --- a/src/com/massivecraft/mcore4/cmd/arg/old/AHInteger.java +++ /dev/null @@ -1,17 +0,0 @@ -package com.massivecraft.mcore4.cmd.arg.old; - - -public class AHInteger extends AHPrimitive -{ - @Override - protected String getPrimitiveName() - { - return "integer"; - } - - @Override - protected Integer unsafeConvert(String str) throws Exception - { - return Integer.parseInt(str); - } -} \ No newline at end of file diff --git a/src/com/massivecraft/mcore4/cmd/arg/old/AHMaterial.java b/src/com/massivecraft/mcore4/cmd/arg/old/AHMaterial.java deleted file mode 100644 index 59c5406d..00000000 --- a/src/com/massivecraft/mcore4/cmd/arg/old/AHMaterial.java +++ /dev/null @@ -1,24 +0,0 @@ -package com.massivecraft.mcore4.cmd.arg.old; - -import org.bukkit.Material; -import org.bukkit.command.CommandSender; - -import com.massivecraft.mcore4.MPlugin; - -public class AHMaterial extends AHBase -{ - @Override - public Material parse(String str, String style, CommandSender sender, MPlugin p) - { - this.error.clear(); - Material ret = Material.matchMaterial(str); - - if (ret == null) - { - this.error.add("No material matching \"

"+str+"\"."); - this.error.add("Suggestion: http://www.minecraftwiki.net/wiki/Data_values"); - } - - return ret; - } -} diff --git a/src/com/massivecraft/mcore4/cmd/arg/old/AHMultiverse.java b/src/com/massivecraft/mcore4/cmd/arg/old/AHMultiverse.java deleted file mode 100644 index 63fc9d55..00000000 --- a/src/com/massivecraft/mcore4/cmd/arg/old/AHMultiverse.java +++ /dev/null @@ -1,32 +0,0 @@ -package com.massivecraft.mcore4.cmd.arg.old; - -import org.bukkit.command.CommandSender; - -import com.massivecraft.mcore4.MPlugin; -import com.massivecraft.mcore4.Permission; -import com.massivecraft.mcore4.usys.Multiverse; -import com.massivecraft.mcore4.usys.MultiverseColl; -import com.massivecraft.mcore4.util.Txt; - - -public class AHMultiverse extends AHBase -{ - @Override - public Multiverse parse(String str, String style, CommandSender sender, MPlugin p) - { - this.error.clear(); - - Multiverse ret = MultiverseColl.i.get(str); - - if (ret == null) - { - this.error.add("No multiverse called \"

"+str+"\"."); - if (Permission.USYS_MULTIVERSE_LIST.has(sender, false)) - { - this.error.add("Use "+Txt.implodeCommaAndDot(MultiverseColl.i.getIds(), "%s", ", ", " or ", ".")); - } - } - - return ret; - } -} diff --git a/src/com/massivecraft/mcore4/cmd/arg/old/AHPlayer.java b/src/com/massivecraft/mcore4/cmd/arg/old/AHPlayer.java deleted file mode 100644 index 188e7ebc..00000000 --- a/src/com/massivecraft/mcore4/cmd/arg/old/AHPlayer.java +++ /dev/null @@ -1,39 +0,0 @@ -package com.massivecraft.mcore4.cmd.arg.old; - -import java.util.List; - -import org.bukkit.Bukkit; -import org.bukkit.command.CommandSender; -import org.bukkit.entity.Player; - -import com.massivecraft.mcore4.MPlugin; - -public class AHPlayer extends AHBase -{ - @Override - public Player parse(String str, String style, CommandSender sender, MPlugin p) - { - this.error.clear(); - if (str == null) return null; - - if (style != null && style.equals("match")) - { - List players = Bukkit.getServer().matchPlayer(str); - if (players.size() > 0) - { - return players.get(0); - } - this.error.add("No online player's name begins with \"

"+str+"\"."); - } - else - { - Player player = Bukkit.getServer().getPlayerExact(str); - if (player != null) - { - return player; - } - this.error.add("No player online with the exact name \"

"+str+"\"."); - } - return null; - } -} diff --git a/src/com/massivecraft/mcore4/cmd/arg/old/AHPlayerWrapper.java b/src/com/massivecraft/mcore4/cmd/arg/old/AHPlayerWrapper.java deleted file mode 100644 index 4ab00c53..00000000 --- a/src/com/massivecraft/mcore4/cmd/arg/old/AHPlayerWrapper.java +++ /dev/null @@ -1,58 +0,0 @@ -package com.massivecraft.mcore4.cmd.arg.old; - -import org.bukkit.command.CommandSender; - -import com.massivecraft.mcore4.MPlugin; -import com.massivecraft.mcore4.persist.IClassManager; -import com.massivecraft.mcore4.util.PlayerUtil; -import com.massivecraft.mcore4.util.Txt; - -public abstract class AHPlayerWrapper extends AHBase -{ - public abstract Class getClazz(); - - public IClassManager getManager(MPlugin p) - { - return p.persist.getManager(getClazz()); - } - - @Override - public T parse(String str, String style, CommandSender sender, MPlugin p) - { - this.error.clear(); - if (str == null) return null; - - IClassManager manager = this.getManager(p); - T ret; - - if (style != null && style.equalsIgnoreCase("match")) - { - ret = manager.getBestMatch(str); - if (ret != null) - { - return ret; - } - this.error.add("No player name begins with \"

"+str+"\"."); - } - else if (style != null && style.equalsIgnoreCase("matchany")) - { - ret = manager.get(Txt.getBestCIStart(PlayerUtil.getAllVisitorNames(), str)); - if (ret != null) - { - return ret; - } - this.error.add("No player name begins with \"

"+str+"\"."); - } - else - { - ret = manager.get(str); - if (ret != null) - { - return ret; - - } - this.error.add("No player with the exact name \"

"+str+"\"."); - } - return null; - } -} diff --git a/src/com/massivecraft/mcore4/cmd/arg/old/AHPrimitive.java b/src/com/massivecraft/mcore4/cmd/arg/old/AHPrimitive.java deleted file mode 100644 index f486ecad..00000000 --- a/src/com/massivecraft/mcore4/cmd/arg/old/AHPrimitive.java +++ /dev/null @@ -1,29 +0,0 @@ -package com.massivecraft.mcore4.cmd.arg.old; - -import org.bukkit.command.CommandSender; - -import com.massivecraft.mcore4.MPlugin; - -public abstract class AHPrimitive extends AHBase -{ - protected abstract String getPrimitiveName(); - - protected abstract T unsafeConvert(String str) throws Exception; - - @Override - public T parse(String str, String style, CommandSender sender, MPlugin p) - { - this.error.clear(); - if (str == null) return null; - try - { - T ret = this.unsafeConvert(str); - return ret; - } - catch (Exception e) - { - this.error.add("\"

"+str+"\" is not a valid "+this.getPrimitiveName()+"."); - } - return null; - } -} diff --git a/src/com/massivecraft/mcore4/cmd/arg/old/AHWorld.java b/src/com/massivecraft/mcore4/cmd/arg/old/AHWorld.java deleted file mode 100644 index ad115565..00000000 --- a/src/com/massivecraft/mcore4/cmd/arg/old/AHWorld.java +++ /dev/null @@ -1,26 +0,0 @@ -package com.massivecraft.mcore4.cmd.arg.old; - -import org.bukkit.Bukkit; -import org.bukkit.World; -import org.bukkit.command.CommandSender; - -import com.massivecraft.mcore4.MPlugin; - -public class AHWorld extends AHBase -{ - @Override - public World parse(String str, String style, CommandSender sender, MPlugin p) - { - this.error.clear(); - if (str == null) return null; - - World ret = Bukkit.getWorld(str); - - if (ret == null) - { - this.error.add("No world matching \"

"+str+"\"."); - } - - return ret; - } -} diff --git a/src/com/massivecraft/mcore4/cmd/arg/old/AHWorldType.java b/src/com/massivecraft/mcore4/cmd/arg/old/AHWorldType.java deleted file mode 100644 index 4e87f9a0..00000000 --- a/src/com/massivecraft/mcore4/cmd/arg/old/AHWorldType.java +++ /dev/null @@ -1,57 +0,0 @@ -package com.massivecraft.mcore4.cmd.arg.old; - -import org.bukkit.WorldType; -import org.bukkit.command.CommandSender; - -import com.massivecraft.mcore4.MPlugin; - - -public class AHWorldType extends AHBase -{ - @Override - public WorldType parse(String str, String style, CommandSender sender, MPlugin p) - { - this.error.clear(); - - WorldType ret = null; - - // "DEFAULT_1_1" --> "11" - // "LARGE_BIOMES" --> "large" - // "Default" --> "" - str = str.toLowerCase(); - str = str.replace("_", ""); - str = str.replace(".", ""); - str = str.replace("normal", ""); - str = str.replace("default", ""); - str = str.replace("large", ""); - - if (str.equals("")) - { - // "normal" or "default" - ret = WorldType.NORMAL; - } - else if (str.startsWith("flat")) - { - // "flat" - ret = WorldType.FLAT; - } - else if (str.contains("11")) - { - // "VERSION_1_1" - ret = WorldType.VERSION_1_1; - } - else if (str.contains("large")) - { - // "LARGE_BIOMES" - ret = WorldType.LARGE_BIOMES; - } - - if (ret == null) - { - this.error.add("No world type matching \"

"+str+"\"."); - this.error.add("Use normal, flat, 1.1 or largebiomes."); - } - - return ret; - } -} diff --git a/src/com/massivecraft/mcore4/cmd/arg/old/IArgHandler.java b/src/com/massivecraft/mcore4/cmd/arg/old/IArgHandler.java deleted file mode 100644 index 2c2cd3ba..00000000 --- a/src/com/massivecraft/mcore4/cmd/arg/old/IArgHandler.java +++ /dev/null @@ -1,16 +0,0 @@ -package com.massivecraft.mcore4.cmd.arg.old; - -import java.util.Collection; - -import org.bukkit.command.CommandSender; - -import com.massivecraft.mcore4.MPlugin; - -public interface IArgHandler -{ - // Parse result returned - or null and something in the error message. - public T parse(String str, String style, CommandSender sender, MPlugin p); - - // Error here - or null. - public Collection getErrors(); -} diff --git a/src/com/massivecraft/mcore4/persist/Entity.java b/src/com/massivecraft/mcore4/persist/Entity.java deleted file mode 100644 index e5b55684..00000000 --- a/src/com/massivecraft/mcore4/persist/Entity.java +++ /dev/null @@ -1,45 +0,0 @@ -package com.massivecraft.mcore4.persist; - -/** - * Usage of this class is highly optional. You may persist anything. If you are - * creating the class to be persisted yourself, it might be handy to extend this - * Entity class. It just contains a set of shortcut methods. - */ - -// Self referencing generic using the "getThis trick". -// http://www.angelikalanger.com/GenericsFAQ/FAQSections/ProgrammingIdioms.html#FAQ206 -public abstract class Entity> -{ - public abstract IClassManager getManager(); - protected abstract T getThis(); - - public String attach() - { - return this.getManager().attach(getThis()); - } - - public void detach() - { - this.getManager().detachEntity(getThis()); - } - - public boolean attached() - { - return this.getManager().containsEntity(getThis()); - } - - public boolean detached() - { - return ! this.attached(); - } - - public boolean save() - { - return this.getManager().saveEntity(getThis()); - } - - public String getId() - { - return this.getManager().id(getThis()); - } -} diff --git a/src/com/massivecraft/mcore4/persist/IClassManager.java b/src/com/massivecraft/mcore4/persist/IClassManager.java deleted file mode 100644 index 3ad5f744..00000000 --- a/src/com/massivecraft/mcore4/persist/IClassManager.java +++ /dev/null @@ -1,82 +0,0 @@ -package com.massivecraft.mcore4.persist; - -import java.util.Collection; -import java.util.Comparator; -import java.util.Map; - -import com.massivecraft.mcore4.Predictate; - -public interface IClassManager -{ - // What do we handle? - public Class getManagedClass(); - - // This simply creates and returns a new instance - // It does not detach/attach or anything. Just creates a new instance. - public T createNewInstance(); - - // Creativeness - public boolean getIsCreative(); - public void setIsCreative(boolean val); - - // Create new instance with auto increment id - public T create(); - // Create new instance with the requested id - public T create(Object oid); - - // Add & Remove - public String attach(T entity); - public String attach(T entity, Object oid); - public void detachEntity(T entity); - public void detachId(Object oid); - public boolean containsEntity(T entity); - public boolean containsId(Object oid); - - // Disc io triggers - public boolean saveEntity(T entity); - public boolean saveId(Object oid); - public boolean saveAll(); - public boolean loaded(Object oid); - public T load(Object oid); - public boolean loadAll(); - - // Should that instance be saved or not? - // If it is default it should not be saved. - public boolean shouldBeSaved(T entity); - - // Id handling - // Get the id for this entity. Return null if it does not have an ID - public String id(T entity); - - // In some cases certain non string objects can represent a String id. - // This method is used as a wrapper for that. - // TODO: Javadoc: Should start with a null check as well as a String check. - public String idFix(Object oid); - public boolean idCanFix(Class clazz); - - // autoIncrement ids - public String idCurrent(); - public String idNext(boolean advance); - public boolean idUpdateCurrentFor(Object oid); - - // Get the entity for the id. If creative it is created if it does not exist else null. - public T get(Object oid, boolean creative); - public T get(Object oid); - - // Get the entity that best matches the id. - public T getBestMatch(Object oid); - - // Get all - public Collection getAllLoaded(); - public Collection getAllLoaded(Predictate where); - public Collection getAllLoaded(Predictate where, Comparator orderby); - public Collection getAllLoaded(Predictate where, Comparator orderby, Integer limit); - public Collection getAllLoaded(Predictate where, Comparator orderby, Integer limit, Integer offset); - public Collection getAll(); - public Collection getAll(Predictate where); - public Collection getAll(Predictate where, Comparator orderby); - public Collection getAll(Predictate where, Comparator orderby, Integer limit); - public Collection getAll(Predictate where, Comparator orderby, Integer limit, Integer offset); - public Collection getIds(); - public Map getMap(); -} diff --git a/src/com/massivecraft/mcore4/persist/One.java b/src/com/massivecraft/mcore4/persist/One.java deleted file mode 100644 index 3086c18b..00000000 --- a/src/com/massivecraft/mcore4/persist/One.java +++ /dev/null @@ -1,156 +0,0 @@ -package com.massivecraft.mcore4.persist; - -import java.io.File; -import java.lang.reflect.Type; -import java.util.logging.Level; - -import com.massivecraft.mcore4.MPlugin; -import com.massivecraft.mcore4.util.DiscUtil; - -// TODO: Give better name and place to differenciate from the entity-orm-ish system in "com.massivecraft.core.persist". - -public class One { - - private MPlugin p; - public One(MPlugin p) - { - this.p = p; - } - - // ------------------------------------------------------------ // - // GET NAME - What should we call this type of object? - // ------------------------------------------------------------ // - - public static String getName(Class clazz) - { - return clazz.getSimpleName().toLowerCase(); - } - - public static String getName(Object o) - { - return getName(o.getClass()); - } - - public static String getName(Type type) - { - return getName(type.getClass()); - } - - // ------------------------------------------------------------ // - // GET FILE - In which file would we like to store this object? - // ------------------------------------------------------------ // - - public File getFile(String name) - { - return new File(p.getDataFolder(), name+".json"); - } - - public File getFile(Class clazz) - { - return getFile(getName(clazz)); - } - - public File getFile(Object obj) - { - return getFile(getName(obj)); - } - - public File getFile(Type type) - { - return getFile(getName(type)); - } - - - // NICE WRAPPERS - - public T loadOrSaveDefault(T def, Class clazz) - { - return loadOrSaveDefault(def, clazz, getFile(clazz)); - } - - public T loadOrSaveDefault(T def, Class clazz, String name) - { - return loadOrSaveDefault(def, clazz, getFile(name)); - } - - public T loadOrSaveDefault(T def, Class clazz, File file) - { - if ( ! file.exists()) - { - p.log("Creating default: "+file); - this.save(def, file); - return def; - } - - T loaded = this.load(clazz, file); - - if (loaded == null) - { - p.log(Level.WARNING, "Using default as I failed to load: "+file); - return def; - } - - return loaded; - } - - // SAVE - - public boolean save(Object instance) - { - return save(instance, getFile(instance)); - } - - public boolean save(Object instance, String name) - { - return save(instance, getFile(name)); - } - - public boolean save(Object instance, File file) - { - return DiscUtil.writeCatch(file, p.gson.toJson(instance)); - } - - // LOAD BY CLASS - - public T load(Class clazz) - { - return load(clazz, getFile(clazz)); - } - - public T load(Class clazz, String name) - { - return load(clazz, getFile(name)); - } - - public T load(Class clazz, File file) - { - String content = DiscUtil.readCatch(file); - if (content == null) - { - return null; - } - - T instance = p.gson.fromJson(content, clazz); - - return instance; - } - - // LOAD BY TYPE - @SuppressWarnings("unchecked") - public T load(Type typeOfT, String name) - { - return (T) load(typeOfT, getFile(name)); - } - - @SuppressWarnings("unchecked") - public T load(Type typeOfT, File file) - { - String content = DiscUtil.readCatch(file); - if (content == null) { - return null; - } - - return (T) p.gson.fromJson(content, typeOfT); - } - -} diff --git a/src/com/massivecraft/mcore4/persist/Persist.java b/src/com/massivecraft/mcore4/persist/Persist.java deleted file mode 100644 index b1885680..00000000 --- a/src/com/massivecraft/mcore4/persist/Persist.java +++ /dev/null @@ -1,177 +0,0 @@ -package com.massivecraft.mcore4.persist; - -import java.util.ArrayList; -import java.util.Collection; -import java.util.Collections; -import java.util.Comparator; -import java.util.HashMap; -import java.util.List; -import java.util.Map; -import java.util.SortedSet; -import java.util.Timer; -import java.util.TreeSet; -import java.util.concurrent.CopyOnWriteArrayList; - -import com.massivecraft.mcore4.Predictate; - -public class Persist -{ - public static List instances = new CopyOnWriteArrayList(); - - private Map, IClassManager> classManagers = new HashMap, IClassManager>(); - public void setManager(Class clazz, IClassManager manager) - { - this.classManagers.put(clazz, manager); - }; - public Map, IClassManager> getClassManagers() - { - return this.classManagers; - } - - private Map, Timer> classSaveTimers = new HashMap, Timer>(); - public synchronized void setSaveInterval(Class clazz, long interval) - { - // Clear old timer - Timer timer = this.classSaveTimers.get(clazz); - if (timer != null) - { - timer.cancel(); - this.classSaveTimers.remove(clazz); - } - - // Create new timer - timer = new Timer(); - this.classSaveTimers.put(clazz, timer); - - // Add the task to the timer - SaveTask task = new SaveTask(this, clazz); - timer.scheduleAtFixedRate(task, interval, interval); - }; - - - @SuppressWarnings("unchecked") - public IClassManager getManager(Class clazz) - { - return (IClassManager) this.classManagers.get(clazz); - } - - @SuppressWarnings("unchecked") - public IClassManager getManager(T entity) - { - return (IClassManager) this.getManager(entity.getClass()); - } - - // -------------------------------------------- // - // CONSTRUCT - // -------------------------------------------- // - - public Persist() - { - instances.add(this); - } - - // -------------------------------------------- // - // SAVE ALL - // -------------------------------------------- // - - public void saveAll() - { - for (IClassManager m : this.classManagers.values()) - { - m.saveAll(); - } - } - - // -------------------------------------------- // - // UTILS - // -------------------------------------------- // - - public static ArrayList uglySQL(Collection items, Predictate where, Comparator orderby, Integer limit, Integer offset) - { - ArrayList ret = new ArrayList(items.size()); - - // WHERE - if (where == null) - { - ret.addAll(items); - } - else - { - for (T item : items) - { - if (where.apply(item)) - { - ret.add(item); - } - } - } - - // ORDERBY - if (orderby != null) - { - Collections.sort(ret, orderby); - } - - // LIMIT AND OFFSET - // Parse args - int fromIndex = 0; - if (offset != null) - { - fromIndex = offset; - } - - int toIndex = ret.size()-1; - if (limit != null) - { - toIndex = offset+limit; - } - - // Clean args - if (fromIndex <= 0) - { - fromIndex = 0; - } - else if (fromIndex > ret.size()-1) - { - fromIndex = ret.size()-1; - } - - if (toIndex < fromIndex) - { - toIndex = fromIndex; - } - else if (toIndex > ret.size()-1) - { - toIndex = ret.size()-1; - } - - // No limit? - if (fromIndex == 0 && toIndex == ret.size()-1) return ret; - return new ArrayList(ret.subList(fromIndex, toIndex)); - } - - // http://stackoverflow.com/questions/2864840/treemap-sort-by-value - public static > SortedSet> entriesSortedByValues(Map map, final boolean ascending) - { - SortedSet> sortedEntries = new TreeSet>( - new Comparator>() - { - @Override public int compare(Map.Entry e1, Map.Entry e2) - { - int res; - if (ascending) - { - res = e1.getValue().compareTo(e2.getValue()); - } - else - { - res = e2.getValue().compareTo(e1.getValue()); - } - return res != 0 ? res : 1; - } - } - ); - sortedEntries.addAll(map.entrySet()); - return sortedEntries; - } -} diff --git a/src/com/massivecraft/mcore4/persist/PlayerEntity.java b/src/com/massivecraft/mcore4/persist/PlayerEntity.java deleted file mode 100644 index d002837d..00000000 --- a/src/com/massivecraft/mcore4/persist/PlayerEntity.java +++ /dev/null @@ -1,79 +0,0 @@ -package com.massivecraft.mcore4.persist; - -import java.util.Collection; - -import org.bukkit.Bukkit; -import org.bukkit.GameMode; -import org.bukkit.entity.Player; - -import com.massivecraft.mcore4.util.Txt; - -public abstract class PlayerEntity> extends Entity -{ - public Player getPlayer() - { - // There is no case sensitivity. - return Bukkit.getPlayerExact(this.getId()); - } - - public boolean isOnline() - { - return this.getPlayer() != null; - } - - public boolean isOffline() - { - return ! isOnline(); - } - - // -------------------------------------------- // - // CHECKER UTILS - // -------------------------------------------- // - public boolean isGameMode(GameMode gm, boolean defaultIfOffline) - { - Player player = this.getPlayer(); - if (player == null || ! player.isOnline()) return defaultIfOffline; - return player.getGameMode() == gm; - } - - // -------------------------------------------- // - // Message Sending Helpers - // -------------------------------------------- // - - public void sendMessage(String msg) - { - Player player = this.getPlayer(); - if (player == null) return; - player.sendMessage(msg); - } - - public void sendMessage(Collection msgs) - { - Player player = this.getPlayer(); - if (player == null) return; - for(String msg : msgs) - { - player.sendMessage(msg); - } - } - - public void msg(String msg) - { - this.sendMessage(Txt.parse(msg)); - } - - public void msg(String msg, Object... args) - { - this.sendMessage(Txt.parse(msg, args)); - } - - public void msg(Collection msgs) - { - Player player = this.getPlayer(); - if (player == null) return; - for(String msg : msgs) - { - player.sendMessage(Txt.parse(msg)); - } - } -} diff --git a/src/com/massivecraft/mcore4/persist/SaveTask.java b/src/com/massivecraft/mcore4/persist/SaveTask.java deleted file mode 100644 index dad33517..00000000 --- a/src/com/massivecraft/mcore4/persist/SaveTask.java +++ /dev/null @@ -1,35 +0,0 @@ -package com.massivecraft.mcore4.persist; - -import java.util.TimerTask; - -public class SaveTask extends TimerTask -{ - private Persist persist; - - private Class clazz; - public Class getToBeSavedClass() { return clazz; } - - public SaveTask(Persist persist, Class clazz) - { - this.persist = persist; - this.clazz = clazz; - } - - public SaveTask(Persist persist) - { - this(persist, null); - } - - @Override - public void run() - { - if (this.clazz == null) - { - this.persist.saveAll(); - } - else - { - this.persist.getManager(this.clazz).saveAll(); - } - } -} diff --git a/src/com/massivecraft/mcore4/persist/gson/GsonClassManager.java b/src/com/massivecraft/mcore4/persist/gson/GsonClassManager.java deleted file mode 100644 index ace7e0d8..00000000 --- a/src/com/massivecraft/mcore4/persist/gson/GsonClassManager.java +++ /dev/null @@ -1,496 +0,0 @@ -package com.massivecraft.mcore4.persist.gson; - -import java.io.File; -import java.util.Collection; -import java.util.Comparator; -import java.util.Map; -import java.util.Map.Entry; -import java.util.Set; -import java.util.concurrent.ConcurrentHashMap; -import java.util.concurrent.ConcurrentSkipListMap; -import java.util.concurrent.ConcurrentSkipListSet; -import java.util.concurrent.CopyOnWriteArraySet; - -import com.massivecraft.mcore4.Predictate; -import com.massivecraft.mcore4.persist.IClassManager; -import com.massivecraft.mcore4.persist.Persist; -import com.massivecraft.mcore4.util.DiscUtil; -import com.massivecraft.mcore4.util.Txt; -import com.massivecraft.mcore4.xlib.gson.Gson; - -public abstract class GsonClassManager implements IClassManager -{ - // -------------------------------------------- // - // NON INTERFACE - // -------------------------------------------- // - protected final static String DOTJSON = ".json"; - protected Set ids; - protected Set entities; - protected Map id2entity; - protected Map entity2id; - protected long idCurrent = 1; - - protected Gson gson; - public Gson getGson() { return gson; } - public void setGson(Gson gson) { this.gson = gson; } - - protected File folder; - public File getFolder() { return folder; } - public void setFolder(File val) { this.folder = val; } - - protected boolean creative; - @Override - public boolean getIsCreative() { return this.creative; } - @Override - public void setIsCreative(boolean val) { this.creative = val; } - - protected boolean didLoadAll = false; - protected void loadIds() - { - if ( ! this.getFolder().exists()) return; - for(File file : this.getFolder().listFiles(JsonFileFilter.getInstance())) - { - this.ids.add(this.idFromFile(file)); - } - } - - protected String idFromFile(File file) - { - if (file == null) return null; - String name = file.getName(); - return name.substring(0, name.length()-5); - } - - protected File fileFromId(Object oid) - { - String id = this.idFix(oid); - if (id == null) return null; - return new File(this.getFolder(), id+DOTJSON); - } - - // -------------------------------------------- // - // CONSTRUCTORS - // -------------------------------------------- // - - public GsonClassManager(Gson gson, File folder, boolean creative, boolean lazy, Set ids, Set entities, Map id2entity, Map entity2id) - { - this.gson = gson; - this.folder = folder; - this.creative = creative; - this.ids = ids; - this.entities = entities; - this.id2entity = id2entity; - this.entity2id = entity2id; - - this.loadIds(); - if ( ! lazy) - { - this.loadAll(); - } - } - - public GsonClassManager(Gson gson, File folder, boolean creative, boolean lazy) - { - this( - gson, - folder, - creative, - lazy, - new ConcurrentSkipListSet(String.CASE_INSENSITIVE_ORDER), - new CopyOnWriteArraySet(), - new ConcurrentSkipListMap(String.CASE_INSENSITIVE_ORDER), - new ConcurrentHashMap() - ); - } - - // -------------------------------------------- // - // INTERFACE IMPLEMENTATION - // -------------------------------------------- // - - @Override - public abstract Class getManagedClass(); - - @Override - public T createNewInstance() - { - try - { - return this.getManagedClass().newInstance(); - } - catch (Exception e) - { - e.printStackTrace(); - } - return null; - } - - @Override - public synchronized T create() - { - return this.create(null); - } - - @Override - public synchronized T create(Object oid) - { - T entity = this.createNewInstance(); - if (this.attach(entity, oid) == null) return null; - return entity; - } - - @Override - public synchronized String attach(T entity) - { - return this.attach(entity, null); - } - - @Override - public synchronized String attach(T entity, Object oid) - { - return this.attach(entity, oid, false); - } - - protected synchronized String attach(T entity, Object oid, boolean allowExistingIdUsage) - { - // Check entity - if (entity == null) return null; - String id = this.id(entity); - if (id != null) return id; - - // Check/Fix id - if (oid == null) - { - id = this.idNext(true); - } - else - { - id = this.idFix(oid); - if (id == null) return null; - if (this.containsId(id) && ! allowExistingIdUsage) return null; - } - - // Attach - this.ids.add(id); - this.entities.add(entity); - this.id2entity.put(id, entity); - this.entity2id.put(entity, id); - - // Update Auto Increment - this.idUpdateCurrentFor(id); - return id; - } - - @Override - public synchronized void detachEntity(T entity) - { - if (entity == null) return; - String id = this.entity2id.get(entity); - if (id == null) return; - this.detach(entity, id); - } - - @Override - public synchronized void detachId(Object oid) - { - String id = this.idFix(oid); - if (id == null) return; - T entity = this.id2entity.get(id); - this.detach(entity, id); - } - - // Assumes the id is correct! For internal use only! - protected synchronized void detach(T entity, String id) - { - if (id != null) - { - this.ids.remove(id); - this.id2entity.remove(id); - this.removeFile(id); - } - - if (entity != null) - { - this.entities.remove(entity); - this.entity2id.remove(entity); - } - } - - protected void removeFile(String id) - { - File file = this.fileFromId(id); - if (file.exists()) - { - file.delete(); - } - } - - @Override - public boolean containsEntity(T entity) - { - return this.entity2id.containsKey(entity); - } - - @Override - public boolean containsId(Object oid) - { - String id = this.idFix(oid); - if (id == null) return false; - return ids.contains(id); - } - - @Override - public boolean saveEntity(T entity) - { - String id = this.id(entity); - return this.save(id, entity); - } - - @Override - public boolean saveId(Object oid) - { - String id = this.idFix(oid); - T entity = this.get(id); - return this.save(id, entity); - } - - protected boolean save(String id, T entity) - { - if (id == null) return false; - if (entity == null) return false; - if (this.shouldBeSaved(entity)) - { - String json = this.getGson().toJson(entity); - File file = this.fileFromId(id); - return DiscUtil.writeCatch(file, json); - } - this.removeFile(id); - // TODO: Remove if loaded?? - - return true; - // TODO: Perhaps implement a logger in the interface? - } - - @Override - public boolean saveAll() - { - // Delete all files we do not care about. - for (File file : this.getFolder().listFiles(JsonFileFilter.getInstance())) - { - String id = this.idFromFile(file); - if ( ! this.containsId(id)) - { - file.delete(); - } - } - - // Save all loaded entites. - for (Entry entry: this.id2entity.entrySet()) - { - this.save(entry.getKey(), entry.getValue()); - } - // TODO: Bogus! This should return if a single error was encountered! - return true; - } - - @Override - public boolean loaded(Object oid) - { - String id = this.idFix(oid); - if (id == null) return false; - return this.id2entity.containsKey(id); - } - - @Override - public synchronized T load(Object oid) - { - String id = this.idFix(oid); - if (id == null) return null; - T entity = this.id2entity.get(id); - if (entity != null) return entity; - if ( ! this.containsId(id)) return null; - File file = this.fileFromId(id); - String json = DiscUtil.readCatch(file); - if (json == null) return null; - entity = this.getGson().fromJson(json, this.getManagedClass()); - this.attach(entity, id, true); - return entity; - } - - @Override - public boolean loadAll() - { - if (this.didLoadAll) return false; - for (String id : this.ids) - { - this.load(id); - } - this.didLoadAll = true; - return true; - } - - @Override - public boolean shouldBeSaved(T entity) - { - return true; - } - - @Override - public String id(T entity) - { - return this.entity2id.get(entity); - } - - @Override - public abstract String idFix(Object oid); - - @Override - public abstract boolean idCanFix(Class clazz); - - @Override - public String idCurrent() - { - return Long.toString(this.idCurrent); - } - - @Override - public synchronized String idNext(boolean advance) - { - long next = this.idCurrent; - String nextString = String.valueOf(next); - while (this.containsId(nextString)) - { - next += 1; - nextString = String.valueOf(next); - } - if (advance) - { - this.idCurrent = next; - } - return nextString; - } - - @Override - public synchronized boolean idUpdateCurrentFor(Object oid) - { - String id = this.idFix(oid); - if (id == null) return false; - long primid; - try - { - primid = Long.parseLong(id); - } - catch (Exception e) - { - // The id was not a number. No need to care about it. - return false; - } - if (this.idCurrent < primid) - { - this.idCurrent = primid; - return true; - } - return false; - } - - @Override - public synchronized T get(Object oid, boolean creative) - { - String id = this.idFix(oid); - if (id == null) return null; - T ret = this.load(id); - if (ret != null) - { - return ret; - } - if ( ! creative) return null; - return this.create(id); - } - - @Override - public T get(Object oid) - { - return this.get(oid, this.getIsCreative()); - } - - @Override - public Collection getAllLoaded() - { - return entities; - } - - @Override - public Collection getAllLoaded(Predictate where) - { - return Persist.uglySQL(this.getAllLoaded(), where, null, null, null); - } - - @Override - public Collection getAllLoaded(Predictate where, Comparator orderby) - { - return Persist.uglySQL(this.getAllLoaded(), where, orderby, null, null); - } - - @Override - public Collection getAllLoaded(Predictate where, Comparator orderby, Integer limit) - { - return Persist.uglySQL(this.getAllLoaded(), where, orderby, limit, null); - } - - @Override - public Collection getAllLoaded(Predictate where, Comparator orderby, Integer limit, Integer offset) - { - return Persist.uglySQL(this.getAllLoaded(), where, orderby, limit, offset); - } - - @Override - public Collection getAll() - { - this.loadAll(); - return entities; - } - - @Override - public Collection getAll(Predictate where) - { - return Persist.uglySQL(this.getAll(), where, null, null, null); - } - - @Override - public Collection getAll(Predictate where, Comparator orderby) - { - return Persist.uglySQL(this.getAll(), where, orderby, null, null); - } - - @Override - public Collection getAll(Predictate where, Comparator orderby, Integer limit) - { - return Persist.uglySQL(this.getAll(), where, orderby, limit, null); - } - - @Override - public Collection getAll(Predictate where, Comparator orderby, Integer limit, Integer offset) - { - return Persist.uglySQL(this.getAll(), where, orderby, limit, offset); - } - - @Override - public Collection getIds() - { - return this.ids; - } - - @Override - public Map getMap() - { - return this.id2entity; - } - - @Override - public T getBestMatch(Object oid) - { - String start = this.idFix(oid); - if (start == null) return null; - String id = Txt.getBestCIStart(this.ids, start); - return this.get(id); - } -} diff --git a/src/com/massivecraft/mcore4/persist/gson/GsonPlayerEntityManager.java b/src/com/massivecraft/mcore4/persist/gson/GsonPlayerEntityManager.java deleted file mode 100644 index f2952821..00000000 --- a/src/com/massivecraft/mcore4/persist/gson/GsonPlayerEntityManager.java +++ /dev/null @@ -1,67 +0,0 @@ -package com.massivecraft.mcore4.persist.gson; - -import java.io.File; -import java.util.Collection; -import java.util.Map; -import java.util.Set; - -import org.bukkit.entity.Player; -import org.bukkit.event.player.PlayerEvent; - -import com.massivecraft.mcore4.Predictate; -import com.massivecraft.mcore4.persist.PlayerEntity; -import com.massivecraft.mcore4.xlib.gson.Gson; - -public abstract class GsonPlayerEntityManager> extends GsonClassManager -{ - public GsonPlayerEntityManager(Gson gson, File folder, boolean creative, boolean lazy) - { - super(gson, folder, creative, lazy); - } - - public GsonPlayerEntityManager(Gson gson, File folder, boolean creative, boolean lazy, Set ids, Set entities, Map id2entity, Map entity2id) - { - super(gson, folder, creative, lazy, ids, entities, id2entity, entity2id); - } - - @Override - public String idFix(Object oid) - { - if (oid == null) return null; - if (oid instanceof String) return (String) oid; - if (oid instanceof Player) return ((Player)oid).getName(); - if (oid instanceof PlayerEvent) return ((PlayerEvent)oid).getPlayer().getName(); - return null; - } - - @Override - public boolean idCanFix(Class clazz) - { - if (clazz == null) return false; - if (String.class.equals(clazz)) return true; - if (Player.class.equals(clazz)) return true; - return false; - } - - public Collection getAllOnline() - { - return this.getAllLoaded(new Predictate() - { - public boolean apply(T entity) - { - return entity.isOnline(); - } - }); - } - - public Collection getAllOffline() - { - return this.getAll(new Predictate() - { - public boolean apply(T entity) - { - return entity.isOffline(); - } - }); - } -} diff --git a/src/com/massivecraft/mcore4/persist/gson/JsonFileFilter.java b/src/com/massivecraft/mcore4/persist/gson/JsonFileFilter.java deleted file mode 100644 index 10af4cf3..00000000 --- a/src/com/massivecraft/mcore4/persist/gson/JsonFileFilter.java +++ /dev/null @@ -1,26 +0,0 @@ -package com.massivecraft.mcore4.persist.gson; - -import java.io.File; -import java.io.FileFilter; - -public class JsonFileFilter implements FileFilter -{ - private static JsonFileFilter instance = null; - private final static String DOTJSON = ".json"; - public static JsonFileFilter getInstance() - { - if (instance == null) - { - instance = new JsonFileFilter(); - } - return instance; - } - - @Override - public boolean accept(File pathname) - { - return pathname.getName().toLowerCase().endsWith(DOTJSON); - } - - private JsonFileFilter() {} -} diff --git a/src/com/massivecraft/mcore4/util/BukkitCommandUtil.java b/src/com/massivecraft/mcore4/util/BukkitCommandUtil.java new file mode 100644 index 00000000..14d581f8 --- /dev/null +++ b/src/com/massivecraft/mcore4/util/BukkitCommandUtil.java @@ -0,0 +1,33 @@ +package com.massivecraft.mcore4.util; + +import java.lang.reflect.Field; +import java.util.Map; + +import org.bukkit.Bukkit; +import org.bukkit.command.Command; +import org.bukkit.command.SimpleCommandMap; +import org.bukkit.craftbukkit.CraftServer; + +public class BukkitCommandUtil +{ + public static SimpleCommandMap getBukkitCommandMap() + { + CraftServer craftServer = (CraftServer)Bukkit.getServer(); + return craftServer.getCommandMap(); + } + + @SuppressWarnings("unchecked") + public static Map getKnownCommandsFromSimpleCommandMap(SimpleCommandMap scm) + { + try + { + Field field = SimpleCommandMap.class.getDeclaredField("knownCommands"); + field.setAccessible(true); + return (Map) field.get(scm); + } + catch (Exception e) + { + return null; + } + } +} diff --git a/src/com/massivecraft/mcore4/util/LibLoader.java b/src/com/massivecraft/mcore4/util/LibLoader.java index 5a32fbc1..a88da85c 100644 --- a/src/com/massivecraft/mcore4/util/LibLoader.java +++ b/src/com/massivecraft/mcore4/util/LibLoader.java @@ -2,17 +2,12 @@ package com.massivecraft.mcore4.util; import java.io.File; +import com.massivecraft.mcore4.MCore; import com.massivecraft.mcore4.MPlugin; public class LibLoader { - MPlugin p; - public LibLoader(MPlugin p) - { - this.p = p; - } - - public boolean require(String filename, String url) + public static boolean require(String filename, String url, MPlugin p) { if ( ! include(filename, url)) { @@ -23,7 +18,7 @@ public class LibLoader return true; } - public boolean include (String filename, String url) + public static boolean include (String filename, String url) { File file = getFile(filename); if ( ! file.exists()) @@ -31,14 +26,13 @@ public class LibLoader File parent = file.getParentFile(); if (parent != null && !parent.exists()) parent.mkdirs(); - p.log("Downloading library "+filename); + MCore.p.log("Downloading library "+filename); if ( ! DiscUtil.downloadUrl(url, file)) { - p.log("Failed to download "+filename); + MCore.p.log(Txt.parse("Failed to download %s", filename)); return false; } } - return ClassLoadHack.load(file); } diff --git a/src/com/massivecraft/mcore4/util/MUtil.java b/src/com/massivecraft/mcore4/util/MUtil.java index eff0b443..44587d77 100644 --- a/src/com/massivecraft/mcore4/util/MUtil.java +++ b/src/com/massivecraft/mcore4/util/MUtil.java @@ -166,7 +166,7 @@ public class MUtil // -------------------------------------------- // //http://stackoverflow.com/questions/2864840/treemap-sort-by-value - public static > SortedSet> entriesSortedByValues(Map map) { + /*public static > SortedSet> entriesSortedByValues(Map map) { SortedSet> sortedEntries = new TreeSet>( new Comparator>() { @Override public int compare(Map.Entry e1, Map.Entry e2) { @@ -177,6 +177,36 @@ public class MUtil ); sortedEntries.addAll(map.entrySet()); return sortedEntries; + }*/ + + // http://stackoverflow.com/questions/2864840/treemap-sort-by-value + public static > SortedSet> entriesSortedByValues(Map map) + { + return entriesSortedByValues(map, true); + } + + public static > SortedSet> entriesSortedByValues(Map map, final boolean ascending) + { + SortedSet> sortedEntries = new TreeSet>( + new Comparator>() + { + @Override public int compare(Map.Entry e1, Map.Entry e2) + { + int res; + if (ascending) + { + res = e1.getValue().compareTo(e2.getValue()); + } + else + { + res = e2.getValue().compareTo(e1.getValue()); + } + return res != 0 ? res : 1; + } + } + ); + sortedEntries.addAll(map.entrySet()); + return sortedEntries; } // -------------------------------------------- // diff --git a/src/com/massivecraft/mcore4/util/Txt.java b/src/com/massivecraft/mcore4/util/Txt.java index ddbfa581..0ddf71fd 100644 --- a/src/com/massivecraft/mcore4/util/Txt.java +++ b/src/com/massivecraft/mcore4/util/Txt.java @@ -427,7 +427,7 @@ public class Txt if (unitCountParts.size() == 0) return "just now"; - ret += implodeCommaAndDot(unitCountParts); + ret += implodeCommaAnd(unitCountParts); ret += " "; if (millis <= 0) {