119 [$windowId, $slotId] =
ItemStackContainerIdTranslator::translate($info->getContainerName()->getContainerId(), $this->inventoryManager->getCurrentWindowId(), $info->getSlotId());
120 $windowAndSlot = $this->inventoryManager->locateWindowAndSlot($windowId, $slotId);
121 if($windowAndSlot ===
null){
122 throw new ItemStackRequestProcessException(
"No open inventory matches container UI ID: " . $info->getContainerName()->getContainerId() .
", slot ID: " . $info->getSlotId());
124 [$inventory, $slot] = $windowAndSlot;
126 throw new ItemStackRequestProcessException(
"No such inventory slot :" . $this->prettyInventoryAndSlot($inventory, $slot));
129 if($info->getStackId() !== $this->request->getRequestId()){
130 $this->matchItemStack($inventory, $slot, $info->getStackId());
133 return [$this->builder->getInventory($inventory), $slot];
152 return $this->takeCreatedItem($count);
154 $this->requestSlotInfos[] = $slotInfo;
155 [$inventory, $slot] = $this->getBuilderInventoryAndSlot($slotInfo);
161 $existingItem = $inventory->
getItem($slot);
162 if($existingItem->getCount() < $count){
163 throw new ItemStackRequestProcessException($this->prettyInventoryAndSlot($inventory, $slot) .
": Cannot take $count items from a stack of " . $existingItem->getCount());
166 $removed = $existingItem->pop($count);
167 $inventory->
setItem($slot, $existingItem);
206 if($item !== null && $item->isNull()){
209 if($this->nextCreatedItem !==
null){
212 if($this->createdItemFromCreativeInventory && $this->createdItemsTakenCount > 0){
213 $this->nextCreatedItem->setCount($this->createdItemsTakenCount);
215 }elseif($this->createdItemsTakenCount < $this->nextCreatedItem->getCount()){
216 throw new ItemStackRequestProcessException(
"Not all of the previous created item was taken");
219 $this->nextCreatedItem = $item;
220 $this->createdItemFromCreativeInventory = $creative;
221 $this->createdItemsTakenCount = 0;
228 if($this->specialTransaction !== null){
231 if($repetitions < 1){
234 if($repetitions > 256){
238 throw new ItemStackRequestProcessException(
"Cannot craft a recipe more than 256 times");
240 $craftingManager = $this->player->getServer()->getCraftingManager();
241 $recipe = $craftingManager->getCraftingRecipeFromIndex($recipeId);
242 if($recipe ===
null){
243 throw new ItemStackRequestProcessException(
"No such crafting recipe index: $recipeId");
246 $this->specialTransaction =
new CraftingTransaction($this->player, $craftingManager, [], $recipe, $repetitions);
252 $craftingResults = $recipe->getResultsFor($this->player->getCraftingGrid());
253 foreach($craftingResults as $k => $craftingResult){
254 $craftingResult->setCount($craftingResult->getCount() * $repetitions);
255 $this->craftingResults[$k] = $craftingResult;
257 if(count($this->craftingResults) === 1){
259 $this->setNextCreatedItem($this->craftingResults[array_key_first($this->craftingResults)]);
313 $this->transferItems($action->getSource(), $action->getDestination(), $action->getCount());
315 $this->requestSlotInfos[] = $action->getSlot1();
316 $this->requestSlotInfos[] = $action->getSlot2();
318 [$inventory1, $slot1] = $this->getBuilderInventoryAndSlot($action->getSlot1());
319 [$inventory2, $slot2] = $this->getBuilderInventoryAndSlot($action->getSlot2());
321 $item1 = $inventory1->getItem($slot1);
322 $item2 = $inventory2->getItem($slot2);
323 $inventory1->setItem($slot1, $item2);
324 $inventory2->setItem($slot2, $item1);
325 }elseif($action instanceof DropStackRequestAction){
327 $dropped = $this->removeItemFromSlot($action->getSource(), $action->getCount());
328 $this->builder->addAction(
new DropItemAction($dropped));
330 }elseif($action instanceof DestroyStackRequestAction){
331 $destroyed = $this->removeItemFromSlot($action->getSource(), $action->getCount());
332 $this->builder->addAction(
new DestroyItemAction($destroyed));
334 }elseif($action instanceof CreativeCreateStackRequestAction){
335 $item = $this->player->getCreativeInventory()->getItem($action->getCreativeItemId());
337 throw new ItemStackRequestProcessException(
"No such creative item index: " . $action->getCreativeItemId());
340 $this->setNextCreatedItem($item,
true);
341 }elseif($action instanceof CraftRecipeStackRequestAction){
342 $window = $this->player->getCurrentWindow();
343 if($window instanceof EnchantInventory){
344 $optionId = $this->inventoryManager->getEnchantingTableOptionIndex($action->getRecipeId());
345 if($optionId !==
null && ($option = $window->getOption($optionId)) !==
null){
346 $this->specialTransaction =
new EnchantingTransaction($this->player, $option, $optionId + 1);
347 $this->setNextCreatedItem($window->getOutput($optionId));
350 $this->beginCrafting($action->getRecipeId(), $action->getRepetitions());
352 }elseif($action instanceof CraftRecipeAutoStackRequestAction){
353 $this->beginCrafting($action->getRecipeId(), $action->getRepetitions());
354 }elseif($action instanceof CraftingConsumeInputStackRequestAction){
355 $this->assertDoingCrafting();
356 $this->removeItemFromSlot($action->getSource(), $action->getCount());
358 }elseif($action instanceof CraftingCreateSpecificResultStackRequestAction){
359 $this->assertDoingCrafting();
361 $nextResultItem = $this->craftingResults[$action->getResultIndex()] ??
null;
362 if($nextResultItem ===
null){
363 throw new ItemStackRequestProcessException(
"No such crafting result index: " . $action->getResultIndex());
365 $this->setNextCreatedItem($nextResultItem);
366 }elseif($action instanceof DeprecatedCraftingResultsStackRequestAction){
368 }elseif($action instanceof MineBlockStackRequestAction){
369 $slot = $action->getHotbarSlot();
370 $this->requestSlotInfos[] =
new ItemStackRequestSlotInfo(
new FullContainerName(ContainerUIIds::HOTBAR), $slot, $action->getStackId());
371 $inventory = $this->player->getInventory();
373 $predictedDamage = $action->getPredictedDurability();
374 if($usedItem instanceof Durable && $predictedDamage >= 0 && $predictedDamage <= $usedItem->getMaxDurability()){
375 $usedItem->setDamage($predictedDamage);
376 $this->inventoryManager->addPredictedSlotChange($inventory, $slot, $usedItem);
379 throw new ItemStackRequestProcessException(
"Unhandled item stack request action");