> */ private array $changedSlots = []; public function __construct( private int $requestId, private InventoryManager $inventoryManager ){} public function addSlot(int $containerInterfaceId, int $slotId) : void{ $this->changedSlots[$containerInterfaceId][$slotId] = $slotId; } /** * @phpstan-return array{Inventory, int} */ private function getInventoryAndSlot(int $containerInterfaceId, int $slotId) : ?array{ [$windowId, $slotId] = ItemStackContainerIdTranslator::translate($containerInterfaceId, $this->inventoryManager->getCurrentWindowId(), $slotId); $windowAndSlot = $this->inventoryManager->locateWindowAndSlot($windowId, $slotId); if($windowAndSlot === null){ return null; } [$inventory, $slot] = $windowAndSlot; if(!$inventory->slotExists($slot)){ return null; } return [$inventory, $slot]; } public function build() : ItemStackResponse{ $responseInfosByContainer = []; foreach($this->changedSlots as $containerInterfaceId => $slotIds){ if($containerInterfaceId === ContainerUIIds::CREATED_OUTPUT){ continue; } foreach($slotIds as $slotId){ $inventoryAndSlot = $this->getInventoryAndSlot($containerInterfaceId, $slotId); if($inventoryAndSlot === null){ //a plugin may have closed the inventory during an event, or the slot may have been invalid continue; } [$inventory, $slot] = $inventoryAndSlot; $itemStackInfo = $this->inventoryManager->getItemStackInfo($inventory, $slot); if($itemStackInfo === null){ throw new AssumptionFailedError("ItemStackInfo should never be null for an open inventory"); } $item = $inventory->getItem($slot); $responseInfosByContainer[$containerInterfaceId][] = new ItemStackResponseSlotInfo( $slotId, $slotId, $item->getCount(), $itemStackInfo->getStackId(), $item->getCustomName(), $item instanceof Durable ? $item->getDamage() : 0, ); } } $responseContainerInfos = []; foreach($responseInfosByContainer as $containerInterfaceId => $responseInfos){ $responseContainerInfos[] = new ItemStackResponseContainerInfo(new FullContainerName($containerInterfaceId, 0), $responseInfos); } return new ItemStackResponse(ItemStackResponse::RESULT_OK, $this->requestId, $responseContainerInfos); } }