Inventory: reduce API duplication by using a Set for viewers

This commit is contained in:
Dylan K. Taylor
2020-05-14 14:13:28 +01:00
parent 3dafee6aa6
commit 4437756987
8 changed files with 25 additions and 47 deletions

View File

@ -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);
}));
}

View File

@ -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)){

View File

@ -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);
})