mirror of
https://github.com/pmmp/PocketMine-MP.git
synced 2025-09-08 10:53:05 +00:00
Cleaned up the InventoryTransactionPacket decoding clusterfuck
@shoghicp, y u do dis... I almost created a sub-packet architecture to deal with this shit :( This mess really ought to be split into multiple packets. Perhaps the PacketPool can be extended to do that in the future.
This commit is contained in:
@ -26,8 +26,16 @@ namespace pocketmine\network\mcpe\protocol;
|
||||
#include <rules/DataPacket.h>
|
||||
|
||||
use pocketmine\network\mcpe\handler\SessionHandler;
|
||||
use pocketmine\network\mcpe\protocol\types\NetworkInventoryAction;
|
||||
use pocketmine\network\mcpe\protocol\types\MismatchTransactionData;
|
||||
use pocketmine\network\mcpe\protocol\types\NormalTransactionData;
|
||||
use pocketmine\network\mcpe\protocol\types\ReleaseItemTransactionData;
|
||||
use pocketmine\network\mcpe\protocol\types\TransactionData;
|
||||
use pocketmine\network\mcpe\protocol\types\UseItemOnEntityTransactionData;
|
||||
use pocketmine\network\mcpe\protocol\types\UseItemTransactionData;
|
||||
|
||||
/**
|
||||
* This packet effectively crams multiple packets into one.
|
||||
*/
|
||||
class InventoryTransactionPacket extends DataPacket{
|
||||
public const NETWORK_ID = ProtocolInfo::INVENTORY_TRANSACTION_PACKET;
|
||||
|
||||
@ -37,118 +45,38 @@ 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 $transactionType;
|
||||
|
||||
/**
|
||||
* @var bool
|
||||
* NOTE: THIS FIELD DOES NOT EXIST IN THE PROTOCOL, it's merely used for convenience for PocketMine-MP to easily
|
||||
* determine whether we're doing a crafting transaction.
|
||||
*/
|
||||
public $isCraftingPart = false;
|
||||
/**
|
||||
* @var bool
|
||||
* NOTE: THIS FIELD DOES NOT EXIST IN THE PROTOCOL, it's merely used for convenience for PocketMine-MP to easily
|
||||
* determine whether we're doing a crafting transaction.
|
||||
*/
|
||||
public $isFinalCraftingPart = false;
|
||||
|
||||
/** @var NetworkInventoryAction[] */
|
||||
public $actions = [];
|
||||
|
||||
/** @var \stdClass */
|
||||
/** @var TransactionData */
|
||||
public $trData;
|
||||
|
||||
protected function decodePayload() : void{
|
||||
$this->transactionType = $this->getUnsignedVarInt();
|
||||
$transactionType = $this->getUnsignedVarInt();
|
||||
|
||||
for($i = 0, $count = $this->getUnsignedVarInt(); $i < $count; ++$i){
|
||||
$this->actions[] = (new NetworkInventoryAction())->read($this);
|
||||
}
|
||||
|
||||
$this->trData = new \stdClass();
|
||||
|
||||
switch($this->transactionType){
|
||||
switch($transactionType){
|
||||
case self::TYPE_NORMAL:
|
||||
$this->trData = new NormalTransactionData();
|
||||
break;
|
||||
case self::TYPE_MISMATCH:
|
||||
//Regular ComplexInventoryTransaction doesn't read any extra data
|
||||
$this->trData = new MismatchTransactionData();
|
||||
break;
|
||||
case self::TYPE_USE_ITEM:
|
||||
$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 = new UseItemTransactionData();
|
||||
break;
|
||||
case self::TYPE_USE_ITEM_ON_ENTITY:
|
||||
$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();
|
||||
$this->trData = new UseItemOnEntityTransactionData();
|
||||
break;
|
||||
case self::TYPE_RELEASE_ITEM:
|
||||
$this->trData->actionType = $this->getUnsignedVarInt();
|
||||
$this->trData->hotbarSlot = $this->getVarInt();
|
||||
$this->trData->itemInHand = $this->getSlot();
|
||||
$this->trData->headPos = $this->getVector3();
|
||||
$this->trData = new ReleaseItemTransactionData();
|
||||
break;
|
||||
default:
|
||||
throw new \UnexpectedValueException("Unknown transaction type $this->transactionType");
|
||||
throw new \UnexpectedValueException("Unknown transaction type $transactionType");
|
||||
}
|
||||
|
||||
$this->trData->decode($this);
|
||||
}
|
||||
|
||||
protected function encodePayload() : void{
|
||||
$this->putUnsignedVarInt($this->transactionType);
|
||||
|
||||
$this->putUnsignedVarInt(count($this->actions));
|
||||
foreach($this->actions as $action){
|
||||
$action->write($this);
|
||||
}
|
||||
|
||||
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);
|
||||
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 \UnexpectedValueException("Unknown transaction type $this->transactionType");
|
||||
}
|
||||
$this->putUnsignedVarInt($this->trData->getTypeId());
|
||||
$this->trData->encode($this);
|
||||
}
|
||||
|
||||
public function handle(SessionHandler $handler) : bool{
|
||||
|
Reference in New Issue
Block a user