From 295d9bc80b8b2ff1c4a163d617db69709162b60a Mon Sep 17 00:00:00 2001 From: "Dylan K. Taylor" Date: Wed, 8 Mar 2017 19:34:12 +0000 Subject: [PATCH] Cleaned up muddled varint/varlong mess, added separate methods for entity unique and runtime ids, moved some MCPE-protocol-specific methods out of BinaryStream --- .../network/mcpe/protocol/AddEntityPacket.php | 8 +- .../mcpe/protocol/AddHangingEntityPacket.php | 6 +- .../mcpe/protocol/AddItemEntityPacket.php | 4 +- .../mcpe/protocol/AddPaintingPacket.php | 6 +- .../network/mcpe/protocol/AddPlayerPacket.php | 4 +- .../network/mcpe/protocol/AnimatePacket.php | 4 +- .../mcpe/protocol/BlockEntityDataPacket.php | 4 +- .../mcpe/protocol/BlockEventPacket.php | 2 +- .../mcpe/protocol/CommandStepPacket.php | 21 +---- .../mcpe/protocol/ContainerOpenPacket.php | 4 +- .../network/mcpe/protocol/DataPacket.php | 84 ++++++++++++++++++- .../mcpe/protocol/EntityEventPacket.php | 4 +- .../network/mcpe/protocol/ExplodePacket.php | 2 +- .../network/mcpe/protocol/InteractPacket.php | 4 +- .../mcpe/protocol/ItemFrameDropItemPacket.php | 2 +- .../mcpe/protocol/MobArmorEquipmentPacket.php | 4 +- .../network/mcpe/protocol/MobEffectPacket.php | 2 +- .../mcpe/protocol/MobEquipmentPacket.php | 4 +- .../mcpe/protocol/MoveEntityPacket.php | 4 +- .../mcpe/protocol/MovePlayerPacket.php | 4 +- .../mcpe/protocol/PlayerActionPacket.php | 8 +- .../mcpe/protocol/PlayerListPacket.php | 2 +- .../mcpe/protocol/RemoveBlockPacket.php | 2 +- .../mcpe/protocol/RemoveEntityPacket.php | 2 +- .../mcpe/protocol/SetEntityDataPacket.php | 2 +- .../mcpe/protocol/SetEntityLinkPacket.php | 4 +- .../mcpe/protocol/SetEntityMotionPacket.php | 2 +- .../mcpe/protocol/SetSpawnPositionPacket.php | 2 +- .../mcpe/protocol/ShowCreditsPacket.php | 4 +- .../network/mcpe/protocol/StartGamePacket.php | 6 +- .../mcpe/protocol/TakeItemEntityPacket.php | 4 +- .../mcpe/protocol/UpdateAttributesPacket.php | 2 +- .../mcpe/protocol/UpdateBlockPacket.php | 2 +- .../network/mcpe/protocol/UseItemPacket.php | 2 +- src/pocketmine/utils/Binary.php | 4 +- src/pocketmine/utils/BinaryStream.php | 62 +++++++------- 36 files changed, 177 insertions(+), 110 deletions(-) diff --git a/src/pocketmine/network/mcpe/protocol/AddEntityPacket.php b/src/pocketmine/network/mcpe/protocol/AddEntityPacket.php index cbb5a974c..84f69f379 100644 --- a/src/pocketmine/network/mcpe/protocol/AddEntityPacket.php +++ b/src/pocketmine/network/mcpe/protocol/AddEntityPacket.php @@ -50,8 +50,8 @@ class AddEntityPacket extends DataPacket{ public function encode(){ $this->reset(); - $this->putEntityId($this->eid); //EntityUniqueID - TODO: verify this - $this->putEntityId($this->eid); + $this->putEntityUniqueId($this->eid); + $this->putEntityRuntimeId($this->eid); $this->putUnsignedVarInt($this->type); $this->putVector3f($this->x, $this->y, $this->z); $this->putVector3f($this->speedX, $this->speedY, $this->speedZ); @@ -67,8 +67,8 @@ class AddEntityPacket extends DataPacket{ $this->putEntityMetadata($this->metadata); $this->putUnsignedVarInt(count($this->links)); foreach($this->links as $link){ - $this->putEntityId($link[0]); - $this->putEntityId($link[1]); + $this->putEntityUniqueId($link[0]); + $this->putEntityUniqueId($link[1]); $this->putByte($link[2]); } } diff --git a/src/pocketmine/network/mcpe/protocol/AddHangingEntityPacket.php b/src/pocketmine/network/mcpe/protocol/AddHangingEntityPacket.php index 4550e9823..5d2d4310e 100644 --- a/src/pocketmine/network/mcpe/protocol/AddHangingEntityPacket.php +++ b/src/pocketmine/network/mcpe/protocol/AddHangingEntityPacket.php @@ -41,9 +41,9 @@ class AddHangingEntityPacket extends DataPacket{ public function encode(){ $this->reset(); - $this->putEntityId($this->entityUniqueId); - $this->putEntityId($this->entityRuntimeId); - $this->putBlockCoords($this->x, $this->y, $this->z); + $this->putEntityUniqueId($this->entityUniqueId); + $this->putEntityRuntimeId($this->entityRuntimeId); + $this->putBlockPosition($this->x, $this->y, $this->z); $this->putVarInt($this->unknown); } diff --git a/src/pocketmine/network/mcpe/protocol/AddItemEntityPacket.php b/src/pocketmine/network/mcpe/protocol/AddItemEntityPacket.php index f1fd7930e..48d7f0130 100644 --- a/src/pocketmine/network/mcpe/protocol/AddItemEntityPacket.php +++ b/src/pocketmine/network/mcpe/protocol/AddItemEntityPacket.php @@ -44,8 +44,8 @@ class AddItemEntityPacket extends DataPacket{ public function encode(){ $this->reset(); - $this->putEntityId($this->eid); //EntityUniqueID - $this->putEntityId($this->eid); //EntityRuntimeID + $this->putEntityUniqueId($this->eid); + $this->putEntityRuntimeId($this->eid); $this->putSlot($this->item); $this->putVector3f($this->x, $this->y, $this->z); $this->putVector3f($this->speedX, $this->speedY, $this->speedZ); diff --git a/src/pocketmine/network/mcpe/protocol/AddPaintingPacket.php b/src/pocketmine/network/mcpe/protocol/AddPaintingPacket.php index c2af0e398..9ae5b2988 100644 --- a/src/pocketmine/network/mcpe/protocol/AddPaintingPacket.php +++ b/src/pocketmine/network/mcpe/protocol/AddPaintingPacket.php @@ -42,9 +42,9 @@ class AddPaintingPacket extends DataPacket{ public function encode(){ $this->reset(); - $this->putEntityId($this->eid); //EntityUniqueID - $this->putEntityId($this->eid); //EntityRuntimeID - $this->putBlockCoords($this->x, $this->y, $this->z); + $this->putEntityUniqueId($this->eid); + $this->putEntityRuntimeId($this->eid); + $this->putBlockPosition($this->x, $this->y, $this->z); $this->putVarInt($this->direction); $this->putString($this->title); } diff --git a/src/pocketmine/network/mcpe/protocol/AddPlayerPacket.php b/src/pocketmine/network/mcpe/protocol/AddPlayerPacket.php index aad154a7d..b64c3b428 100644 --- a/src/pocketmine/network/mcpe/protocol/AddPlayerPacket.php +++ b/src/pocketmine/network/mcpe/protocol/AddPlayerPacket.php @@ -51,8 +51,8 @@ class AddPlayerPacket extends DataPacket{ $this->reset(); $this->putUUID($this->uuid); $this->putString($this->username); - $this->putEntityId($this->eid); //EntityUniqueID - $this->putEntityId($this->eid); //EntityRuntimeID + $this->putEntityUniqueId($this->eid); + $this->putEntityRuntimeId($this->eid); $this->putVector3f($this->x, $this->y, $this->z); $this->putVector3f($this->speedX, $this->speedY, $this->speedZ); $this->putLFloat($this->pitch); diff --git a/src/pocketmine/network/mcpe/protocol/AnimatePacket.php b/src/pocketmine/network/mcpe/protocol/AnimatePacket.php index ecd0e64de..fba81c904 100644 --- a/src/pocketmine/network/mcpe/protocol/AnimatePacket.php +++ b/src/pocketmine/network/mcpe/protocol/AnimatePacket.php @@ -34,13 +34,13 @@ class AnimatePacket extends DataPacket{ public function decode(){ $this->action = $this->getVarInt(); - $this->eid = $this->getEntityId(); + $this->eid = $this->getEntityRuntimeId(); } public function encode(){ $this->reset(); $this->putVarInt($this->action); - $this->putEntityId($this->eid); + $this->putEntityRuntimeId($this->eid); } public function handle(NetworkSession $session) : bool{ diff --git a/src/pocketmine/network/mcpe/protocol/BlockEntityDataPacket.php b/src/pocketmine/network/mcpe/protocol/BlockEntityDataPacket.php index 9a5eeea48..cc1fbc52d 100644 --- a/src/pocketmine/network/mcpe/protocol/BlockEntityDataPacket.php +++ b/src/pocketmine/network/mcpe/protocol/BlockEntityDataPacket.php @@ -35,13 +35,13 @@ class BlockEntityDataPacket extends DataPacket{ public $namedtag; public function decode(){ - $this->getBlockCoords($this->x, $this->y, $this->z); + $this->getBlockPosition($this->x, $this->y, $this->z); $this->namedtag = $this->get(true); } public function encode(){ $this->reset(); - $this->putBlockCoords($this->x, $this->y, $this->z); + $this->putBlockPosition($this->x, $this->y, $this->z); $this->put($this->namedtag); } diff --git a/src/pocketmine/network/mcpe/protocol/BlockEventPacket.php b/src/pocketmine/network/mcpe/protocol/BlockEventPacket.php index bae6ae7ae..ba0667a4a 100644 --- a/src/pocketmine/network/mcpe/protocol/BlockEventPacket.php +++ b/src/pocketmine/network/mcpe/protocol/BlockEventPacket.php @@ -41,7 +41,7 @@ class BlockEventPacket extends DataPacket{ public function encode(){ $this->reset(); - $this->putBlockCoords($this->x, $this->y, $this->z); + $this->putBlockPosition($this->x, $this->y, $this->z); $this->putVarInt($this->case1); $this->putVarInt($this->case2); } diff --git a/src/pocketmine/network/mcpe/protocol/CommandStepPacket.php b/src/pocketmine/network/mcpe/protocol/CommandStepPacket.php index 1678fc431..26ccf3233 100644 --- a/src/pocketmine/network/mcpe/protocol/CommandStepPacket.php +++ b/src/pocketmine/network/mcpe/protocol/CommandStepPacket.php @@ -28,25 +28,13 @@ use pocketmine\network\mcpe\NetworkSession; class CommandStepPacket extends DataPacket{ const NETWORK_ID = ProtocolInfo::COMMAND_STEP_PACKET; - /** - * unknown (string) - * unknown (string) - * unknown (uvarint) - * unknown (uvarint) - * unknown (bool) - * unknown (uvarint64) - * unknown (string) - * unknown (string) - * https://gist.github.com/dktapps/8285b93af4ca38e0104bfeb9a6c87afd - */ - public $command; public $overload; public $uvarint1; public $uvarint2; public $bool; public $uvarint64; - public $args; //JSON formatted command arguments + public $args; public $string4; public function decode(){ @@ -55,12 +43,11 @@ class CommandStepPacket extends DataPacket{ $this->uvarint1 = $this->getUnsignedVarInt(); $this->uvarint2 = $this->getUnsignedVarInt(); $this->bool = (bool) $this->getByte(); - $this->uvarint64 = $this->getUnsignedVarInt(); //TODO: varint64 + $this->uvarint64 = $this->getUnsignedVarLong(); $this->args = json_decode($this->getString()); $this->string4 = $this->getString(); - while(!$this->feof()){ - $this->getByte(); //prevent assertion errors. TODO: find out why there are always 3 extra bytes at the end of this packet. - } + + $this->get(true); //TODO: read command origin data } public function encode(){ diff --git a/src/pocketmine/network/mcpe/protocol/ContainerOpenPacket.php b/src/pocketmine/network/mcpe/protocol/ContainerOpenPacket.php index ee6356d2f..698c9d058 100644 --- a/src/pocketmine/network/mcpe/protocol/ContainerOpenPacket.php +++ b/src/pocketmine/network/mcpe/protocol/ContainerOpenPacket.php @@ -46,8 +46,8 @@ class ContainerOpenPacket extends DataPacket{ $this->putByte($this->windowid); $this->putByte($this->type); $this->putVarInt($this->slots); - $this->putBlockCoords($this->x, $this->y, $this->z); - $this->putEntityId($this->entityId); + $this->putBlockPosition($this->x, $this->y, $this->z); + $this->putEntityUniqueId($this->entityId); } public function handle(NetworkSession $session) : bool{ diff --git a/src/pocketmine/network/mcpe/protocol/DataPacket.php b/src/pocketmine/network/mcpe/protocol/DataPacket.php index f06fc6ff7..d08193289 100644 --- a/src/pocketmine/network/mcpe/protocol/DataPacket.php +++ b/src/pocketmine/network/mcpe/protocol/DataPacket.php @@ -122,7 +122,7 @@ abstract class DataPacket extends BinaryStream{ $value[2] = $this->getVarInt(); //z break; case Entity::DATA_TYPE_LONG: - $value = $this->getVarInt(); //TODO: varint64 proper support + $value = $this->getVarLong(); break; case Entity::DATA_TYPE_VECTOR3F: $value = [0.0, 0.0, 0.0]; @@ -178,7 +178,7 @@ abstract class DataPacket extends BinaryStream{ $this->putVarInt($d[1][2]); //z break; case Entity::DATA_TYPE_LONG: - $this->putVarInt($d[1]); //TODO: varint64 support + $this->putVarLong($d[1]); break; case Entity::DATA_TYPE_VECTOR3F: //TODO: change this implementation (use objects) @@ -186,4 +186,84 @@ abstract class DataPacket extends BinaryStream{ } } } + + /** + * Reads and returns an EntityUniqueID + * @return int|string + */ + public function getEntityUniqueId(){ + return $this->getVarLong(); + } + + /** + * Writes an EntityUniqueID + * @param int|string $eid + */ + public function putEntityUniqueId($eid){ + $this->putVarLong($eid); + } + + /** + * Reads and returns an EntityRuntimeID + * @return int|string + */ + public function getEntityRuntimeId(){ + return $this->getUnsignedVarLong(); + } + + /** + * Writes an EntityUniqueID + * @param int|string $eid + */ + public function putEntityRuntimeId($eid){ + $this->putUnsignedVarLong($eid); + } + + /** + * Writes an block position with unsigned Y coordinate. + * @param int $x + * @param int $y 0-255 + * @param int $z + */ + public function getBlockPosition(&$x, &$y, &$z){ + $x = $this->getVarInt(); + $y = $this->getUnsignedVarInt(); + $z = $this->getVarInt(); + } + + /** + * Reads a block position with unsigned Y coordinate. + * @param int &$x + * @param int &$y + * @param int &$z + */ + public function putBlockPosition($x, $y, $z){ + $this->putVarInt($x); + $this->putUnsignedVarInt($y); + $this->putVarInt($z); + } + + /** + * Reads a floating-point vector3 rounded to 4dp. + * @param float $x + * @param float $y + * @param float $z + */ + public function getVector3f(&$x, &$y, &$z){ + $x = $this->getLFloat(4); + $y = $this->getLFloat(4); + $z = $this->getLFloat(4); + } + + /** + * Writes a floating-point vector3 + * @param float $x + * @param float $y + * @param float $z + */ + public function putVector3f($x, $y, $z){ + $this->putLFloat($x); + $this->putLFloat($y); + $this->putLFloat($z); + } } diff --git a/src/pocketmine/network/mcpe/protocol/EntityEventPacket.php b/src/pocketmine/network/mcpe/protocol/EntityEventPacket.php index 388aab8b9..da6357aa7 100644 --- a/src/pocketmine/network/mcpe/protocol/EntityEventPacket.php +++ b/src/pocketmine/network/mcpe/protocol/EntityEventPacket.php @@ -52,14 +52,14 @@ class EntityEventPacket extends DataPacket{ public $unknown; public function decode(){ - $this->eid = $this->getEntityId(); + $this->eid = $this->getEntityRuntimeId(); $this->event = $this->getByte(); $this->unknown = $this->getVarInt(); } public function encode(){ $this->reset(); - $this->putEntityId($this->eid); + $this->putEntityRuntimeId($this->eid); $this->putByte($this->event); $this->putVarInt($this->unknown); } diff --git a/src/pocketmine/network/mcpe/protocol/ExplodePacket.php b/src/pocketmine/network/mcpe/protocol/ExplodePacket.php index 6892308cc..c298e6351 100644 --- a/src/pocketmine/network/mcpe/protocol/ExplodePacket.php +++ b/src/pocketmine/network/mcpe/protocol/ExplodePacket.php @@ -51,7 +51,7 @@ class ExplodePacket extends DataPacket{ $this->putUnsignedVarInt(count($this->records)); if(count($this->records) > 0){ foreach($this->records as $record){ - $this->putBlockCoords($record->x, $record->y, $record->z); + $this->putBlockPosition($record->x, $record->y, $record->z); } } } diff --git a/src/pocketmine/network/mcpe/protocol/InteractPacket.php b/src/pocketmine/network/mcpe/protocol/InteractPacket.php index 08c026112..604342505 100644 --- a/src/pocketmine/network/mcpe/protocol/InteractPacket.php +++ b/src/pocketmine/network/mcpe/protocol/InteractPacket.php @@ -39,13 +39,13 @@ class InteractPacket extends DataPacket{ public function decode(){ $this->action = $this->getByte(); - $this->target = $this->getEntityId(); + $this->target = $this->getEntityRuntimeId(); } public function encode(){ $this->reset(); $this->putByte($this->action); - $this->putEntityId($this->target); + $this->putEntityRuntimeId($this->target); } public function handle(NetworkSession $session) : bool{ diff --git a/src/pocketmine/network/mcpe/protocol/ItemFrameDropItemPacket.php b/src/pocketmine/network/mcpe/protocol/ItemFrameDropItemPacket.php index e9cca9b7e..9a101c420 100644 --- a/src/pocketmine/network/mcpe/protocol/ItemFrameDropItemPacket.php +++ b/src/pocketmine/network/mcpe/protocol/ItemFrameDropItemPacket.php @@ -34,7 +34,7 @@ class ItemFrameDropItemPacket extends DataPacket{ public $z; public function decode(){ - $this->getBlockCoords($this->x, $this->y, $this->z); + $this->getBlockPosition($this->x, $this->y, $this->z); } public function encode(){ diff --git a/src/pocketmine/network/mcpe/protocol/MobArmorEquipmentPacket.php b/src/pocketmine/network/mcpe/protocol/MobArmorEquipmentPacket.php index 8464ad9c4..63cf63587 100644 --- a/src/pocketmine/network/mcpe/protocol/MobArmorEquipmentPacket.php +++ b/src/pocketmine/network/mcpe/protocol/MobArmorEquipmentPacket.php @@ -33,7 +33,7 @@ class MobArmorEquipmentPacket extends DataPacket{ public $slots = []; public function decode(){ - $this->eid = $this->getEntityId(); + $this->eid = $this->getEntityRuntimeId(); $this->slots[0] = $this->getSlot(); $this->slots[1] = $this->getSlot(); $this->slots[2] = $this->getSlot(); @@ -42,7 +42,7 @@ class MobArmorEquipmentPacket extends DataPacket{ public function encode(){ $this->reset(); - $this->putEntityId($this->eid); + $this->putEntityRuntimeId($this->eid); $this->putSlot($this->slots[0]); $this->putSlot($this->slots[1]); $this->putSlot($this->slots[2]); diff --git a/src/pocketmine/network/mcpe/protocol/MobEffectPacket.php b/src/pocketmine/network/mcpe/protocol/MobEffectPacket.php index 512a25d3d..be64a6c25 100644 --- a/src/pocketmine/network/mcpe/protocol/MobEffectPacket.php +++ b/src/pocketmine/network/mcpe/protocol/MobEffectPacket.php @@ -46,7 +46,7 @@ class MobEffectPacket extends DataPacket{ public function encode(){ $this->reset(); - $this->putEntityId($this->eid); + $this->putEntityRuntimeId($this->eid); $this->putByte($this->eventId); $this->putVarInt($this->effectId); $this->putVarInt($this->amplifier); diff --git a/src/pocketmine/network/mcpe/protocol/MobEquipmentPacket.php b/src/pocketmine/network/mcpe/protocol/MobEquipmentPacket.php index 7091181e5..2299b4072 100644 --- a/src/pocketmine/network/mcpe/protocol/MobEquipmentPacket.php +++ b/src/pocketmine/network/mcpe/protocol/MobEquipmentPacket.php @@ -36,7 +36,7 @@ class MobEquipmentPacket extends DataPacket{ public $unknownByte; public function decode(){ - $this->eid = $this->getEntityId(); //EntityRuntimeID + $this->eid = $this->getEntityRuntimeId(); $this->item = $this->getSlot(); $this->slot = $this->getByte(); $this->selectedSlot = $this->getByte(); @@ -45,7 +45,7 @@ class MobEquipmentPacket extends DataPacket{ public function encode(){ $this->reset(); - $this->putEntityId($this->eid); //EntityRuntimeID + $this->putEntityRuntimeId($this->eid); $this->putSlot($this->item); $this->putByte($this->slot); $this->putByte($this->selectedSlot); diff --git a/src/pocketmine/network/mcpe/protocol/MoveEntityPacket.php b/src/pocketmine/network/mcpe/protocol/MoveEntityPacket.php index 1f7d0e9ff..fbda02c9e 100644 --- a/src/pocketmine/network/mcpe/protocol/MoveEntityPacket.php +++ b/src/pocketmine/network/mcpe/protocol/MoveEntityPacket.php @@ -38,7 +38,7 @@ class MoveEntityPacket extends DataPacket{ public $pitch; public function decode(){ - $this->eid = $this->getEntityId(); + $this->eid = $this->getEntityRuntimeId(); $this->getVector3f($this->x, $this->y, $this->z); $this->pitch = $this->getByte() * (360.0 / 256); $this->yaw = $this->getByte() * (360.0 / 256); @@ -47,7 +47,7 @@ class MoveEntityPacket extends DataPacket{ public function encode(){ $this->reset(); - $this->putEntityId($this->eid); + $this->putEntityRuntimeId($this->eid); $this->putVector3f($this->x, $this->y, $this->z); $this->putByte($this->pitch / (360.0 / 256)); $this->putByte($this->yaw / (360.0 / 256)); diff --git a/src/pocketmine/network/mcpe/protocol/MovePlayerPacket.php b/src/pocketmine/network/mcpe/protocol/MovePlayerPacket.php index 608dbd94b..24a5644e6 100644 --- a/src/pocketmine/network/mcpe/protocol/MovePlayerPacket.php +++ b/src/pocketmine/network/mcpe/protocol/MovePlayerPacket.php @@ -44,7 +44,7 @@ class MovePlayerPacket extends DataPacket{ public $onGround; public function decode(){ - $this->eid = $this->getEntityId(); //EntityRuntimeID + $this->eid = $this->getEntityRuntimeId(); $this->getVector3f($this->x, $this->y, $this->z); $this->pitch = $this->getLFloat(); $this->yaw = $this->getLFloat(); @@ -55,7 +55,7 @@ class MovePlayerPacket extends DataPacket{ public function encode(){ $this->reset(); - $this->putEntityId($this->eid); //EntityRuntimeID + $this->putEntityRuntimeId($this->eid); $this->putVector3f($this->x, $this->y, $this->z); $this->putLFloat($this->pitch); $this->putLFloat($this->yaw); diff --git a/src/pocketmine/network/mcpe/protocol/PlayerActionPacket.php b/src/pocketmine/network/mcpe/protocol/PlayerActionPacket.php index 741d5a035..6d04ec3d5 100644 --- a/src/pocketmine/network/mcpe/protocol/PlayerActionPacket.php +++ b/src/pocketmine/network/mcpe/protocol/PlayerActionPacket.php @@ -55,17 +55,17 @@ class PlayerActionPacket extends DataPacket{ public $face; public function decode(){ - $this->eid = $this->getEntityId(); + $this->eid = $this->getEntityRuntimeId(); $this->action = $this->getVarInt(); - $this->getBlockCoords($this->x, $this->y, $this->z); + $this->getBlockPosition($this->x, $this->y, $this->z); $this->face = $this->getVarInt(); } public function encode(){ $this->reset(); - $this->putEntityId($this->eid); + $this->putEntityRuntimeId($this->eid); $this->putVarInt($this->action); - $this->putBlockCoords($this->x, $this->y, $this->z); + $this->putBlockPosition($this->x, $this->y, $this->z); $this->putVarInt($this->face); } diff --git a/src/pocketmine/network/mcpe/protocol/PlayerListPacket.php b/src/pocketmine/network/mcpe/protocol/PlayerListPacket.php index bca812beb..07e95483f 100644 --- a/src/pocketmine/network/mcpe/protocol/PlayerListPacket.php +++ b/src/pocketmine/network/mcpe/protocol/PlayerListPacket.php @@ -53,7 +53,7 @@ class PlayerListPacket extends DataPacket{ foreach($this->entries as $d){ if($this->type === self::TYPE_ADD){ $this->putUUID($d[0]); - $this->putEntityId($d[1]); + $this->putEntityUniqueId($d[1]); $this->putString($d[2]); $this->putString($d[3]); $this->putString($d[4]); diff --git a/src/pocketmine/network/mcpe/protocol/RemoveBlockPacket.php b/src/pocketmine/network/mcpe/protocol/RemoveBlockPacket.php index d9312cb1a..65025be1e 100644 --- a/src/pocketmine/network/mcpe/protocol/RemoveBlockPacket.php +++ b/src/pocketmine/network/mcpe/protocol/RemoveBlockPacket.php @@ -34,7 +34,7 @@ class RemoveBlockPacket extends DataPacket{ public $z; public function decode(){ - $this->getBlockCoords($this->x, $this->y, $this->z); + $this->getBlockPosition($this->x, $this->y, $this->z); } public function encode(){ diff --git a/src/pocketmine/network/mcpe/protocol/RemoveEntityPacket.php b/src/pocketmine/network/mcpe/protocol/RemoveEntityPacket.php index 8ec65467e..a75da9742 100644 --- a/src/pocketmine/network/mcpe/protocol/RemoveEntityPacket.php +++ b/src/pocketmine/network/mcpe/protocol/RemoveEntityPacket.php @@ -37,7 +37,7 @@ class RemoveEntityPacket extends DataPacket{ public function encode(){ $this->reset(); - $this->putEntityId($this->eid); + $this->putEntityUniqueId($this->eid); } public function handle(NetworkSession $session) : bool{ diff --git a/src/pocketmine/network/mcpe/protocol/SetEntityDataPacket.php b/src/pocketmine/network/mcpe/protocol/SetEntityDataPacket.php index 705eaec10..25b5437b5 100644 --- a/src/pocketmine/network/mcpe/protocol/SetEntityDataPacket.php +++ b/src/pocketmine/network/mcpe/protocol/SetEntityDataPacket.php @@ -38,7 +38,7 @@ class SetEntityDataPacket extends DataPacket{ public function encode(){ $this->reset(); - $this->putEntityId($this->eid); + $this->putEntityRuntimeId($this->eid); $this->putEntityMetadata($this->metadata); } diff --git a/src/pocketmine/network/mcpe/protocol/SetEntityLinkPacket.php b/src/pocketmine/network/mcpe/protocol/SetEntityLinkPacket.php index 288faf95c..486df6b17 100644 --- a/src/pocketmine/network/mcpe/protocol/SetEntityLinkPacket.php +++ b/src/pocketmine/network/mcpe/protocol/SetEntityLinkPacket.php @@ -39,8 +39,8 @@ class SetEntityLinkPacket extends DataPacket{ public function encode(){ $this->reset(); - $this->putEntityId($this->from); - $this->putEntityId($this->to); + $this->putEntityUniqueId($this->from); + $this->putEntityUniqueId($this->to); $this->putByte($this->type); } diff --git a/src/pocketmine/network/mcpe/protocol/SetEntityMotionPacket.php b/src/pocketmine/network/mcpe/protocol/SetEntityMotionPacket.php index 3b7f2d51e..ec66b3953 100644 --- a/src/pocketmine/network/mcpe/protocol/SetEntityMotionPacket.php +++ b/src/pocketmine/network/mcpe/protocol/SetEntityMotionPacket.php @@ -40,7 +40,7 @@ class SetEntityMotionPacket extends DataPacket{ public function encode(){ $this->reset(); - $this->putEntityId($this->eid); + $this->putEntityRuntimeId($this->eid); $this->putVector3f($this->motionX, $this->motionY, $this->motionZ); } diff --git a/src/pocketmine/network/mcpe/protocol/SetSpawnPositionPacket.php b/src/pocketmine/network/mcpe/protocol/SetSpawnPositionPacket.php index 086f68ee8..0c1acd0bc 100644 --- a/src/pocketmine/network/mcpe/protocol/SetSpawnPositionPacket.php +++ b/src/pocketmine/network/mcpe/protocol/SetSpawnPositionPacket.php @@ -42,7 +42,7 @@ class SetSpawnPositionPacket extends DataPacket{ public function encode(){ $this->reset(); $this->putVarInt($this->unknown); - $this->putBlockCoords($this->x, $this->y, $this->z); + $this->putBlockPosition($this->x, $this->y, $this->z); $this->putBool($this->unknownBool); } diff --git a/src/pocketmine/network/mcpe/protocol/ShowCreditsPacket.php b/src/pocketmine/network/mcpe/protocol/ShowCreditsPacket.php index ed82bc0e7..03b888b0e 100644 --- a/src/pocketmine/network/mcpe/protocol/ShowCreditsPacket.php +++ b/src/pocketmine/network/mcpe/protocol/ShowCreditsPacket.php @@ -37,13 +37,13 @@ class ShowCreditsPacket extends DataPacket{ public $status; public function decode(){ - $this->playerEid = $this->getEntityId(); + $this->playerEid = $this->getEntityRuntimeId(); $this->status = $this->getVarInt(); } public function encode(){ $this->reset(); - $this->putEntityId($this->playerEid); + $this->putEntityRuntimeId($this->playerEid); $this->putVarInt($this->status); } diff --git a/src/pocketmine/network/mcpe/protocol/StartGamePacket.php b/src/pocketmine/network/mcpe/protocol/StartGamePacket.php index 25d85314b..0c43a515b 100644 --- a/src/pocketmine/network/mcpe/protocol/StartGamePacket.php +++ b/src/pocketmine/network/mcpe/protocol/StartGamePacket.php @@ -58,8 +58,8 @@ class StartGamePacket extends DataPacket{ public function encode(){ $this->reset(); - $this->putEntityId($this->entityUniqueId); //EntityUniqueID - $this->putEntityId($this->entityRuntimeId); //EntityRuntimeID + $this->putEntityUniqueId($this->entityUniqueId); //EntityUniqueID + $this->putEntityRuntimeId($this->entityRuntimeId); //EntityRuntimeID $this->putVector3f($this->x, $this->y, $this->z); $this->putLFloat(0); //TODO: find out what these are (yaw/pitch?) $this->putLFloat(0); @@ -68,7 +68,7 @@ class StartGamePacket extends DataPacket{ $this->putVarInt($this->generator); $this->putVarInt($this->gamemode); $this->putVarInt($this->difficulty); - $this->putBlockCoords($this->spawnX, $this->spawnY, $this->spawnZ); + $this->putBlockPosition($this->spawnX, $this->spawnY, $this->spawnZ); $this->putBool($this->hasAchievementsDisabled); $this->putVarInt($this->dayCycleStopTime); $this->putBool($this->eduMode); diff --git a/src/pocketmine/network/mcpe/protocol/TakeItemEntityPacket.php b/src/pocketmine/network/mcpe/protocol/TakeItemEntityPacket.php index 80d446b6e..d4b4b4fc1 100644 --- a/src/pocketmine/network/mcpe/protocol/TakeItemEntityPacket.php +++ b/src/pocketmine/network/mcpe/protocol/TakeItemEntityPacket.php @@ -38,8 +38,8 @@ class TakeItemEntityPacket extends DataPacket{ public function encode(){ $this->reset(); - $this->putEntityId($this->target); - $this->putEntityId($this->eid); + $this->putEntityRuntimeId($this->target); + $this->putEntityRuntimeId($this->eid); } public function handle(NetworkSession $session) : bool{ diff --git a/src/pocketmine/network/mcpe/protocol/UpdateAttributesPacket.php b/src/pocketmine/network/mcpe/protocol/UpdateAttributesPacket.php index d3da0bb78..9f369579f 100644 --- a/src/pocketmine/network/mcpe/protocol/UpdateAttributesPacket.php +++ b/src/pocketmine/network/mcpe/protocol/UpdateAttributesPacket.php @@ -41,7 +41,7 @@ class UpdateAttributesPacket extends DataPacket{ public function encode(){ $this->reset(); - $this->putEntityId($this->entityId); + $this->putEntityRuntimeId($this->entityId); $this->putUnsignedVarInt(count($this->entries)); foreach($this->entries as $entry){ $this->putLFloat($entry->getMinValue()); diff --git a/src/pocketmine/network/mcpe/protocol/UpdateBlockPacket.php b/src/pocketmine/network/mcpe/protocol/UpdateBlockPacket.php index 2a29ba5d6..b7ac09198 100644 --- a/src/pocketmine/network/mcpe/protocol/UpdateBlockPacket.php +++ b/src/pocketmine/network/mcpe/protocol/UpdateBlockPacket.php @@ -51,7 +51,7 @@ class UpdateBlockPacket extends DataPacket{ public function encode(){ $this->reset(); - $this->putBlockCoords($this->x, $this->y, $this->z); + $this->putBlockPosition($this->x, $this->y, $this->z); $this->putUnsignedVarInt($this->blockId); $this->putUnsignedVarInt(($this->flags << 4) | $this->blockData); } diff --git a/src/pocketmine/network/mcpe/protocol/UseItemPacket.php b/src/pocketmine/network/mcpe/protocol/UseItemPacket.php index 0a2b726bb..505123d02 100644 --- a/src/pocketmine/network/mcpe/protocol/UseItemPacket.php +++ b/src/pocketmine/network/mcpe/protocol/UseItemPacket.php @@ -44,7 +44,7 @@ class UseItemPacket extends DataPacket{ public $slot; public function decode(){ - $this->getBlockCoords($this->x, $this->y, $this->z); + $this->getBlockPosition($this->x, $this->y, $this->z); $this->blockId = $this->getUnsignedVarInt(); $this->face = $this->getVarInt(); $this->getVector3f($this->fx, $this->fy, $this->fz); diff --git a/src/pocketmine/utils/Binary.php b/src/pocketmine/utils/Binary.php index 668c6ed6f..efbc26b13 100644 --- a/src/pocketmine/utils/Binary.php +++ b/src/pocketmine/utils/Binary.php @@ -412,7 +412,7 @@ class Binary{ * Reads a 64-bit zigzag-encoded variable-length integer from the supplied stream. * @param \pocketmine\nbt\NBT|BinaryStream $stream * - * @return int + * @return int|string */ public static function readVarLong($stream){ if(PHP_INT_SIZE === 8){ @@ -476,7 +476,6 @@ class Binary{ for($i = 0; $i <= 63; $i += 7){ $b = $stream->getByte(); $value = bcadd($value, bcmul($b & 0x7f, bcpow("2", "$i"))); - var_dump($value); if(($b & 0x80) === 0){ return $value; @@ -497,7 +496,6 @@ class Binary{ for($i = 0; $i <= 63; $i += 7){ $b = $stream->getByte(); $value |= (($b & 0x7f) << $i); - var_dump($value); if(($b & 0x80) === 0){ return $value; diff --git a/src/pocketmine/utils/BinaryStream.php b/src/pocketmine/utils/BinaryStream.php index 506a162f0..156ce4523 100644 --- a/src/pocketmine/utils/BinaryStream.php +++ b/src/pocketmine/utils/BinaryStream.php @@ -233,66 +233,68 @@ class BinaryStream extends \stdClass{ $this->put($v); } - //TODO: varint64 - /** - * Reads an unsigned varint32 from the stream. + * Reads a 32-bit variable-length unsigned integer from the buffer and returns it. + * @return int */ public function getUnsignedVarInt(){ return Binary::readUnsignedVarInt($this); } /** - * Writes an unsigned varint32 to the stream. + * Writes a 32-bit variable-length unsigned integer to the end of the buffer. + * @param int $v */ public function putUnsignedVarInt($v){ $this->put(Binary::writeUnsignedVarInt($v)); } /** - * Reads a signed varint32 from the stream. + * Reads a 32-bit zigzag-encoded variable-length integer from the buffer and returns it. + * @return int */ public function getVarInt(){ return Binary::readVarInt($this); } /** - * Writes a signed varint32 to the stream. + * Writes a 32-bit zigzag-encoded variable-length integer to the end of the buffer. + * @param int $v */ public function putVarInt($v){ $this->put(Binary::writeVarInt($v)); } - public function getEntityId(){ - return $this->getVarInt(); + /** + * Reads a 64-bit variable-length integer from the buffer and returns it. + * @return int|string int, or the string representation of an int64 on 32-bit platforms + */ + public function getUnsignedVarLong(){ + return Binary::readUnsignedVarLong($this); } - public function putEntityId($v){ - $this->putVarInt($v); + /** + * Writes a 64-bit variable-length integer to the end of the buffer. + * @param int|string $v int, or the string representation of an int64 on 32-bit platforms + */ + public function putUnsignedVarLong($v){ + $this->buffer .= Binary::writeUnsignedVarLong($v); } - public function getBlockCoords(&$x, &$y, &$z){ - $x = $this->getVarInt(); - $y = $this->getUnsignedVarInt(); - $z = $this->getVarInt(); + /** + * Reads a 64-bit zigzag-encoded variable-length integer from the buffer and returns it. + * @return int|string int, or the string representation of an int64 on 32-bit platforms + */ + public function getVarLong(){ + return Binary::readVarLong($this); } - public function putBlockCoords($x, $y, $z){ - $this->putVarInt($x); - $this->putUnsignedVarInt($y); - $this->putVarInt($z); - } - - public function getVector3f(&$x, &$y, &$z){ - $x = $this->getLFloat(4); - $y = $this->getLFloat(4); - $z = $this->getLFloat(4); - } - - public function putVector3f($x, $y, $z){ - $this->putLFloat($x); - $this->putLFloat($y); - $this->putLFloat($z); + /** + * Writes a 64-bit zigzag-encoded variable-length integer to the end of the buffer. + * @param int|string $v int, or the string representation of an int64 on 32-bit platforms + */ + public function putVarLong($v){ + $this->buffer .= Binary::writeVarLong($v); } public function feof(){