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
PHP: public function setItem($index, int $id, $count, $dmg, $player){ $item = Item::get($id); $item->setDamage($dmg); $item->setCount($count); $player->setItem($index, $item); } }
That'd cause an error because of one too many right curly brackets. Here's a fixed version with presets PHP: public function setItem(int $index, int $id = 0, int $count = 1, $dmg = 0, Player $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 $index, int $id = 0, int $count = 1, $dmg = 0, Player $player) { $item = Item::get($id, $dmg, $count); $player->setItem($player->getInventory()->getHotbarSlotIndex($index), $item);}
PHP: public function onJoin(PlayerJoinEvent $event) { $item = Item::get(Item::DIAMOND_SWORD, 0, 1); $player->getInventory()->setItem($player->getInventory()->getHotbarSlotIndex(4), $item);} Now was that so hard?
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()" (EXCEPTION) in "/Kaz/src/Kaz/Main" at line 309
It should be $player->getInventory()->setItem(/*blablabla*/); instead of $player->setItem(/*blablabla*/);
PHP: $player->getInventory()->setItem(4, $item); EDIT: I didn't see someone had already posted this. My bad.
Oops didn't notice that I referenced a method from the PlayerInventory directly to the Player, sorry about that.