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

Setting (?) items in the hotbar

Discussion in 'Development' started by Legoboy0215, Dec 21, 2016.

  1. Legoboy0215

    Legoboy0215 Witch

    Messages:
    64
    GitHub:
    legoboy0215
    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?
     
  2. Jack Noordhuis

    Jack Noordhuis Zombie Pigman Poggit Reviewer

    Messages:
    618
    GitHub:
    JackNoordhuis
    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.
     
  3. Legoboy0215

    Legoboy0215 Witch

    Messages:
    64
    GitHub:
    legoboy0215
    2016-12-21_183257.png
     
  4. Muqsit

    Muqsit Chicken

    Messages:
    1,548
    GitHub:
    muqsit
    PHP:
    $inv->setItem($hotbarIndex$invIndex);
     
    Legoboy0215 likes this.
  5. Jack Noordhuis

    Jack Noordhuis Zombie Pigman Poggit Reviewer

    Messages:
    618
    GitHub:
    JackNoordhuis
    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.
     
  6. Legoboy0215

    Legoboy0215 Witch

    Messages:
    64
    GitHub:
    legoboy0215
    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.
     
  7. Jack Noordhuis

    Jack Noordhuis Zombie Pigman Poggit Reviewer

    Messages:
    618
    GitHub:
    JackNoordhuis
    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.
     
    Muqsit likes this.
  8. Legoboy0215

    Legoboy0215 Witch

    Messages:
    64
    GitHub:
    legoboy0215
    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.
     
  9. dktapps

    dktapps Administrator Staff Member PMMP Team

    Messages:
    774
    GitHub:
    dktapps
    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.
     
  10. Legoboy0215

    Legoboy0215 Witch

    Messages:
    64
    GitHub:
    legoboy0215
    Ah ok.
     
  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.