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:
$transaction = new SimpleInventoryTransaction($this, $actions);
@ -2218,10 +2218,10 @@ class Player extends Human implements CommandSender, ChunkLoader, IPlayer{
return true;
case InventoryTransactionPacket::TYPE_USE_ITEM:
$blockVector = new Vector3($packet->transactionData->x, $packet->transactionData->y, $packet->transactionData->z);
$face = $packet->transactionData->face;
$blockVector = new Vector3($packet->trData->x, $packet->trData->y, $packet->trData->z);
$face = $packet->trData->face;
$type = $packet->transactionData->useItemActionType;
$type = $packet->trData->actionType;
switch($type){
case InventoryTransactionPacket::USE_ITEM_ACTION_CLICK_BLOCK:
$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()){
}elseif($this->isCreative()){
$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;
}
}elseif(!$this->inventory->getItemInHand()->equals($packet->transactionData->itemInHand)){
}elseif(!$this->inventory->getItemInHand()->equals($packet->trData->itemInHand)){
$this->inventory->sendHeldItem($this);
}else{
$item = $this->inventory->getItemInHand();
$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()){
$this->inventory->setItemInHand($item);
$this->inventory->sendHeldItem($this->hasSpawned);
@ -2298,7 +2298,7 @@ class Player extends Human implements CommandSender, ChunkLoader, IPlayer{
if($this->isCreative()){
$item = $this->inventory->getItemInHand();
}elseif(!$this->inventory->getItemInHand()->equals($packet->transactionData->itemInHand)){
}elseif(!$this->inventory->getItemInHand()->equals($packet->trData->itemInHand)){
$this->inventory->sendHeldItem($this);
return true;
}else{
@ -2328,12 +2328,12 @@ class Player extends Human implements CommandSender, ChunkLoader, IPlayer{
}
break;
case InventoryTransactionPacket::TYPE_USE_ITEM_ON_ENTITY:
$target = $this->level->getEntity($packet->transactionData->entityRuntimeId);
$target = $this->level->getEntity($packet->trData->entityRuntimeId);
if($target === null){
return false;
}
$type = $packet->transactionData->actionType;
$type = $packet->trData->actionType;
switch($type){
case InventoryTransactionPacket::USE_ITEM_ON_ENTITY_ACTION_INTERACT:
@ -2407,7 +2407,7 @@ class Player extends Human implements CommandSender, ChunkLoader, IPlayer{
break;
case InventoryTransactionPacket::TYPE_RELEASE_ITEM:
$type = $packet->transactionData->releaseItemActionType;
$type = $packet->trData->actionType;
switch($type){
case InventoryTransactionPacket::RELEASE_ITEM_ACTION_RELEASE:
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_CREATE_ITEM = 1;
/** @var int */
public $transactionType;
/** @var NetworkInventoryAction[] */
public $actions = [];
/** @var \stdClass */
public $transactionData;
public $trData;
protected function decodePayload(){
$type = $this->getUnsignedVarInt();
$this->transactionType = $this->getUnsignedVarInt();
$actionCount = $this->getUnsignedVarInt();
for($i = 0; $i < $actionCount; ++$i){
$this->actions[] = $this->decodeInventoryAction();
for($i = 0, $count = $this->getUnsignedVarInt(); $i < $count; ++$i){
$this->actions[] = (new NetworkInventoryAction())->read($this);
}
$this->transactionData = new \stdClass();
$this->transactionData->transactionType = $type;
$this->trData = new \stdClass();
switch($type){
switch($this->transactionType){
case self::TYPE_NORMAL:
case self::TYPE_MISMATCH:
//Regular ComplexInventoryTransaction doesn't read any extra data
break;
case self::TYPE_USE_ITEM:
$this->transactionData->useItemActionType = $this->getUnsignedVarInt();
$this->getBlockPosition($this->transactionData->x, $this->transactionData->y, $this->transactionData->z);
$this->transactionData->face = $this->getVarInt();
$this->transactionData->hotbarSlot = $this->getVarInt();
$this->transactionData->itemInHand = $this->getSlot();
$this->transactionData->playerPos = $this->getVector3Obj();
$this->transactionData->clickPos = $this->getVector3Obj();
$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->getVector3Obj();
$this->trData->clickPos = $this->getVector3Obj();
break;
case self::TYPE_USE_ITEM_ON_ENTITY:
$this->transactionData->entityRuntimeId = $this->getEntityRuntimeId();
$this->transactionData->actionType = $this->getUnsignedVarInt();
$this->transactionData->hotbarSlot = $this->getVarInt();
$this->transactionData->itemInHand = $this->getSlot();
$this->transactionData->vector1 = $this->getVector3Obj();
$this->transactionData->vector2 = $this->getVector3Obj();
$this->trData->entityRuntimeId = $this->getEntityRuntimeId();
$this->trData->actionType = $this->getUnsignedVarInt();
$this->trData->hotbarSlot = $this->getVarInt();
$this->trData->itemInHand = $this->getSlot();
$this->trData->vector1 = $this->getVector3Obj();
$this->trData->vector2 = $this->getVector3Obj();
break;
case self::TYPE_RELEASE_ITEM:
$this->transactionData->releaseItemActionType = $this->getUnsignedVarInt();
$this->transactionData->hotbarSlot = $this->getVarInt();
$this->transactionData->itemInHand = $this->getSlot();
$this->transactionData->headPos = $this->getVector3Obj();
$this->trData->actionType = $this->getUnsignedVarInt();
$this->trData->hotbarSlot = $this->getVarInt();
$this->trData->itemInHand = $this->getSlot();
$this->trData->headPos = $this->getVector3Obj();
break;
default:
throw new \UnexpectedValueException("Unknown transaction type $type");
throw new \UnexpectedValueException("Unknown transaction type $this->transactionType");
}
}
protected function encodePayload(){
//TODO
}
$this->putUnsignedVarInt($this->transactionType);
protected function decodeInventoryAction(){
$action = new NetworkInventoryAction();
$action->read($this);
return $action;
$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->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{

View File

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