Permalink
Browse files

Support sorting enchanted books (#197)

  • Loading branch information...
BillyGalbreath authored and me4502 committed Nov 21, 2016
1 parent 46f7131 commit 0d467eb5619661d30e8375556535aef9dd2c1973
@@ -101,8 +101,9 @@ public boolean sortItemStack(final ItemStack item) {
}
public boolean isInAboveContainer(ItemStack item) {
-
- return InventoryUtil.doesBlockHaveInventory(chestBlock) && InventoryUtil.doesInventoryContain(((InventoryHolder) chestBlock.getState()).getInventory(), false, new ItemStack(item.getType(), 1, item.getDurability()));
+ ItemStack itemClone = item.clone();
+ itemClone.setAmount(1);
+ return InventoryUtil.doesBlockHaveInventory(chestBlock) && InventoryUtil.doesInventoryContain(((InventoryHolder) chestBlock.getState()).getInventory(), false, itemClone);
}
public static class Factory extends AbstractICFactory {
@@ -10,6 +10,7 @@
import org.bukkit.entity.Player;
import org.bukkit.inventory.*;
import org.bukkit.inventory.meta.ItemMeta;
+import org.bukkit.inventory.meta.EnchantmentStorageMeta;
import org.bukkit.material.MaterialData;
import java.util.ArrayList;
@@ -300,6 +301,36 @@ public static boolean areItemMetaIdentical(ItemMeta meta, ItemMeta meta2) {
CraftBookPlugin.logDebugMessage("Enchants are the same", "item-checks.meta.enchants");
+ //StoredEnchants
+ if (meta instanceof EnchantmentStorageMeta) {
+ if (!(meta2 instanceof EnchantmentStorageMeta))
+ return false; // meta type mismatch
+
+ EnchantmentStorageMeta storageMeta = (EnchantmentStorageMeta) meta;
+ List<Enchantment> storedEnchantments = new ArrayList<Enchantment>();
+ if (storageMeta.hasStoredEnchants())
+ storedEnchantments.addAll(storageMeta.getStoredEnchants().keySet());
+
+ EnchantmentStorageMeta storageMeta2 = (EnchantmentStorageMeta) meta2;
+ List<Enchantment> storedEnchantments2 = new ArrayList<Enchantment>();
+ if (storageMeta2.hasStoredEnchants())
+ storedEnchantments2.addAll(storageMeta2.getStoredEnchants().keySet());
+
+ if (storedEnchantments.size() != storedEnchantments2.size())
+ return false; // mismatch enchantment counts
+ CraftBookPlugin.logDebugMessage("Has same stored enchantment lengths", "item-checks.meta.enchants");
+
+ for (Enchantment ench : storedEnchantments) {
+ if (!storedEnchantments2.contains(ench))
+ return false; // mismatch stored enchantments
+ if (storageMeta.getStoredEnchantLevel(ench) != storageMeta2.getStoredEnchantLevel(ench))
+ return false; // mismatch stored enchantment levels
+ }
+
+ CraftBookPlugin.logDebugMessage("Stored enchants are the same", "item-checks.meta.enchants");
+ } else if (meta2 instanceof EnchantmentStorageMeta)
+ return false; // meta type mismatch
+
return true;
}

0 comments on commit 0d467eb

Please sign in to comment.