First look at ItemStackRequest usage (very unstable)

This commit is contained in:
Dylan K. Taylor
2022-08-18 17:38:57 +01:00
parent d6af2b12f4
commit 2b7510945a
8 changed files with 560 additions and 94 deletions

View File

@ -0,0 +1,288 @@
<?php
/*
*
* ____ _ _ __ __ _ __ __ ____
* | _ \ ___ ___| | _____| |_| \/ (_)_ __ ___ | \/ | _ \
* | |_) / _ \ / __| |/ / _ \ __| |\/| | | '_ \ / _ \_____| |\/| | |_) |
* | __/ (_) | (__| < __/ |_| | | | | | | | __/_____| | | | __/
* |_| \___/ \___|_|\_\___|\__|_| |_|_|_| |_|\___| |_| |_|_|
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Lesser General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* @author PocketMine Team
* @link http://www.pocketmine.net/
*
*
*/
declare(strict_types=1);
namespace pocketmine\network\mcpe\handler;
use pocketmine\block\inventory\CraftingTableInventory;
use pocketmine\inventory\Inventory;
use pocketmine\inventory\transaction\action\DestroyItemAction;
use pocketmine\inventory\transaction\action\DropItemAction;
use pocketmine\inventory\transaction\CraftingTransaction;
use pocketmine\inventory\transaction\InventoryTransaction;
use pocketmine\inventory\transaction\TransactionBuilder;
use pocketmine\inventory\transaction\TransactionBuilderInventory;
use pocketmine\item\Item;
use pocketmine\item\VanillaItems;
use pocketmine\network\mcpe\InventoryManager;
use pocketmine\network\mcpe\protocol\types\inventory\ContainerIds;
use pocketmine\network\mcpe\protocol\types\inventory\ContainerUIIds;
use pocketmine\network\mcpe\protocol\types\inventory\stackrequest\CraftingConsumeInputStackRequestAction;
use pocketmine\network\mcpe\protocol\types\inventory\stackrequest\CraftingMarkSecondaryResultStackRequestAction;
use pocketmine\network\mcpe\protocol\types\inventory\stackrequest\CraftRecipeAutoStackRequestAction;
use pocketmine\network\mcpe\protocol\types\inventory\stackrequest\CraftRecipeStackRequestAction;
use pocketmine\network\mcpe\protocol\types\inventory\stackrequest\DeprecatedCraftingResultsStackRequestAction;
use pocketmine\network\mcpe\protocol\types\inventory\stackrequest\DestroyStackRequestAction;
use pocketmine\network\mcpe\protocol\types\inventory\stackrequest\DropStackRequestAction;
use pocketmine\network\mcpe\protocol\types\inventory\stackrequest\ItemStackRequest;
use pocketmine\network\mcpe\protocol\types\inventory\stackrequest\ItemStackRequestAction;
use pocketmine\network\mcpe\protocol\types\inventory\stackrequest\ItemStackRequestSlotInfo;
use pocketmine\network\mcpe\protocol\types\inventory\stackrequest\PlaceStackRequestAction;
use pocketmine\network\mcpe\protocol\types\inventory\stackrequest\SwapStackRequestAction;
use pocketmine\network\mcpe\protocol\types\inventory\stackrequest\TakeStackRequestAction;
use pocketmine\network\mcpe\protocol\types\inventory\stackresponse\ItemStackResponse;
use pocketmine\network\mcpe\protocol\types\inventory\stackresponse\ItemStackResponseContainerInfo;
use pocketmine\network\mcpe\protocol\types\inventory\stackresponse\ItemStackResponseSlotInfo;
use pocketmine\network\PacketHandlingException;
use pocketmine\player\Player;
use function get_class;
use function var_dump;
final class ItemStackRequestExecutor{
private TransactionBuilder $builder;
/** @var ItemStackRequestSlotInfo[] */
private array $requestSlotInfos = [];
private bool $crafting = false;
public function __construct(
private Player $player,
private InventoryManager $inventoryManager,
private ItemStackRequest $request
){
$this->builder = new TransactionBuilder();
}
private function translateContainerId(int $containerInterfaceId) : int{
return match($containerInterfaceId){
ContainerUIIds::ARMOR => ContainerIds::ARMOR,
ContainerUIIds::HOTBAR,
ContainerUIIds::INVENTORY,
ContainerUIIds::COMBINED_HOTBAR_AND_INVENTORY => ContainerIds::INVENTORY,
ContainerUIIds::OFFHAND => ContainerIds::OFFHAND,
ContainerUIIds::ANVIL_INPUT,
ContainerUIIds::ANVIL_MATERIAL,
ContainerUIIds::BEACON_PAYMENT,
ContainerUIIds::CARTOGRAPHY_ADDITIONAL,
ContainerUIIds::CARTOGRAPHY_INPUT,
ContainerUIIds::COMPOUND_CREATOR_INPUT,
ContainerUIIds::CRAFTING_INPUT,
ContainerUIIds::CREATED_OUTPUT,
ContainerUIIds::CURSOR,
ContainerUIIds::ENCHANTING_INPUT,
ContainerUIIds::ENCHANTING_MATERIAL,
ContainerUIIds::GRINDSTONE_ADDITIONAL,
ContainerUIIds::GRINDSTONE_INPUT,
ContainerUIIds::LAB_TABLE_INPUT,
ContainerUIIds::LOOM_DYE,
ContainerUIIds::LOOM_INPUT,
ContainerUIIds::LOOM_MATERIAL,
ContainerUIIds::MATERIAL_REDUCER_INPUT,
ContainerUIIds::MATERIAL_REDUCER_OUTPUT,
ContainerUIIds::SMITHING_TABLE_INPUT,
ContainerUIIds::SMITHING_TABLE_MATERIAL,
ContainerUIIds::STONECUTTER_INPUT,
ContainerUIIds::TRADE2_INGREDIENT1,
ContainerUIIds::TRADE2_INGREDIENT2,
ContainerUIIds::TRADE_INGREDIENT1,
ContainerUIIds::TRADE_INGREDIENT2 => ContainerIds::UI,
ContainerUIIds::BARREL,
ContainerUIIds::BLAST_FURNACE_INGREDIENT,
ContainerUIIds::BREWING_STAND_FUEL,
ContainerUIIds::BREWING_STAND_INPUT,
ContainerUIIds::BREWING_STAND_RESULT,
ContainerUIIds::FURNACE_FUEL,
ContainerUIIds::FURNACE_INGREDIENT,
ContainerUIIds::FURNACE_RESULT,
ContainerUIIds::LEVEL_ENTITY, //chest
ContainerUIIds::SHULKER_BOX,
ContainerUIIds::SMOKER_INGREDIENT => $this->inventoryManager->getCurrentWindowId(),
//all preview slots are ignored, since the client shouldn't be modifying those directly
default => throw new PacketHandlingException("Unexpected container UI ID $containerInterfaceId")
};
}
/**
* @phpstan-return array{Inventory, int}
*/
private function getInventoryAndSlot(ItemStackRequestSlotInfo $info) : array{
$windowId = $this->translateContainerId($info->getContainerId());
$info = $this->inventoryManager->locateWindowAndSlot($windowId, $info->getSlotId());
if($info === null){
throw new PacketHandlingException("Stack request action cannot target an inventory that is not open");
}
[$inventory, $slot] = $info;
if(!$inventory->slotExists($slot)){
throw new PacketHandlingException("Stack request action cannot target an inventory slot that does not exist");
}
return [$inventory, $slot];
}
/**
* @phpstan-return array{TransactionBuilderInventory, int}
*/
private function getBuilderInventoryAndSlot(ItemStackRequestSlotInfo $info) : array{
[$inventory, $slot] = $this->getInventoryAndSlot($info);
if(
$info->getStackId() !== $this->request->getRequestId() && //using TransactionBuilderInventory enables this to work
!$this->inventoryManager->matchItemStack($inventory, $slot, $info->getStackId())
){
throw new PacketHandlingException("Inventory " . $info->getContainerId() . ", slot " . $slot . ": server-side item does not match expected");
}
return [$this->builder->getInventory($inventory), $slot];
}
private function transferItems(ItemStackRequestSlotInfo $source, ItemStackRequestSlotInfo $destination, int $count) : void{
[$sourceInventory, $sourceSlot] = $this->getBuilderInventoryAndSlot($source);
[$targetInventory, $targetSlot] = $this->getBuilderInventoryAndSlot($destination);
$oldSourceItem = $sourceInventory->getItem($sourceSlot);
$oldTargetItem = $targetInventory->getItem($targetSlot);
if(!$targetInventory->isSlotEmpty($targetSlot) && !$oldTargetItem->canStackWith($oldSourceItem)){
throw new PacketHandlingException("Can only transfer items into an empty slot, or a slot containing the same item");
}
[$newSourceItem, $newTargetItem] = $this->splitStack($oldSourceItem, $count, $oldTargetItem->getCount());
$sourceInventory->setItem($sourceSlot, $newSourceItem);
$targetInventory->setItem($targetSlot, $newTargetItem);
}
/**
* @phpstan-return array{Item, Item}
*/
private function splitStack(Item $item, int $transferredCount, int $targetCount) : array{
if($item->getCount() < $transferredCount){
throw new PacketHandlingException("Cannot take $transferredCount items from a stack of " . $item->getCount());
}
$leftover = clone $item;
$removed = $leftover->pop($transferredCount);
$removed->setCount($removed->getCount() + $targetCount);
if($leftover->isNull()){
$leftover = VanillaItems::AIR();
}
return [$leftover, $removed];
}
private function processItemStackRequestAction(ItemStackRequestAction $action) : void{
if(
$action instanceof TakeStackRequestAction ||
$action instanceof PlaceStackRequestAction
){
$this->requestSlotInfos[] = $action->getSource();
$this->requestSlotInfos[] = $action->getDestination();
$this->transferItems($action->getSource(), $action->getDestination(), $action->getCount());
}elseif($action instanceof SwapStackRequestAction){
$this->requestSlotInfos[] = $action->getSlot1();
$this->requestSlotInfos[] = $action->getSlot2();
[$inventory1, $slot1] = $this->getBuilderInventoryAndSlot($action->getSlot1());
[$inventory2, $slot2] = $this->getBuilderInventoryAndSlot($action->getSlot2());
$item1 = $inventory1->getItem($slot1);
$item2 = $inventory2->getItem($slot2);
$inventory1->setItem($slot1, $item2);
$inventory2->setItem($slot2, $item1);
}elseif($action instanceof DropStackRequestAction){
$this->requestSlotInfos[] = $action->getSource();
[$inventory, $slot] = $this->getBuilderInventoryAndSlot($action->getSource());
$oldItem = $inventory->getItem($slot);
[$leftover, $dropped] = $this->splitStack($oldItem, $action->getCount(), 0);
//TODO: this action has a "randomly" field, I have no idea what it's used for
$inventory->setItem($slot, $leftover);
$this->builder->addAction(new DropItemAction($dropped));
}elseif($action instanceof DestroyStackRequestAction){
$this->requestSlotInfos[] = $action->getSource();
[$inventory, $slot] = $this->getBuilderInventoryAndSlot($action->getSource());
$oldItem = $inventory->getItem($slot);
[$leftover, $destroyed] = $this->splitStack($oldItem, $action->getCount(), 0);
$inventory->setItem($slot, $leftover);
$this->builder->addAction(new DestroyItemAction($destroyed));
}elseif($action instanceof CraftingConsumeInputStackRequestAction){
//we don't need this for the PM system
$this->requestSlotInfos[] = $action->getSource();
$this->crafting = true;
}elseif(
$action instanceof CraftRecipeStackRequestAction || //TODO
$action instanceof CraftRecipeAutoStackRequestAction || //TODO
$action instanceof CraftingMarkSecondaryResultStackRequestAction || //no obvious use
$action instanceof DeprecatedCraftingResultsStackRequestAction //no obvious use
){
$this->crafting = true;
}else{
throw new PacketHandlingException("Unhandled item stack request action: " . get_class($action));
}
}
public function generateInventoryTransaction() : InventoryTransaction{
foreach($this->request->getActions() as $action){
$this->processItemStackRequestAction($action);
}
$inventoryActions = $this->builder->generateActions();
return $this->crafting ?
new CraftingTransaction($this->player, $this->player->getServer()->getCraftingManager(), $inventoryActions) :
new InventoryTransaction($this->player, $inventoryActions);
}
public function buildItemStackResponse(bool $success) : ItemStackResponse{
$responseInfosByContainer = [];
foreach($this->requestSlotInfos as $requestInfo){
[$inventory, $slot] = $this->getInventoryAndSlot($requestInfo);
$item = $inventory->getItem($slot);
$info = $this->inventoryManager->trackItemStack($inventory, $slot, $item, $this->request->getRequestId());
$responseInfosByContainer[$requestInfo->getContainerId()][] = new ItemStackResponseSlotInfo(
$requestInfo->getSlotId(),
$requestInfo->getSlotId(),
$info->getItemStack()->getCount(),
$info->getStackId(),
$item->hasCustomName() ? $item->getCustomName() : "",
0
);
}
$responseContainerInfos = [];
foreach($responseInfosByContainer as $containerId => $responseInfos){
$responseContainerInfos[] = new ItemStackResponseContainerInfo($containerId, $responseInfos);
}
return new ItemStackResponse($success ? ItemStackResponse::RESULT_OK : ItemStackResponse::RESULT_ERROR, $this->request->getRequestId(), $responseContainerInfos);
}
}