From cb5f71f2c8df55c34c0f92d967f333fc7f1ad453 Mon Sep 17 00:00:00 2001 From: Olof Larsson Date: Mon, 19 Mar 2012 12:03:52 +0100 Subject: [PATCH] Dropping some spout usage. Adding date argument reader --- plugin.yml | 2 +- src/com/massivecraft/mcore2/cmd/Cmd.java | 3 +++ .../massivecraft/mcore2/cmd/arg/AHDate.java | 21 +++++++++++++++ .../mcore2/gson/InventoryTypeAdapter.java | 7 +++-- .../massivecraft/mcore2/persist/Persist.java | 27 +++++++++++++++++++ 5 files changed, 57 insertions(+), 3 deletions(-) create mode 100644 src/com/massivecraft/mcore2/cmd/arg/AHDate.java diff --git a/plugin.yml b/plugin.yml index 8236bbbe..3dc39741 100644 --- a/plugin.yml +++ b/plugin.yml @@ -1,6 +1,6 @@ name: mcore2 version: 1.0.0 -main: com.massivecraft.mcore2dev.MCore +main: com.massivecraft.mcore2.MCore authors: [Olof Larsson, Brett Flannigan] commands: mcoresilenteater: diff --git a/src/com/massivecraft/mcore2/cmd/Cmd.java b/src/com/massivecraft/mcore2/cmd/Cmd.java index c9a9a14c..efcf4127 100644 --- a/src/com/massivecraft/mcore2/cmd/Cmd.java +++ b/src/com/massivecraft/mcore2/cmd/Cmd.java @@ -1,5 +1,6 @@ package com.massivecraft.mcore2.cmd; +import java.util.Date; import java.util.HashMap; import java.util.HashSet; import java.util.List; @@ -12,6 +13,7 @@ import org.bukkit.command.CommandSender; import org.bukkit.entity.Player; import com.massivecraft.mcore2.cmd.arg.AHBoolean; +import com.massivecraft.mcore2.cmd.arg.AHDate; import com.massivecraft.mcore2.cmd.arg.AHDouble; import com.massivecraft.mcore2.cmd.arg.AHFloat; import com.massivecraft.mcore2.cmd.arg.AHInteger; @@ -53,6 +55,7 @@ public class Cmd { this.setArgHandler(Boolean.class, new AHBoolean()); this.setArgHandler(Double.class, new AHDouble()); + this.setArgHandler(Date.class, new AHDate()); this.setArgHandler(Float.class, new AHFloat()); this.setArgHandler(Integer.class, new AHInteger()); this.setArgHandler(Material.class, new AHMaterial()); diff --git a/src/com/massivecraft/mcore2/cmd/arg/AHDate.java b/src/com/massivecraft/mcore2/cmd/arg/AHDate.java new file mode 100644 index 00000000..b0d16345 --- /dev/null +++ b/src/com/massivecraft/mcore2/cmd/arg/AHDate.java @@ -0,0 +1,21 @@ +package com.massivecraft.mcore2.cmd.arg; + +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/mcore2/gson/InventoryTypeAdapter.java b/src/com/massivecraft/mcore2/gson/InventoryTypeAdapter.java index 87501257..ed9cf1c6 100644 --- a/src/com/massivecraft/mcore2/gson/InventoryTypeAdapter.java +++ b/src/com/massivecraft/mcore2/gson/InventoryTypeAdapter.java @@ -6,10 +6,10 @@ import java.util.Collection; import java.util.Map.Entry; import java.util.logging.Logger; +import org.bukkit.craftbukkit.inventory.CraftInventoryCustom; import org.bukkit.enchantments.Enchantment; import org.bukkit.inventory.Inventory; import org.bukkit.inventory.ItemStack; -import org.getspout.spoutapi.SpoutManager; import com.massivecraft.mcore2.lib.gson.JsonDeserializationContext; import com.massivecraft.mcore2.lib.gson.JsonDeserializer; @@ -82,7 +82,10 @@ public class InventoryTypeAdapter implements JsonDeserializer, JsonSe itemStacks.add(stack); } - return SpoutManager.getInventoryBuilder().construct(itemStacks, ""); + + Inventory ret = new CraftInventoryCustom(null, itemStacks.size(), "items"); + ret.setContents(itemStacks.toArray(new ItemStack[0])); + return ret; } catch (Exception ex) { diff --git a/src/com/massivecraft/mcore2/persist/Persist.java b/src/com/massivecraft/mcore2/persist/Persist.java index c2f5999d..7849f7bf 100644 --- a/src/com/massivecraft/mcore2/persist/Persist.java +++ b/src/com/massivecraft/mcore2/persist/Persist.java @@ -14,7 +14,9 @@ import java.util.Collections; import java.util.Comparator; import java.util.HashMap; import java.util.Map; +import java.util.SortedSet; import java.util.Timer; +import java.util.TreeSet; import com.massivecraft.mcore2.Predictate; @@ -209,4 +211,29 @@ public class Persist } return ret; } + + // 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; + } }