I'm trying to add item piece by piece but everything from victims inventory is added. How do i make it so that item is added one by one and not all at once PHP: $fighter = $ev->getDamager(); $victim = $ev->getEntity();foreach($victim->getInventory()->getContents() as $items){ $fighter->getInventory()->addItem($items);
Piece by piece may be a very expensive operation. If you could tell what you're trying to do, that will help. Spoiler: Piece by piece method PHP: while(!$item->isNull()){ $inventory->addItem($item->pop());}
I'm trying to make it so when I hit player some of their items come to my inventory and it removes it from the player inventory
I assume you are trying to add all items from the victim's inventory to the fighter's inventory and throw the items on the ground if the fighter's inventory gets full in the process. Correct? (P.S. reload the page, I added a spoiler to the first reply).
I don't want to add all of the items thats the thing I want to take it slot by slot if possible or randomly
Yes because you are not calling anything inside the for-loop or breaking it in any way? For randomization you can do something like this: PHP: $contents = $inventory->getContents();$slots = array_rand($contents, $numberOfItems);foreach($slots as $slot){ $fighter->getInventory()->addItem($contents[$slot]); $victim->getInventory()->clear($slot);}
PHP: $item = Item::get(0, 0, 1); $inv->setItem(0, $item); //sets the item of the first slot in the item bar to air (it is like removing it)
@Muqsit I think the OP wants the items to disappear from the victim's inventory and appear in the attacker's inventory one by one.