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

Solved Count Items

Comments in 'Plugin Development' started by JackboyPlay, Aug 6, 2016.

  1. JackboyPlay
    Offline

    JackboyPlay Active Member

    Joined:
    Apr 25, 2015
    Posts:
    507
    Minecraft User:
    JackboyPlay
    Hi, how can I count all items in my inventory? Example: I have 2 dirt 1 stone and 1 oak log in the inventory and the plugin count this and outputs 4
  2. EschieEsh
    Offline

    EschieEsh Active Member

    Joined:
    Jun 28, 2014
    Posts:
    69
    Minecraft User:
    Escherion
    PHP:
    $items_count 0;
    foreach(
    $player->getInventory()->getContents() as $item){
        
    $items_count $items_count $item->getCount();
    }
    $player->sendMessage("You have $items_count items in your inventory");
    ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    You can use docs.pocketmine.net to answer your question :

    http://docs.pocketmine.net/db/d39/i...entory.html#a7108a85a772dc4233fa35e0291e1ac64

    Or you can look at other threads. This has already been answered here :

    http://forums.pocketmine.net/threads/check-count-of-items-to-all-players.12686/
    Last edited: Aug 6, 2016
    MyNameIsTriXz and Survingo like this.
  3. AndrewBit
    Offline

    AndrewBit Notable Member

    Joined:
    Jun 18, 2015
    Posts:
    435
    Minecraft User:
    AndrewBit4
    Instead $something = $something + 2 you can also use $something += 2

    PHP:
    $items_count += $item->getCount();
    Anyway, as with
    PHP:
    $event->setCancelled();
    or
    PHP:
    $event->setCancelled(true);
    you can write whatever you feel more readable.
    Last edited: Aug 6, 2016
    Survingo likes this.
  4. EschieEsh
    Offline

    EschieEsh Active Member

    Joined:
    Jun 28, 2014
    Posts:
    69
    Minecraft User:
    Escherion
    Using x+=y is a good idea because it is shorter to type and by this way you avoid typing the same variable one more time. That reduces the chance of having a typo and I agree with this.

    But it doesn't really matter. People should always look at the line they are writing on. If they are not observant/careful, they will end up mistyping the variable regardless if it is x=x+y or x+=y. Also, it is not a big deal if they type the var one more time. So, at this point, it depends on the user's coding style.
  5. SOFe
    Offline

    SOFe Banned

    Joined:
    May 28, 2016
    Posts:
    386
    Minecraft User:
    Herobrine
    This is a matter of code convenience. Also increases readability. Compare:
    PHP:
    $this->getConfig()->getAll()["a"]["b"]["c"] = $this->getConfig()->getAll()["a"]["b"]["c"] + 2;
    $this->getConfig()->getAll()["a"]["b"]["c"] += 2;
    On the other hand, if you love functional programming, you can use array_map with array_sum:
    PHP:
    $count array_sum(array_map(function(Item $item){
      return 
    $item->getCount();
    }, 
    $inventory->getContents()));
    Or use array_reduce:
    PHP:
    $count array_reduce($inventory->getContents(), function(int $carryItem $item){
      return 
    $carry $item->getCount();
    }, 
    0);
    Note: only for lovers of functional programming.
    Last edited: Aug 7, 2016
    Survingo likes this.
  6. SOFe
    Offline

    SOFe Banned

    Joined:
    May 28, 2016
    Posts:
    386
    Minecraft User:
    Herobrine
    Yes, we all know that these are other ways to get/retrieve the count. Nobody said you are wrong. You don't need to explain that.
    (But if you use the word "receive", it would be misleading because "receive" in programming usually refers to receiving data through a stream, not the return value from a function)
    EschieEsh likes this.

Share This Page

Advertisement