Remove InventoryEventProcessor, use closures instead

This commit is contained in:
Dylan K. Taylor 2018-12-04 19:02:26 +00:00
parent 05e968d9fd
commit b50e29085e
8 changed files with 44 additions and 174 deletions

View File

@ -26,11 +26,12 @@ namespace pocketmine\entity;
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\EntityInventoryEventProcessor;
use pocketmine\inventory\Inventory;
use pocketmine\inventory\InventoryHolder;
use pocketmine\inventory\PlayerInventory;
use pocketmine\item\Consumable;
@ -613,8 +614,8 @@ class Human extends Creature implements ProjectileSource, InventoryHolder{
$inventoryTag = $nbt->getListTag("Inventory");
if($inventoryTag !== null){
$armorListener = $this->armorInventory->getEventProcessor();
$this->armorInventory->setEventProcessor(null);
$armorListener = $this->armorInventory->getSlotChangeListener();
$this->armorInventory->setSlotChangeListener(null);
/** @var CompoundTag $item */
foreach($inventoryTag as $i => $item){
@ -628,7 +629,7 @@ class Human extends Creature implements ProjectileSource, InventoryHolder{
}
}
$this->armorInventory->setEventProcessor($armorListener);
$this->armorInventory->setSlotChangeListener($armorListener);
}
$enderChestInventoryTag = $nbt->getListTag("EnderChestInventory");
@ -641,7 +642,15 @@ class Human extends Creature implements ProjectileSource, InventoryHolder{
$this->inventory->setHeldItemIndex($nbt->getInt("SelectedInventorySlot", 0), false);
$this->inventory->setEventProcessor(new EntityInventoryEventProcessor($this));
$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));

View File

