mirror of
https://github.com/pmmp/PocketMine-MP.git
synced 2025-04-21 08:17:34 +00:00
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:
parent
34a7405820
commit
eb9188c309
@ -26,9 +26,9 @@ namespace pocketmine\entity\object;
|
||||
use pocketmine\entity\Entity;
|
||||
use pocketmine\entity\EntitySizeInfo;
|
||||
use pocketmine\entity\Location;
|
||||
use pocketmine\event\entity\EntityItemPickupEvent;
|
||||
use pocketmine\event\entity\ItemDespawnEvent;
|
||||
use pocketmine\event\entity\ItemSpawnEvent;
|
||||
use pocketmine\event\inventory\InventoryPickupItemEvent;
|
||||
use pocketmine\item\Item;
|
||||
use pocketmine\math\Vector3;
|
||||
use pocketmine\nbt\tag\CompoundTag;
|
||||
@ -227,12 +227,15 @@ class ItemEntity extends Entity{
|
||||
|
||||
$item = $this->getItem();
|
||||
$playerInventory = $player->getInventory();
|
||||
|
||||
if($player->hasFiniteResources() and !$playerInventory->canAddItem($item)){
|
||||
return;
|
||||
if(!$playerInventory->canAddItem($item)){
|
||||
$playerInventory = null;
|
||||
}
|
||||
|
||||
$ev = new EntityItemPickupEvent($player, $this, $item, $playerInventory);
|
||||
if($player->hasFiniteResources() and $playerInventory === null){
|
||||
$ev->cancel();
|
||||
}
|
||||
|
||||
$ev = new InventoryPickupItemEvent($playerInventory, $this);
|
||||
$ev->call();
|
||||
if($ev->isCancelled()){
|
||||
return;
|
||||
@ -242,7 +245,7 @@ class ItemEntity extends Entity{
|
||||
$viewer->getNetworkSession()->onPlayerPickUpItem($player, $this);
|
||||
}
|
||||
|
||||
$playerInventory->addItem(clone $item);
|
||||
$ev->getInventory()?->addItem($ev->getItem());
|
||||
$this->flagForDespawn();
|
||||
}
|
||||
}
|
||||
|
@ -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();
|
||||
}
|
||||
|
||||
|
80
src/event/entity/EntityItemPickupEvent.php
Normal file
80
src/event/entity/EntityItemPickupEvent.php
Normal file
@ -0,0 +1,80 @@
|
||||
<?php
|
||||
|
||||
/*
|
||||
*
|
||||
* ____ _ _ __ __ _ __ __ ____
|
||||
* | _ \ ___ ___| | _____| |_| \/ (_)_ __ ___ | \/ | _ \
|
||||
* | |_) / _ \ / __| |/ / _ \ __| |\/| | | '_ \ / _ \_____| |\/| | |_) |
|
||||
* | __/ (_) | (__| < __/ |_| | | | | | | | __/_____| | | | __/
|
||||
* |_| \___/ \___|_|\_\___|\__|_| |_|_|_| |_|\___| |_| |_|_|
|
||||
*
|
||||
* This program is free software: you can redistribute it and/or modify
|
||||
* it under the terms of the GNU Lesser General Public License as published by
|
||||
* the Free Software Foundation, either version 3 of the License, or
|
||||
* (at your option) any later version.
|
||||
*
|
||||
* @author PocketMine Team
|
||||
* @link http://www.pocketmine.net/
|
||||
*
|
||||
*
|
||||
*/
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace pocketmine\event\entity;
|
||||
|
||||
use pocketmine\entity\Entity;
|
||||
use pocketmine\event\Cancellable;
|
||||
use pocketmine\event\CancellableTrait;
|
||||
use pocketmine\inventory\Inventory;
|
||||
use pocketmine\item\Item;
|
||||
|
||||
/**
|
||||
* Called when an entity picks up an item, arrow, etc.
|
||||
* @phpstan-extends EntityEvent<Entity>
|
||||
*/
|
||||
class EntityItemPickupEvent extends EntityEvent implements Cancellable{
|
||||
use CancellableTrait;
|
||||
|
||||
public function __construct(
|
||||
Entity $collector,
|
||||
private Entity $origin,
|
||||
private Item $item,
|
||||
private ?Inventory $inventory
|
||||
){
|
||||
$this->entity = $collector;
|
||||
}
|
||||
|
||||
public function getOrigin() : Entity{
|
||||
return $this->origin;
|
||||
}
|
||||
|
||||
/**
|
||||
* Items to be received
|
||||
*/
|
||||
public function getItem() : Item{
|
||||
return clone $this->item;
|
||||
}
|
||||
|
||||
/**
|
||||
* Change the items to receive.
|
||||
*/
|
||||
public function setItem(Item $item) : void{
|
||||
$this->item = clone $item;
|
||||
}
|
||||
|
||||
/**
|
||||
* Inventory to which received items will be added.
|
||||
*/
|
||||
public function getInventory() : ?Inventory{
|
||||
return $this->inventory;
|
||||
}
|
||||
|
||||
/**
|
||||
* Change the inventory to which received items are added.
|
||||
*/
|
||||
public function setInventory(?Inventory $inventory) : void{
|
||||
$this->inventory = $inventory;
|
||||
}
|
||||
|
||||
}
|
@ -1,45 +0,0 @@
|
||||
<?php
|
||||
|
||||
/*
|
||||
*
|
||||
* ____ _ _ __ __ _ __ __ ____
|
||||
* | _ \ ___ ___| | _____| |_| \/ (_)_ __ ___ | \/ | _ \
|
||||
* | |_) / _ \ / __| |/ / _ \ __| |\/| | | '_ \ / _ \_____| |\/| | |_) |
|
||||
* | __/ (_) | (__| < __/ |_| | | | | | | | __/_____| | | | __/
|
||||
* |_| \___/ \___|_|\_\___|\__|_| |_|_|_| |_|\___| |_| |_|_|
|
||||
*
|
||||
* This program is free software: you can redistribute it and/or modify
|
||||
* it under the terms of the GNU Lesser General Public License as published by
|
||||
* the Free Software Foundation, either version 3 of the License, or
|
||||
* (at your option) any later version.
|
||||
*
|
||||
* @author PocketMine Team
|
||||
* @link http://www.pocketmine.net/
|
||||
*
|
||||
*
|
||||
*/
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace pocketmine\event\inventory;
|
||||
|
||||
use pocketmine\entity\projectile\Arrow;
|
||||
use pocketmine\event\Cancellable;
|
||||
use pocketmine\event\CancellableTrait;
|
||||
use pocketmine\inventory\Inventory;
|
||||
|
||||
class InventoryPickupArrowEvent extends InventoryEvent implements Cancellable{
|
||||
use CancellableTrait;
|
||||
|
||||
/** @var Arrow */
|
||||
private $arrow;
|
||||
|
||||
public function __construct(Inventory $inventory, Arrow $arrow){
|
||||
$this->arrow = $arrow;
|
||||
parent::__construct($inventory);
|
||||
}
|
||||
|
||||
public function getArrow() : Arrow{
|
||||
return $this->arrow;
|
||||
}
|
||||
}
|
@ -1,45 +0,0 @@
|
||||
<?php
|
||||
|
||||
/*
|
||||
*
|
||||
* ____ _ _ __ __ _ __ __ ____
|
||||
* | _ \ ___ ___| | _____| |_| \/ (_)_ __ ___ | \/ | _ \
|
||||
* | |_) / _ \ / __| |/ / _ \ __| |\/| | | '_ \ / _ \_____| |\/| | |_) |
|
||||
* | __/ (_) | (__| < __/ |_| | | | | | | | __/_____| | | | __/
|
||||
* |_| \___/ \___|_|\_\___|\__|_| |_|_|_| |_|\___| |_| |_|_|
|
||||
*
|
||||
* This program is free software: you can redistribute it and/or modify
|
||||
* it under the terms of the GNU Lesser General Public License as published by
|
||||
* the Free Software Foundation, either version 3 of the License, or
|
||||
* (at your option) any later version.
|
||||
*
|
||||
* @author PocketMine Team
|
||||
* @link http://www.pocketmine.net/
|
||||
*
|
||||
*
|
||||
*/
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace pocketmine\event\inventory;
|
||||
|
||||
use pocketmine\entity\object\ItemEntity;
|
||||
use pocketmine\event\Cancellable;
|
||||
use pocketmine\event\CancellableTrait;
|
||||
use pocketmine\inventory\Inventory;
|
||||
|
||||
class InventoryPickupItemEvent extends InventoryEvent implements Cancellable{
|
||||
use CancellableTrait;
|
||||
|
||||
/** @var ItemEntity */
|
||||
private $itemEntity;
|
||||
|
||||
public function __construct(Inventory $inventory, ItemEntity $itemEntity){
|
||||
$this->itemEntity = $itemEntity;
|
||||
parent::__construct($inventory);
|
||||
}
|
||||
|
||||
public function getItemEntity() : ItemEntity{
|
||||
return $this->itemEntity;
|
||||
}
|
||||
}
|
Loading…
x
Reference in New Issue
Block a user