mirror of
https://github.com/pmmp/PocketMine-MP.git
synced 2025-04-21 08:17:34 +00:00
* 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:
parent
8e5aca70b4
commit
f6481eab8f
@ -30,6 +30,7 @@ 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\InventoryHolder;
|
||||
use pocketmine\inventory\PlayerInventory;
|
||||
use pocketmine\item\Consumable;
|
||||
@ -570,6 +571,9 @@ class Human extends Creature implements ProjectileSource, InventoryHolder{
|
||||
|
||||
$inventoryTag = $this->namedtag->getListTag("Inventory");
|
||||
if($inventoryTag !== null){
|
||||
$armorListener = $this->armorInventory->getEventProcessor();
|
||||
$this->armorInventory->setEventProcessor(null);
|
||||
|
||||
/** @var CompoundTag $item */
|
||||
foreach($inventoryTag as $i => $item){
|
||||
$slot = $item->getByte("Slot");
|
||||
@ -581,6 +585,8 @@ class Human extends Creature implements ProjectileSource, InventoryHolder{
|
||||
$this->inventory->setItem($slot - 9, Item::nbtDeserialize($item));
|
||||
}
|
||||
}
|
||||
|
||||
$this->armorInventory->setEventProcessor($armorListener);
|
||||
}
|
||||
|
||||
$enderChestInventoryTag = $this->namedtag->getListTag("EnderChestInventory");
|
||||
@ -593,6 +599,7 @@ class Human extends Creature implements ProjectileSource, InventoryHolder{
|
||||
|
||||
$this->inventory->setHeldItemIndex($this->namedtag->getInt("SelectedInventorySlot", 0), false);
|
||||
|
||||
$this->inventory->setEventProcessor(new EntityInventoryEventProcessor($this));
|
||||
|
||||
$this->setFood((float) $this->namedtag->getInt("foodLevel", (int) $this->getFood(), true));
|
||||
$this->setExhaustion($this->namedtag->getFloat("foodExhaustionLevel", $this->getExhaustion(), true));
|
||||
|
@ -31,6 +31,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\item\Armor;
|
||||
use pocketmine\item\Consumable;
|
||||
use pocketmine\item\enchantment\Enchantment;
|
||||
@ -76,6 +77,8 @@ abstract class Living extends Entity implements Damageable{
|
||||
parent::initEntity();
|
||||
|
||||
$this->armorInventory = new ArmorInventory($this);
|
||||
//TODO: load/save armor inventory contents
|
||||
$this->armorInventory->setEventProcessor(new ArmorInventoryEventProcessor($this));
|
||||
|
||||
$health = $this->getMaxHealth();
|
||||
|
||||
|
@ -24,13 +24,11 @@ declare(strict_types=1);
|
||||
namespace pocketmine\inventory;
|
||||
|
||||
use pocketmine\entity\Living;
|
||||
use pocketmine\event\entity\EntityArmorChangeEvent;
|
||||
use pocketmine\item\Item;
|
||||
use pocketmine\network\mcpe\protocol\InventoryContentPacket;
|
||||
use pocketmine\network\mcpe\protocol\InventorySlotPacket;
|
||||
use pocketmine\network\mcpe\protocol\MobArmorEquipmentPacket;
|
||||
use pocketmine\Player;
|
||||
use pocketmine\Server;
|
||||
|
||||
class ArmorInventory extends BaseInventory{
|
||||
public const SLOT_HEAD = 0;
|
||||
@ -90,15 +88,6 @@ class ArmorInventory extends BaseInventory{
|
||||
return $this->setItem(self::SLOT_FEET, $boots);
|
||||
}
|
||||
|
||||
protected function doSetItemEvents(int $index, Item $newItem) : ?Item{
|
||||
Server::getInstance()->getPluginManager()->callEvent($ev = new EntityArmorChangeEvent($this->getHolder(), $this->getItem($index), $newItem, $index));
|
||||
if($ev->isCancelled()){
|
||||
return null;
|
||||
}
|
||||
|
||||
return $ev->getNewItem();
|
||||
}
|
||||
|
||||
public function sendSlot(int $index, $target) : void{
|
||||
if($target instanceof Player){
|
||||
$target = [$target];
|
||||
|
47
src/pocketmine/inventory/ArmorInventoryEventProcessor.php
Normal file
47
src/pocketmine/inventory/ArmorInventoryEventProcessor.php
Normal file
@ -0,0 +1,47 @@
|
||||
<?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;
|
||||
use pocketmine\Server;
|
||||
|
||||
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{
|
||||
Server::getInstance()->getPluginManager()->callEvent($ev = new EntityArmorChangeEvent($this->entity, $oldItem, $newItem, $slot));
|
||||
if($ev->isCancelled()){
|
||||
return null;
|
||||
}
|
||||
|
||||
return $ev->getNewItem();
|
||||
}
|
||||
}
|
@ -45,6 +45,8 @@ abstract class BaseInventory implements Inventory{
|
||||
protected $slots = [];
|
||||
/** @var Player[] */
|
||||
protected $viewers = [];
|
||||
/** @var InventoryEventProcessor */
|
||||
protected $eventProcessor;
|
||||
|
||||
/**
|
||||
* @param Item[] $items
|
||||
@ -153,10 +155,6 @@ abstract class BaseInventory implements Inventory{
|
||||
$this->clearAll();
|
||||
}
|
||||
|
||||
protected function doSetItemEvents(int $index, Item $newItem) : ?Item{
|
||||
return $newItem;
|
||||
}
|
||||
|
||||
public function setItem(int $index, Item $item, bool $send = true) : bool{
|
||||
if($item->isNull()){
|
||||
$item = ItemFactory::get(Item::AIR, 0, 0);
|
||||
@ -164,14 +162,18 @@ abstract class BaseInventory implements Inventory{
|
||||
$item = clone $item;
|
||||
}
|
||||
|
||||
$newItem = $this->doSetItemEvents($index, $item);
|
||||
if($newItem === null){
|
||||
return false;
|
||||
$oldItem = $this->getItem($index);
|
||||
if($this->eventProcessor !== null){
|
||||
$newItem = $this->eventProcessor->onSlotChange($this, $index, $oldItem, $item);
|
||||
if($newItem === null){
|
||||
return false;
|
||||
}
|
||||
}else{
|
||||
$newItem = $item;
|
||||
}
|
||||
|
||||
$old = $this->getItem($index);
|
||||
$this->slots[$index] = $newItem->isNull() ? null : $newItem;
|
||||
$this->onSlotChange($index, $old, $send);
|
||||
$this->onSlotChange($index, $oldItem, $send);
|
||||
|
||||
return true;
|
||||
}
|
||||
@ -471,4 +473,12 @@ abstract class BaseInventory implements Inventory{
|
||||
public function slotExists(int $slot) : bool{
|
||||
return $slot >= 0 and $slot < $this->slots->getSize();
|
||||
}
|
||||
|
||||
public function getEventProcessor() : ?InventoryEventProcessor{
|
||||
return $this->eventProcessor;
|
||||
}
|
||||
|
||||
public function setEventProcessor(?InventoryEventProcessor $eventProcessor) : void{
|
||||
$this->eventProcessor = $eventProcessor;
|
||||
}
|
||||
}
|
||||
|
@ -28,28 +28,20 @@ use pocketmine\event\entity\EntityInventoryChangeEvent;
|
||||
use pocketmine\item\Item;
|
||||
use pocketmine\Server;
|
||||
|
||||
abstract class EntityInventory extends BaseInventory{
|
||||
class EntityInventoryEventProcessor implements InventoryEventProcessor{
|
||||
/** @var Entity */
|
||||
protected $holder;
|
||||
private $entity;
|
||||
|
||||
public function __construct(Entity $holder, array $items = [], int $size = null, string $title = null){
|
||||
$this->holder = $holder;
|
||||
parent::__construct($items, $size, $title);
|
||||
public function __construct(Entity $entity){
|
||||
$this->entity = $entity;
|
||||
}
|
||||
|
||||
protected function doSetItemEvents(int $index, Item $newItem) : ?Item{
|
||||
Server::getInstance()->getPluginManager()->callEvent($ev = new EntityInventoryChangeEvent($this->getHolder(), $this->getItem($index), $newItem, $index));
|
||||
public function onSlotChange(Inventory $inventory, int $slot, Item $oldItem, Item $newItem) : ?Item{
|
||||
Server::getInstance()->getPluginManager()->callEvent($ev = new EntityInventoryChangeEvent($this->entity, $oldItem, $newItem, $slot));
|
||||
if($ev->isCancelled()){
|
||||
return null;
|
||||
}
|
||||
|
||||
return $ev->getNewItem();
|
||||
}
|
||||
|
||||
/**
|
||||
* @return Entity
|
||||
*/
|
||||
public function getHolder(){
|
||||
return $this->holder;
|
||||
}
|
||||
}
|
@ -102,10 +102,4 @@ class FurnaceInventory extends ContainerInventory{
|
||||
public function setSmelting(Item $item) : bool{
|
||||
return $this->setItem(0, $item);
|
||||
}
|
||||
|
||||
public function onSlotChange(int $index, Item $before, bool $send) : void{
|
||||
parent::onSlotChange($index, $before, $send);
|
||||
|
||||
$this->getHolder()->scheduleUpdate();
|
||||
}
|
||||
}
|
||||
|
@ -247,4 +247,14 @@ interface Inventory{
|
||||
* @return bool
|
||||
*/
|
||||
public function slotExists(int $slot) : bool;
|
||||
|
||||
/**
|
||||
* @return null|InventoryEventProcessor
|
||||
*/
|
||||
public function getEventProcessor() : ?InventoryEventProcessor;
|
||||
|
||||
/**
|
||||
* @param null|InventoryEventProcessor $eventProcessor
|
||||
*/
|
||||
public function setEventProcessor(?InventoryEventProcessor $eventProcessor) : void;
|
||||
}
|
||||
|
48
src/pocketmine/inventory/InventoryEventProcessor.php
Normal file
48
src/pocketmine/inventory/InventoryEventProcessor.php
Normal file
@ -0,0 +1,48 @@
|
||||
<?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;
|
||||
}
|
@ -31,7 +31,7 @@ use pocketmine\network\mcpe\protocol\MobEquipmentPacket;
|
||||
use pocketmine\network\mcpe\protocol\types\ContainerIds;
|
||||
use pocketmine\Player;
|
||||
|
||||
class PlayerInventory extends EntityInventory{
|
||||
class PlayerInventory extends BaseInventory{
|
||||
|
||||
/** @var Human */
|
||||
protected $holder;
|
||||
@ -43,7 +43,8 @@ class PlayerInventory extends EntityInventory{
|
||||
* @param Human $player
|
||||
*/
|
||||
public function __construct(Human $player){
|
||||
parent::__construct($player);
|
||||
$this->holder = $player;
|
||||
parent::__construct();
|
||||
}
|
||||
|
||||
public function getName() : string{
|
||||
@ -210,5 +211,4 @@ class PlayerInventory extends EntityInventory{
|
||||
public function getHolder(){
|
||||
return $this->holder;
|
||||
}
|
||||
|
||||
}
|
||||
|
@ -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{
|
||||
|
Loading…
x
Reference in New Issue
Block a user