115 [$windowId, $slotId] =
ItemStackContainerIdTranslator::translate($info->getContainerName()->getContainerId(), $this->inventoryManager->getCurrentWindowId(), $info->getSlotId());
116 $windowAndSlot = $this->inventoryManager->locateWindowAndSlot($windowId, $slotId);
117 if($windowAndSlot ===
null){
118 throw new ItemStackRequestProcessException(
"No open inventory matches container UI ID: " . $info->getContainerName()->getContainerId() .
", slot ID: " . $info->getSlotId());
120 [$inventory, $slot] = $windowAndSlot;
122 throw new ItemStackRequestProcessException(
"No such inventory slot :" . $this->prettyInventoryAndSlot($inventory, $slot));
125 if($info->getStackId() !== $this->request->getRequestId()){
126 $this->matchItemStack($inventory, $slot, $info->getStackId());
129 return [$this->builder->getInventory($inventory), $slot];
148 return $this->takeCreatedItem($count);
150 $this->requestSlotInfos[] = $slotInfo;
151 [$inventory, $slot] = $this->getBuilderInventoryAndSlot($slotInfo);
157 $existingItem = $inventory->
getItem($slot);
158 if($existingItem->getCount() < $count){
159 throw new ItemStackRequestProcessException($this->prettyInventoryAndSlot($inventory, $slot) .
": Cannot take $count items from a stack of " . $existingItem->getCount());
162 $removed = $existingItem->pop($count);
163 $inventory->
setItem($slot, $existingItem);
202 if($item !== null && $item->isNull()){
205 if($this->nextCreatedItem !==
null){
208 if($this->createdItemFromCreativeInventory && $this->createdItemsTakenCount > 0){
209 $this->nextCreatedItem->setCount($this->createdItemsTakenCount);
211 }elseif($this->createdItemsTakenCount < $this->nextCreatedItem->getCount()){
212 throw new ItemStackRequestProcessException(
"Not all of the previous created item was taken");
215 $this->nextCreatedItem = $item;
216 $this->createdItemFromCreativeInventory = $creative;
217 $this->createdItemsTakenCount = 0;
224 if($this->specialTransaction !== null){
227 if($repetitions < 1){
230 if($repetitions > 256){
234 throw new ItemStackRequestProcessException(
"Cannot craft a recipe more than 256 times");
236 $craftingManager = $this->player->getServer()->getCraftingManager();
237 $recipe = $craftingManager->getCraftingRecipeFromIndex($recipeId);
238 if($recipe ===
null){
239 throw new ItemStackRequestProcessException(
"No such crafting recipe index: $recipeId");
242 $this->specialTransaction =
new CraftingTransaction($this->player, $craftingManager, [], $recipe, $repetitions);
248 $craftingResults = $recipe->getResultsFor($this->player->getCraftingGrid());
249 foreach($craftingResults as $k => $craftingResult){
250 $craftingResult->setCount($craftingResult->getCount() * $repetitions);
251 $this->craftingResults[$k] = $craftingResult;
253 if(count($this->craftingResults) === 1){
255 $this->setNextCreatedItem($this->craftingResults[array_key_first($this->craftingResults)]);
267 $createdItem = $this->nextCreatedItem;
268 if($createdItem ===
null){
272 if(!$this->createdItemFromCreativeInventory){
273 $availableCount = $createdItem->getCount() - $this->createdItemsTakenCount;
274 if($count > $availableCount){
275 throw new ItemStackRequestProcessException(
"Not enough created items available to be taken (have $availableCount, tried to take $count)");
279 $this->createdItemsTakenCount += $count;
280 $takenItem = clone $createdItem;
281 $takenItem->setCount($count);
282 if(!$this->createdItemFromCreativeInventory && $this->createdItemsTakenCount >= $createdItem->getCount()){
283 $this->setNextCreatedItem(
null);
309 $this->transferItems($action->getSource(), $action->getDestination(), $action->getCount());
311 $this->requestSlotInfos[] = $action->getSlot1();
312 $this->requestSlotInfos[] = $action->getSlot2();
314 [$inventory1, $slot1] = $this->getBuilderInventoryAndSlot($action->getSlot1());
315 [$inventory2, $slot2] = $this->getBuilderInventoryAndSlot($action->getSlot2());
317 $item1 = $inventory1->getItem($slot1);
318 $item2 = $inventory2->getItem($slot2);
319 $inventory1->setItem($slot1, $item2);
320 $inventory2->setItem($slot2, $item1);
321 }elseif($action instanceof DropStackRequestAction){
323 $dropped = $this->removeItemFromSlot($action->getSource(), $action->getCount());
324 $this->builder->addAction(
new DropItemAction($dropped));
326 }elseif($action instanceof DestroyStackRequestAction){
327 $destroyed = $this->removeItemFromSlot($action->getSource(), $action->getCount());
328 $this->builder->addAction(
new DestroyItemAction($destroyed));
330 }elseif($action instanceof CreativeCreateStackRequestAction){
331 $item = $this->player->getCreativeInventory()->getItem($action->getCreativeItemId());
333 throw new ItemStackRequestProcessException(
"No such creative item index: " . $action->getCreativeItemId());
336 $this->setNextCreatedItem($item,
true);
337 }elseif($action instanceof CraftRecipeStackRequestAction){
338 $window = $this->player->getCurrentWindow();
339 if($window instanceof EnchantInventory){
340 $optionId = $this->inventoryManager->getEnchantingTableOptionIndex($action->getRecipeId());
341 if($optionId !==
null && ($option = $window->getOption($optionId)) !==
null){
342 $this->specialTransaction =
new EnchantingTransaction($this->player, $option, $optionId + 1);
343 $this->setNextCreatedItem($window->getOutput($optionId));
346 $this->beginCrafting($action->getRecipeId(), $action->getRepetitions());
348 }elseif($action instanceof CraftRecipeAutoStackRequestAction){
349 $this->beginCrafting($action->getRecipeId(), $action->getRepetitions());
350 }elseif($action instanceof CraftingConsumeInputStackRequestAction){
351 $this->assertDoingCrafting();
352 $this->removeItemFromSlot($action->getSource(), $action->getCount());
354 }elseif($action instanceof CraftingCreateSpecificResultStackRequestAction){
355 $this->assertDoingCrafting();
357 $nextResultItem = $this->craftingResults[$action->getResultIndex()] ??
null;
358 if($nextResultItem ===
null){
359 throw new ItemStackRequestProcessException(
"No such crafting result index: " . $action->getResultIndex());
361 $this->setNextCreatedItem($nextResultItem);
362 }elseif($action instanceof DeprecatedCraftingResultsStackRequestAction){
365 throw new ItemStackRequestProcessException(
"Unhandled item stack request action");
373 foreach($this->request->getActions() as $k => $action){
375 $this->processItemStackRequestAction($action);
377 throw new ItemStackRequestProcessException(
"Error processing action $k (" . (
new \ReflectionClass($action))->getShortName() .
"): " . $e->getMessage(), 0, $e);
380 $this->setNextCreatedItem(
null);
381 $inventoryActions = $this->builder->generateActions();
384 foreach($inventoryActions as $action){
385 $transaction->addAction($action);