Give InventoryManager internals clearer names

and stop mixing 'window' and 'inventory' terminology...
This commit is contained in:
Dylan K. Taylor 2023-03-20 16:53:57 +00:00
parent c91168db66
commit 59bae9b077
No known key found for this signature in database
GPG Key ID: 8927471A91CAFD3D
2 changed files with 35 additions and 91 deletions

View File

@ -1,64 +0,0 @@
<?php
/*
*
* ____ _ _ __ __ _ __ __ ____
* | _ \ ___ ___| | _____| |_| \/ (_)_ __ ___ | \/ | _ \
* | |_) / _ \ / __| |/ / _ \ __| |\/| | | '_ \ / _ \_____| |\/| | |_) |
* | __/ (_) | (__| < __/ |_| | | | | | | | __/_____| | | | __/
* |_| \___/ \___|_|\_\___|\__|_| |_|_|_| |_|\___| |_| |_|_|
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Lesser General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* @author PocketMine Team
* @link http://www.pocketmine.net/
*
*
*/
declare(strict_types=1);
namespace pocketmine\network\mcpe;
use pocketmine\inventory\Inventory;
final class ComplexWindowMapEntry{
/**
* @var int[]
* @phpstan-var array<int, int>
*/
private array $reverseSlotMap = [];
/**
* @param int[] $slotMap
* @phpstan-param array<int, int> $slotMap
*/
public function __construct(
private Inventory $inventory,
private array $slotMap
){
foreach($slotMap as $slot => $index){
$this->reverseSlotMap[$index] = $slot;
}
}
public function getInventory() : Inventory{ return $this->inventory; }
/**
* @return int[]
* @phpstan-return array<int, int>
*/
public function getSlotMap() : array{ return $this->slotMap; }
public function mapNetToCore(int $slot) : ?int{
return $this->slotMap[$slot] ?? null;
}
public function mapCoreToNet(int $slot) : ?int{
return $this->reverseSlotMap[$slot] ?? null;
}
}

View File

