Advertisement
  1. This site uses cookies. By continuing to use this site, you are agreeing to our use of cookies. Learn More.

Add An Item To The Players Hotbar

Comments in 'Plugin Development' started by Calrizer, Jul 10, 2015.

  1. Calrizer
    Offline

    Calrizer Active Member

    Joined:
    Jan 17, 2015
    Posts:
    102
    Minecraft User:
    callumdrain
    Hello,
    Today I'm wondering how to set an item given to the player into a given slot on the players hotbar.
    I know already how to add items into the players inventory, just not how to get the slot on the hot bar.
    I think it has to do with this function,
    PHP:
    use pocketmine/inventory/PlayerInventory;

    setHotbarSlotIndex()
    But I'm not sure how to implement it.

    Any feedback is appreciated,

    -Calrizer
  2. CraftYourBukkit
    Offline

    CraftYourBukkit Notable Member Plugin Developer

    Joined:
    Jan 20, 2015
    Posts:
    1,022
    Plugins:
    2
    Minecraft User:
    CraftYourBukkit
    PHP:
    $human->getInventory()->setHotbatSlotIndex();
    There's no need(/way) to implement it, it is a function.
  3. Calrizer
    Offline

    Calrizer Active Member

    Joined:
    Jan 17, 2015
    Posts:
    102
    Minecraft User:
    callumdrain
    No I think theres some confusion. Heres my real life example:
    I'm trying to give the player 'Item A' into the first slot and 'Item B' into the second.
  4. ahoj_truhliku
    Offline

    ahoj_truhliku Banned

    Joined:
    Jul 9, 2015
    Posts:
    89
    Minecraft User:
    Creeperface001
    player must have item in inventory and $index is item in inventory and $slot is hotbar slot
    PEMapModder likes this.
  5. PEMapModder
    Offline

    PEMapModder Notable Member Plugin Developer

    Joined:
    Oct 9, 2013
    Posts:
    7,294
    Plugins:
    11
    Minecraft User:
    PEMapModder
    The use statement is useless.
  6. Calrizer
    Offline

    Calrizer Active Member

    Joined:
    Jan 17, 2015
    Posts:
    102
    Minecraft User:
    callumdrain
    So I've been doing a few tests of this and I still can't get it to work, heres my code:
    PHP:
    $items = [
                
    Item::get(33201),
                
    Item::get(26701)
            ];

    $p->getInventory()->addItem($items[0]);
    $p->getInventory()->addItem($items[1]);
    $p->getInventory()->setHotbarSlotIndex(3321);
    $p->getInventory()->setHotbarSlotIndex(2672);
    Can any of you see something obviously wrong with it and maybe find a solution.
  7. CraftYourBukkit
    Offline

    CraftYourBukkit Notable Member Plugin Developer

    Joined:
    Jan 20, 2015
    Posts:
    1,022
    Plugins:
    2
    Minecraft User:
    CraftYourBukkit
    It is
    Inventory::setHotbarSlotIndex($index, $slot); so $index is the slot in the inventory, not the item id
    PEMapModder likes this.

Share This Page

Advertisement