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

Naming Items

Discussion in 'Development' started by RoyalMCPE, Dec 11, 2016.

  1. RoyalMCPE

    RoyalMCPE Slime

    Messages:
    87
    How would I rename Items by using NBT?
     
  2. InspectorGadget

    InspectorGadget Zombie Pigman

    Messages:
    462
    GitHub:
    InspectorGadget
    I would suggest using
    PHP:
    $item->setCustomName("Diamond Sword");
    // You can use \n to get to a new line!
     
  3. Aviv

    Aviv Baby Zombie

    Messages:
    156
    The easy way is
    but if you want to set the item name using nbt:
    PHP:
    $item Item::get($itemId$itemDamage$itemCount);
    $nbt = new CompoundTag("", [
    new 
    CompoundTag("display", [
    new 
    StringTag("Name""type your name here")
    ])
    ]);
    $item->setCompoundTag($nbt); // this will also remove any other nbt tags
    or
    PHP:
    $item Item::get($itemId$itemDamage$itemCountNBT::parseJSON('{display:{Name:"your name here"}}'));
     
  4. InspectorGadget

    InspectorGadget Zombie Pigman

    Messages:
    462
    GitHub:
    InspectorGadget
    How could you method be easy? My method is easy and also has the same outcome as your. I'm not sure if NBT has \n
     
  5. Aviv

    Aviv Baby Zombie

    Messages:
    156
    i showed him how to change item's name using nbt from all the ways, now he can choose what is easy or hard for him, and nbt has \n (i tried)
     
  6. Aviv

    Aviv Baby Zombie

    Messages:
    156
    and i even said that the easy way is yours, but he asked how the change the item's name using nbt.
     
  7. SOFe

    SOFe Administrator Staff Member PMMP Team Poggit Admin

    Messages:
    1,968
    GitHub:
    sof3
    Always prefer using API methods over directly editing the NBT so that plugins will continue to work even if the NBT format is somehow changed in some versions. NBT changes are not documented or guaranteed consistent in the API.
     
    OnTheVerge likes this.
  8. RoyalMCPE

    RoyalMCPE Slime

    Messages:
    87
    I would use setCustomName but you can't remove 1 item you have to remove the whole stack
     
  9. Noobie

    Noobie Silverfish

    Messages:
    16
    PHP:
    $item->setCount($item->getCount() - 1);
     
  10. Aviv

    Aviv Baby Zombie

    Messages:
    156
    then you need to update the item in the player's inventory
    correct way:
    PHP:
    $item $player->getInventory()->getItemInHand();
    $slot $player->getInventory()->getHeldItemSlot();
    $item->setCount($item->getCount() - 1);
    $player->getInventory()->clear($slot);
    $player->getInventory()->setItem($slot$item);
     
  11. Aviv

    Aviv Baby Zombie

    Messages:
    156
    You are right, i just answeared what i was asked :)
     
  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.