@ -69,18 +69,21 @@ use function spl_object_id;
* @phpstan-type ContainerOpenClosure \Closure(int $id, Inventory $inventory) : (list<ClientboundPacket>|null) * @phpstan-type ContainerOpenClosure \Closure(int $id, Inventory $inventory) : (list<ClientboundPacket>|null)
*/ */
class InventoryManager{ class InventoryManager{
/** @var Inventory[] */
private array $windowMap = [];
/** /**
* @var ComplexWindowMapEntry[] * @var Inventory[] network window ID => Inventory
* @phpstan-var array<int, ComplexWindowMapEntry> * @phpstan-var array<int, Inventory>
*/ */
private array $complexWindows = []; private array $networkIdToInventoryMap = [];
/** /**
* @var ComplexWindowMapEntry[] * @var ComplexInventoryMapEntry[] spl_object_id(Inventory) => ComplexWindowMapEntry
* @phpstan-var array<int, ComplexWindowMapEntry> * @phpstan-var array<int, ComplexInventoryMapEntry>
*/ */
private array $complexSlotToWindowMap = []; private array $complexInventorySlotMaps = [];
/**
* @var ComplexInventoryMapEntry[] net slot ID => ComplexWindowMapEntry
* @phpstan-var array<int, ComplexInventoryMapEntry>
*/
private array $complexSlotToInventoryMap = [];
private int $lastInventoryNetworkId = ContainerIds::FIRST; private int $lastInventoryNetworkId = ContainerIds::FIRST;
@ -126,7 +129,7 @@ class InventoryManager{
} }
private function add(int $id, Inventory $inventory) : void{ private function add(int $id, Inventory $inventory) : void{
$this->windowMap[$id] = $inventory; $this->networkIdToInventoryMap[$id] = $inventory;
} }
private function addDynamic(Inventory $inventory) : int{ private function addDynamic(Inventory $inventory) : int{
@ -140,29 +143,34 @@ class InventoryManager{
* @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 ComplexWindowMapEntry($inventory, is_int($slotMap) ? [$slotMap => 0] : $slotMap); $entry = new ComplexInventoryMapEntry($inventory, is_int($slotMap) ? [$slotMap => 0] : $slotMap);
$this->complexWindows[spl_object_id($inventory)] = $entry; $this->complexInventorySlotMaps[spl_object_id($inventory)] = $entry;
foreach($entry->getSlotMap() as $netSlot => $coreSlot){ foreach($entry->getSlotMap() as $netSlot => $coreSlot){
$this->complexSlotToWindowMap[$netSlot] = $entry; $this->complexSlotToInventoryMap[$netSlot] = $entry;
} }
} }
private function remove(int $id) : void{ private function remove(int $id) : void{
$inventory = $this->windowMap[$id]; $inventory = $this->networkIdToInventoryMap[$id];
unset($this->windowMap[$id]); unset($this->networkIdToInventoryMap[$id]);
if($this->getWindowId($inventory) === null){ if($this->getWindowId($inventory) === null){
$splObjectId = spl_object_id($inventory); $splObjectId = spl_object_id($inventory);
unset($this->initiatedSlotChanges[$splObjectId], $this->itemStackInfos[$splObjectId], $this->complexWindows[$splObjectId]); unset(
foreach($this->complexSlotToWindowMap as $netSlot => $entry){ $this->initiatedSlotChanges[$splObjectId],
$this->itemStackInfos[$splObjectId],
$this->complexInventorySlotMaps[$splObjectId],
$this->pendingSlotSyncs[$splObjectId]
);
foreach($this->complexSlotToInventoryMap as $netSlot => $entry){
if($entry->getInventory() === $inventory){ if($entry->getInventory() === $inventory){
unset($this->complexSlotToWindowMap[$netSlot]); unset($this->complexSlotToInventoryMap[$netSlot]);
} }
} }
} }
} }
public function getWindowId(Inventory $inventory) : ?int{ public function getWindowId(Inventory $inventory) : ?int{
return ($id = array_search($inventory, $this->windowMap, true)) !== false ? $id : null; return ($id = array_search($inventory, $this->networkIdToInventoryMap, true)) !== false ? $id : null;
} }
public function getCurrentWindowId() : int{ public function getCurrentWindowId() : int{
@ -174,15 +182,15 @@ class InventoryManager{
*/ */
public function locateWindowAndSlot(int $windowId, int $netSlotId) : ?array{ public function locateWindowAndSlot(int $windowId, int $netSlotId) : ?array{
if($windowId === ContainerIds::UI){ if($windowId === ContainerIds::UI){
$entry = $this->complexSlotToWindowMap[$netSlotId] ?? null; $entry = $this->complexSlotToInventoryMap[$netSlotId] ?? null;
if($entry === null){ if($entry === null){
return null; return null;
} }
$coreSlotId = $entry->mapNetToCore($netSlotId); $coreSlotId = $entry->mapNetToCore($netSlotId);
return $coreSlotId !== null ? [$entry->getInventory(), $coreSlotId] : null; return $coreSlotId !== null ? [$entry->getInventory(), $coreSlotId] : null;
} }
if(isset($this->windowMap[$windowId])){ if(isset($this->networkIdToInventoryMap[$windowId])){
return [$this->windowMap[$windowId], $netSlotId]; return [$this->networkIdToInventoryMap[$windowId], $netSlotId];
} }
return null; return null;
} }
@ -344,7 +352,7 @@ class InventoryManager{
} }
public function onCurrentWindowRemove() : void{ public function onCurrentWindowRemove() : void{
if(isset($this->windowMap[$this->lastInventoryNetworkId])){ if(isset($this->networkIdToInventoryMap[$this->lastInventoryNetworkId])){
$this->remove($this->lastInventoryNetworkId); $this->remove($this->lastInventoryNetworkId);
$this->session->sendDataPacket(ContainerClosePacket::create($this->lastInventoryNetworkId, true)); $this->session->sendDataPacket(ContainerClosePacket::create($this->lastInventoryNetworkId, true));
if($this->pendingCloseWindowId !== null){ if($this->pendingCloseWindowId !== null){
@ -356,7 +364,7 @@ class InventoryManager{
public function onClientRemoveWindow(int $id) : void{ public function onClientRemoveWindow(int $id) : void{
if($id === $this->lastInventoryNetworkId){ if($id === $this->lastInventoryNetworkId){
if(isset($this->windowMap[$id]) && $id !== $this->pendingCloseWindowId){ if(isset($this->networkIdToInventoryMap[$id]) && $id !== $this->pendingCloseWindowId){
$this->remove($id); $this->remove($id);
$this->player->removeCurrentWindow(); $this->player->removeCurrentWindow();
} }
@ -398,7 +406,7 @@ class InventoryManager{
if($itemStackInfo === null){ if($itemStackInfo === null){
throw new \LogicException("Cannot sync an untracked inventory slot"); throw new \LogicException("Cannot sync an untracked inventory slot");
} }
$slotMap = $this->complexWindows[spl_object_id($inventory)] ?? null; $slotMap = $this->complexInventorySlotMaps[spl_object_id($inventory)] ?? null;
if($slotMap !== null){ if($slotMap !== null){
$windowId = ContainerIds::UI; $windowId = ContainerIds::UI;
$netSlot = $slotMap->mapCoreToNet($slot) ?? throw new AssumptionFailedError("We already have an ItemStackInfo, so this should not be null"); $netSlot = $slotMap->mapCoreToNet($slot) ?? throw new AssumptionFailedError("We already have an ItemStackInfo, so this should not be null");
@ -438,7 +446,7 @@ class InventoryManager{
} }
public function syncContents(Inventory $inventory) : void{ public function syncContents(Inventory $inventory) : void{
$slotMap = $this->complexWindows[spl_object_id($inventory)] ?? null; $slotMap = $this->complexInventorySlotMaps[spl_object_id($inventory)] ?? null;
if($slotMap !== null){ if($slotMap !== null){
$windowId = ContainerIds::UI; $windowId = ContainerIds::UI;
}else{ }else{
@ -471,10 +479,10 @@ class InventoryManager{
} }
public function syncAll() : void{ public function syncAll() : void{
foreach($this->windowMap as $inventory){ foreach($this->networkIdToInventoryMap as $inventory){
$this->syncContents($inventory); $this->syncContents($inventory);
} }
foreach($this->complexWindows as $entry){ foreach($this->complexInventorySlotMaps as $entry){
$this->syncContents($entry->getInventory()); $this->syncContents($entry->getInventory());
} }
} }