PocketMine-MP 5.33.2 git-1133d49c924b4358c79d44eeb97dcbf56cb4d1eb
Loading...
Searching...
No Matches
ItemStackResponseBuilder.php
1<?php
2
3/*
4 *
5 * ____ _ _ __ __ _ __ __ ____
6 * | _ \ ___ ___| | _____| |_| \/ (_)_ __ ___ | \/ | _ \
7 * | |_) / _ \ / __| |/ / _ \ __| |\/| | | '_ \ / _ \_____| |\/| | |_) |
8 * | __/ (_) | (__| < __/ |_| | | | | | | | __/_____| | | | __/
9 * |_| \___/ \___|_|\_\___|\__|_| |_|_|_| |_|\___| |_| |_|_|
10 *
11 * This program is free software: you can redistribute it and/or modify
12 * it under the terms of the GNU Lesser General Public License as published by
13 * the Free Software Foundation, either version 3 of the License, or
14 * (at your option) any later version.
15 *
16 * @author PocketMine Team
17 * @link http://www.pocketmine.net/
18 *
19 *
20 */
21
22declare(strict_types=1);
23
24namespace pocketmine\network\mcpe\handler;
25
36
38
43 private array $changedSlots = [];
44
45 public function __construct(
46 private int $requestId,
47 private InventoryManager $inventoryManager
48 ){}
49
50 public function addSlot(int $containerInterfaceId, int $slotId) : void{
51 $this->changedSlots[$containerInterfaceId][$slotId] = $slotId;
52 }
53
57 private function locateWindowAndSlot(int $containerInterfaceId, int $slotId) : ?array{
58 [$windowId, $slotId] = ItemStackContainerIdTranslator::translate($containerInterfaceId, $this->inventoryManager->getCurrentWindowId(), $slotId);
59 return $this->inventoryManager->locateWindowAndSlot($windowId, $slotId);
60 }
61
62 public function build() : ItemStackResponse{
63 $responseInfosByContainer = [];
64 foreach($this->changedSlots as $containerInterfaceId => $slotIds){
65 if($containerInterfaceId === ContainerUIIds::CREATED_OUTPUT){
66 continue;
67 }
68 foreach($slotIds as $slotId){
69 $windowAndSlot = $this->locateWindowAndSlot($containerInterfaceId, $slotId);
70 if($windowAndSlot === null){
71 //a plugin may have closed the inventory during an event, or the slot may have been invalid
72 continue;
73 }
74 [$window, $slot] = $windowAndSlot;
75
76 $itemStackInfo = $this->inventoryManager->getItemStackInfo($window, $slot);
77 if($itemStackInfo === null){
78 throw new AssumptionFailedError("ItemStackInfo should never be null for an open inventory");
79 }
80 $item = $window->getInventory()->getItem($slot);
81
82 $responseInfosByContainer[$containerInterfaceId][] = new ItemStackResponseSlotInfo(
83 $slotId,
84 $slotId,
85 $item->getCount(),
86 $itemStackInfo->getStackId(),
87 $item->getCustomName(),
88 $item->getCustomName(),
89 $item instanceof Durable ? $item->getDamage() : 0,
90 );
91 }
92 }
93
94 $responseContainerInfos = [];
95 foreach($responseInfosByContainer as $containerInterfaceId => $responseInfos){
96 $responseContainerInfos[] = new ItemStackResponseContainerInfo(new FullContainerName($containerInterfaceId), $responseInfos);
97 }
98
99 return new ItemStackResponse(ItemStackResponse::RESULT_OK, $this->requestId, $responseContainerInfos);
100 }
101}
static translate(int $containerInterfaceId, int $currentWindowId, int $slotId)