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

How do you properly set NBT

Discussion in 'Development' started by Thunder33345, Feb 27, 2017.

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

    Thunder33345 Moderator Staff Member

    Messages:
    2,137
    GitHub:
    Thunder33345
    How do you properly set NBT, add NBT, remove NBT, get NBT, verify NBT
    since people are still using name to verify things like: if(itemname equals banhammer)ban($damaged); as an very bad example
    so i figured why not i ask this question for other people who want to store data in item name
    and we can use this thread as a resource and future references for new dev who want to store data
     
    Levi and OnTheVerge like this.
  2. Muqsit

    Muqsit Chicken

    Messages:
    1,548
    GitHub:
    muqsit
    Adding NBT tag "test" (string), which has a value of "hey".
    PHP:
    /** @var Item $item */
    $nbt $item->getNamedTag() ?? new CompoundTag("", []);
    $nbt->test = new StringTag("test""hey");
    $item->setNamedTag($nbt);
    If item has NBT tag "test" (string), do something.
    PHP:
    /** @var Item $item */
    if (isset($item->getNamedTag()->test)) {
        
    //$item has NBT tag: test.
        /** @var string $test */
        
    $test $item->getNamedTag()->test;//"hey"
    }
    Adding NBT tag "test" (integer).
    PHP:
    /** @var Item $item */
    $nbt $item->getNamedTag() ?? new CompoundTag("", []);
    $nbt->test = new IntTag("test"69);
    $item->setNamedTag($nbt);
    If item has NBT tag "test" (integer), do something.
    PHP:
    /** @var Item $item */
    if (isset($item->getNamedTag()->test)) {
        
    //$item has NBT tag: test.
        /** @var int $test */
        
    $test $item->getNamedTag()->test->getValue();//69
    }
    Unsetting/Removing NBT tag "test".
    PHP:
    /** @var Item $item */
    $nbt $item->getNamedTag() ?? new CompoundTag("", []);
    unset(
    $nbt->test);
    $item->setNamedTag($nbt);
    If the $item was in a (Player)$player's hand, you must update it after interfering with the NBT tag by...
    PHP:
    $player->getInventory()->setItemInHand($item);
     
    Last edited: May 12, 2017
    artulloss, Levi, Teamblocket and 7 others like this.
  3. Muqsit

    Muqsit Chicken

    Messages:
    1,548
    GitHub:
    muqsit
    For saving an array of integers to NBT, use IntArrayTag
    PHP:
    /** @var Item $item */
    $nbt $item->getNamedTag() ?? new CompoundTag("", []);
    $nbt->testarray = new IntArrayTag("testarray", [0831]);
    $item->setNamedTag($nbt);

    /** Checking if item has "testarray" tag. */
    $nbt $item->getNamedTag() ?? new CompoundTag("", []);
    if (isset(
    $nbt->testarray)) {
        
    $array $nbt->testarray->getValue();//array(0, 8, 3, 1)
    }

    You can also customize entity's and tile's NBT.
    PHP:
    /** @var Tile|Entity $xyz */
    $xyz->namedtag->test = new StringTag("test""hey");
     
    Last edited: May 12, 2017
  4. Thunder33345

    Thunder33345 Moderator Staff Member

    Messages:
    2,137
    GitHub:
    Thunder33345
    Thanks a lot to responding my question few more question
    out of curiosity what would happen if $nbt->test = new StringTag("notMatchedToTest", "hey"); will it be assigned to $nbt->notMatchedToTest or to be invalidated?
    what about other types of tag then https://github.com/pmmp/PocketMine-MP/tree/master/src/pocketmine/nbt/tag
     
  5. Muqsit

    Muqsit Chicken

    Messages:
    1,548
    GitHub:
    muqsit
    Hmm, haven't tried that. But since argument 0 describes the name for the tag, the mismatch wont allow you to access it in any way.

    The rest of the tags are what their class name describes.
    Byte tag => Setting a byte value.
    Float tag => Setting a float int value.
    I really don't know what EndTag is used for, though.
    Refer to https://docs.oracle.com/javase/tutorial/java/nutsandbolts/datatypes.html for information on data types.
     
    jasonwynn10 likes this.
  6. Az928

    Az928 Baby Zombie

    Messages:
    140
    GitHub:
    theaz928
    Doesn't work for some reason, here's my code
    PHP:
    $nbt $inHand->getNamedTag() ?? new CompoundTag("", []);
                               
    $nbt->test = new StringTag("Test""Hi!");
                             
    $inHand->setNamedTag($nbt);
                             
    $inv->setItemInHand($inHand);
    No errors also, but the tag doesn't appear on the item
     
  7. Muqsit

    Muqsit Chicken

    Messages:
    1,548
    GitHub:
    muqsit
    It must be new StringTag("test", "Hi!");
    :facepalm:
     
    Levi likes this.
  8. Az928

    Az928 Baby Zombie

    Messages:
    140
    GitHub:
    theaz928
    Ah my bad :facepalm: , thanks for help
     
  9. Muqsit

    Muqsit Chicken

    Messages:
    1,548
    GitHub:
    muqsit
    Actually, keep the $nbt->test as is, change the tag to StringTag("test", "Hi!");
     
  10. Az928

    Az928 Baby Zombie

    Messages:
    140
    GitHub:
    theaz928
    Still not working muq :/
     
  11. Muqsit

    Muqsit Chicken

    Messages:
    1,548
    GitHub:
    muqsit
    Send the code
     
  12. Az928

    Az928 Baby Zombie

    Messages:
    140
    GitHub:
    theaz928
    PHP:
     if($inHand->getId() == 276){
                            if(
    $inv->contains(Item::get(388,0,20))){
                               
    //$inHand->setCustomName($inHand->getName()."\n§0§r§7Bless I ");
                               //$inHand->addEnchantment(Enchantment::getEnchantment(12));
                               
    $nbt $inHand->getNamedTag() ?? new CompoundTag("", []);
                               
    $nbt->test = new StringTag("test""Hi!");
                             
    $inHand->setNamedTag($nbt);
                             
    $inv->setItemInHand($inHand);
                               
    $inv->removeItem(Item::get(388,0,20));
                               
    $sender->sendMessage("§6>§a Combined successfully! Relog if you can't see the tag.");
                               
    //$sender->getInventory()->setItemInHand($inHand);
                              
    return true;
     
  13. Muqsit

    Muqsit Chicken

    Messages:
    1,548
    GitHub:
    muqsit
    That should work. How do you know that it doesn't work?
     
  14. Az928

    Az928 Baby Zombie

    Messages:
    140
    GitHub:
    theaz928
    When I run the command and hold the item, everything works fine but the tag doesn't appear when u hold the item...
     
  15. Muqsit

    Muqsit Chicken

    Messages:
    1,548
    GitHub:
    muqsit
    It's a custom NBT tag. It doesn't "appear" on items.
     
  16. Az928

    Az928 Baby Zombie

    Messages:
    140
    GitHub:
    theaz928
    Then how to set nbt that appears?
     
  17. Thunder33345

    Thunder33345 Moderator Staff Member

    Messages:
    2,137
    GitHub:
    Thunder33345
    only the name tag appears
    most are only internal and detectable by plugins
     
    jasonwynn10 and Muqsit like this.
  18. Muqsit

    Muqsit Chicken

    Messages:
    1,548
    GitHub:
    muqsit
    You can't use custom NBTs for this purpose. NBTs are used to store data.
     
  19. Az928

    Az928 Baby Zombie

    Messages:
    140
    GitHub:
    theaz928
    Ok how to use multiple string tags at once cuz it doesn't work if I do that manually/in ur way.... I may sound nooby :facepalm:
     
  20. jasonwynn10

    jasonwynn10 Moderator Poggit Reviewer

    Messages:
    1,489
    GitHub:
    jasonwynn10
    Are you just looking to set an item's name label?
     
    Levi and Lewis Brindley like this.
Thread Status:
Not open for further replies.
  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.