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

Solved how to put an item to 5th slot

Discussion in 'Development' started by kazuya, Jun 8, 2017.

  1. kazuya

    kazuya Slime

    Messages:
    79
    GitHub:
    xdqrknez
    how to put an item to 5th slot using addItem()

    look at the picture
     

    Attached Files:

  2. Miste

    Miste Baby Zombie

    Messages:
    109
    GitHub:
    Misteboss
    you need to use setItem()
    Exemple :
    PHP:
    use pocketmine\item\Item;

    $slot 5//Slot of the hotbar
    $id 1//Id of the item (here cobblestone)
    $damage 0//Damage of the item (for most of items/blocks it's 0)
    $number 1//How much item do you want in this slot
    $item Item::get($id$damage$number); //Item in correct form

    $player->getInventory()->setItem($slot$item);
    PS : Sorry for my bad english
     
    Last edited: Oct 17, 2017
  3. SergeyIvanov

    SergeyIvanov Witch

    Messages:
    59
    GitHub:
    sergeyivanov14
    $player->getInventory()->setItem(4, $item);
     
    jasonwynn10 and corytortoise like this.
  4. kazuya

    kazuya Slime

    Messages:
    79
    GitHub:
    xdqrknez
    That goes inside the player’s inventory, not the bar slots that you see in screen
     
  5. DayKoala

    DayKoala Silverfish

    Messages:
    22
    GitHub:
    daykoala
    PHP:
     public function setItem($indexint $id$count$dmg$player){
          
       
    $item Item::get($id);
       
    $item->setDamage($dmg);
       
    $item->setCount($count);
       
    $player->setItem($index$item);
       }
       }
     
  6. Irish

    Irish Baby Zombie

    Messages:
    156
    GitHub:
    irishpacks
    That'd cause an error because of one too many right curly brackets. Here's a fixed version with presets :D
    PHP:
    public function setItem(int $indexint $id 0int $count 1$dmg 0Player $player) {
        
    $item Item::get($id$dmg$count);
        
    $player->setItem($index$item);
    }
    Anyways I believe that'd end up inside of the player's inventory, so use PlayerInventory::getHotbarSlotIndex() or what it'd look like as code $player->getInventory()->getHotbarSlotIndex()

    PlayerInventory::getHotbarSlotIndex() will return the inventory slot that the corresponding hotbar slot is.

    The new code would be:

    PHP:
    public function setItem(int $indexint $id 0int $count 1$dmg 0Player $player) {
        
    $item Item::get($id$dmg$count);
        
    $player->setItem($player->getInventory()->getHotbarSlotIndex($index), $item);
    }
     
    Last edited: Jun 8, 2017
    corytortoise and Sandertv like this.
  7. kazuya

    kazuya Slime

    Messages:
    79
    GitHub:
    xdqrknez
    I want to add the item to the player’s hot bar when they join though
     
  8. Irish

    Irish Baby Zombie

    Messages:
    156
    GitHub:
    irishpacks
    PHP:
    public function onJoin(PlayerJoinEvent $event) {
        
    $item Item::get(Item::DIAMOND_SWORD01);
        
    $player->getInventory()->setItem($player->getInventory()->getHotbarSlotIndex(4), $item);
    }
    Now was that so hard?
     
    Last edited: Jun 9, 2017
    kazuya likes this.
  9. kazuya

    kazuya Slime

    Messages:
    79
    GitHub:
    xdqrknez
    hmmmm
    PHP:
     Could not pass event 'pocketmine\event\player\PlayerJoinEvent' to 'Kaz v1.0.0'Call to undefined method pocketmine\Player::setItem() on Kaz\Main
    [17:54:14] [Server thread/CRITICAL]: Error"Call to undefined method pocketmine\Player::setItem()" (EXCEPTIONin "/Kaz/src/Kaz/Main" at line 309
     
  10. Sandertv

    Sandertv Zombie Pigman Poggit Reviewer

    Messages:
    786
    GitHub:
    Sandertv
    It should be $player->getInventory()->setItem(/*blablabla*/);
    instead of $player->setItem(/*blablabla*/);
     
    jasonwynn10 likes this.
  11. Junkdude

    Junkdude Zombie

    Messages:
    346
    GitHub:
    JunkDaCoder
    PHP:
    $player->getInventory()->setItem(4$item);
    EDIT: I didn't see someone had already posted this. My bad.
     
    Last edited: Jun 9, 2017
  12. corytortoise

    corytortoise Skeleton

    Messages:
    825
    GitHub:
    corytortoise
    Have you even read this thread? The inventory slot index 4 is not the 5th hotbar slot.
     
  13. Junkdude

    Junkdude Zombie

    Messages:
    346
    GitHub:
    JunkDaCoder
    I saw that as I was scrolling up my bad
     
  14. Irish

    Irish Baby Zombie

    Messages:
    156
    GitHub:
    irishpacks
    Oops didn't notice that I referenced a method from the PlayerInventory directly to the Player, sorry about that.
     
    jasonwynn10 likes this.
  15. Lowkey

    Lowkey Slime

    Messages:
    94
    If that doesn't work, try using sendContents(Player $player)
     
  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.