From 709f95833b8389bb5a0448bd0c8007e0a686abd7 Mon Sep 17 00:00:00 2001 From: md_5 Date: Sun, 15 Feb 2015 12:47:07 +1100 Subject: [PATCH] API for setting/getting hideFlags. Implementation of the hideFlags to be saved in NBT. --- .../craftbukkit/inventory/CraftMetaItem.java | 81 ++++++++++++++++++- 1 file changed, 77 insertions(+), 4 deletions(-) diff --git a/src/main/java/org/bukkit/craftbukkit/inventory/CraftMetaItem.java b/src/main/java/org/bukkit/craftbukkit/inventory/CraftMetaItem.java index 4681262aa5..28871ab36f 100644 --- a/src/main/java/org/bukkit/craftbukkit/inventory/CraftMetaItem.java +++ b/src/main/java/org/bukkit/craftbukkit/inventory/CraftMetaItem.java @@ -29,6 +29,7 @@ import org.bukkit.configuration.serialization.SerializableAs; import org.bukkit.craftbukkit.Overridden; import org.bukkit.craftbukkit.inventory.CraftMetaItem.ItemMetaKey.Specific; import org.bukkit.enchantments.Enchantment; +import org.bukkit.inventory.ItemFlag; import org.bukkit.inventory.meta.ItemMeta; import org.bukkit.inventory.meta.Repairable; @@ -37,6 +38,8 @@ import com.google.common.collect.ImmutableList; import com.google.common.collect.ImmutableMap; import com.google.common.collect.Sets; import java.util.Arrays; +import java.util.EnumSet; +import java.util.HashSet; import java.util.Set; /** @@ -201,11 +204,14 @@ class CraftMetaItem implements ItemMeta, Repairable { static final ItemMetaKey ATTRIBUTES_UUID_HIGH = new ItemMetaKey("UUIDMost"); @Specific(Specific.To.NBT) static final ItemMetaKey ATTRIBUTES_UUID_LOW = new ItemMetaKey("UUIDLeast"); + @Specific(Specific.To.NBT) + static final ItemMetaKey HIDEFLAGS = new ItemMetaKey("ItemFlags", "hideFlags"); private String displayName; private List lore; private Map enchantments; private int repairCost; + private int hideFlag; private final NBTTagList attributes; private static final Set HANDLED_TAGS = Sets.newHashSet(); @@ -230,6 +236,7 @@ class CraftMetaItem implements ItemMeta, Repairable { this.repairCost = meta.repairCost; this.attributes = meta.attributes; + this.hideFlag = meta.hideFlag; this.unhandledTags.putAll(meta.unhandledTags); } @@ -258,6 +265,9 @@ class CraftMetaItem implements ItemMeta, Repairable { repairCost = tag.getInt(REPAIR.NBT); } + if (tag.hasKey(HIDEFLAGS.NBT)) { + hideFlag = tag.getInt(HIDEFLAGS.NBT); + } if (tag.get(ATTRIBUTES.NBT) instanceof NBTTagList) { NBTTagList save = null; @@ -349,6 +359,19 @@ class CraftMetaItem implements ItemMeta, Repairable { } attributes = null; + + Set hideFlags = SerializableMeta.getObject(Set.class, map, HIDEFLAGS.BUKKIT, true); + if (hideFlags != null) { + for (Object hideFlagObject : hideFlags) { + String hideFlagString = (String) hideFlagObject; + try { + ItemFlag hideFlatEnum = ItemFlag.valueOf(hideFlagString); + addItemFlags(hideFlatEnum); + } catch (IllegalArgumentException ex) { + // Ignore when we got a old String which does not map to a Enum value anymore + } + } + } } static Map buildEnchantments(Map map, ItemMetaKey key) { @@ -379,6 +402,10 @@ class CraftMetaItem implements ItemMeta, Repairable { setDisplayTag(itemTag, LORE.NBT, createStringList(lore)); } + if (hideFlag != 0) { + itemTag.setInt(HIDEFLAGS.NBT, hideFlag); + } + applyEnchantments(enchantments, itemTag, ENCHANTMENTS); if (hasRepairCost()) { @@ -443,7 +470,7 @@ class CraftMetaItem implements ItemMeta, Repairable { @Overridden boolean isEmpty() { - return !(hasDisplayName() || hasEnchants() || hasLore() || hasAttributes() || hasRepairCost() || !unhandledTags.isEmpty()); + return !(hasDisplayName() || hasEnchants() || hasLore() || hasAttributes() || hasRepairCost() || !unhandledTags.isEmpty() || hideFlag != 0); } public String getDisplayName() { @@ -510,6 +537,43 @@ class CraftMetaItem implements ItemMeta, Repairable { return checkConflictingEnchants(enchantments, ench); } + @Override + public void addItemFlags(ItemFlag... hideFlags) { + for (ItemFlag f : hideFlags) { + this.hideFlag |= getBitModifier(f); + } + } + + @Override + public void removeItemFlags(ItemFlag... hideFlags) { + for (ItemFlag f : hideFlags) { + this.hideFlag &= ~getBitModifier(f); + } + } + + @Override + public Set getItemFlags() { + Set currentFlags = EnumSet.noneOf(ItemFlag.class); + + for (ItemFlag f : ItemFlag.values()) { + if (hasItemFlag(f)) { + currentFlags.add(f); + } + } + + return currentFlags; + } + + @Override + public boolean hasItemFlag(ItemFlag flag) { + int bitModifier = getBitModifier(flag); + return (this.hideFlag & bitModifier) == bitModifier; + } + + private byte getBitModifier(ItemFlag hideFlag) { + return (byte) (1 << hideFlag.ordinal()); + } + public List getLore() { return this.lore == null ? null : new ArrayList(this.lore); } @@ -561,7 +625,7 @@ class CraftMetaItem implements ItemMeta, Repairable { && (this.hasLore() ? that.hasLore() && this.lore.equals(that.lore) : !that.hasLore()) && (this.hasAttributes() ? that.hasAttributes() && this.attributes.equals(that.attributes) : !that.hasAttributes()) && (this.hasRepairCost() ? that.hasRepairCost() && this.repairCost == that.repairCost : !that.hasRepairCost()) - && (this.unhandledTags.equals(that.unhandledTags)); + && (this.hideFlag == that.hideFlag); } /** @@ -587,7 +651,7 @@ class CraftMetaItem implements ItemMeta, Repairable { hash = 61 * hash + (hasEnchants() ? this.enchantments.hashCode() : 0); hash = 61 * hash + (hasAttributes() ? this.attributes.hashCode() : 0); hash = 61 * hash + (hasRepairCost() ? this.repairCost : 0); - hash = 61 * hash + unhandledTags.hashCode(); + hash = 61 * hash + hideFlag; return hash; } @@ -602,6 +666,7 @@ class CraftMetaItem implements ItemMeta, Repairable { if (this.enchantments != null) { clone.enchantments = new HashMap(this.enchantments); } + clone.hideFlag = this.hideFlag; return clone; } catch (CloneNotSupportedException e) { throw new Error(e); @@ -631,6 +696,14 @@ class CraftMetaItem implements ItemMeta, Repairable { builder.put(REPAIR.BUKKIT, repairCost); } + Set hideFlags = new HashSet(); + for (ItemFlag hideFlagEnum : getItemFlags()) { + hideFlags.add(hideFlagEnum.name()); + } + if (!hideFlags.isEmpty()) { + builder.put(HIDEFLAGS.BUKKIT, hideFlags); + } + return builder; } @@ -711,7 +784,7 @@ class CraftMetaItem implements ItemMeta, Repairable { CraftMetaEnchantedBook.STORED_ENCHANTMENTS.NBT, CraftMetaCharge.EXPLOSION.NBT )); - } +} return HANDLED_TAGS; } }