1. The forums will be archived and moved to a read only mode in about 2 weeks (mid march).

Remove NBT (NamedTag) from an item.

Discussion in 'Development' started by Muqsit, Dec 30, 2016.

  1. Muqsit

    Muqsit Chicken

    Messages:
    1,548
    GitHub:
    muqsit
    Hey there,
    I would like to remove an NBT tag from an item.
    PHP:
    //Item $item
    $tag = new CompoundTag("", []);
    $tag->ench = new ListTag('ench', []);
    $item->setNamedTag($tag);
    That was my code to make an item *shine* without enchants.
    Now how do I remove the tag "ench" from $item?
    I have tried...
    PHP:
    unset($item->getNamedTag()->ench);
    Player::getInventory()->setItemInHand($item);
    //unset because you can check if item had the ench tag with isset($item->getNamedTag()->ench); :3
     
  2. GamakCZ

    GamakCZ Zombie Pigman

    Messages:
    598
    GitHub:
    GamakCZ
    Bite item to remove and re-add.

    PHP:
    Player::getInventory()->removeItem($item1);
    Player::getInventory()->addItem($slot$item2);
     
  3. Thunder33345

    Thunder33345 Moderator Staff Member

    Messages:
    2,137
    GitHub:
    Thunder33345
    I would assume something like set it to null or a new empty list tag
     
    Muqsit likes this.
  4. Muqsit

    Muqsit Chicken

    Messages:
    1,548
    GitHub:
    muqsit
    Player::getInventory()->setItemInHand($item);
    Is better!
     
    GamakCZ likes this.
  5. Jack Noordhuis

    Jack Noordhuis Zombie Pigman Poggit Reviewer

    Messages:
    618
    GitHub:
    JackNoordhuis
    Just set the items named tag to an empty compound tag or call Item::clearNamedTag().
    PHP:
    $item->clearNamedTag();
    // or
    $item->setNamedTag(new CompoundTag("", []));
     
  6. Muqsit

    Muqsit Chicken

    Messages:
    1,548
    GitHub:
    muqsit
    Thanks a lot.
    I was actually right, but thank you for that. I was about to search for clearing the nbts.
    Actually unset() does work. I just forgot to setNamedTag() again.

    For the future visitors..
    PHP:
    $nbt $item->getNamedTag();
    unset(
    $nbt->ench);
    $item->setNamedTag($nbt);
     
    Skullex likes this.
  1. This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
    By continuing to use this site, you are consenting to our use of cookies.