From 78c09267e5858010c80396f099c92b40137fe29d Mon Sep 17 00:00:00 2001 From: "Dylan K. Taylor" Date: Wed, 7 Jun 2017 13:53:10 +0100 Subject: [PATCH] Typehinted things in BinaryStream, sorted some methods and related bugfixes --- src/pocketmine/Player.php | 7 +- .../network/mcpe/protocol/BatchPacket.php | 2 +- .../mcpe/protocol/BlockEntityDataPacket.php | 2 +- .../mcpe/protocol/CommandStepPacket.php | 2 +- .../network/mcpe/protocol/DataPacket.php | 2 +- .../mcpe/protocol/MoveEntityPacket.php | 4 +- .../network/mcpe/protocol/StartGamePacket.php | 6 +- .../network/mcpe/protocol/UnknownPacket.php | 2 +- .../mcpe/protocol/UpdateTradePacket.php | 2 +- src/pocketmine/utils/BinaryStream.php | 269 ++++++++++-------- 10 files changed, 162 insertions(+), 136 deletions(-) diff --git a/src/pocketmine/Player.php b/src/pocketmine/Player.php index 8301b5e2e..d546e97c9 100644 --- a/src/pocketmine/Player.php +++ b/src/pocketmine/Player.php @@ -1867,12 +1867,12 @@ class Player extends Human implements CommandSender, InventoryHolder, ChunkLoade $pk->spawnX = $spawnPosition->getFloorX(); $pk->spawnY = $spawnPosition->getFloorY(); $pk->spawnZ = $spawnPosition->getFloorZ(); - $pk->hasAchievementsDisabled = 1; + $pk->hasAchievementsDisabled = true; $pk->dayCycleStopTime = -1; //TODO: implement this properly - $pk->eduMode = 0; + $pk->eduMode = false; $pk->rainLevel = 0; //TODO: implement these properly $pk->lightningLevel = 0; - $pk->commandsEnabled = 1; + $pk->commandsEnabled = true; $pk->levelId = ""; $pk->worldName = $this->server->getMotd(); $this->dataPacket($pk); @@ -3962,6 +3962,7 @@ class Player extends Human implements CommandSender, InventoryHolder, ChunkLoade $pk->pitch = $pitch; $pk->yaw = $yaw; $pk->mode = $mode; + $pk->onGround = $this->onGround; if($targets !== null){ $this->server->broadcastPacket($targets, $pk); diff --git a/src/pocketmine/network/mcpe/protocol/BatchPacket.php b/src/pocketmine/network/mcpe/protocol/BatchPacket.php index 448e8e2eb..8d510b8e6 100644 --- a/src/pocketmine/network/mcpe/protocol/BatchPacket.php +++ b/src/pocketmine/network/mcpe/protocol/BatchPacket.php @@ -46,7 +46,7 @@ class BatchPacket extends DataPacket{ } public function decode(){ - $this->payload = $this->get(true); + $this->payload = $this->get(0); } public function encode(){ diff --git a/src/pocketmine/network/mcpe/protocol/BlockEntityDataPacket.php b/src/pocketmine/network/mcpe/protocol/BlockEntityDataPacket.php index dfb40e848..29f2a2d26 100644 --- a/src/pocketmine/network/mcpe/protocol/BlockEntityDataPacket.php +++ b/src/pocketmine/network/mcpe/protocol/BlockEntityDataPacket.php @@ -38,7 +38,7 @@ class BlockEntityDataPacket extends DataPacket{ public function decode(){ $this->getBlockPosition($this->x, $this->y, $this->z); - $this->namedtag = $this->get(true); + $this->namedtag = $this->get(0); } public function encode(){ diff --git a/src/pocketmine/network/mcpe/protocol/CommandStepPacket.php b/src/pocketmine/network/mcpe/protocol/CommandStepPacket.php index 0c8c4f87e..ca701c429 100644 --- a/src/pocketmine/network/mcpe/protocol/CommandStepPacket.php +++ b/src/pocketmine/network/mcpe/protocol/CommandStepPacket.php @@ -49,7 +49,7 @@ class CommandStepPacket extends DataPacket{ $this->inputJson = json_decode($this->getString()); $this->outputJson = json_decode($this->getString()); - $this->get(true); //TODO: read command origin data + $this->get(0); //TODO: read command origin data } public function encode(){ diff --git a/src/pocketmine/network/mcpe/protocol/DataPacket.php b/src/pocketmine/network/mcpe/protocol/DataPacket.php index 4e09d5c32..999781a39 100644 --- a/src/pocketmine/network/mcpe/protocol/DataPacket.php +++ b/src/pocketmine/network/mcpe/protocol/DataPacket.php @@ -117,7 +117,7 @@ abstract class DataPacket extends BinaryStream{ $value = $this->getByte(); break; case Entity::DATA_TYPE_SHORT: - $value = $this->getLShort(true); //signed + $value = $this->getSignedLShort(); break; case Entity::DATA_TYPE_INT: $value = $this->getVarInt(); diff --git a/src/pocketmine/network/mcpe/protocol/MoveEntityPacket.php b/src/pocketmine/network/mcpe/protocol/MoveEntityPacket.php index 09fb989e3..1ee92be15 100644 --- a/src/pocketmine/network/mcpe/protocol/MoveEntityPacket.php +++ b/src/pocketmine/network/mcpe/protocol/MoveEntityPacket.php @@ -38,8 +38,8 @@ class MoveEntityPacket extends DataPacket{ public $yaw; public $headYaw; public $pitch; - public $onGround; - public $teleported; + public $onGround = false; + public $teleported = false; public function decode(){ $this->entityRuntimeId = $this->getEntityRuntimeId(); diff --git a/src/pocketmine/network/mcpe/protocol/StartGamePacket.php b/src/pocketmine/network/mcpe/protocol/StartGamePacket.php index 040abeef7..cc0713044 100644 --- a/src/pocketmine/network/mcpe/protocol/StartGamePacket.php +++ b/src/pocketmine/network/mcpe/protocol/StartGamePacket.php @@ -47,13 +47,13 @@ class StartGamePacket extends DataPacket{ public $spawnX; public $spawnY; public $spawnZ; - public $hasAchievementsDisabled = 1; + public $hasAchievementsDisabled = true; public $dayCycleStopTime = -1; //-1 = not stopped, any positive value = stopped at that time - public $eduMode = 0; + public $eduMode = false; public $rainLevel; public $lightningLevel; public $commandsEnabled; - public $isTexturePacksRequired = 0; + public $isTexturePacksRequired = true; public $levelId = ""; //base64 string, usually the same as world folder name in vanilla public $worldName; public $premiumWorldTemplateId = ""; diff --git a/src/pocketmine/network/mcpe/protocol/UnknownPacket.php b/src/pocketmine/network/mcpe/protocol/UnknownPacket.php index 47d046069..f8d914a49 100644 --- a/src/pocketmine/network/mcpe/protocol/UnknownPacket.php +++ b/src/pocketmine/network/mcpe/protocol/UnknownPacket.php @@ -45,7 +45,7 @@ class UnknownPacket extends DataPacket{ public function decode(){ $this->offset -= 1; //Rewind one byte so we can read the PID - $this->payload = $this->get(true); + $this->payload = $this->get(0); } public function encode(){ diff --git a/src/pocketmine/network/mcpe/protocol/UpdateTradePacket.php b/src/pocketmine/network/mcpe/protocol/UpdateTradePacket.php index afa8fe834..4086090cd 100644 --- a/src/pocketmine/network/mcpe/protocol/UpdateTradePacket.php +++ b/src/pocketmine/network/mcpe/protocol/UpdateTradePacket.php @@ -53,7 +53,7 @@ class UpdateTradePacket extends DataPacket{ $this->traderEid = $this->getEntityUniqueId(); $this->playerEid = $this->getEntityUniqueId(); $this->displayName = $this->getString(); - $this->offers = $this->get(true); + $this->offers = $this->get(0); } public function encode(){ diff --git a/src/pocketmine/utils/BinaryStream.php b/src/pocketmine/utils/BinaryStream.php index 0f6e213fa..31619b2ce 100644 --- a/src/pocketmine/utils/BinaryStream.php +++ b/src/pocketmine/utils/BinaryStream.php @@ -29,10 +29,12 @@ use pocketmine\item\Item; class BinaryStream{ + /** @var int */ public $offset; + /** @var string */ public $buffer; - public function __construct($buffer = "", $offset = 0){ + public function __construct(string $buffer = "", int $offset = 0){ $this->buffer = $buffer; $this->offset = $offset; } @@ -42,24 +44,24 @@ class BinaryStream{ $this->offset = 0; } - public function setBuffer($buffer = null, $offset = 0){ + public function setBuffer(string $buffer = "", int $offset = 0){ $this->buffer = $buffer; - $this->offset = (int) $offset; + $this->offset = $offset; } - public function getOffset(){ + public function getOffset() : int{ return $this->offset; } - public function getBuffer(){ + public function getBuffer() : string{ return $this->buffer; } - public function get($len){ + public function get(int $len) : string{ if($len < 0){ $this->offset = strlen($this->buffer) - 1; return ""; - }elseif($len === true){ + }elseif($len === 0){ $str = substr($this->buffer, $this->offset); $this->offset = strlen($this->buffer); return $str; @@ -68,115 +70,19 @@ class BinaryStream{ return $len === 1 ? $this->buffer{$this->offset++} : substr($this->buffer, ($this->offset += $len) - $len, $len); } - public function put($str){ + public function put(string $str){ $this->buffer .= $str; } + public function getBool() : bool{ return $this->get(1) !== "\x00"; } - public function putBool($v){ + public function putBool(bool $v){ $this->buffer .= ($v ? "\x01" : "\x00"); } - public function getLong(){ - return Binary::readLong($this->get(8)); - } - - public function putLong($v){ - $this->buffer .= Binary::writeLong($v); - } - - public function getInt(){ - return Binary::readInt($this->get(4)); - } - - public function putInt($v){ - $this->buffer .= Binary::writeInt($v); - } - - public function getLLong(){ - return Binary::readLLong($this->get(8)); - } - - public function putLLong($v){ - $this->buffer .= Binary::writeLLong($v); - } - - public function getLInt(){ - return Binary::readLInt($this->get(4)); - } - - public function putLInt($v){ - $this->buffer .= Binary::writeLInt($v); - } - - public function getSignedShort(){ - return Binary::readSignedShort($this->get(2)); - } - - public function putShort($v){ - $this->buffer .= Binary::writeShort($v); - } - - public function getShort(){ - return Binary::readShort($this->get(2)); - } - - public function putSignedShort($v){ - $this->buffer .= Binary::writeShort($v); - } - - public function getFloat(){ - return Binary::readFloat($this->get(4)); - } - - public function getRoundedFloat(int $accuracy){ - return Binary::readRoundedFloat($this->get(4), $accuracy); - } - - public function putFloat($v){ - $this->buffer .= Binary::writeFloat($v); - } - - public function getLShort($signed = true){ - return $signed ? Binary::readSignedLShort($this->get(2)) : Binary::readLShort($this->get(2)); - } - - public function putLShort($v){ - $this->buffer .= Binary::writeLShort($v); - } - - public function getLFloat(){ - return Binary::readLFloat($this->get(4)); - } - - public function getRoundedLFloat(int $accuracy){ - return Binary::readRoundedLFloat($this->get(4), $accuracy); - } - - public function putLFloat($v){ - $this->buffer .= Binary::writeLFloat($v); - } - - - public function getTriad(){ - return Binary::readTriad($this->get(3)); - } - - public function putTriad($v){ - $this->buffer .= Binary::writeTriad($v); - } - - - public function getLTriad(){ - return Binary::readLTriad($this->get(3)); - } - - public function putLTriad($v){ - $this->buffer .= Binary::writeLTriad($v); - } public function getByte() : int{ return ord($this->buffer{$this->offset++}); @@ -186,7 +92,131 @@ class BinaryStream{ $this->buffer .= chr($v); } - public function getUUID(){ + + public function getShort() : int{ + return Binary::readShort($this->get(2)); + } + + public function getSignedShort() : int{ + return Binary::readSignedShort($this->get(2)); + } + + public function putShort(int $v){ + $this->buffer .= Binary::writeShort($v); + } + + public function getLShort() : int{ + return Binary::readLShort($this->get(2)); + } + + public function getSignedLShort() : int{ + return Binary::readSignedLShort($this->get(2)); + } + + public function putLShort(int $v){ + $this->buffer .= Binary::writeLShort($v); + } + + + public function getTriad() : int{ + return Binary::readTriad($this->get(3)); + } + + public function putTriad(int $v){ + $this->buffer .= Binary::writeTriad($v); + } + + public function getLTriad() : int{ + return Binary::readLTriad($this->get(3)); + } + + public function putLTriad(int $v){ + $this->buffer .= Binary::writeLTriad($v); + } + + + public function getInt() : int{ + return Binary::readInt($this->get(4)); + } + + public function putInt(int $v){ + $this->buffer .= Binary::writeInt($v); + } + + public function getLInt() : int{ + return Binary::readLInt($this->get(4)); + } + + public function putLInt(int $v){ + $this->buffer .= Binary::writeLInt($v); + } + + + public function getFloat() : float{ + return Binary::readFloat($this->get(4)); + } + + public function getRoundedFloat(int $accuracy) : float{ + return Binary::readRoundedFloat($this->get(4), $accuracy); + } + + public function putFloat(float $v){ + $this->buffer .= Binary::writeFloat($v); + } + + public function getLFloat() : float{ + return Binary::readLFloat($this->get(4)); + } + + public function getRoundedLFloat(int $accuracy) : float{ + return Binary::readRoundedLFloat($this->get(4), $accuracy); + } + + public function putLFloat(float $v){ + $this->buffer .= Binary::writeLFloat($v); + } + + + /** + * @return int|string + */ + public function getLong(){ + return Binary::readLong($this->get(8)); + } + + /** + * @param int|string $v + */ + public function putLong($v){ + $this->buffer .= Binary::writeLong($v); + } + + /** + * @return int|string + */ + public function getLLong(){ + return Binary::readLLong($this->get(8)); + } + + /** + * @param int|string $v + */ + public function putLLong($v){ + $this->buffer .= Binary::writeLLong($v); + } + + + public function getString() : string{ + return $this->get($this->getUnsignedVarInt()); + } + + public function putString(string $v){ + $this->putUnsignedVarInt(strlen($v)); + $this->put($v); + } + + + public function getUUID() : UUID{ //This is actually two little-endian longs: UUID Most followed by UUID Least $part1 = $this->getLInt(); $part0 = $this->getLInt(); @@ -202,7 +232,7 @@ class BinaryStream{ $this->putLInt($uuid->getPart(2)); } - public function getSlot(){ + public function getSlot() : Item{ $id = $this->getVarInt(); if($id <= 0){ return Item::get(0, 0, 0); @@ -260,20 +290,11 @@ class BinaryStream{ $this->putVarInt(0); //CanDestroy entry count (TODO) } - public function getString(){ - return $this->get($this->getUnsignedVarInt()); - } - - public function putString($v){ - $this->putUnsignedVarInt(strlen($v)); - $this->put($v); - } - /** * Reads a 32-bit variable-length unsigned integer from the buffer and returns it. * @return int */ - public function getUnsignedVarInt(){ + public function getUnsignedVarInt() : int{ return Binary::readUnsignedVarInt($this->buffer, $this->offset); } @@ -281,7 +302,7 @@ class BinaryStream{ * Writes a 32-bit variable-length unsigned integer to the end of the buffer. * @param int $v */ - public function putUnsignedVarInt($v){ + public function putUnsignedVarInt(int $v){ $this->put(Binary::writeUnsignedVarInt($v)); } @@ -289,7 +310,7 @@ class BinaryStream{ * Reads a 32-bit zigzag-encoded variable-length integer from the buffer and returns it. * @return int */ - public function getVarInt(){ + public function getVarInt() : int{ return Binary::readVarInt($this->buffer, $this->offset); } @@ -297,7 +318,7 @@ class BinaryStream{ * Writes a 32-bit zigzag-encoded variable-length integer to the end of the buffer. * @param int $v */ - public function putVarInt($v){ + public function putVarInt(int $v){ $this->put(Binary::writeVarInt($v)); } @@ -333,7 +354,11 @@ class BinaryStream{ $this->buffer .= Binary::writeVarLong($v); } - public function feof(){ + /** + * Returns whether the offset has reached the end of the buffer. + * @return bool + */ + public function feof() : bool{ return !isset($this->buffer{$this->offset}); } }