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

Items in player inventory

Discussion in 'Development' started by GamakCZ, Jan 24, 2017.

  1. GamakCZ

    GamakCZ Zombie Pigman

    Messages:
    598
    GitHub:
    GamakCZ
    How do I get a number of tools in the player's inventory?
     
  2. wolfdale

    wolfdale Zombie Pigman

    Messages:
    535
    GitHub:
    diamond-gold
    PHP:
    $tools 0;
    foreach(
    $player->getInventory()->getContents() as $item)
        if(
    $item instanceof \pocketmine\item\Tool$tools++;
    #do smth with $tools
     
  3. kaliiks

    kaliiks Zombie

    Messages:
    250
    PHP:
    foreach($p->getInventory()->getContents() as $tools){
    $tools->getCount();
     
    GamakCZ likes this.
  4. Redux

    Redux Spider Jockey

    Messages:
    49
    GitHub:
    reduxredstone
    This does not solve the problem. OP asked to get the number of all the tools in the inventory, not to get the count of each item. Your suggestion loops through each item in the inventory and gets it's count regardless of item type. The solution by wolfdale is the correct one, as it counts only tools.
     
  5. Muqsit

    Muqsit Chicken

    Messages:
    1,548
    GitHub:
    muqsit
    PHP:
    $opt 0;
    /** @var Player $player */
    foreach ($player->getInventory()->getContents() as $item) {
        if (
    $item instanceof Tool$opt += $item->getCount();
    }
    //return $opt;
     
  6. Jack Noordhuis

    Jack Noordhuis Zombie Pigman Poggit Reviewer

    Messages:
    618
    GitHub:
    JackNoordhuis
    It's useless calling Item::getCount() due to all tools having a max stack size of one.
     
    jasonwynn10 and Redux like this.
  7. Muqsit

    Muqsit Chicken

    Messages:
    1,548
    GitHub:
    muqsit
    You can force stack an item up to "99+" in PE.
     
    jasonwynn10 likes this.
  8. Jack Noordhuis

    Jack Noordhuis Zombie Pigman Poggit Reviewer

    Messages:
    618
    GitHub:
    JackNoordhuis
    In PE yes, in PocketMine, no. The server handles the inventory, unless you overwrite the item and increase the max stack size you will never be able to have multiple tools, even of the same type, in one slot.
     
    Last edited: Jan 25, 2017
  9. Muqsit

    Muqsit Chicken

    Messages:
    1,548
    GitHub:
    muqsit
    You sure? EssentialsPE's /i (itemid) command can do that.
    wxw.PNG
     
  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.