Added encode for InventoryTransactionPacket and refactor some stuff

This commit is contained in:
Dylan K. Taylor 2017-09-13 11:14:04 +01:00
parent 23a38400e2
commit 0fac3b9a9d
3 changed files with 77 additions and 43 deletions

View File

@ -2189,7 +2189,7 @@ class Player extends Human implements CommandSender, ChunkLoader, IPlayer{
} }
} }
switch($packet->transactionData->transactionType){ switch($packet->transactionType){
case InventoryTransactionPacket::TYPE_NORMAL: case InventoryTransactionPacket::TYPE_NORMAL:
$transaction = new SimpleInventoryTransaction($this, $actions); $transaction = new SimpleInventoryTransaction($this, $actions);
@ -2218,10 +2218,10 @@ class Player extends Human implements CommandSender, ChunkLoader, IPlayer{
return true; return true;
case InventoryTransactionPacket::TYPE_USE_ITEM: case InventoryTransactionPacket::TYPE_USE_ITEM:
$blockVector = new Vector3($packet->transactionData->x, $packet->transactionData->y, $packet->transactionData->z); $blockVector = new Vector3($packet->trData->x, $packet->trData->y, $packet->trData->z);
$face = $packet->transactionData->face; $face = $packet->trData->face;
$type = $packet->transactionData->useItemActionType; $type = $packet->trData->actionType;
switch($type){ switch($type){
case InventoryTransactionPacket::USE_ITEM_ACTION_CLICK_BLOCK: case InventoryTransactionPacket::USE_ITEM_ACTION_CLICK_BLOCK:
$this->setGenericFlag(self::DATA_FLAG_ACTION, false); $this->setGenericFlag(self::DATA_FLAG_ACTION, false);
@ -2229,15 +2229,15 @@ class Player extends Human implements CommandSender, ChunkLoader, IPlayer{
if(!$this->canInteract($blockVector->add(0.5, 0.5, 0.5), 13) or $this->isSpectator()){ if(!$this->canInteract($blockVector->add(0.5, 0.5, 0.5), 13) or $this->isSpectator()){
}elseif($this->isCreative()){ }elseif($this->isCreative()){
$item = $this->inventory->getItemInHand(); $item = $this->inventory->getItemInHand();
if($this->level->useItemOn($blockVector, $item, $face, $packet->transactionData->clickPos, $this, true) === true){ if($this->level->useItemOn($blockVector, $item, $face, $packet->trData->clickPos, $this, true) === true){
return true; return true;
} }
}elseif(!$this->inventory->getItemInHand()->equals($packet->transactionData->itemInHand)){ }elseif(!$this->inventory->getItemInHand()->equals($packet->trData->itemInHand)){
$this->inventory->sendHeldItem($this); $this->inventory->sendHeldItem($this);
}else{ }else{
$item = $this->inventory->getItemInHand(); $item = $this->inventory->getItemInHand();
$oldItem = clone $item; $oldItem = clone $item;
if($this->level->useItemOn($blockVector, $item, $face, $packet->transactionData->clickPos, $this, true)){ if($this->level->useItemOn($blockVector, $item, $face, $packet->trData->clickPos, $this, true)){
if(!$item->equals($oldItem) or $item->getCount() !== $oldItem->getCount()){ if(!$item->equals($oldItem) or $item->getCount() !== $oldItem->getCount()){
$this->inventory->setItemInHand($item); $this->inventory->setItemInHand($item);
$this->inventory->sendHeldItem($this->hasSpawned); $this->inventory->sendHeldItem($this->hasSpawned);
@ -2298,7 +2298,7 @@ class Player extends Human implements CommandSender, ChunkLoader, IPlayer{
if($this->isCreative()){ if($this->isCreative()){
$item = $this->inventory->getItemInHand(); $item = $this->inventory->getItemInHand();
}elseif(!$this->inventory->getItemInHand()->equals($packet->transactionData->itemInHand)){ }elseif(!$this->inventory->getItemInHand()->equals($packet->trData->itemInHand)){
$this->inventory->sendHeldItem($this); $this->inventory->sendHeldItem($this);
return true; return true;
}else{ }else{
@ -2328,12 +2328,12 @@ class Player extends Human implements CommandSender, ChunkLoader, IPlayer{
} }
break; break;
case InventoryTransactionPacket::TYPE_USE_ITEM_ON_ENTITY: case InventoryTransactionPacket::TYPE_USE_ITEM_ON_ENTITY:
$target = $this->level->getEntity($packet->transactionData->entityRuntimeId); $target = $this->level->getEntity($packet->trData->entityRuntimeId);
if($target === null){ if($target === null){
return false; return false;
} }
$type = $packet->transactionData->actionType; $type = $packet->trData->actionType;
switch($type){ switch($type){
case InventoryTransactionPacket::USE_ITEM_ON_ENTITY_ACTION_INTERACT: case InventoryTransactionPacket::USE_ITEM_ON_ENTITY_ACTION_INTERACT:
@ -2407,7 +2407,7 @@ class Player extends Human implements CommandSender, ChunkLoader, IPlayer{
break; break;
case InventoryTransactionPacket::TYPE_RELEASE_ITEM: case InventoryTransactionPacket::TYPE_RELEASE_ITEM:
$type = $packet->transactionData->releaseItemActionType; $type = $packet->trData->actionType;
switch($type){ switch($type){
case InventoryTransactionPacket::RELEASE_ITEM_ACTION_RELEASE: case InventoryTransactionPacket::RELEASE_ITEM_ACTION_RELEASE:
if($this->isUsingItem()){ if($this->isUsingItem()){

View File

@ -56,64 +56,95 @@ class InventoryTransactionPacket extends DataPacket{
const ACTION_MAGIC_SLOT_CREATIVE_DELETE_ITEM = 0; const ACTION_MAGIC_SLOT_CREATIVE_DELETE_ITEM = 0;
const ACTION_MAGIC_SLOT_CREATIVE_CREATE_ITEM = 1; const ACTION_MAGIC_SLOT_CREATIVE_CREATE_ITEM = 1;
/** @var int */
public $transactionType;
/** @var NetworkInventoryAction[] */ /** @var NetworkInventoryAction[] */
public $actions = []; public $actions = [];
/** @var \stdClass */ /** @var \stdClass */
public $transactionData; public $trData;
protected function decodePayload(){ protected function decodePayload(){
$type = $this->getUnsignedVarInt(); $this->transactionType = $this->getUnsignedVarInt();
$actionCount = $this->getUnsignedVarInt(); for($i = 0, $count = $this->getUnsignedVarInt(); $i < $count; ++$i){
for($i = 0; $i < $actionCount; ++$i){ $this->actions[] = (new NetworkInventoryAction())->read($this);
$this->actions[] = $this->decodeInventoryAction();
} }
$this->transactionData = new \stdClass(); $this->trData = new \stdClass();
$this->transactionData->transactionType = $type;
switch($type){ switch($this->transactionType){
case self::TYPE_NORMAL: case self::TYPE_NORMAL:
case self::TYPE_MISMATCH: case self::TYPE_MISMATCH:
//Regular ComplexInventoryTransaction doesn't read any extra data //Regular ComplexInventoryTransaction doesn't read any extra data
break; break;
case self::TYPE_USE_ITEM: case self::TYPE_USE_ITEM:
$this->transactionData->useItemActionType = $this->getUnsignedVarInt(); $this->trData->actionType = $this->getUnsignedVarInt();
$this->getBlockPosition($this->transactionData->x, $this->transactionData->y, $this->transactionData->z); $this->getBlockPosition($this->trData->x, $this->trData->y, $this->trData->z);
$this->transactionData->face = $this->getVarInt(); $this->trData->face = $this->getVarInt();
$this->transactionData->hotbarSlot = $this->getVarInt(); $this->trData->hotbarSlot = $this->getVarInt();
$this->transactionData->itemInHand = $this->getSlot(); $this->trData->itemInHand = $this->getSlot();
$this->transactionData->playerPos = $this->getVector3Obj(); $this->trData->playerPos = $this->getVector3Obj();
$this->transactionData->clickPos = $this->getVector3Obj(); $this->trData->clickPos = $this->getVector3Obj();
break; break;
case self::TYPE_USE_ITEM_ON_ENTITY: case self::TYPE_USE_ITEM_ON_ENTITY:
$this->transactionData->entityRuntimeId = $this->getEntityRuntimeId(); $this->trData->entityRuntimeId = $this->getEntityRuntimeId();
$this->transactionData->actionType = $this->getUnsignedVarInt(); $this->trData->actionType = $this->getUnsignedVarInt();
$this->transactionData->hotbarSlot = $this->getVarInt(); $this->trData->hotbarSlot = $this->getVarInt();
$this->transactionData->itemInHand = $this->getSlot(); $this->trData->itemInHand = $this->getSlot();
$this->transactionData->vector1 = $this->getVector3Obj(); $this->trData->vector1 = $this->getVector3Obj();
$this->transactionData->vector2 = $this->getVector3Obj(); $this->trData->vector2 = $this->getVector3Obj();
break; break;
case self::TYPE_RELEASE_ITEM: case self::TYPE_RELEASE_ITEM:
$this->transactionData->releaseItemActionType = $this->getUnsignedVarInt(); $this->trData->actionType = $this->getUnsignedVarInt();
$this->transactionData->hotbarSlot = $this->getVarInt(); $this->trData->hotbarSlot = $this->getVarInt();
$this->transactionData->itemInHand = $this->getSlot(); $this->trData->itemInHand = $this->getSlot();
$this->transactionData->headPos = $this->getVector3Obj(); $this->trData->headPos = $this->getVector3Obj();
break; break;
default: default:
throw new \UnexpectedValueException("Unknown transaction type $type"); throw new \UnexpectedValueException("Unknown transaction type $this->transactionType");
} }
} }
protected function encodePayload(){ protected function encodePayload(){
//TODO $this->putUnsignedVarInt($this->transactionType);
}
protected function decodeInventoryAction(){ $this->putUnsignedVarInt(count($this->actions));
$action = new NetworkInventoryAction(); foreach($this->actions as $action){
$action->read($this); $action->write($this);
return $action; }
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->putVector3Obj($this->trData->playerPos);
$this->putVector3Obj($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->putVector3Obj($this->trData->vector1);
$this->putVector3Obj($this->trData->vector2);
break;
case self::TYPE_RELEASE_ITEM:
$this->putUnsignedVarInt($this->trData->actionType);
$this->putVarInt($this->trData->hotbarSlot);
$this->putSlot($this->trData->itemInHand);
$this->putVector3Obj($this->trData->headPos);
break;
default:
throw new \UnexpectedValueException("Unknown transaction type $this->transactionType");
}
} }
public function handle(NetworkSession $session) : bool{ public function handle(NetworkSession $session) : bool{

View File

@ -86,6 +86,7 @@ class NetworkInventoryAction{
/** /**
* @param InventoryTransactionPacket $packet * @param InventoryTransactionPacket $packet
* @return $this
*/ */
public function read(InventoryTransactionPacket $packet){ public function read(InventoryTransactionPacket $packet){
$this->sourceType = $packet->getUnsignedVarInt(); $this->sourceType = $packet->getUnsignedVarInt();
@ -107,6 +108,8 @@ class NetworkInventoryAction{
$this->inventorySlot = $packet->getUnsignedVarInt(); $this->inventorySlot = $packet->getUnsignedVarInt();
$this->oldItem = $packet->getSlot(); $this->oldItem = $packet->getSlot();
$this->newItem = $packet->getSlot(); $this->newItem = $packet->getSlot();
return $this;
} }
/** /**