From 82f4b3b1d97e08fb649a5e461aa98e335021fe26 Mon Sep 17 00:00:00 2001 From: md_5 Date: Mon, 20 Aug 2018 12:49:31 +1000 Subject: SPIGOT-4292: Ignore itemstacks with invalid enchants --- src/main/java/org/bukkit/craftbukkit/inventory/CraftItemStack.java | 5 ++++- src/main/java/org/bukkit/craftbukkit/inventory/CraftMetaItem.java | 2 +- src/main/java/org/bukkit/craftbukkit/util/CraftNamespacedKey.java | 5 +++++ 3 files changed, 10 insertions(+), 2 deletions(-) (limited to 'src') diff --git a/src/main/java/org/bukkit/craftbukkit/inventory/CraftItemStack.java b/src/main/java/org/bukkit/craftbukkit/inventory/CraftItemStack.java index f5a39c2e..ef4c49f1 100644 --- a/src/main/java/org/bukkit/craftbukkit/inventory/CraftItemStack.java +++ b/src/main/java/org/bukkit/craftbukkit/inventory/CraftItemStack.java @@ -299,7 +299,10 @@ public final class CraftItemStack extends ItemStack { String id = ((NBTTagCompound) list.get(i)).getString(ENCHANTMENTS_ID.NBT); int level = 0xffff & ((NBTTagCompound) list.get(i)).getShort(ENCHANTMENTS_LVL.NBT); - result.put(Enchantment.getByKey(CraftNamespacedKey.fromString(id)), level); + Enchantment enchant = Enchantment.getByKey(CraftNamespacedKey.fromStringOrNull(id)); + if (enchant != null) { + result.put(enchant, level); + } } return result.build(); diff --git a/src/main/java/org/bukkit/craftbukkit/inventory/CraftMetaItem.java b/src/main/java/org/bukkit/craftbukkit/inventory/CraftMetaItem.java index e0f28caa..77266427 100644 --- a/src/main/java/org/bukkit/craftbukkit/inventory/CraftMetaItem.java +++ b/src/main/java/org/bukkit/craftbukkit/inventory/CraftMetaItem.java @@ -381,7 +381,7 @@ class CraftMetaItem implements ItemMeta, Damageable, Repairable { String id = ((NBTTagCompound) ench.get(i)).getString(ENCHANTMENTS_ID.NBT); int level = 0xffff & ((NBTTagCompound) ench.get(i)).getShort(ENCHANTMENTS_LVL.NBT); - Enchantment enchant = Enchantment.getByKey(CraftNamespacedKey.fromString(id)); + Enchantment enchant = Enchantment.getByKey(CraftNamespacedKey.fromStringOrNull(id)); if (enchant != null) { enchantments.put(enchant, level); } diff --git a/src/main/java/org/bukkit/craftbukkit/util/CraftNamespacedKey.java b/src/main/java/org/bukkit/craftbukkit/util/CraftNamespacedKey.java index d8831f61..03d2392a 100644 --- a/src/main/java/org/bukkit/craftbukkit/util/CraftNamespacedKey.java +++ b/src/main/java/org/bukkit/craftbukkit/util/CraftNamespacedKey.java @@ -8,6 +8,11 @@ public final class CraftNamespacedKey { public CraftNamespacedKey() { } + public static NamespacedKey fromStringOrNull(String string) { + MinecraftKey minecraft = MinecraftKey.a(string); + return (minecraft == null) ? null : fromMinecraft(minecraft); + } + public static NamespacedKey fromString(String string) { return fromMinecraft(new MinecraftKey(string)); } -- cgit v1.2.3