Fixed up offhand handling for ItemStackRequest, fixes #5723

This commit is contained in:
Dylan K. Taylor 2023-04-27 13:25:08 +01:00
parent 5a54d09869
commit 7c19f14cf5
No known key found for this signature in database
GPG Key ID: 8927471A91CAFD3D
4 changed files with 16 additions and 19 deletions

View File

@ -356,8 +356,8 @@ class InGamePacketHandler extends PacketHandler{
//rejects the transaction. The most common example of this is equipping armor by right-click, which doesn't send //rejects the transaction. The most common example of this is equipping armor by right-click, which doesn't send
//a legacy prediction action for the destination armor slot. //a legacy prediction action for the destination armor slot.
foreach($packet->requestChangedSlots as $containerInfo){ foreach($packet->requestChangedSlots as $containerInfo){
$windowId = ItemStackContainerIdTranslator::translate($containerInfo->getContainerId(), $this->inventoryManager->getCurrentWindowId()); foreach($containerInfo->getChangedSlotIndexes() as $netSlot){
foreach($containerInfo->getChangedSlotIndexes() as $slot){ [$windowId, $slot] = ItemStackContainerIdTranslator::translate($containerInfo->getContainerId(), $this->inventoryManager->getCurrentWindowId(), $netSlot);
$inventoryAndSlot = $this->inventoryManager->locateWindowAndSlot($windowId, $slot); $inventoryAndSlot = $this->inventoryManager->locateWindowAndSlot($windowId, $slot);
if($inventoryAndSlot !== null){ //trigger the normal slot sync logic if($inventoryAndSlot !== null){ //trigger the normal slot sync logic
$this->inventoryManager->onSlotChange($inventoryAndSlot[0], $inventoryAndSlot[1]); $this->inventoryManager->onSlotChange($inventoryAndSlot[0], $inventoryAndSlot[1]);

View File

@ -33,15 +33,21 @@ final class ItemStackContainerIdTranslator{
//NOOP //NOOP
} }
public static function translate(int $containerInterfaceId, int $currentWindowId) : int{ /**
* @return int[]
* @phpstan-return array{int, int}
* @throws PacketHandlingException
*/
public static function translate(int $containerInterfaceId, int $currentWindowId, int $slotId) : array{
return match($containerInterfaceId){ return match($containerInterfaceId){
ContainerUIIds::ARMOR => ContainerIds::ARMOR, ContainerUIIds::ARMOR => [ContainerIds::ARMOR, $slotId],
ContainerUIIds::HOTBAR, ContainerUIIds::HOTBAR,
ContainerUIIds::INVENTORY, ContainerUIIds::INVENTORY,
ContainerUIIds::COMBINED_HOTBAR_AND_INVENTORY => ContainerIds::INVENTORY, ContainerUIIds::COMBINED_HOTBAR_AND_INVENTORY => [ContainerIds::INVENTORY, $slotId],
ContainerUIIds::OFFHAND => ContainerIds::OFFHAND, //TODO: HACK! The client sends an incorrect slot ID for the offhand as of 1.19.70 (though this doesn't really matter since the offhand has only 1 slot anyway)
ContainerUIIds::OFFHAND => [ContainerIds::OFFHAND, 0],
ContainerUIIds::ANVIL_INPUT, ContainerUIIds::ANVIL_INPUT,
ContainerUIIds::ANVIL_MATERIAL, ContainerUIIds::ANVIL_MATERIAL,
@ -68,7 +74,7 @@ final class ItemStackContainerIdTranslator{
ContainerUIIds::TRADE2_INGREDIENT1, ContainerUIIds::TRADE2_INGREDIENT1,
ContainerUIIds::TRADE2_INGREDIENT2, ContainerUIIds::TRADE2_INGREDIENT2,
ContainerUIIds::TRADE_INGREDIENT1, ContainerUIIds::TRADE_INGREDIENT1,
ContainerUIIds::TRADE_INGREDIENT2 => ContainerIds::UI, ContainerUIIds::TRADE_INGREDIENT2 => [ContainerIds::UI, $slotId],
ContainerUIIds::BARREL, ContainerUIIds::BARREL,
ContainerUIIds::BLAST_FURNACE_INGREDIENT, ContainerUIIds::BLAST_FURNACE_INGREDIENT,
@ -80,7 +86,7 @@ final class ItemStackContainerIdTranslator{
ContainerUIIds::FURNACE_RESULT, ContainerUIIds::FURNACE_RESULT,
ContainerUIIds::LEVEL_ENTITY, //chest ContainerUIIds::LEVEL_ENTITY, //chest
ContainerUIIds::SHULKER_BOX, ContainerUIIds::SHULKER_BOX,
ContainerUIIds::SMOKER_INGREDIENT => $currentWindowId, ContainerUIIds::SMOKER_INGREDIENT => [$currentWindowId, $slotId],
//all preview slots are ignored, since the client shouldn't be modifying those directly //all preview slots are ignored, since the client shouldn't be modifying those directly

View File

@ -111,12 +111,7 @@ class ItemStackRequestExecutor{
* @throws ItemStackRequestProcessException * @throws ItemStackRequestProcessException
*/ */
protected function getBuilderInventoryAndSlot(ItemStackRequestSlotInfo $info) : array{ protected function getBuilderInventoryAndSlot(ItemStackRequestSlotInfo $info) : array{
$windowId = ItemStackContainerIdTranslator::translate($info->getContainerId(), $this->inventoryManager->getCurrentWindowId()); [$windowId, $slotId] = ItemStackContainerIdTranslator::translate($info->getContainerId(), $this->inventoryManager->getCurrentWindowId(), $info->getSlotId());
$slotId = $info->getSlotId();
if($info->getContainerId() === ContainerUIIds::OFFHAND && $slotId === 1){
//TODO: HACK! The client sends an incorrect slot ID for the offhand as of 1.19.70
$slotId = 0;
}
$windowAndSlot = $this->inventoryManager->locateWindowAndSlot($windowId, $slotId); $windowAndSlot = $this->inventoryManager->locateWindowAndSlot($windowId, $slotId);
if($windowAndSlot === null){ if($windowAndSlot === null){
throw new ItemStackRequestProcessException("No open inventory matches container UI ID: " . $info->getContainerId() . ", slot ID: " . $info->getSlotId()); throw new ItemStackRequestProcessException("No open inventory matches container UI ID: " . $info->getContainerId() . ", slot ID: " . $info->getSlotId());

View File

@ -53,11 +53,7 @@ final class ItemStackResponseBuilder{
* @phpstan-return array{Inventory, int} * @phpstan-return array{Inventory, int}
*/ */
private function getInventoryAndSlot(int $containerInterfaceId, int $slotId) : ?array{ private function getInventoryAndSlot(int $containerInterfaceId, int $slotId) : ?array{
if($containerInterfaceId === ContainerUIIds::OFFHAND && $slotId === 1){ [$windowId, $slotId] = ItemStackContainerIdTranslator::translate($containerInterfaceId, $this->inventoryManager->getCurrentWindowId(), $slotId);
//TODO: HACK! The client sends an incorrect slot ID for the offhand as of 1.19.70
$slotId = 0;
}
$windowId = ItemStackContainerIdTranslator::translate($containerInterfaceId, $this->inventoryManager->getCurrentWindowId());
$windowAndSlot = $this->inventoryManager->locateWindowAndSlot($windowId, $slotId); $windowAndSlot = $this->inventoryManager->locateWindowAndSlot($windowId, $slotId);
if($windowAndSlot === null){ if($windowAndSlot === null){
return null; return null;