So I have this for loop that fills a player's inventory with dirt. I want the first 9 dirt blocks to be in their hotbar. How would I achieve this?
I've personally created a few loops that order a players' hotbar. Here's an example: PHP: /** * Give a player an array of items and order them correctly in their hot bar * * @param Player $player * @param Item[] $items * @param bool $shouldCloneItems */ public static function giveItems(Player $player, array $items, $shouldCloneItems = false) { for($i = 0, $hotbarIndex = 0, $invIndex = 0, $inv = $player->getInventory(), $itemCount = count($items); $i < $itemCount; $i++, $invIndex++) { $inv->setItem($invIndex, ($shouldCloneItems ? clone $items[$i] : $items[$i])); if($hotbarIndex <= 9) { $inv->setHotbarSlotIndex($hotbarIndex, $invIndex); $hotbarIndex++; } continue; } $inv->sendContents($player); } The basic idea behind this function is that it takes an array of items and attempts to link the items in a player's inventory to the same index in their hotbar. The hotbar and inventory indexes both start at 0 and it attempts to fill the hotbar from there.
I'm not going to spoon feed you code up to date code, you gotta do something yourself. I copied and pasted this function from a plugin I made.
I looked through some old forum threads. You posted almost the same thing? I honestly have no idea since the code you gave me was the one I used to use until it stopped working in an update.
Look for the up to date/working method. @Muqsit has already provided you with a working function. Now you just need to put the pieces together yourself.
That is exactly why I asked on this thread. Eh... Everywhere I look, PlayerInventory::setItem has two function parameters, $index and $item. There is no $hotbarIndex.
The first 9 slots are supposed to link to the hotbar slots by default, for stuff like Win10 Edition. I pushed a fix for this to mcpe-1.0 yesterday evening.