Cleaned up muddled varint/varlong mess, added separate methods for entity unique and runtime ids, moved some MCPE-protocol-specific methods out of BinaryStream

This commit is contained in:
Dylan K. Taylor 2017-03-08 19:34:12 +00:00
parent 3a044f0154
commit 295d9bc80b
36 changed files with 177 additions and 110 deletions

View File

@ -50,8 +50,8 @@ class AddEntityPacket extends DataPacket{
public function encode(){ public function encode(){
$this->reset(); $this->reset();
$this->putEntityId($this->eid); //EntityUniqueID - TODO: verify this $this->putEntityUniqueId($this->eid);
$this->putEntityId($this->eid); $this->putEntityRuntimeId($this->eid);
$this->putUnsignedVarInt($this->type); $this->putUnsignedVarInt($this->type);
$this->putVector3f($this->x, $this->y, $this->z); $this->putVector3f($this->x, $this->y, $this->z);
$this->putVector3f($this->speedX, $this->speedY, $this->speedZ); $this->putVector3f($this->speedX, $this->speedY, $this->speedZ);
@ -67,8 +67,8 @@ class AddEntityPacket extends DataPacket{
$this->putEntityMetadata($this->metadata); $this->putEntityMetadata($this->metadata);
$this->putUnsignedVarInt(count($this->links)); $this->putUnsignedVarInt(count($this->links));
foreach($this->links as $link){ foreach($this->links as $link){
$this->putEntityId($link[0]); $this->putEntityUniqueId($link[0]);
$this->putEntityId($link[1]); $this->putEntityUniqueId($link[1]);
$this->putByte($link[2]); $this->putByte($link[2]);
} }
} }

View File

@ -41,9 +41,9 @@ class AddHangingEntityPacket extends DataPacket{
public function encode(){ public function encode(){
$this->reset(); $this->reset();
$this->putEntityId($this->entityUniqueId); $this->putEntityUniqueId($this->entityUniqueId);
$this->putEntityId($this->entityRuntimeId); $this->putEntityRuntimeId($this->entityRuntimeId);
$this->putBlockCoords($this->x, $this->y, $this->z); $this->putBlockPosition($this->x, $this->y, $this->z);
$this->putVarInt($this->unknown); $this->putVarInt($this->unknown);
} }

View File

@ -44,8 +44,8 @@ class AddItemEntityPacket extends DataPacket{
public function encode(){ public function encode(){
$this->reset(); $this->reset();
$this->putEntityId($this->eid); //EntityUniqueID $this->putEntityUniqueId($this->eid);
$this->putEntityId($this->eid); //EntityRuntimeID $this->putEntityRuntimeId($this->eid);
$this->putSlot($this->item); $this->putSlot($this->item);
$this->putVector3f($this->x, $this->y, $this->z); $this->putVector3f($this->x, $this->y, $this->z);
$this->putVector3f($this->speedX, $this->speedY, $this->speedZ); $this->putVector3f($this->speedX, $this->speedY, $this->speedZ);

View File

@ -42,9 +42,9 @@ class AddPaintingPacket extends DataPacket{
public function encode(){ public function encode(){
$this->reset(); $this->reset();
$this->putEntityId($this->eid); //EntityUniqueID $this->putEntityUniqueId($this->eid);
$this->putEntityId($this->eid); //EntityRuntimeID $this->putEntityRuntimeId($this->eid);
$this->putBlockCoords($this->x, $this->y, $this->z); $this->putBlockPosition($this->x, $this->y, $this->z);
$this->putVarInt($this->direction); $this->putVarInt($this->direction);
$this->putString($this->title); $this->putString($this->title);
} }

View File

@ -51,8 +51,8 @@ class AddPlayerPacket extends DataPacket{
$this->reset(); $this->reset();
$this->putUUID($this->uuid); $this->putUUID($this->uuid);
$this->putString($this->username); $this->putString($this->username);
$this->putEntityId($this->eid); //EntityUniqueID $this->putEntityUniqueId($this->eid);
$this->putEntityId($this->eid); //EntityRuntimeID $this->putEntityRuntimeId($this->eid);
$this->putVector3f($this->x, $this->y, $this->z); $this->putVector3f($this->x, $this->y, $this->z);
$this->putVector3f($this->speedX, $this->speedY, $this->speedZ); $this->putVector3f($this->speedX, $this->speedY, $this->speedZ);
$this->putLFloat($this->pitch); $this->putLFloat($this->pitch);

View File

@ -34,13 +34,13 @@ class AnimatePacket extends DataPacket{
public function decode(){ public function decode(){
$this->action = $this->getVarInt(); $this->action = $this->getVarInt();
$this->eid = $this->getEntityId(); $this->eid = $this->getEntityRuntimeId();
} }
public function encode(){ public function encode(){
$this->reset(); $this->reset();
$this->putVarInt($this->action); $this->putVarInt($this->action);
$this->putEntityId($this->eid); $this->putEntityRuntimeId($this->eid);
} }
public function handle(NetworkSession $session) : bool{ public function handle(NetworkSession $session) : bool{

View File

@ -35,13 +35,13 @@ class BlockEntityDataPacket extends DataPacket{
public $namedtag; public $namedtag;
public function decode(){ public function decode(){
$this->getBlockCoords($this->x, $this->y, $this->z); $this->getBlockPosition($this->x, $this->y, $this->z);
$this->namedtag = $this->get(true); $this->namedtag = $this->get(true);
} }
public function encode(){ public function encode(){
$this->reset(); $this->reset();
$this->putBlockCoords($this->x, $this->y, $this->z); $this->putBlockPosition($this->x, $this->y, $this->z);
$this->put($this->namedtag); $this->put($this->namedtag);
} }

View File

@ -41,7 +41,7 @@ class BlockEventPacket extends DataPacket{
public function encode(){ public function encode(){
$this->reset(); $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->case1);
$this->putVarInt($this->case2); $this->putVarInt($this->case2);
} }

View File

@ -28,25 +28,13 @@ use pocketmine\network\mcpe\NetworkSession;
class CommandStepPacket extends DataPacket{ class CommandStepPacket extends DataPacket{
const NETWORK_ID = ProtocolInfo::COMMAND_STEP_PACKET; 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 $command;
public $overload; public $overload;
public $uvarint1; public $uvarint1;
public $uvarint2; public $uvarint2;
public $bool; public $bool;
public $uvarint64; public $uvarint64;
public $args; //JSON formatted command arguments public $args;
public $string4; public $string4;
public function decode(){ public function decode(){
@ -55,12 +43,11 @@ class CommandStepPacket extends DataPacket{
$this->uvarint1 = $this->getUnsignedVarInt(); $this->uvarint1 = $this->getUnsignedVarInt();
$this->uvarint2 = $this->getUnsignedVarInt(); $this->uvarint2 = $this->getUnsignedVarInt();
$this->bool = (bool) $this->getByte(); $this->bool = (bool) $this->getByte();
$this->uvarint64 = $this->getUnsignedVarInt(); //TODO: varint64 $this->uvarint64 = $this->getUnsignedVarLong();
$this->args = json_decode($this->getString()); $this->args = json_decode($this->getString());
$this->string4 = $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(){ public function encode(){

View File

@ -46,8 +46,8 @@ class ContainerOpenPacket extends DataPacket{
$this->putByte($this->windowid); $this->putByte($this->windowid);
$this->putByte($this->type); $this->putByte($this->type);
$this->putVarInt($this->slots); $this->putVarInt($this->slots);
$this->putBlockCoords($this->x, $this->y, $this->z); $this->putBlockPosition($this->x, $this->y, $this->z);
$this->putEntityId($this->entityId); $this->putEntityUniqueId($this->entityId);
} }
public function handle(NetworkSession $session) : bool{ public function handle(NetworkSession $session) : bool{

View File

@ -122,7 +122,7 @@ abstract class DataPacket extends BinaryStream{
$value[2] = $this->getVarInt(); //z $value[2] = $this->getVarInt(); //z
break; break;
case Entity::DATA_TYPE_LONG: case Entity::DATA_TYPE_LONG:
$value = $this->getVarInt(); //TODO: varint64 proper support $value = $this->getVarLong();
break; break;
case Entity::DATA_TYPE_VECTOR3F: case Entity::DATA_TYPE_VECTOR3F:
$value = [0.0, 0.0, 0.0]; $value = [0.0, 0.0, 0.0];
@ -178,7 +178,7 @@ abstract class DataPacket extends BinaryStream{
$this->putVarInt($d[1][2]); //z $this->putVarInt($d[1][2]); //z
break; break;
case Entity::DATA_TYPE_LONG: case Entity::DATA_TYPE_LONG:
$this->putVarInt($d[1]); //TODO: varint64 support $this->putVarLong($d[1]);
break; break;
case Entity::DATA_TYPE_VECTOR3F: case Entity::DATA_TYPE_VECTOR3F:
//TODO: change this implementation (use objects) //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);
}
} }

View File

@ -52,14 +52,14 @@ class EntityEventPacket extends DataPacket{
public $unknown; public $unknown;
public function decode(){ public function decode(){
$this->eid = $this->getEntityId(); $this->eid = $this->getEntityRuntimeId();
$this->event = $this->getByte(); $this->event = $this->getByte();
$this->unknown = $this->getVarInt(); $this->unknown = $this->getVarInt();
} }
public function encode(){ public function encode(){
$this->reset(); $this->reset();
$this->putEntityId($this->eid); $this->putEntityRuntimeId($this->eid);
$this->putByte($this->event); $this->putByte($this->event);
$this->putVarInt($this->unknown); $this->putVarInt($this->unknown);
} }

View File

@ -51,7 +51,7 @@ class ExplodePacket extends DataPacket{
$this->putUnsignedVarInt(count($this->records)); $this->putUnsignedVarInt(count($this->records));
if(count($this->records) > 0){ if(count($this->records) > 0){
foreach($this->records as $record){ foreach($this->records as $record){
$this->putBlockCoords($record->x, $record->y, $record->z); $this->putBlockPosition($record->x, $record->y, $record->z);
} }
} }
} }

View File

@ -39,13 +39,13 @@ class InteractPacket extends DataPacket{
public function decode(){ public function decode(){
$this->action = $this->getByte(); $this->action = $this->getByte();
$this->target = $this->getEntityId(); $this->target = $this->getEntityRuntimeId();
} }
public function encode(){ public function encode(){
$this->reset(); $this->reset();
$this->putByte($this->action); $this->putByte($this->action);
$this->putEntityId($this->target); $this->putEntityRuntimeId($this->target);
} }
public function handle(NetworkSession $session) : bool{ public function handle(NetworkSession $session) : bool{

View File

@ -34,7 +34,7 @@ class ItemFrameDropItemPacket extends DataPacket{
public $z; public $z;
public function decode(){ public function decode(){
$this->getBlockCoords($this->x, $this->y, $this->z); $this->getBlockPosition($this->x, $this->y, $this->z);
} }
public function encode(){ public function encode(){

View File

@ -33,7 +33,7 @@ class MobArmorEquipmentPacket extends DataPacket{
public $slots = []; public $slots = [];
public function decode(){ public function decode(){
$this->eid = $this->getEntityId(); $this->eid = $this->getEntityRuntimeId();
$this->slots[0] = $this->getSlot(); $this->slots[0] = $this->getSlot();
$this->slots[1] = $this->getSlot(); $this->slots[1] = $this->getSlot();
$this->slots[2] = $this->getSlot(); $this->slots[2] = $this->getSlot();
@ -42,7 +42,7 @@ class MobArmorEquipmentPacket extends DataPacket{
public function encode(){ public function encode(){
$this->reset(); $this->reset();
$this->putEntityId($this->eid); $this->putEntityRuntimeId($this->eid);
$this->putSlot($this->slots[0]); $this->putSlot($this->slots[0]);
$this->putSlot($this->slots[1]); $this->putSlot($this->slots[1]);
$this->putSlot($this->slots[2]); $this->putSlot($this->slots[2]);

View File

@ -46,7 +46,7 @@ class MobEffectPacket extends DataPacket{
public function encode(){ public function encode(){
$this->reset(); $this->reset();
$this->putEntityId($this->eid); $this->putEntityRuntimeId($this->eid);
$this->putByte($this->eventId); $this->putByte($this->eventId);
$this->putVarInt($this->effectId); $this->putVarInt($this->effectId);
$this->putVarInt($this->amplifier); $this->putVarInt($this->amplifier);

View File

@ -36,7 +36,7 @@ class MobEquipmentPacket extends DataPacket{
public $unknownByte; public $unknownByte;
public function decode(){ public function decode(){
$this->eid = $this->getEntityId(); //EntityRuntimeID $this->eid = $this->getEntityRuntimeId();
$this->item = $this->getSlot(); $this->item = $this->getSlot();
$this->slot = $this->getByte(); $this->slot = $this->getByte();
$this->selectedSlot = $this->getByte(); $this->selectedSlot = $this->getByte();
@ -45,7 +45,7 @@ class MobEquipmentPacket extends DataPacket{
public function encode(){ public function encode(){
$this->reset(); $this->reset();
$this->putEntityId($this->eid); //EntityRuntimeID $this->putEntityRuntimeId($this->eid);
$this->putSlot($this->item); $this->putSlot($this->item);
$this->putByte($this->slot); $this->putByte($this->slot);
$this->putByte($this->selectedSlot); $this->putByte($this->selectedSlot);

View File

@ -38,7 +38,7 @@ class MoveEntityPacket extends DataPacket{
public $pitch; public $pitch;
public function decode(){ public function decode(){
$this->eid = $this->getEntityId(); $this->eid = $this->getEntityRuntimeId();
$this->getVector3f($this->x, $this->y, $this->z); $this->getVector3f($this->x, $this->y, $this->z);
$this->pitch = $this->getByte() * (360.0 / 256); $this->pitch = $this->getByte() * (360.0 / 256);
$this->yaw = $this->getByte() * (360.0 / 256); $this->yaw = $this->getByte() * (360.0 / 256);
@ -47,7 +47,7 @@ class MoveEntityPacket extends DataPacket{
public function encode(){ public function encode(){
$this->reset(); $this->reset();
$this->putEntityId($this->eid); $this->putEntityRuntimeId($this->eid);
$this->putVector3f($this->x, $this->y, $this->z); $this->putVector3f($this->x, $this->y, $this->z);
$this->putByte($this->pitch / (360.0 / 256)); $this->putByte($this->pitch / (360.0 / 256));
$this->putByte($this->yaw / (360.0 / 256)); $this->putByte($this->yaw / (360.0 / 256));

View File

@ -44,7 +44,7 @@ class MovePlayerPacket extends DataPacket{
public $onGround; public $onGround;
public function decode(){ public function decode(){
$this->eid = $this->getEntityId(); //EntityRuntimeID $this->eid = $this->getEntityRuntimeId();
$this->getVector3f($this->x, $this->y, $this->z); $this->getVector3f($this->x, $this->y, $this->z);
$this->pitch = $this->getLFloat(); $this->pitch = $this->getLFloat();
$this->yaw = $this->getLFloat(); $this->yaw = $this->getLFloat();
@ -55,7 +55,7 @@ class MovePlayerPacket extends DataPacket{
public function encode(){ public function encode(){
$this->reset(); $this->reset();
$this->putEntityId($this->eid); //EntityRuntimeID $this->putEntityRuntimeId($this->eid);
$this->putVector3f($this->x, $this->y, $this->z); $this->putVector3f($this->x, $this->y, $this->z);
$this->putLFloat($this->pitch); $this->putLFloat($this->pitch);
$this->putLFloat($this->yaw); $this->putLFloat($this->yaw);

View File

@ -55,17 +55,17 @@ class PlayerActionPacket extends DataPacket{
public $face; public $face;
public function decode(){ public function decode(){
$this->eid = $this->getEntityId(); $this->eid = $this->getEntityRuntimeId();
$this->action = $this->getVarInt(); $this->action = $this->getVarInt();
$this->getBlockCoords($this->x, $this->y, $this->z); $this->getBlockPosition($this->x, $this->y, $this->z);
$this->face = $this->getVarInt(); $this->face = $this->getVarInt();
} }
public function encode(){ public function encode(){
$this->reset(); $this->reset();
$this->putEntityId($this->eid); $this->putEntityRuntimeId($this->eid);
$this->putVarInt($this->action); $this->putVarInt($this->action);
$this->putBlockCoords($this->x, $this->y, $this->z); $this->putBlockPosition($this->x, $this->y, $this->z);
$this->putVarInt($this->face); $this->putVarInt($this->face);
} }

View File

@ -53,7 +53,7 @@ class PlayerListPacket extends DataPacket{
foreach($this->entries as $d){ foreach($this->entries as $d){
if($this->type === self::TYPE_ADD){ if($this->type === self::TYPE_ADD){
$this->putUUID($d[0]); $this->putUUID($d[0]);
$this->putEntityId($d[1]); $this->putEntityUniqueId($d[1]);
$this->putString($d[2]); $this->putString($d[2]);
$this->putString($d[3]); $this->putString($d[3]);
$this->putString($d[4]); $this->putString($d[4]);

View File

@ -34,7 +34,7 @@ class RemoveBlockPacket extends DataPacket{
public $z; public $z;
public function decode(){ public function decode(){
$this->getBlockCoords($this->x, $this->y, $this->z); $this->getBlockPosition($this->x, $this->y, $this->z);
} }
public function encode(){ public function encode(){

View File

@ -37,7 +37,7 @@ class RemoveEntityPacket extends DataPacket{
public function encode(){ public function encode(){
$this->reset(); $this->reset();
$this->putEntityId($this->eid); $this->putEntityUniqueId($this->eid);
} }
public function handle(NetworkSession $session) : bool{ public function handle(NetworkSession $session) : bool{

View File

@ -38,7 +38,7 @@ class SetEntityDataPacket extends DataPacket{
public function encode(){ public function encode(){
$this->reset(); $this->reset();
$this->putEntityId($this->eid); $this->putEntityRuntimeId($this->eid);
$this->putEntityMetadata($this->metadata); $this->putEntityMetadata($this->metadata);
} }

View File

@ -39,8 +39,8 @@ class SetEntityLinkPacket extends DataPacket{
public function encode(){ public function encode(){
$this->reset(); $this->reset();
$this->putEntityId($this->from); $this->putEntityUniqueId($this->from);
$this->putEntityId($this->to); $this->putEntityUniqueId($this->to);
$this->putByte($this->type); $this->putByte($this->type);
} }

View File

@ -40,7 +40,7 @@ class SetEntityMotionPacket extends DataPacket{
public function encode(){ public function encode(){
$this->reset(); $this->reset();
$this->putEntityId($this->eid); $this->putEntityRuntimeId($this->eid);
$this->putVector3f($this->motionX, $this->motionY, $this->motionZ); $this->putVector3f($this->motionX, $this->motionY, $this->motionZ);
} }

View File

@ -42,7 +42,7 @@ class SetSpawnPositionPacket extends DataPacket{
public function encode(){ public function encode(){
$this->reset(); $this->reset();
$this->putVarInt($this->unknown); $this->putVarInt($this->unknown);
$this->putBlockCoords($this->x, $this->y, $this->z); $this->putBlockPosition($this->x, $this->y, $this->z);
$this->putBool($this->unknownBool); $this->putBool($this->unknownBool);
} }

View File

@ -37,13 +37,13 @@ class ShowCreditsPacket extends DataPacket{
public $status; public $status;
public function decode(){ public function decode(){
$this->playerEid = $this->getEntityId(); $this->playerEid = $this->getEntityRuntimeId();
$this->status = $this->getVarInt(); $this->status = $this->getVarInt();
} }
public function encode(){ public function encode(){
$this->reset(); $this->reset();
$this->putEntityId($this->playerEid); $this->putEntityRuntimeId($this->playerEid);
$this->putVarInt($this->status); $this->putVarInt($this->status);
} }

View File

@ -58,8 +58,8 @@ class StartGamePacket extends DataPacket{
public function encode(){ public function encode(){
$this->reset(); $this->reset();
$this->putEntityId($this->entityUniqueId); //EntityUniqueID $this->putEntityUniqueId($this->entityUniqueId); //EntityUniqueID
$this->putEntityId($this->entityRuntimeId); //EntityRuntimeID $this->putEntityRuntimeId($this->entityRuntimeId); //EntityRuntimeID
$this->putVector3f($this->x, $this->y, $this->z); $this->putVector3f($this->x, $this->y, $this->z);
$this->putLFloat(0); //TODO: find out what these are (yaw/pitch?) $this->putLFloat(0); //TODO: find out what these are (yaw/pitch?)
$this->putLFloat(0); $this->putLFloat(0);
@ -68,7 +68,7 @@ class StartGamePacket extends DataPacket{
$this->putVarInt($this->generator); $this->putVarInt($this->generator);
$this->putVarInt($this->gamemode); $this->putVarInt($this->gamemode);
$this->putVarInt($this->difficulty); $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->putBool($this->hasAchievementsDisabled);
$this->putVarInt($this->dayCycleStopTime); $this->putVarInt($this->dayCycleStopTime);
$this->putBool($this->eduMode); $this->putBool($this->eduMode);

View File

@ -38,8 +38,8 @@ class TakeItemEntityPacket extends DataPacket{
public function encode(){ public function encode(){
$this->reset(); $this->reset();
$this->putEntityId($this->target); $this->putEntityRuntimeId($this->target);
$this->putEntityId($this->eid); $this->putEntityRuntimeId($this->eid);
} }
public function handle(NetworkSession $session) : bool{ public function handle(NetworkSession $session) : bool{

View File

@ -41,7 +41,7 @@ class UpdateAttributesPacket extends DataPacket{
public function encode(){ public function encode(){
$this->reset(); $this->reset();
$this->putEntityId($this->entityId); $this->putEntityRuntimeId($this->entityId);
$this->putUnsignedVarInt(count($this->entries)); $this->putUnsignedVarInt(count($this->entries));
foreach($this->entries as $entry){ foreach($this->entries as $entry){
$this->putLFloat($entry->getMinValue()); $this->putLFloat($entry->getMinValue());

View File

@ -51,7 +51,7 @@ class UpdateBlockPacket extends DataPacket{
public function encode(){ public function encode(){
$this->reset(); $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->blockId);
$this->putUnsignedVarInt(($this->flags << 4) | $this->blockData); $this->putUnsignedVarInt(($this->flags << 4) | $this->blockData);
} }

View File

@ -44,7 +44,7 @@ class UseItemPacket extends DataPacket{
public $slot; public $slot;
public function decode(){ public function decode(){
$this->getBlockCoords($this->x, $this->y, $this->z); $this->getBlockPosition($this->x, $this->y, $this->z);
$this->blockId = $this->getUnsignedVarInt(); $this->blockId = $this->getUnsignedVarInt();
$this->face = $this->getVarInt(); $this->face = $this->getVarInt();
$this->getVector3f($this->fx, $this->fy, $this->fz); $this->getVector3f($this->fx, $this->fy, $this->fz);

View File

@ -412,7 +412,7 @@ class Binary{
* Reads a 64-bit zigzag-encoded variable-length integer from the supplied stream. * Reads a 64-bit zigzag-encoded variable-length integer from the supplied stream.
* @param \pocketmine\nbt\NBT|BinaryStream $stream * @param \pocketmine\nbt\NBT|BinaryStream $stream
* *
* @return int * @return int|string
*/ */
public static function readVarLong($stream){ public static function readVarLong($stream){
if(PHP_INT_SIZE === 8){ if(PHP_INT_SIZE === 8){
@ -476,7 +476,6 @@ class Binary{
for($i = 0; $i <= 63; $i += 7){ for($i = 0; $i <= 63; $i += 7){
$b = $stream->getByte(); $b = $stream->getByte();
$value = bcadd($value, bcmul($b & 0x7f, bcpow("2", "$i"))); $value = bcadd($value, bcmul($b & 0x7f, bcpow("2", "$i")));
var_dump($value);
if(($b & 0x80) === 0){ if(($b & 0x80) === 0){
return $value; return $value;
@ -497,7 +496,6 @@ class Binary{
for($i = 0; $i <= 63; $i += 7){ for($i = 0; $i <= 63; $i += 7){
$b = $stream->getByte(); $b = $stream->getByte();
$value |= (($b & 0x7f) << $i); $value |= (($b & 0x7f) << $i);
var_dump($value);
if(($b & 0x80) === 0){ if(($b & 0x80) === 0){
return $value; return $value;

View File

@ -233,66 +233,68 @@ class BinaryStream extends \stdClass{
$this->put($v); $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(){ public function getUnsignedVarInt(){
return Binary::readUnsignedVarInt($this); 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){ public function putUnsignedVarInt($v){
$this->put(Binary::writeUnsignedVarInt($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(){ public function getVarInt(){
return Binary::readVarInt($this); 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){ public function putVarInt($v){
$this->put(Binary::writeVarInt($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(); * Reads a 64-bit zigzag-encoded variable-length integer from the buffer and returns it.
$y = $this->getUnsignedVarInt(); * @return int|string int, or the string representation of an int64 on 32-bit platforms
$z = $this->getVarInt(); */
public function getVarLong(){
return Binary::readVarLong($this);
} }
public function putBlockCoords($x, $y, $z){ /**
$this->putVarInt($x); * Writes a 64-bit zigzag-encoded variable-length integer to the end of the buffer.
$this->putUnsignedVarInt($y); * @param int|string $v int, or the string representation of an int64 on 32-bit platforms
$this->putVarInt($z); */
} public function putVarLong($v){
$this->buffer .= Binary::writeVarLong($v);
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);
} }
public function feof(){ public function feof(){