Suppose in the player's pocket there are rocks, wood, sand that I want to check how many stones there should be
PHP: /** @var Player $player */ $cobble = 0; foreach ($player->getInventory()->getContents() as $item) { if($item->getId() === Item::COBBLESTONE) { $cobble += $item->getCount(); } }
Ok, but when it has about 128 stones, it sends outMessage, it will output about 2 lines 1 line 64 1 line 128. is there any way to accumulate it?
suppose in the player's pocket there are 2 stone stacks that when sending Message it sends two lines: line 1 is 64 stones line 2 is 128 stones. Is there a way to sendMessage 1 line?
The code @GamakCZ sent does exactly what you want. The combined count of all cobblestone blocks is stored in the $cobble variable. See, this would only send one message: PHP: /** @var Player $player */ $cobble = 0; foreach ($player->getInventory()->getContents() as $item) { if($item->getId() === Item::COBBLESTONE) { $cobble += $item->getCount(); } } $player->sendMessage("You have $cobble cobblestones in your inventory");