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

getCount() error

Discussion in 'Plugin Help' started by M4nt0s, Jan 23, 2019.

  1. M4nt0s

    M4nt0s Spider Jockey

    Messages:
    31
    GitHub:
    M4nt0s
    Code:
        $items_count = 0;
    foreach($event->getPlayer()->getInventory()->getContents() as $item){
        $items_count = $items_count + $item->getCount();
    }
    $event->getPlayer()->sendMessage("You have $items_count items in your inventory");

    Error: Server thread/CRITICAL Error: "Call to a member function getCount() on array" (EXCEPTION)
    Can't see any error. Thx for help.
     
  2. Diduhless

    Diduhless Baby Zombie

    Messages:
    199
    GitHub:
    Diduhless
    There's no need of a loop for that, you can just use this:
    PHP:
    $items_count count($event->getPlayer()->getInventory()->getContents());
     
  3. wolfdale

    wolfdale Zombie Pigman

    Messages:
    535
    GitHub:
    diamond-gold
    That does a different thing from what the OP wants
    OP wanted to count the total number of items not the number of stacks

    The code you posted does not seem to be the one causing the error as $item will not be an array
     
    Marabou and corytortoise like this.
  4. Diduhless

    Diduhless Baby Zombie

    Messages:
    199
    GitHub:
    Diduhless
    Is the $item variable overwritten?
     
  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.