mirror of
https://github.com/pmmp/PocketMine-MP.git
synced 2025-05-16 10:49:10 +00:00
Inventory: reduce API duplication by using a Set for viewers
This commit is contained in:
parent
3dafee6aa6
commit
4437756987
@ -54,7 +54,7 @@ class BrewingStand extends Spawnable implements Container, Nameable{
|
||||
public function __construct(World $world, Vector3 $pos){
|
||||
parent::__construct($world, $pos);
|
||||
$this->inventory = new BrewingStandInventory($this->pos);
|
||||
$this->inventory->addListeners(CallbackInventoryListener::onAnyChange(function(Inventory $unused) : void{
|
||||
$this->inventory->getListeners()->add(CallbackInventoryListener::onAnyChange(function(Inventory $unused) : void{
|
||||
$this->pos->getWorldNonNull()->scheduleDelayedBlockUpdate($this->pos, 1);
|
||||
}));
|
||||
}
|
||||
|
@ -48,14 +48,14 @@ trait ContainerTrait{
|
||||
$inventoryTag = $tag->getListTag(Container::TAG_ITEMS);
|
||||
|
||||
$inventory = $this->getRealInventory();
|
||||
$listeners = $inventory->getListeners();
|
||||
$inventory->removeListeners(...$listeners); //prevent any events being fired by initialization
|
||||
$listeners = $inventory->getListeners()->toArray();
|
||||
$inventory->getListeners()->remove(...$listeners); //prevent any events being fired by initialization
|
||||
$inventory->clearAll();
|
||||
/** @var CompoundTag $itemNBT */
|
||||
foreach($inventoryTag as $itemNBT){
|
||||
$inventory->setItem($itemNBT->getByte("Slot"), Item::nbtDeserialize($itemNBT));
|
||||
}
|
||||
$inventory->addListeners(...$listeners);
|
||||
$inventory->getListeners()->add(...$listeners);
|
||||
}
|
||||
|
||||
if($tag->hasTag(Container::TAG_LOCK, StringTag::class)){
|
||||
|
@ -58,7 +58,7 @@ class Furnace extends Spawnable implements Container, Nameable{
|
||||
public function __construct(World $world, Vector3 $pos){
|
||||
parent::__construct($world, $pos);
|
||||
$this->inventory = new FurnaceInventory($this->pos);
|
||||
$this->inventory->addListeners(CallbackInventoryListener::onAnyChange(
|
||||
$this->inventory->getListeners()->add(CallbackInventoryListener::onAnyChange(
|
||||
function(Inventory $unused) : void{
|
||||
$this->pos->getWorldNonNull()->scheduleDelayedBlockUpdate($this->pos, 1);
|
||||
})
|
||||
|
@ -221,8 +221,8 @@ class Human extends Living implements ProjectileSource, InventoryHolder{
|
||||
|
||||
$inventoryTag = $nbt->getListTag("Inventory");
|
||||
if($inventoryTag !== null){
|
||||
$armorListeners = $this->armorInventory->getListeners();
|
||||
$this->armorInventory->removeListeners(...$armorListeners);
|
||||
$armorListeners = $this->armorInventory->getListeners()->toArray();
|
||||
$this->armorInventory->getListeners()->clear();
|
||||
|
||||
/** @var CompoundTag $item */
|
||||
foreach($inventoryTag as $i => $item){
|
||||
@ -236,7 +236,7 @@ class Human extends Living implements ProjectileSource, InventoryHolder{
|
||||
}
|
||||
}
|
||||
|
||||
$this->armorInventory->addListeners(...$armorListeners);
|
||||
$this->armorInventory->getListeners()->add(...$armorListeners);
|
||||
}
|
||||
|
||||
$enderChestInventoryTag = $nbt->getListTag("EnderChestInventory");
|
||||
|
@ -110,7 +110,7 @@ abstract class Living extends Entity{
|
||||
|
||||
$this->armorInventory = new ArmorInventory($this);
|
||||
//TODO: load/save armor inventory contents
|
||||
$this->armorInventory->addListeners(CallbackInventoryListener::onAnyChange(
|
||||
$this->armorInventory->getListeners()->add(CallbackInventoryListener::onAnyChange(
|
||||
function(Inventory $unused) : void{
|
||||
foreach($this->getViewers() as $viewer){
|
||||
$viewer->getNetworkSession()->onMobArmorChange($this);
|
||||
|
@ -23,6 +23,7 @@ declare(strict_types=1);
|
||||
|
||||
namespace pocketmine\inventory;
|
||||
|
||||
use Ds\Set;
|
||||
use pocketmine\item\Item;
|
||||
use pocketmine\item\ItemFactory;
|
||||
use pocketmine\player\Player;
|
||||
@ -43,11 +44,15 @@ abstract class BaseInventory implements Inventory{
|
||||
protected $slots;
|
||||
/** @var Player[] */
|
||||
protected $viewers = [];
|
||||
/** @var InventoryListener[] */
|
||||
protected $listeners = [];
|
||||
/**
|
||||
* @var InventoryListener[]|Set
|
||||
* @phpstan-var Set<InventoryListener>
|
||||
*/
|
||||
protected $listeners;
|
||||
|
||||
public function __construct(int $size){
|
||||
$this->slots = new \SplFixedArray($size);
|
||||
$this->listeners = new Set();
|
||||
}
|
||||
|
||||
/**
|
||||
@ -92,8 +97,8 @@ abstract class BaseInventory implements Inventory{
|
||||
|
||||
$oldContents = $this->slots->toArray();
|
||||
|
||||
$listeners = $this->listeners;
|
||||
$this->listeners = [];
|
||||
$listeners = $this->listeners->toArray();
|
||||
$this->listeners->clear();
|
||||
$viewers = $this->viewers;
|
||||
$this->viewers = [];
|
||||
|
||||
@ -105,7 +110,7 @@ abstract class BaseInventory implements Inventory{
|
||||
}
|
||||
}
|
||||
|
||||
$this->addListeners(...$listeners); //don't directly write, in case listeners were added while operation was in progress
|
||||
$this->listeners->add(...$listeners); //don't directly write, in case listeners were added while operation was in progress
|
||||
foreach($viewers as $id => $viewer){
|
||||
$this->viewers[$id] = $viewer;
|
||||
}
|
||||
@ -372,23 +377,7 @@ abstract class BaseInventory implements Inventory{
|
||||
return $slot >= 0 and $slot < $this->slots->getSize();
|
||||
}
|
||||
|
||||
public function addListeners(InventoryListener ...$listeners) : void{
|
||||
foreach($listeners as $listener){
|
||||
$this->listeners[spl_object_id($listener)] = $listener;
|
||||
}
|
||||
}
|
||||
|
||||
public function removeListeners(InventoryListener ...$listeners) : void{
|
||||
foreach($listeners as $listener){
|
||||
unset($this->listeners[spl_object_id($listener)]);
|
||||
}
|
||||
}
|
||||
|
||||
public function removeAllListeners() : void{
|
||||
$this->listeners = [];
|
||||
}
|
||||
|
||||
public function getListeners() : array{
|
||||
public function getListeners() : Set{
|
||||
return $this->listeners;
|
||||
}
|
||||
}
|
||||
|
@ -26,6 +26,7 @@ declare(strict_types=1);
|
||||
*/
|
||||
namespace pocketmine\inventory;
|
||||
|
||||
use Ds\Set;
|
||||
use pocketmine\item\Item;
|
||||
use pocketmine\player\Player;
|
||||
|
||||
@ -155,19 +156,8 @@ interface Inventory{
|
||||
public function slotExists(int $slot) : bool;
|
||||
|
||||
/**
|
||||
* @param InventoryListener ...$listeners
|
||||
* @return InventoryListener[]|Set
|
||||
* @phpstan-return Set<InventoryListener>
|
||||
*/
|
||||
public function addListeners(InventoryListener ...$listeners) : void;
|
||||
|
||||
/**
|
||||
* @param InventoryListener ...$listeners
|
||||
*/
|
||||
public function removeListeners(InventoryListener ...$listeners) : void;
|
||||
|
||||
public function removeAllListeners() : void;
|
||||
|
||||
/**
|
||||
* @return InventoryListener[]
|
||||
*/
|
||||
public function getListeners() : array;
|
||||
public function getListeners() : Set;
|
||||
}
|
||||
|
@ -29,8 +29,7 @@ use pocketmine\item\Item;
|
||||
* Classes implementing this interface can be injected into inventories to receive notifications when content changes
|
||||
* occur.
|
||||
* @see CallbackInventoryListener for a closure-based listener
|
||||
* @see Inventory::addListeners()
|
||||
* @see Inventory::removeListeners()
|
||||
* @see Inventory::getListeners()
|
||||
*/
|
||||
interface InventoryListener{
|
||||
|
||||
|
Loading…
x
Reference in New Issue
Block a user