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
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/
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.
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.
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 $carry, Item $item){ return $carry + $item->getCount();}, 0); Note: only for lovers of functional programming.
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)