ContainerTrait: avoid absurdly inefficient use of setItem()

this substantially improves the performance of loading containers such as chests.
This commit is contained in:
Dylan K. Taylor 2022-01-01 15:05:32 +00:00
parent 72f2c794ab
commit 3c6146b5e0
No known key found for this signature in database
GPG Key ID: 8927471A91CAFD3D

View File

@ -48,11 +48,14 @@ trait ContainerTrait{
$inventory = $this->getRealInventory();
$listeners = $inventory->getListeners()->toArray();
$inventory->getListeners()->remove(...$listeners); //prevent any events being fired by initialization
$inventory->clearAll();
$newContents = [];
/** @var CompoundTag $itemNBT */
foreach($inventoryTag as $itemNBT){
$inventory->setItem($itemNBT->getByte("Slot"), Item::nbtDeserialize($itemNBT));
$newContents[$itemNBT->getByte("Slot")] = Item::nbtDeserialize($itemNBT);
}
$inventory->setContents($newContents);
$inventory->getListeners()->add(...$listeners);
}