@ -24,6 +24,7 @@ declare(strict_types=1);
namespace pocketmine\entity;
use pocketmine\block\Block;
use pocketmine\event\entity\EntityArmorChangeEvent;
use pocketmine\event\entity\EntityDamageByChildEntityEvent;
use pocketmine\event\entity\EntityDamageByEntityEvent;
use pocketmine\event\entity\EntityDamageEvent;
@ -31,7 +32,7 @@ use pocketmine\event\entity\EntityDeathEvent;
use pocketmine\event\entity\EntityEffectAddEvent;
use pocketmine\event\entity\EntityEffectRemoveEvent;
use pocketmine\inventory\ArmorInventory;
use pocketmine\inventory\ArmorInventoryEventProcessor;
use pocketmine\inventory\Inventory;
use pocketmine\item\Armor;
use pocketmine\item\Consumable;
use pocketmine\item\Durable;
@ -79,7 +80,15 @@ abstract class Living extends Entity implements Damageable{
$this->armorInventory = new ArmorInventory($this);
//TODO: load/save armor inventory contents
$this->armorInventory->setEventProcessor(new ArmorInventoryEventProcessor($this));
$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();

View File

@ -1,47 +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\inventory;
use pocketmine\entity\Entity;
use pocketmine\event\entity\EntityArmorChangeEvent;
use pocketmine\item\Item;
class ArmorInventoryEventProcessor implements InventoryEventProcessor{
/** @var Entity */
private $entity;
public function __construct(Entity $entity){
$this->entity = $entity;
}
public function onSlotChange(Inventory $inventory, int $slot, Item $oldItem, Item $newItem) : ?Item{
$ev = new EntityArmorChangeEvent($this->entity, $oldItem, $newItem, $slot);
$ev->call();
if($ev->isCancelled()){
return null;
}
return $ev->getNewItem();
}
}

View File

@ -32,6 +32,7 @@ use pocketmine\network\mcpe\protocol\InventoryContentPacket;
use pocketmine\network\mcpe\protocol\InventorySlotPacket;
use pocketmine\network\mcpe\protocol\types\ContainerIds;
use pocketmine\Player;
use pocketmine\utils\Utils;
abstract class BaseInventory implements Inventory{
@ -45,8 +46,8 @@ abstract class BaseInventory implements Inventory{
protected $slots = [];
/** @var Player[] */
protected $viewers = [];
/** @var InventoryEventProcessor */
protected $eventProcessor;
/** @var \Closure */
protected $slotChangeListener;
/**
* @param Item[] $items
@ -163,8 +164,8 @@ abstract class BaseInventory implements Inventory{
}
$oldItem = $this->getItem($index);
if($this->eventProcessor !== null){
$newItem = $this->eventProcessor->onSlotChange($this, $index, $oldItem, $item);
if($this->slotChangeListener !== null){
$newItem = ($this->slotChangeListener)($this, $index, $oldItem, $item);
if($newItem === null){
return false;
}
@ -476,11 +477,14 @@ abstract class BaseInventory implements Inventory{
return $slot >= 0 and $slot < $this->slots->getSize();
}
public function getEventProcessor() : ?InventoryEventProcessor{
return $this->eventProcessor;
public function getSlotChangeListener() : ?\Closure{
return $this->slotChangeListener;
}
public function setEventProcessor(?InventoryEventProcessor $eventProcessor) : void{
$this->eventProcessor = $eventProcessor;
public function setSlotChangeListener(?\Closure $eventProcessor) : void{
if($eventProcessor !== null){
Utils::validateCallableSignature(function(Inventory $inventory, int $slot, Item $oldItem, Item $newItem) : ?Item{}, $eventProcessor);
}
$this->slotChangeListener = $eventProcessor;
}
}

View File

@ -1,47 +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\inventory;
use pocketmine\entity\Entity;
use pocketmine\event\entity\EntityInventoryChangeEvent;
use pocketmine\item\Item;
class EntityInventoryEventProcessor implements InventoryEventProcessor{
/** @var Entity */
private $entity;
public function __construct(Entity $entity){
$this->entity = $entity;
}
public function onSlotChange(Inventory $inventory, int $slot, Item $oldItem, Item $newItem) : ?Item{
$ev = new EntityInventoryChangeEvent($this->entity, $oldItem, $newItem, $slot);
$ev->call();
if($ev->isCancelled()){
return null;
}
return $ev->getNewItem();
}
}

View File

@ -250,12 +250,12 @@ interface Inventory{
public function slotExists(int $slot) : bool;
/**
* @return null|InventoryEventProcessor
* @return null|\Closure
*/
public function getEventProcessor() : ?InventoryEventProcessor;
public function getSlotChangeListener() : ?\Closure;
/**
* @param null|InventoryEventProcessor $eventProcessor
* @param \Closure|null $eventProcessor
*/
public function setEventProcessor(?InventoryEventProcessor $eventProcessor) : void;
public function setSlotChangeListener(?\Closure $eventProcessor) : void;
}

View File

@ -1,48 +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\inventory;
use pocketmine\item\Item;
/**
* This interface can be used to listen for events on a specific Inventory.
*
* If you want to listen to changes on an inventory, create a class implementing this interface and implement its
* methods, then register it onto the inventory or inventories that you want to receive events for.
*/
interface InventoryEventProcessor{
/**
* Called prior to a slot in the given inventory changing. This is called by inventories that this listener is
* attached to.
*
* @param Inventory $inventory
* @param int $slot
* @param Item $oldItem
* @param Item $newItem
*
* @return Item|null that should be used in place of $newItem, or null if the slot change should not proceed.
*/
public function onSlotChange(Inventory $inventory, int $slot, Item $oldItem, Item $newItem) : ?Item;
}

View File

@ -29,7 +29,6 @@ use pocketmine\event\inventory\FurnaceSmeltEvent;
use pocketmine\inventory\FurnaceInventory;
use pocketmine\inventory\FurnaceRecipe;
use pocketmine\inventory\Inventory;
use pocketmine\inventory\InventoryEventProcessor;
use pocketmine\inventory\InventoryHolder;
use pocketmine\item\Item;
use pocketmine\item\ItemFactory;
@ -73,18 +72,9 @@ class Furnace extends Spawnable implements InventoryHolder, Container, Nameable{
$this->inventory = new FurnaceInventory($this);
$this->loadItems($nbt);
$this->inventory->setEventProcessor(new class($this) implements InventoryEventProcessor{
/** @var Furnace */
private $furnace;
public function __construct(Furnace $furnace){
$this->furnace = $furnace;
}
public function onSlotChange(Inventory $inventory, int $slot, Item $oldItem, Item $newItem) : ?Item{
$this->furnace->scheduleUpdate();
return $newItem;
}
$this->inventory->setSlotChangeListener(function(Inventory $inventory, int $slot, Item $oldItem, Item $newItem) : ?Item{
$this->scheduleUpdate();
return $newItem;
});
}