ItemEntity: implement partial itemstack pickups in the dumbest way possible

Given the various limitations and flexibilities posed by EntityItemPickupEvent, I settled on this as the simplest way to deal with the problem.

- EntityItemPickupEvent may have its destination inventory changed, so we can't cache the result of getAddableItemQuantity() to use after the event.
- The item itself may have changed, so even if we thought we could add some items before the change, we might not be able to afterwards.

Considering the above facts, it's better to just give the whole itemstack to EntityItemPickupEvent, and let plugins use getAddableItemQuantity() on their own to decide if their chosen inventory can accommodate the item or not.
If it can't, then we'll just drop it on the ground.
This also fixes a potential issue where plugins changing the item to a custom one might end up with their items and the actual items both just vanishing if the target inventory was full.
closes #4499
This commit is contained in:
Dylan K. Taylor 2021-10-17 22:37:49 +01:00
parent a794d24c81
commit c70b80c273
No known key found for this signature in database
GPG Key ID: 8927471A91CAFD3D

View File

@ -227,8 +227,8 @@ class ItemEntity extends Entity{
$item = $this->getItem();
$playerInventory = match(true){
$player->getOffHandInventory()->getItem(0)->canStackWith($item) and $player->getOffHandInventory()->canAddItem($item) => $player->getOffHandInventory(),
$player->getInventory()->canAddItem($item) => $player->getInventory(),
$player->getOffHandInventory()->getItem(0)->canStackWith($item) and $player->getOffHandInventory()->getAddableItemQuantity($item) > 0 => $player->getOffHandInventory(),
$player->getInventory()->getAddableItemQuantity($item) > 0 => $player->getInventory(),
default => null
};
@ -246,7 +246,12 @@ class ItemEntity extends Entity{
$viewer->getNetworkSession()->onPlayerPickUpItem($player, $this);
}
$ev->getInventory()?->addItem($ev->getItem());
$inventory = $ev->getInventory();
if($inventory !== null){
foreach($inventory->addItem($ev->getItem()) as $remains){
$this->getWorld()->dropItem($this->location, $remains, new Vector3(0, 0, 0));
}
}
$this->flagForDespawn();
}
}