Fixed crash when opening main inventory

the InventoryManagerEntry was getting overwritten, since we don't expect to open the same inventory with two different window IDs.
This commit is contained in:
Dylan K. Taylor 2023-03-20 18:40:18 +00:00
parent a83fc85f1e
commit e8085e22a0
No known key found for this signature in database
GPG Key ID: 8927471A91CAFD3D

View File

@ -118,30 +118,48 @@ class InventoryManager{
}); });
} }
private function add(int $id, Inventory $inventory) : void{ private function associateIdWithInventory(int $id, Inventory $inventory) : void{
$this->inventories[spl_object_id($inventory)] = new InventoryManagerEntry($inventory);
$this->networkIdToInventoryMap[$id] = $inventory; $this->networkIdToInventoryMap[$id] = $inventory;
} }
private function addDynamic(Inventory $inventory) : int{ private function getNewWindowId() : int{
$this->inventories[spl_object_id($inventory)] = new InventoryManagerEntry($inventory);
$this->lastInventoryNetworkId = max(ContainerIds::FIRST, ($this->lastInventoryNetworkId + 1) % ContainerIds::LAST); $this->lastInventoryNetworkId = max(ContainerIds::FIRST, ($this->lastInventoryNetworkId + 1) % ContainerIds::LAST);
$this->add($this->lastInventoryNetworkId, $inventory);
return $this->lastInventoryNetworkId; return $this->lastInventoryNetworkId;
} }
private function add(int $id, Inventory $inventory) : void{
if(isset($this->inventories[spl_object_id($inventory)])){
throw new \InvalidArgumentException("Inventory " . get_class($inventory) . " is already tracked");
}
$this->inventories[spl_object_id($inventory)] = new InventoryManagerEntry($inventory);
$this->associateIdWithInventory($id, $inventory);
}
private function addDynamic(Inventory $inventory) : int{
if(isset($this->inventories[spl_object_id($inventory)])){
throw new \InvalidArgumentException("Inventory " . get_class($inventory) . " is already tracked");
}
$this->inventories[spl_object_id($inventory)] = new InventoryManagerEntry($inventory);
$id = $this->getNewWindowId();
$this->add($id, $inventory);
return $id;
}
/** /**
* @param int[]|int $slotMap * @param int[]|int $slotMap
* @phpstan-param array<int, int>|int $slotMap * @phpstan-param array<int, int>|int $slotMap
*/ */
private function addComplex(array|int $slotMap, Inventory $inventory) : void{ private function addComplex(array|int $slotMap, Inventory $inventory) : void{
$entry = new ComplexInventoryMapEntry($inventory, is_int($slotMap) ? [$slotMap => 0] : $slotMap); if(isset($this->inventories[spl_object_id($inventory)])){
throw new \InvalidArgumentException("Inventory " . get_class($inventory) . " is already tracked");
}
$complexSlotMap = new ComplexInventoryMapEntry($inventory, is_int($slotMap) ? [$slotMap => 0] : $slotMap);
$this->inventories[spl_object_id($inventory)] = new InventoryManagerEntry( $this->inventories[spl_object_id($inventory)] = new InventoryManagerEntry(
$inventory, $inventory,
$entry $complexSlotMap
); );
foreach($entry->getSlotMap() as $netSlot => $coreSlot){ foreach($complexSlotMap->getSlotMap() as $netSlot => $coreSlot){
$this->complexSlotToInventoryMap[$netSlot] = $entry; $this->complexSlotToInventoryMap[$netSlot] = $complexSlotMap;
} }
} }
@ -329,7 +347,8 @@ class InventoryManager{
$this->onCurrentWindowRemove(); $this->onCurrentWindowRemove();
$this->openWindowDeferred(function() : void{ $this->openWindowDeferred(function() : void{
$windowId = $this->addDynamic($this->player->getInventory()); $windowId = $this->getNewWindowId();
$this->associateIdWithInventory($windowId, $this->player->getInventory());
$this->session->sendDataPacket(ContainerOpenPacket::entityInv( $this->session->sendDataPacket(ContainerOpenPacket::entityInv(
$windowId, $windowId,