PocketMine-MP 5.23.3 git-f7687af337d001ddbcc47b8e773f014a33faa662
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
35
37
42 private array $changedSlots = [];
43
44 public function __construct(
45 private int $requestId,
46 private InventoryManager $inventoryManager
47 ){}
48
49 public function addSlot(int $containerInterfaceId, int $slotId) : void{
50 $this->changedSlots[$containerInterfaceId][$slotId] = $slotId;
51 }
52
56 private function getInventoryAndSlot(int $containerInterfaceId, int $slotId) : ?array{
57 [$windowId, $slotId] = ItemStackContainerIdTranslator::translate($containerInterfaceId, $this->inventoryManager->getCurrentWindowId(), $slotId);
58 $windowAndSlot = $this->inventoryManager->locateWindowAndSlot($windowId, $slotId);
59 if($windowAndSlot === null){
60 return null;
61 }
62 [$inventory, $slot] = $windowAndSlot;
63 if(!$inventory->slotExists($slot)){
64 return null;
65 }
66
67 return [$inventory, $slot];
68 }
69
70 public function build() : ItemStackResponse{
71 $responseInfosByContainer = [];
72 foreach($this->changedSlots as $containerInterfaceId => $slotIds){
73 if($containerInterfaceId === ContainerUIIds::CREATED_OUTPUT){
74 continue;
75 }
76 foreach($slotIds as $slotId){
77 $inventoryAndSlot = $this->getInventoryAndSlot($containerInterfaceId, $slotId);
78 if($inventoryAndSlot === null){
79 //a plugin may have closed the inventory during an event, or the slot may have been invalid
80 continue;
81 }
82 [$inventory, $slot] = $inventoryAndSlot;
83
84 $itemStackInfo = $this->inventoryManager->getItemStackInfo($inventory, $slot);
85 if($itemStackInfo === null){
86 throw new AssumptionFailedError("ItemStackInfo should never be null for an open inventory");
87 }
88 $item = $inventory->getItem($slot);
89
90 $responseInfosByContainer[$containerInterfaceId][] = new ItemStackResponseSlotInfo(
91 $slotId,
92 $slotId,
93 $item->getCount(),
94 $itemStackInfo->getStackId(),
95 $item->getCustomName(),
96 $item->getCustomName(),
97 $item instanceof Durable ? $item->getDamage() : 0,
98 );
99 }
100 }
101
102 $responseContainerInfos = [];
103 foreach($responseInfosByContainer as $containerInterfaceId => $responseInfos){
104 $responseContainerInfos[] = new ItemStackResponseContainerInfo(new FullContainerName($containerInterfaceId), $responseInfos);
105 }
106
107 return new ItemStackResponse(ItemStackResponse::RESULT_OK, $this->requestId, $responseContainerInfos);
108 }
109}
static translate(int $containerInterfaceId, int $currentWindowId, int $slotId)