From dec6c9f49b1af99320564ca504bb842e9ab02443 Mon Sep 17 00:00:00 2001 From: "Dylan K. Taylor" Date: Sun, 19 May 2019 16:02:03 +0100 Subject: [PATCH] Removed EntityInventoryChangeEvent and EntityArmorChangeEvent there is nothing that these events do that can't be fulfilled by transactions. They complicate the internal implementation and produce unexpected behaviour for plugins when cancelled. TL;DR: Use transactions. That's what they are there for. --- src/pocketmine/entity/Human.php | 12 --- src/pocketmine/entity/Living.php | 11 --- .../event/entity/EntityArmorChangeEvent.php | 28 ------- .../entity/EntityInventoryChangeEvent.php | 81 ------------------- src/pocketmine/inventory/BaseInventory.php | 16 ++-- src/pocketmine/tile/Furnace.php | 3 +- 6 files changed, 7 insertions(+), 144 deletions(-) delete mode 100644 src/pocketmine/event/entity/EntityArmorChangeEvent.php delete mode 100644 src/pocketmine/event/entity/EntityInventoryChangeEvent.php diff --git a/src/pocketmine/entity/Human.php b/src/pocketmine/entity/Human.php index 9ef0df5ca..240d51b04 100644 --- a/src/pocketmine/entity/Human.php +++ b/src/pocketmine/entity/Human.php @@ -28,12 +28,10 @@ use pocketmine\entity\effect\EffectInstance; use pocketmine\entity\projectile\ProjectileSource; use pocketmine\entity\utils\ExperienceUtils; use pocketmine\event\entity\EntityDamageEvent; -use pocketmine\event\entity\EntityInventoryChangeEvent; use pocketmine\event\entity\EntityRegainHealthEvent; use pocketmine\event\player\PlayerExhaustEvent; use pocketmine\event\player\PlayerExperienceChangeEvent; use pocketmine\inventory\EnderChestInventory; -use pocketmine\inventory\Inventory; use pocketmine\inventory\InventoryHolder; use pocketmine\inventory\PlayerInventory; use pocketmine\item\Consumable; @@ -648,16 +646,6 @@ class Human extends Creature implements ProjectileSource, InventoryHolder{ $this->inventory->setHeldItemIndex($nbt->getInt("SelectedInventorySlot", 0), false); - $this->inventory->setSlotChangeListener(function(Inventory $inventory, int $slot, Item $oldItem, Item $newItem) : ?Item{ - $ev = new EntityInventoryChangeEvent($this, $oldItem, $newItem, $slot); - $ev->call(); - if($ev->isCancelled()){ - return null; - } - - return $ev->getNewItem(); - }); - $this->setFood((float) $nbt->getInt("foodLevel", (int) $this->getFood(), true)); $this->setExhaustion($nbt->getFloat("foodExhaustionLevel", $this->getExhaustion(), true)); $this->setSaturation($nbt->getFloat("foodSaturationLevel", $this->getSaturation(), true)); diff --git a/src/pocketmine/entity/Living.php b/src/pocketmine/entity/Living.php index 6605bca06..a1072cab0 100644 --- a/src/pocketmine/entity/Living.php +++ b/src/pocketmine/entity/Living.php @@ -26,7 +26,6 @@ namespace pocketmine\entity; use pocketmine\block\Block; use pocketmine\entity\effect\Effect; use pocketmine\entity\effect\EffectInstance; -use pocketmine\event\entity\EntityArmorChangeEvent; use pocketmine\event\entity\EntityDamageByChildEntityEvent; use pocketmine\event\entity\EntityDamageByEntityEvent; use pocketmine\event\entity\EntityDamageEvent; @@ -34,7 +33,6 @@ use pocketmine\event\entity\EntityDeathEvent; use pocketmine\event\entity\EntityEffectAddEvent; use pocketmine\event\entity\EntityEffectRemoveEvent; use pocketmine\inventory\ArmorInventory; -use pocketmine\inventory\Inventory; use pocketmine\item\Armor; use pocketmine\item\Consumable; use pocketmine\item\Durable; @@ -96,15 +94,6 @@ abstract class Living extends Entity implements Damageable{ $this->armorInventory = new ArmorInventory($this); //TODO: load/save armor inventory contents - $this->armorInventory->setSlotChangeListener(function(Inventory $inventory, int $slot, Item $oldItem, Item $newItem) : ?Item{ - $ev = new EntityArmorChangeEvent($this, $oldItem, $newItem, $slot); - $ev->call(); - if($ev->isCancelled()){ - return null; - } - - return $ev->getNewItem(); - }); $health = $this->getMaxHealth(); diff --git a/src/pocketmine/event/entity/EntityArmorChangeEvent.php b/src/pocketmine/event/entity/EntityArmorChangeEvent.php deleted file mode 100644 index 8d53832aa..000000000 --- a/src/pocketmine/event/entity/EntityArmorChangeEvent.php +++ /dev/null @@ -1,28 +0,0 @@ -entity = $entity; - $this->oldItem = $oldItem; - $this->newItem = $newItem; - $this->slot = $slot; - } - - /** - * Returns the inventory slot number affected by the event. - * @return int - */ - public function getSlot() : int{ - return $this->slot; - } - - /** - * Returns the item which will be in the slot after the event. - * @return Item - */ - public function getNewItem() : Item{ - return $this->newItem; - } - - /** - * @param Item $item - */ - public function setNewItem(Item $item) : void{ - $this->newItem = $item; - } - - /** - * Returns the item currently in the slot. - * @return Item - */ - public function getOldItem() : Item{ - return $this->oldItem; - } -} diff --git a/src/pocketmine/inventory/BaseInventory.php b/src/pocketmine/inventory/BaseInventory.php index ba2967f29..bb1b52ad0 100644 --- a/src/pocketmine/inventory/BaseInventory.php +++ b/src/pocketmine/inventory/BaseInventory.php @@ -134,18 +134,14 @@ abstract class BaseInventory implements Inventory{ } $oldItem = $this->getItem($index); - if($this->slotChangeListener !== null){ - $newItem = ($this->slotChangeListener)($this, $index, $oldItem, $item); - if($newItem === null){ - return false; - } - }else{ - $newItem = $item; - } - $this->slots[$index] = $newItem->isNull() ? null : $newItem; + $this->slots[$index] = $item->isNull() ? null : $item; $this->onSlotChange($index, $oldItem, $send); + if($this->slotChangeListener !== null){ + ($this->slotChangeListener)($this, $index); + } + return true; } @@ -440,7 +436,7 @@ abstract class BaseInventory implements Inventory{ public function setSlotChangeListener(?\Closure $eventProcessor) : void{ if($eventProcessor !== null){ - Utils::validateCallableSignature(function(Inventory $inventory, int $slot, Item $oldItem, Item $newItem) : ?Item{}, $eventProcessor); + Utils::validateCallableSignature(function(Inventory $inventory, int $slot) : void{}, $eventProcessor); } $this->slotChangeListener = $eventProcessor; } diff --git a/src/pocketmine/tile/Furnace.php b/src/pocketmine/tile/Furnace.php index ebf281c80..26ce225ab 100644 --- a/src/pocketmine/tile/Furnace.php +++ b/src/pocketmine/tile/Furnace.php @@ -60,9 +60,8 @@ class Furnace extends Spawnable implements InventoryHolder, Container, Nameable{ public function __construct(World $world, Vector3 $pos){ $this->inventory = new FurnaceInventory($this); - $this->inventory->setSlotChangeListener(function(Inventory $inventory, int $slot, Item $oldItem, Item $newItem) : ?Item{ + $this->inventory->setSlotChangeListener(function(Inventory $inventory, int $slot) : void{ $this->scheduleUpdate(); - return $newItem; }); parent::__construct($world, $pos);