Revert "Backport InventoryTransactionPacket impl from PM4"

This reverts commit cb06be615a.

we can't push this to stable because it would break plugins without any
way to know (no protocol or API change).

At most, this should have been wrapped into a protocol change.
This commit is contained in:
Dylan K. Taylor
2021-03-22 21:21:11 +00:00
parent cb06be615a
commit c7cdaeae85
9 changed files with 392 additions and 850 deletions

View File

@ -25,15 +25,9 @@ namespace pocketmine\network\mcpe\protocol;
#include <rules/DataPacket.h>
use pocketmine\network\mcpe\NetworkSession as PacketHandlerInterface;
use pocketmine\network\mcpe\NetworkSession;
use pocketmine\network\mcpe\protocol\types\inventory\InventoryTransactionChangedSlotsHack;
use pocketmine\network\mcpe\protocol\types\inventory\MismatchTransactionData;
use pocketmine\network\mcpe\protocol\types\inventory\NormalTransactionData;
use pocketmine\network\mcpe\protocol\types\inventory\ReleaseItemTransactionData;
use pocketmine\network\mcpe\protocol\types\inventory\TransactionData;
use pocketmine\network\mcpe\protocol\types\inventory\UseItemOnEntityTransactionData;
use pocketmine\network\mcpe\protocol\types\inventory\UseItemTransactionData;
use UnexpectedValueException as PacketDecodeException;
use pocketmine\network\mcpe\protocol\types\NetworkInventoryAction;
use function count;
class InventoryTransactionPacket extends DataPacket{
@ -45,70 +39,137 @@ class InventoryTransactionPacket extends DataPacket{
public const TYPE_USE_ITEM_ON_ENTITY = 3;
public const TYPE_RELEASE_ITEM = 4;
public const USE_ITEM_ACTION_CLICK_BLOCK = 0;
public const USE_ITEM_ACTION_CLICK_AIR = 1;
public const USE_ITEM_ACTION_BREAK_BLOCK = 2;
public const RELEASE_ITEM_ACTION_RELEASE = 0; //bow shoot
public const RELEASE_ITEM_ACTION_CONSUME = 1; //eat food, drink potion
public const USE_ITEM_ON_ENTITY_ACTION_INTERACT = 0;
public const USE_ITEM_ON_ENTITY_ACTION_ATTACK = 1;
/** @var int */
public $requestId;
/** @var InventoryTransactionChangedSlotsHack[] */
public $requestChangedSlots;
/** @var int */
public $transactionType;
/** @var bool */
public $hasItemStackIds;
/** @var TransactionData */
/** @var NetworkInventoryAction[] */
public $actions = [];
/** @var \stdClass */
public $trData;
protected function decodePayload() : void{
$in = $this;
$this->requestId = $in->readGenericTypeNetworkId();
protected function decodePayload(){
$this->requestId = $this->readGenericTypeNetworkId();
$this->requestChangedSlots = [];
if($this->requestId !== 0){
for($i = 0, $len = $in->getUnsignedVarInt(); $i < $len; ++$i){
$this->requestChangedSlots[] = InventoryTransactionChangedSlotsHack::read($in);
for($i = 0, $len = $this->getUnsignedVarInt(); $i < $len; ++$i){
$this->requestChangedSlots[] = InventoryTransactionChangedSlotsHack::read($this);
}
}
$transactionType = $in->getUnsignedVarInt();
$this->transactionType = $this->getUnsignedVarInt();
$this->hasItemStackIds = $in->getBool();
$this->hasItemStackIds = $this->getBool();
switch($transactionType){
for($i = 0, $count = $this->getUnsignedVarInt(); $i < $count; ++$i){
$this->actions[] = $action = (new NetworkInventoryAction())->read($this, $this->hasItemStackIds);
}
$this->trData = new \stdClass();
switch($this->transactionType){
case self::TYPE_NORMAL:
$this->trData = new NormalTransactionData();
break;
case self::TYPE_MISMATCH:
$this->trData = new MismatchTransactionData();
//Regular ComplexInventoryTransaction doesn't read any extra data
break;
case self::TYPE_USE_ITEM:
$this->trData = new UseItemTransactionData();
$this->trData->actionType = $this->getUnsignedVarInt();
$this->getBlockPosition($this->trData->x, $this->trData->y, $this->trData->z);
$this->trData->face = $this->getVarInt();
$this->trData->hotbarSlot = $this->getVarInt();
$this->trData->itemInHand = $this->getSlot();
$this->trData->playerPos = $this->getVector3();
$this->trData->clickPos = $this->getVector3();
$this->trData->blockRuntimeId = $this->getUnsignedVarInt();
break;
case self::TYPE_USE_ITEM_ON_ENTITY:
$this->trData = new UseItemOnEntityTransactionData();
$this->trData->entityRuntimeId = $this->getEntityRuntimeId();
$this->trData->actionType = $this->getUnsignedVarInt();
$this->trData->hotbarSlot = $this->getVarInt();
$this->trData->itemInHand = $this->getSlot();
$this->trData->playerPos = $this->getVector3();
$this->trData->clickPos = $this->getVector3();
break;
case self::TYPE_RELEASE_ITEM:
$this->trData = new ReleaseItemTransactionData();
$this->trData->actionType = $this->getUnsignedVarInt();
$this->trData->hotbarSlot = $this->getVarInt();
$this->trData->itemInHand = $this->getSlot();
$this->trData->headPos = $this->getVector3();
break;
default:
throw new PacketDecodeException("Unknown transaction type $transactionType");
throw new \UnexpectedValueException("Unknown transaction type $this->transactionType");
}
$this->trData->decode($in, $this->hasItemStackIds);
}
protected function encodePayload() : void{
$out = $this;
$out->writeGenericTypeNetworkId($this->requestId);
protected function encodePayload(){
$this->writeGenericTypeNetworkId($this->requestId);
if($this->requestId !== 0){
$out->putUnsignedVarInt(count($this->requestChangedSlots));
$this->putUnsignedVarInt(count($this->requestChangedSlots));
foreach($this->requestChangedSlots as $changedSlots){
$changedSlots->write($out);
$changedSlots->write($this);
}
}
$out->putUnsignedVarInt($this->trData->getTypeId());
$this->putUnsignedVarInt($this->transactionType);
$out->putBool($this->hasItemStackIds);
$this->putBool($this->hasItemStackIds);
$this->trData->encode($out, $this->hasItemStackIds);
$this->putUnsignedVarInt(count($this->actions));
foreach($this->actions as $action){
$action->write($this, $this->hasItemStackIds);
}
switch($this->transactionType){
case self::TYPE_NORMAL:
case self::TYPE_MISMATCH:
break;
case self::TYPE_USE_ITEM:
$this->putUnsignedVarInt($this->trData->actionType);
$this->putBlockPosition($this->trData->x, $this->trData->y, $this->trData->z);
$this->putVarInt($this->trData->face);
$this->putVarInt($this->trData->hotbarSlot);
$this->putSlot($this->trData->itemInHand);
$this->putVector3($this->trData->playerPos);
$this->putVector3($this->trData->clickPos);
$this->putUnsignedVarInt($this->trData->blockRuntimeId);
break;
case self::TYPE_USE_ITEM_ON_ENTITY:
$this->putEntityRuntimeId($this->trData->entityRuntimeId);
$this->putUnsignedVarInt($this->trData->actionType);
$this->putVarInt($this->trData->hotbarSlot);
$this->putSlot($this->trData->itemInHand);
$this->putVector3($this->trData->playerPos);
$this->putVector3($this->trData->clickPos);
break;
case self::TYPE_RELEASE_ITEM:
$this->putUnsignedVarInt($this->trData->actionType);
$this->putVarInt($this->trData->hotbarSlot);
$this->putSlot($this->trData->itemInHand);
$this->putVector3($this->trData->headPos);
break;
default:
throw new \InvalidArgumentException("Unknown transaction type $this->transactionType");
}
}
public function handle(PacketHandlerInterface $handler) : bool{
return $handler->handleInventoryTransaction($this);
public function handle(NetworkSession $session) : bool{
return $session->handleInventoryTransaction($this);
}
}

View File

@ -1,50 +0,0 @@
<?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\protocol\types\inventory;
use pocketmine\network\mcpe\NetworkBinaryStream as PacketSerializer;
use pocketmine\network\mcpe\protocol\InventoryTransactionPacket;
use UnexpectedValueException as PacketDecodeException;
use function count;
class MismatchTransactionData extends TransactionData{
public function getTypeId() : int{
return InventoryTransactionPacket::TYPE_MISMATCH;
}
protected function decodeData(PacketSerializer $stream) : void{
if(count($this->actions) > 0){
throw new PacketDecodeException("Mismatch transaction type should not have any actions associated with it, but got " . count($this->actions));
}
}
protected function encodeData(PacketSerializer $stream) : void{
}
public static function new() : self{
return new self; //no arguments
}
}

View File

@ -1,54 +0,0 @@
<?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\protocol\types\inventory;
use pocketmine\network\mcpe\NetworkBinaryStream as PacketSerializer;
use pocketmine\network\mcpe\protocol\InventoryTransactionPacket;
use pocketmine\network\mcpe\protocol\types\NetworkInventoryAction;
class NormalTransactionData extends TransactionData{
public function getTypeId() : int{
return InventoryTransactionPacket::TYPE_NORMAL;
}
protected function decodeData(PacketSerializer $stream) : void{
}
protected function encodeData(PacketSerializer $stream) : void{
}
/**
* @param NetworkInventoryAction[] $actions
*
* @return NormalTransactionData
*/
public static function new(array $actions) : self{
$result = new self();
$result->actions = $actions;
return $result;
}
}

View File

@ -1,92 +0,0 @@
<?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\protocol\types\inventory;
use pocketmine\item\Item as ItemStack;
use pocketmine\math\Vector3;
use pocketmine\network\mcpe\NetworkBinaryStream as PacketSerializer;
use pocketmine\network\mcpe\protocol\InventoryTransactionPacket;
use pocketmine\network\mcpe\protocol\types\NetworkInventoryAction;
class ReleaseItemTransactionData extends TransactionData{
public const ACTION_RELEASE = 0; //bow shoot
public const ACTION_CONSUME = 1; //eat food, drink potion
/** @var int */
private $actionType;
/** @var int */
private $hotbarSlot;
/** @var ItemStack */
private $itemInHand;
/** @var Vector3 */
private $headPos;
public function getActionType() : int{
return $this->actionType;
}
public function getHotbarSlot() : int{
return $this->hotbarSlot;
}
public function getItemInHand() : ItemStack{
return $this->itemInHand;
}
public function getHeadPos() : Vector3{
return $this->headPos;
}
public function getTypeId() : int{
return InventoryTransactionPacket::TYPE_RELEASE_ITEM;
}
protected function decodeData(PacketSerializer $stream) : void{
$this->actionType = $stream->getUnsignedVarInt();
$this->hotbarSlot = $stream->getVarInt();
$this->itemInHand = $stream->getSlot();
$this->headPos = $stream->getVector3();
}
protected function encodeData(PacketSerializer $stream) : void{
$stream->putUnsignedVarInt($this->actionType);
$stream->putVarInt($this->hotbarSlot);
$stream->putSlot($this->itemInHand);
$stream->putVector3($this->headPos);
}
/**
* @param NetworkInventoryAction[] $actions
*/
public static function new(array $actions, int $actionType, int $hotbarSlot, ItemStack $itemInHand, Vector3 $headPos) : self{
$result = new self;
$result->actions = $actions;
$result->actionType = $actionType;
$result->hotbarSlot = $hotbarSlot;
$result->itemInHand = $itemInHand;
$result->headPos = $headPos;
return $result;
}
}

View File

@ -1,72 +0,0 @@
<?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\protocol\types\inventory;
use pocketmine\network\mcpe\NetworkBinaryStream as PacketSerializer;
use pocketmine\network\mcpe\protocol\types\NetworkInventoryAction;
use pocketmine\utils\BinaryDataException;
use UnexpectedValueException as PacketDecodeException;
use function count;
abstract class TransactionData{
/** @var NetworkInventoryAction[] */
protected $actions = [];
/**
* @return NetworkInventoryAction[]
*/
final public function getActions() : array{
return $this->actions;
}
abstract public function getTypeId() : int;
/**
* @throws BinaryDataException
* @throws PacketDecodeException
*/
final public function decode(PacketSerializer $stream, bool $hasItemStackIds) : void{
$actionCount = $stream->getUnsignedVarInt();
for($i = 0; $i < $actionCount; ++$i){
$this->actions[] = (new NetworkInventoryAction())->read($stream, $hasItemStackIds);
}
$this->decodeData($stream);
}
/**
* @throws BinaryDataException
* @throws PacketDecodeException
*/
abstract protected function decodeData(PacketSerializer $stream) : void;
final public function encode(PacketSerializer $stream, bool $hasItemStackIds) : void{
$stream->putUnsignedVarInt(count($this->actions));
foreach($this->actions as $action){
$action->write($stream, $hasItemStackIds);
}
$this->encodeData($stream);
}
abstract protected function encodeData(PacketSerializer $stream) : void;
}

View File

@ -1,109 +0,0 @@
<?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\protocol\types\inventory;
use pocketmine\item\Item as ItemStack;
use pocketmine\math\Vector3;
use pocketmine\network\mcpe\NetworkBinaryStream as PacketSerializer;
use pocketmine\network\mcpe\protocol\InventoryTransactionPacket;
use pocketmine\network\mcpe\protocol\types\NetworkInventoryAction;
class UseItemOnEntityTransactionData extends TransactionData{
public const ACTION_INTERACT = 0;
public const ACTION_ATTACK = 1;
/** @var int */
private $entityRuntimeId;
/** @var int */
private $actionType;
/** @var int */
private $hotbarSlot;
/** @var ItemStack */
private $itemInHand;
/** @var Vector3 */
private $playerPos;
/** @var Vector3 */
private $clickPos;
public function getEntityRuntimeId() : int{
return $this->entityRuntimeId;
}
public function getActionType() : int{
return $this->actionType;
}
public function getHotbarSlot() : int{
return $this->hotbarSlot;
}
public function getItemInHand() : ItemStack{
return $this->itemInHand;
}
public function getPlayerPos() : Vector3{
return $this->playerPos;
}
public function getClickPos() : Vector3{
return $this->clickPos;
}
public function getTypeId() : int{
return InventoryTransactionPacket::TYPE_USE_ITEM_ON_ENTITY;
}
protected function decodeData(PacketSerializer $stream) : void{
$this->entityRuntimeId = $stream->getEntityRuntimeId();
$this->actionType = $stream->getUnsignedVarInt();
$this->hotbarSlot = $stream->getVarInt();
$this->itemInHand = $stream->getSlot();
$this->playerPos = $stream->getVector3();
$this->clickPos = $stream->getVector3();
}
protected function encodeData(PacketSerializer $stream) : void{
$stream->putEntityRuntimeId($this->entityRuntimeId);
$stream->putUnsignedVarInt($this->actionType);
$stream->putVarInt($this->hotbarSlot);
$stream->putSlot($this->itemInHand);
$stream->putVector3($this->playerPos);
$stream->putVector3($this->clickPos);
}
/**
* @param NetworkInventoryAction[] $actions
*/
public static function new(array $actions, int $entityRuntimeId, int $actionType, int $hotbarSlot, ItemStack $itemInHand, Vector3 $playerPos, Vector3 $clickPos) : self{
$result = new self;
$result->actions = $actions;
$result->entityRuntimeId = $entityRuntimeId;
$result->actionType = $actionType;
$result->hotbarSlot = $hotbarSlot;
$result->itemInHand = $itemInHand;
$result->playerPos = $playerPos;
$result->clickPos = $clickPos;
return $result;
}
}

View File

@ -1,130 +0,0 @@
<?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\protocol\types\inventory;
use pocketmine\item\Item as ItemStack;
use pocketmine\math\Vector3;
use pocketmine\network\mcpe\NetworkBinaryStream as PacketSerializer;
use pocketmine\network\mcpe\protocol\InventoryTransactionPacket;
use pocketmine\network\mcpe\protocol\types\NetworkInventoryAction;
class UseItemTransactionData extends TransactionData{
public const ACTION_CLICK_BLOCK = 0;
public const ACTION_CLICK_AIR = 1;
public const ACTION_BREAK_BLOCK = 2;
/** @var int */
private $actionType;
/** @var Vector3 */
private $blockPos;
/** @var int */
private $face;
/** @var int */
private $hotbarSlot;
/** @var ItemStack */
private $itemInHand;
/** @var Vector3 */
private $playerPos;
/** @var Vector3 */
private $clickPos;
/** @var int */
private $blockRuntimeId;
public function getActionType() : int{
return $this->actionType;
}
public function getBlockPos() : Vector3{
return $this->blockPos;
}
public function getFace() : int{
return $this->face;
}
public function getHotbarSlot() : int{
return $this->hotbarSlot;
}
public function getItemInHand() : ItemStack{
return $this->itemInHand;
}
public function getPlayerPos() : Vector3{
return $this->playerPos;
}
public function getClickPos() : Vector3{
return $this->clickPos;
}
public function getBlockRuntimeId() : int{
return $this->blockRuntimeId;
}
public function getTypeId() : int{
return InventoryTransactionPacket::TYPE_USE_ITEM;
}
protected function decodeData(PacketSerializer $stream) : void{
$this->actionType = $stream->getUnsignedVarInt();
$x = $y = $z = 0;
$stream->getBlockPosition($x, $y, $z);
$this->blockPos = new Vector3($x, $y, $z);
$this->face = $stream->getVarInt();
$this->hotbarSlot = $stream->getVarInt();
$this->itemInHand = $stream->getSlot();
$this->playerPos = $stream->getVector3();
$this->clickPos = $stream->getVector3();
$this->blockRuntimeId = $stream->getUnsignedVarInt();
}
protected function encodeData(PacketSerializer $stream) : void{
$stream->putUnsignedVarInt($this->actionType);
$stream->putBlockPosition($this->blockPos->x, $this->blockPos->y, $this->blockPos->z);
$stream->putVarInt($this->face);
$stream->putVarInt($this->hotbarSlot);
$stream->putSlot($this->itemInHand);
$stream->putVector3($this->playerPos);
$stream->putVector3($this->clickPos);
$stream->putUnsignedVarInt($this->blockRuntimeId);
}
/**
* @param NetworkInventoryAction[] $actions
*/
public static function new(array $actions, int $actionType, Vector3 $blockPos, int $face, int $hotbarSlot, ItemStack $itemInHand, Vector3 $playerPos, Vector3 $clickPos, int $blockRuntimeId) : self{
$result = new self;
$result->actions = $actions;
$result->actionType = $actionType;
$result->blockPos = $blockPos;
$result->face = $face;
$result->hotbarSlot = $hotbarSlot;
$result->itemInHand = $itemInHand;
$result->playerPos = $playerPos;
$result->clickPos = $clickPos;
$result->blockRuntimeId = $blockRuntimeId;
return $result;
}
}