Attach an invisible, arbitrary piece of data to an ItemStack

Discussion in 'Plugin Development' started by Maurdekye, Feb 8, 2014.

Thread Status:
Not open for further replies.
  1. Offline

    Maurdekye

    I want to tag certain items to prevent them from being used. I know how to prevent them from being used, but it's the tag i'm having trouble with. I've been using the lore to store info up until now, and it works fine, but the tag is visible and kind of ugly. I want something completely invisible, like entity metadata.

    Edit: I summon.. the Great, the Powerful, the Almighty xTrollxDudex ! Please help me, I'm not sure many others on this forum could give a proper answer.
     
  2. Offline

    mattrick

  3. Offline

    Maurdekye

    mattrick Well, if I were to use the first suggested method in that forum, a blank line would still show. And that's practically the same as what I have now. But the second method (suggested in the last post) seems relatively plausible, except for the fact I have no idea how to implement it.
     
  4. Offline

    xTrollxDudex

    Maurdekye
    Nbt was what I was thinking as well, let me see if I can take a whack at it:
    PHP:
    private static final String CONSTANT_MOD_VAR "CanPlaceOn";

    public 
    void tagItem(ItemStack item) {
        
    NBTTagCompound tag CraftItemStack.asNMSCopy(item).getTag();
        
    NBTTagList canPlaceOn = new NBTTagList();

        if(
    tag.hasKey(CONSTANT_MOD_VAR)) {
            
    canPlaceOn tag.getList(CONSTANT_MOD_VAR);
        }

        
    canPlaceOn.add(new NBTTagString("Tagged"));
    }

    public 
    boolean canUse(ItemStack item) {
        
    NBTTagCompound tag CraftItemStack.asNMSCopy(item).getTag();
        
    NBTTagList canPlaceOn = new NBTTagList();

        if(
    tag.hasKey(CONSTANT_MOD_VAR)) {
            
    canPlaceOn tag.getList(CONSTANT_MOD_VAR);
        } else {
            return 
    false;
        }

        if(
    canPlaceOn.size() < 1) {
            return 
    false;
        }

        
    boolean exists false;
        for(
    int i 0<= canPlaceOn.size() - 1i++) {
            if(
    canPlaceOn.get(i).equals(new NBTTagString("Tagged"))
                
    exists true;
        }

        return 
    exists;
    }
     
  5. Offline

    Maurdekye

    xTrollxDudex Ah, thank you! I knew there were a couple kinks in that summoning spell I had to work out.

    Edit: tag.getList() requires a String AND an int. What should that int be?

    xTrollxDudex Extra tahg, for good measure.

    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: Jun 6, 2016
  6. Offline

    xTrollxDudex

    Maurdekye
    Crap. Use the get method instead, you will need to cast to NBTTagList after using it though.
     
  7. Offline

    calebbfmv

    Code:
        public NBTTagList getList(String s, int i) {
            try {   
                if (this.b(s) != 9) {   
                  return new NBTTagList(); 
          } else {     
          NBTTagList nbttaglist = (NBTTagList) this.map.get(s);
                  return nbttaglist.size() > 0 && nbttaglist.d() != i ? new NBTTagList() : nbttaglist;
                }
            } catch (ClassCastException classcastexception) {
              throw new ReportedException(this.a(s, 9, classcastexception));
            }
        }
    
    Thats the code for getList. Seems like its checking if it is over 6, then, it calls more methods and creates a new one, I think. Hmmmm, confusion
     
  8. Offline

    Maurdekye

    xTrollxDudex The variable tag is returning null. Do I have to create the item's NBTTag?
     
  9. Offline

    calebbfmv

    Maurdekye
    You shouldn't, the only way for it to return null is for the ItemStack to be null
     
  10. Offline

    Maurdekye

    calebbfmv Well, i've done checks, and none of the items i'm calling it on are null, even if their NBTTagCompound is null.
     
  11. Offline

    xTrollxDudex

    Maurdekye
    Then you should just create a new NBTTagList, add a tag to it, and set it to CanPlaceOn tag.
     
  12. Offline

    Maurdekye

  13. Offline

    xTrollxDudex

    Maurdekye
    PHP:
    NBTTagList list = new NBtTagList();
    list.
    add(new NBTTagString("Tagged"));

    tag.set("CanPlaceOn", list);
     
    calebbfmv likes this.
  14. Offline

    Maurdekye

    xTrollxDudex I've replaced my method with this;
    Code:java
    1. public ItemStack makeVital(ItemStack item) {
    2. System.out.println(item.getType().name());
    3. CraftItemStack.asNMSCopy(item).setTag(new NBTTagCompound());
    4. NBTTagCompound tag = CraftItemStack.asNMSCopy(item).getTag();
    5. NBTTagList list = new NBTTagList();
    6. list.add(new NBTTagString("Vital"));
    7. tag.set("CanPlaceOn", list);
    8. return item;
    9. }

    And i'm getting an NPE on line 7. I'm fairly sure I didn't implement it correctly though.
     
  15. Offline

    xTrollxDudex

    Maurdekye
    Or create a new NBTTagCompound on its own and add a list, then set tag? The only reason it could NPE is if the list is null or the tag is null. Set only adds the NBTBase to a HashMap in NBTTagCompound...
     
  16. Offline

    Maurdekye

    xTrollxDudex I don't understand what you're talking about, and the tag is the null object; I checked. Is there a specific piece of code you can give me that doesn't throw NPE's ?
     
  17. Offline

    xTrollxDudex

    Maurdekye
    I haven't worked with NBT too often in the past, not sure now.
    PHP:
    NBTTagCompound tag = new NBTTagCompound();
    NBTTagList list = new NBTTagList();
    list.
    add(new NBTTagString("Vital"));
    tag.set("CanPlaceOn", list);
    CraftItemStack.asNMSCopy(item).setTag(tag);
     
  18. Offline

    Maurdekye

    xTrollxDudex Sorry for asking, but do you think you could repost new versions of your canUse and tagItem methods?
     
  19. Offline

    bobacadodl

Thread Status:
Not open for further replies.

Share This Page