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

Item name not setting

Discussion in 'Development' started by Junkdude, Dec 18, 2016.

  1. Junkdude

    Junkdude Zombie

    Messages:
    346
    GitHub:
    JunkDaCoder
    I'm making a custom enchants plugin but this code doesnt set the name, or change it at all, no errors btw
    PHP:
      public function onTap(PlayerInteractEvent $event){
        
    $p $event->getPlayer();
        
    $item $p->getInventory()->getItemInHand();
        
    $itemname1 $item->getCustomName();
        
    $newname $item->setCustomName("rekt");
        
    $name $event->getPlayer()->getName();
        if(
    $event->getBlock()->getId() == 1){
            
    $item->setCustomName($itemname1 "\n" $newName);
        }
      }
     
  2. Muqsit

    Muqsit Chicken

    Messages:
    1,548
    GitHub:
    muqsit
    I dont think its okay to define $item as that. Use this instead:
    PHP:
    $item $event->getItem();
    You need to update the item in hand whenever you add/modify an NBT of the item. If you enchant your item with /enchant, you'll see how the item bounces when its successfully enchanted. This is due to the $item's slot updating. You can update the item by adding this at the end.
    PHP:
    $p->getInventory()->setItemInHand($item);
    (Line stolen from Enchantment.php)
    The person below me explains it better...
     
    Last edited: Dec 18, 2016
    HimbeersaftLP likes this.
  3. SOFe

    SOFe Administrator Staff Member PMMP Team Poggit Admin

    Messages:
    1,968
    GitHub:
    sof3
    The $item you have got only represents the data in the item, but the item in the inventory itself. Calling setCustomName() etc. only manipulates the data in $item, but not the reference from the inventory's side. You have to setItemInHand() to apply the changes back to the inventory.
     
    HimbeersaftLP likes this.
  4. Junkdude

    Junkdude Zombie

    Messages:
    346
    GitHub:
    JunkDaCoder
    Ah, so it was just never updating?
     
  5. Junkdude

    Junkdude Zombie

    Messages:
    346
    GitHub:
    JunkDaCoder
    Nah still nothing
    PHP:
    public function onTap(PlayerInteractEvent $event){
        
    $p $event->getPlayer();
        
    $item $event->getItem();
        
    $itemname1 $item->getCustomName();
        
    $newname $item->setCustomName("rekt");
        if(
    $event->getBlock()->getId() == 1){
            if(
    $item->getId() == 276){
            
    $item->setCustomName("\nTest");
            
    $p->getInventory()->setItemInHand($item);
        }
      }
    }
     
  6. Junkdude

    Junkdude Zombie

    Messages:
    346
    GitHub:
    JunkDaCoder
    Full code, okay nothing happens. No error, the plugin is registered, just nothing happens...
    PHP:
    <?php
    namespace Junkdude;
    use 
    pocketmine\block\Block;
    use 
    pocketmine\event\player\PlayerInteractEvent;
    use 
    pocketmine\event\player\PlayerJoinEvent;
    use 
    pocketmine\inventory\ChestInventory;
    use 
    pocketmine\event\Listener;
    use 
    pocketmine\math\Vector3;
    use 
    pocketmine\nbt\NBT;
    use 
    pocketmine\plugin\PluginBase;
    use 
    SQLite3;
    use 
    pocketmine\Player;
    use 
    pocketmine\Server;

    class 
    Main extends PluginBase implements Listener{

    public function 
    onTap(PlayerInteractEvent $event){
        
    $p $event->getPlayer();
        
    $p->sendmessage("lit");
    }
    }
     
  7. Muqsit

    Muqsit Chicken

    Messages:
    1,548
    GitHub:
    muqsit
    PHP:
    public function onTap(PlayerInteractEvent $event){
        
    $p $event->getPlayer();
        
    $item $event->getItem();
        
    $item->setCustomName("rekt");
        if(
    $event->getBlock()->getId() === 1){
            if(
    $item->getId() === 276){
            
    $item->setCustomName($item->getName()."\nTest");
            
    $p->getInventory()->setItemInHand($item);
        }
      }
    }
     
  8. Junkdude

    Junkdude Zombie

    Messages:
    346
    GitHub:
    JunkDaCoder
    Even this doesnt work, its like the codeisntrunning , but if ido /pl its there
     
  9. SOFe

    SOFe Administrator Staff Member PMMP Team Poggit Admin

    Messages:
    1,968
    GitHub:
    sof3
    What the? Only two lines of code and you call it the whole plugin? This doesn't do anything at all...

    Make sure you registered events.
     
    VCraft and Muqsit like this.
  10. Junkdude

    Junkdude Zombie

    Messages:
    346
    GitHub:
    JunkDaCoder
    Yeah, i forgot to register it onEnable() sigh..
     
  11. Wii54

    Wii54 Silverfish

    Messages:
    16
    GitHub:
    wii54
    why not just $event->getPlayer()->getInventory->getItemInHand()->getNamedTag()->display->Name?
     
  12. Junkdude

    Junkdude Zombie

    Messages:
    346
    GitHub:
    JunkDaCoder
    How do i use this
     
  13. Wii54

    Wii54 Silverfish

    Messages:
    16
    GitHub:
    wii54
    If i remembered correctly, it should be like this
    PHP:
    $item->getNamedTag()->display->Name = new StringTag("Name","itemName");
    I might be wrong, so just try it out
     
  14. Wii54

    Wii54 Silverfish

    Messages:
    16
    GitHub:
    wii54
    And also $item should be $event->getItem()
     
  15. Wii54

    Wii54 Silverfish

    Messages:
    16
    GitHub:
    wii54
    also you need to set the item's NBT afterward
     
  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.