Implemented an InventoryEventProcessor, fixes #1986 (#2176)

* Implemented InventoryEventProcessor, fixes #1986
Event processors can now be registered and unregistered at will. Entity inventory/armor change events are now handled by event processors instead of the inventories themselves, which allows enabling/disabling the calling of these events at will.
This now avoids stupid things happening when initializing inventory contents, since the callers for those events are now registered _after_ the contents are initialized.
This commit is contained in:
Dylan K. Taylor
2018-06-09 17:37:10 +01:00
committed by GitHub
parent 8e5aca70b4
commit f6481eab8f
11 changed files with 159 additions and 43 deletions

View File

@ -29,6 +29,8 @@ use pocketmine\event\inventory\FurnaceBurnEvent;
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;
@ -79,6 +81,20 @@ 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;
}
});
}
protected function writeSaveData(CompoundTag $nbt) : void{