This commit is contained in:
Dylan K. Taylor 2020-03-09 14:39:02 +00:00
commit 2a97c7032e
3 changed files with 27 additions and 16 deletions

View File

@ -90,6 +90,8 @@ abstract class BaseInventory implements Inventory{
$items = array_slice($items, 0, $this->getSize(), true); $items = array_slice($items, 0, $this->getSize(), true);
} }
$oldContents = $this->slots->toArray();
$listeners = $this->listeners; $listeners = $this->listeners;
$this->listeners = []; $this->listeners = [];
$viewers = $this->viewers; $viewers = $this->viewers;
@ -109,7 +111,7 @@ abstract class BaseInventory implements Inventory{
} }
foreach($this->listeners as $listener){ foreach($this->listeners as $listener){
$listener->onContentChange($this); $listener->onContentChange($this, $oldContents);
} }
foreach($this->getViewers() as $viewer){ foreach($this->getViewers() as $viewer){
@ -359,7 +361,7 @@ abstract class BaseInventory implements Inventory{
protected function onSlotChange(int $index, Item $before) : void{ protected function onSlotChange(int $index, Item $before) : void{
foreach($this->listeners as $listener){ foreach($this->listeners as $listener){
$listener->onSlotChange($this, $index); $listener->onSlotChange($this, $index, $before);
} }
foreach($this->viewers as $viewer){ foreach($this->viewers as $viewer){
$viewer->getNetworkSession()->getInvManager()->syncSlot($this, $index); $viewer->getNetworkSession()->getInvManager()->syncSlot($this, $index);

View File

@ -23,6 +23,7 @@ declare(strict_types=1);
namespace pocketmine\inventory; namespace pocketmine\inventory;
use pocketmine\item\Item;
use pocketmine\utils\Utils; use pocketmine\utils\Utils;
class CallbackInventoryChangeListener implements InventoryChangeListener{ class CallbackInventoryChangeListener implements InventoryChangeListener{
@ -31,25 +32,25 @@ class CallbackInventoryChangeListener implements InventoryChangeListener{
/** /**
* @var \Closure|null * @var \Closure|null
* @phpstan-var (\Closure(Inventory, int) : void)|null * @phpstan-var (\Closure(Inventory, int, Item) : void)|null
*/ */
private $onSlotChangeCallback; private $onSlotChangeCallback;
/** /**
* @var \Closure|null * @var \Closure|null
* @phpstan-var (\Closure(Inventory) : void)|null * @phpstan-var (\Closure(Inventory, Item[]) : void)|null
*/ */
private $onContentChangeCallback; private $onContentChangeCallback;
/** /**
* @phpstan-param (\Closure(Inventory, int) : void)|null $onSlotChange * @phpstan-param (\Closure(Inventory, int, Item) : void)|null $onSlotChange
* @phpstan-param (\Closure(Inventory) : void)|null $onContentChange * @phpstan-param (\Closure(Inventory, Item[]) : void)|null $onContentChange
*/ */
public function __construct(?\Closure $onSlotChange, ?\Closure $onContentChange){ public function __construct(?\Closure $onSlotChange, ?\Closure $onContentChange){
if($onSlotChange !== null){ if($onSlotChange !== null){
Utils::validateCallableSignature(function(Inventory $inventory, int $slot) : void{}, $onSlotChange); Utils::validateCallableSignature(function(Inventory $inventory, int $slot, Item $oldItem) : void{}, $onSlotChange);
} }
if($onContentChange !== null){ if($onContentChange !== null){
Utils::validateCallableSignature(function(Inventory $inventory) : void{}, $onContentChange); Utils::validateCallableSignature(function(Inventory $inventory, array $oldContents) : void{}, $onContentChange);
} }
$this->onSlotChangeCallback = $onSlotChange; $this->onSlotChangeCallback = $onSlotChange;
@ -61,20 +62,23 @@ class CallbackInventoryChangeListener implements InventoryChangeListener{
*/ */
public static function onAnyChange(\Closure $onChange) : self{ public static function onAnyChange(\Closure $onChange) : self{
return new self( return new self(
static function(Inventory $inventory, int $unused) use ($onChange) : void{ static function(Inventory $inventory, int $unused, Item $unusedB) use ($onChange) : void{
$onChange($inventory); $onChange($inventory);
}, },
static function(Inventory $inventory) use ($onChange) : void{ static function(Inventory $inventory, array $unused) use ($onChange) : void{
$onChange($inventory); $onChange($inventory);
} }
); );
} }
public function onSlotChange(Inventory $inventory, int $slot) : void{ public function onSlotChange(Inventory $inventory, int $slot, Item $oldItem) : void{
($this->onSlotChangeCallback)($inventory, $slot); ($this->onSlotChangeCallback)($inventory, $slot, $oldItem);
} }
public function onContentChange(Inventory $inventory) : void{ /**
($this->onContentChangeCallback)($inventory); * @param Item[] $oldContents
*/
public function onContentChange(Inventory $inventory, array $oldContents) : void{
($this->onContentChangeCallback)($inventory, $oldContents);
} }
} }

View File

@ -23,6 +23,8 @@ declare(strict_types=1);
namespace pocketmine\inventory; namespace pocketmine\inventory;
use pocketmine\item\Item;
/** /**
* Classes implementing this interface can be injected into inventories to receive notifications when content changes * Classes implementing this interface can be injected into inventories to receive notifications when content changes
* occur. * occur.
@ -32,7 +34,10 @@ namespace pocketmine\inventory;
*/ */
interface InventoryChangeListener{ interface InventoryChangeListener{
public function onSlotChange(Inventory $inventory, int $slot) : void; public function onSlotChange(Inventory $inventory, int $slot, Item $oldItem) : void;
public function onContentChange(Inventory $inventory) : void; /**
* @param Item[] $oldContents
*/
public function onContentChange(Inventory $inventory, array $oldContents) : void;
} }