SPIGOT-4292: Ignore itemstacks with invalid enchants

This commit is contained in:
md_5 2018-08-20 12:49:31 +10:00
parent bfb9131494
commit 82f4b3b1d9
3 changed files with 10 additions and 2 deletions

View File

@ -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();

View File

@ -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);
}

View File

@ -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));
}