mirror of
https://github.com/pmmp/PocketMine-MP.git
synced 2025-10-19 04:15:04 +00:00
Remove InventoryEventProcessor, use closures instead
This commit is contained in:
@@ -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();
|
||||
}
|
||||
}
|
@@ -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;
|
||||
}
|
||||
}
|
||||
|
@@ -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();
|
||||
}
|
||||
}
|
@@ -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;
|
||||
}
|
||||
|
@@ -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;
|
||||
}
|
Reference in New Issue
Block a user