Replace InventoryPickup*Event with EntityItemPickupEvent (#4384)

The rationale here is that inventories don't actually pick items up - their holders do.
It's especially misleading to say that an inventory is picking up an item in creative mode when the picked-up item can't actually be added to the target inventory in the first place.

This change allows a range of new functionality, such as:
- Allowing survival players to pick items up even when their inventories are full, similarly to creative players
- Changing the destination inventory of collected items (e.g. items could be redirected to the offhand or ender chest inventory, while still allowing other plugins to understand what's happening)

As an added bonus, this obsoletes one more use case for Inventory->getHolder(), bringing us one step closer to removing the cyclic reference nightmare from inventories.

The choice of naming (EntityItemPickup, instead of EntityPickupItem) is to be consistent with other events, where the word order is SubjectObjectActionEvent.
This commit is contained in:
Rush2929
2021-08-21 06:59:35 +09:00
committed by GitHub
parent 34a7405820
commit eb9188c309
5 changed files with 97 additions and 102 deletions

View File

@ -28,8 +28,8 @@ use pocketmine\entity\animation\ArrowShakeAnimation;
use pocketmine\entity\Entity;
use pocketmine\entity\EntitySizeInfo;
use pocketmine\entity\Location;
use pocketmine\event\entity\EntityItemPickupEvent;
use pocketmine\event\entity\ProjectileHitEvent;
use pocketmine\event\inventory\InventoryPickupArrowEvent;
use pocketmine\item\VanillaItems;
use pocketmine\math\RayTraceResult;
use pocketmine\nbt\tag\CompoundTag;
@ -174,13 +174,15 @@ class Arrow extends Projectile{
}
$item = VanillaItems::ARROW();
$playerInventory = $player->getInventory();
if($player->hasFiniteResources() and !$playerInventory->canAddItem($item)){
return;
if(!$playerInventory->canAddItem($item)){
$playerInventory = null;
}
$ev = new InventoryPickupArrowEvent($playerInventory, $this);
$ev = new EntityItemPickupEvent($player, $this, $item, $playerInventory);
if($player->hasFiniteResources() and $playerInventory === null){
$ev->cancel();
}
if($this->pickupMode === self::PICKUP_NONE or ($this->pickupMode === self::PICKUP_CREATIVE and !$player->isCreative())){
$ev->cancel();
}
@ -194,7 +196,7 @@ class Arrow extends Projectile{
$viewer->getNetworkSession()->onPlayerPickUpItem($player, $this);
}
$playerInventory->addItem(clone $item);
$ev->getInventory()?->addItem($ev->getItem());
$this->flagForDespawn();
}