From a64b99c95a507a8d3c108930db1698b432d72554 Mon Sep 17 00:00:00 2001 From: md_5 Date: Tue, 22 Nov 2016 14:35:54 +1100 Subject: [PATCH] Add Unbreakable to ItemMeta --- .../craftbukkit/inventory/CraftMetaItem.java | 38 ++++++++++++++++++- 1 file changed, 36 insertions(+), 2 deletions(-) diff --git a/src/main/java/org/bukkit/craftbukkit/inventory/CraftMetaItem.java b/src/main/java/org/bukkit/craftbukkit/inventory/CraftMetaItem.java index 294f6c903c..a169e101c5 100644 --- a/src/main/java/org/bukkit/craftbukkit/inventory/CraftMetaItem.java +++ b/src/main/java/org/bukkit/craftbukkit/inventory/CraftMetaItem.java @@ -211,12 +211,15 @@ class CraftMetaItem implements ItemMeta, Repairable { static final ItemMetaKey ATTRIBUTES_UUID_LOW = new ItemMetaKey("UUIDLeast"); @Specific(Specific.To.NBT) static final ItemMetaKey HIDEFLAGS = new ItemMetaKey("HideFlags", "ItemFlags"); + @Specific(Specific.To.NBT) + static final ItemMetaKey UNBREAKABLE = new ItemMetaKey("Unbreakable"); private String displayName; private List lore; private Map enchantments; private int repairCost; private int hideFlag; + private boolean unbreakable; private static final Set HANDLED_TAGS = Sets.newHashSet(); @@ -239,6 +242,7 @@ class CraftMetaItem implements ItemMeta, Repairable { this.repairCost = meta.repairCost; this.hideFlag = meta.hideFlag; + this.unbreakable = meta.unbreakable; this.unhandledTags.putAll(meta.unhandledTags); } @@ -270,6 +274,9 @@ class CraftMetaItem implements ItemMeta, Repairable { if (tag.hasKey(HIDEFLAGS.NBT)) { hideFlag = tag.getInt(HIDEFLAGS.NBT); } + if (tag.hasKey(UNBREAKABLE.NBT)) { + unbreakable = tag.getBoolean(UNBREAKABLE.NBT); + } if (tag.get(ATTRIBUTES.NBT) instanceof NBTTagList) { NBTTagList save = null; @@ -371,6 +378,11 @@ class CraftMetaItem implements ItemMeta, Repairable { } } + Boolean unbreakable = SerializableMeta.getObject(Boolean.class, map, UNBREAKABLE.BUKKIT, true); + if (unbreakable != null) { + setUnbreakable(unbreakable); + } + String internal = SerializableMeta.getString(map, "internal", true); if (internal != null) { ByteArrayInputStream buf = new ByteArrayInputStream(Base64.decodeBase64(internal)); @@ -430,6 +442,10 @@ class CraftMetaItem implements ItemMeta, Repairable { itemTag.setInt(REPAIR.NBT, repairCost); } + if (isUnbreakable()) { + itemTag.setBoolean(UNBREAKABLE.NBT, unbreakable); + } + for (Map.Entry e : unhandledTags.entrySet()) { itemTag.set(e.getKey(), e.getValue()); } @@ -484,7 +500,7 @@ class CraftMetaItem implements ItemMeta, Repairable { @Overridden boolean isEmpty() { - return !(hasDisplayName() || hasEnchants() || hasLore() || hasRepairCost() || !unhandledTags.isEmpty() || hideFlag != 0); + return !(hasDisplayName() || hasEnchants() || hasLore() || hasRepairCost() || !unhandledTags.isEmpty() || hideFlag != 0 || isUnbreakable()); } public String getDisplayName() { @@ -609,6 +625,16 @@ class CraftMetaItem implements ItemMeta, Repairable { repairCost = cost; } + @Override + public boolean isUnbreakable() { + return unbreakable; + } + + @Override + public void setUnbreakable(boolean unbreakable) { + this.unbreakable = unbreakable; + } + @Override public final boolean equals(Object object) { if (object == null) { @@ -635,7 +661,8 @@ class CraftMetaItem implements ItemMeta, Repairable { && (this.hasLore() ? that.hasLore() && this.lore.equals(that.lore) : !that.hasLore()) && (this.hasRepairCost() ? that.hasRepairCost() && this.repairCost == that.repairCost : !that.hasRepairCost()) && (this.unhandledTags.equals(that.unhandledTags)) - && (this.hideFlag == that.hideFlag); + && (this.hideFlag == that.hideFlag) + && (this.isUnbreakable() == that.isUnbreakable()); } /** @@ -662,6 +689,7 @@ class CraftMetaItem implements ItemMeta, Repairable { hash = 61 * hash + (hasRepairCost() ? this.repairCost : 0); hash = 61 * hash + unhandledTags.hashCode(); hash = 61 * hash + hideFlag; + hash = 61 * hash + (isUnbreakable() ? 1231 : 1237); return hash; } @@ -677,6 +705,7 @@ class CraftMetaItem implements ItemMeta, Repairable { clone.enchantments = new HashMap(this.enchantments); } clone.hideFlag = this.hideFlag; + clone.unbreakable = this.unbreakable; return clone; } catch (CloneNotSupportedException e) { throw new Error(e); @@ -714,6 +743,10 @@ class CraftMetaItem implements ItemMeta, Repairable { builder.put(HIDEFLAGS.BUKKIT, hideFlags); } + if (isUnbreakable()) { + builder.put(UNBREAKABLE.BUKKIT, unbreakable); + } + final Map internalTags = new HashMap(unhandledTags); serializeInternal(internalTags); if (!internalTags.isEmpty()) { @@ -800,6 +833,7 @@ class CraftMetaItem implements ItemMeta, Repairable { REPAIR.NBT, ENCHANTMENTS.NBT, HIDEFLAGS.NBT, + UNBREAKABLE.NBT, CraftMetaMap.MAP_SCALING.NBT, CraftMetaPotion.POTION_EFFECTS.NBT, CraftMetaPotion.DEFAULT_POTION.NBT,