Custom ItemStack data

Discussion in 'Spigot Plugin Development' started by Shadow48402, Jan 15, 2015.

  1. Hello,

    Is there a way to recognising an item without using the displayname?

    - Shadow
     
  2. Set the item's Metadata by using the setMetaData method.
     
  3. ItemStack doesn't implements Metadatable?
     
  4. Oh sorry, Item implements Metadatable. Perhaps set the Lore instead.
     
  5. Hex

    Hex
    IRC Staff

    You could probably use NBT.
     
  6. I'm not sure if this will work but maybe you can add a blank line to the lore and you can check if your ItemStack is of the one with the blank line by using the method hasLore();

    Again, I'm not sure if this'll work. If it doesn't, post back, and I'll try to think of something else for you.
     
  7. I've never really worked with NBT, but by what I've heard, it's a valid solution to tagging additional data to an item. Grab an NMS ItemStack and there will be a field of the type NBTTagCompound called "tag." In NBTTagCompound, you'll find a wide variety of getters/setters:

    Code (Text):
      public void set(String paramString, NBTBase paramNBTBase)
      {
      this.map.put(paramString, paramNBTBase);
      }
     
      public void setByte(String paramString, byte paramByte)
      {
      this.map.put(paramString, new NBTTagByte(paramByte));
      }
     
      public void setShort(String paramString, short paramShort)
      {
      this.map.put(paramString, new NBTTagShort(paramShort));
      }
     
      public void setInt(String paramString, int paramInt)
      {
      this.map.put(paramString, new NBTTagInt(paramInt));
      }
     
      public void setLong(String paramString, long paramLong)
      {
      this.map.put(paramString, new NBTTagLong(paramLong));
      }
     
      public void setFloat(String paramString, float paramFloat)
      {
      this.map.put(paramString, new NBTTagFloat(paramFloat));
      }
     
      public void setDouble(String paramString, double paramDouble)
      {
      this.map.put(paramString, new NBTTagDouble(paramDouble));
      }
     
      public void setString(String paramString1, String paramString2)
      {
      this.map.put(paramString1, new NBTTagString(paramString2));
      }
     
      public void setByteArray(String paramString, byte[] paramArrayOfByte)
      {
      this.map.put(paramString, new NBTTagByteArray(paramArrayOfByte));
      }
     
      public void setIntArray(String paramString, int[] paramArrayOfInt)
      {
      this.map.put(paramString, new NBTTagIntArray(paramArrayOfInt));
      }
     
      public void setBoolean(String paramString, boolean paramBoolean)
      {
      setByte(paramString, (byte)(paramBoolean ? 1 : 0));
      }
    Code (Text):
      public NBTBase get(String paramString)
      {
      return (NBTBase)this.map.get(paramString);
      }
     
      public byte getByte(String paramString)
      {
      try
      {
      if (!this.map.containsKey(paramString)) {
      return 0;
      }
      return ((NBTNumber)this.map.get(paramString)).f();
      }
      catch (ClassCastException localClassCastException) {}
      return 0;
      }
     
      public short getShort(String paramString)
      {
      try
      {
      if (!this.map.containsKey(paramString)) {
      return 0;
      }
      return ((NBTNumber)this.map.get(paramString)).e();
      }
      catch (ClassCastException localClassCastException) {}
      return 0;
      }
     
      public int getInt(String paramString)
      {
      try
      {
      if (!this.map.containsKey(paramString)) {
      return 0;
      }
      return ((NBTNumber)this.map.get(paramString)).d();
      }
      catch (ClassCastException localClassCastException) {}
      return 0;
      }
     
      public long getLong(String paramString)
      {
      try
      {
      if (!this.map.containsKey(paramString)) {
      return 0L;
      }
      return ((NBTNumber)this.map.get(paramString)).c();
      }
      catch (ClassCastException localClassCastException) {}
      return 0L;
      }
     
      public float getFloat(String paramString)
      {
      try
      {
      if (!this.map.containsKey(paramString)) {
      return 0.0F;
      }
      return ((NBTNumber)this.map.get(paramString)).h();
      }
      catch (ClassCastException localClassCastException) {}
      return 0.0F;
      }
     
      public double getDouble(String paramString)
      {
      try
      {
      if (!this.map.containsKey(paramString)) {
      return 0.0D;
      }
      return ((NBTNumber)this.map.get(paramString)).g();
      }
      catch (ClassCastException localClassCastException) {}
      return 0.0D;
      }
     
      public String getString(String paramString)
      {
      try
      {
      if (!this.map.containsKey(paramString)) {
      return "";
      }
      return ((NBTBase)this.map.get(paramString)).a_();
      }
      catch (ClassCastException localClassCastException) {}
      return "";
      }
     
      public byte[] getByteArray(String paramString)
      {
      try
      {
      if (!this.map.containsKey(paramString)) {
      return new byte[0];
      }
      return ((NBTTagByteArray)this.map.get(paramString)).c();
      }
      catch (ClassCastException localClassCastException)
      {
      throw new ReportedException(a(paramString, 7, localClassCastException));
      }
      }
     
      public int[] getIntArray(String paramString)
      {
      try
      {
      if (!this.map.containsKey(paramString)) {
      return new int[0];
      }
      return ((NBTTagIntArray)this.map.get(paramString)).c();
      }
      catch (ClassCastException localClassCastException)
      {
      throw new ReportedException(a(paramString, 11, localClassCastException));
      }
      }
     
      public NBTTagCompound getCompound(String paramString)
      {
      try
      {
      if (!this.map.containsKey(paramString)) {
      return new NBTTagCompound();
      }
      return (NBTTagCompound)this.map.get(paramString);
      }
      catch (ClassCastException localClassCastException)
      {
      throw new ReportedException(a(paramString, 10, localClassCastException));
      }
      }
     
      public NBTTagList getList(String paramString, int paramInt)
      {
      try
      {
      if (b(paramString) != 9) {
      return new NBTTagList();
      }
      NBTTagList localNBTTagList = (NBTTagList)this.map.get(paramString);
      if ((localNBTTagList.size() > 0) && (localNBTTagList.d() != paramInt)) {
      return new NBTTagList();
      }
      return localNBTTagList;
      }
      catch (ClassCastException localClassCastException)
      {
      throw new ReportedException(a(paramString, 9, localClassCastException));
      }
      }
     
      public boolean getBoolean(String paramString)
      {
      return getByte(paramString) != 0;
      }
    Only make sure not to overwrite existing keys.