Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
33 changes: 9 additions & 24 deletions src/AnimatePacket.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,10 @@

namespace pocketmine\network\mcpe\protocol;

use pmmp\encoding\Byte;
use pmmp\encoding\ByteBufferReader;
use pmmp\encoding\ByteBufferWriter;
use pmmp\encoding\LE;
use pmmp\encoding\VarInt;
use pocketmine\network\mcpe\protocol\serializer\CommonTypes;

class AnimatePacket extends DataPacket implements ClientboundPacket, ServerboundPacket{
Expand All @@ -28,48 +28,33 @@ class AnimatePacket extends DataPacket implements ClientboundPacket, Serverbound
public const ACTION_STOP_SLEEP = 3;
public const ACTION_CRITICAL_HIT = 4;
public const ACTION_MAGICAL_CRITICAL_HIT = 5;
public const ACTION_ROW_RIGHT = 128;
public const ACTION_ROW_LEFT = 129;

public int $action;
public int $actorRuntimeId;
public float $data = 0.0;
public float $rowingTime = 0.0;
public ?string $swingSource = null;

public static function create(int $actorRuntimeId, int $actionId, float $data = 0.0) : self{
public static function create(int $actorRuntimeId, int $action, float $data = 0.0, ?string $swingSource = null) : self{
$result = new self;
$result->actorRuntimeId = $actorRuntimeId;
$result->action = $actionId;
$result->action = $action;
$result->data = $data;
return $result;
}

public static function boatHack(int $actorRuntimeId, int $actionId, float $rowingTime) : self{
if($actionId !== self::ACTION_ROW_LEFT && $actionId !== self::ACTION_ROW_RIGHT){
throw new \InvalidArgumentException("Invalid actionId for boatHack: $actionId");
}

$result = self::create($actorRuntimeId, $actionId);
$result->rowingTime = $rowingTime;
$result->swingSource = $swingSource;
return $result;
}

protected function decodePayload(ByteBufferReader $in) : void{
$this->action = VarInt::readSignedInt($in);
$this->action = Byte::readUnsigned($in);
$this->actorRuntimeId = CommonTypes::getActorRuntimeId($in);
$this->data = LE::readFloat($in);
if($this->action === self::ACTION_ROW_LEFT || $this->action === self::ACTION_ROW_RIGHT){
$this->rowingTime = LE::readFloat($in);
}
$this->swingSource = CommonTypes::readOptional($in, CommonTypes::getString(...));
}

protected function encodePayload(ByteBufferWriter $out) : void{
VarInt::writeSignedInt($out, $this->action);
Byte::writeUnsigned($out, $this->action);
CommonTypes::putActorRuntimeId($out, $this->actorRuntimeId);
LE::writeFloat($out, $this->data);
if($this->action === self::ACTION_ROW_LEFT || $this->action === self::ACTION_ROW_RIGHT){
LE::writeFloat($out, $this->rowingTime);
}
CommonTypes::writeOptional($out, $this->swingSource, CommonTypes::putString(...));
}

public function handle(PacketHandlerInterface $handler) : bool{
Expand Down
6 changes: 2 additions & 4 deletions src/AvailableCommandsPacket.php
Original file line number Diff line number Diff line change
Expand Up @@ -188,9 +188,8 @@ protected function decodePayload(ByteBufferReader $in) : void{
}

$this->enums = [];
$valueListSize = count($this->enumValues);
for($i = 0, $size = VarInt::readUnsignedInt($in); $i < $size; $i++){
$this->enums[] = CommandEnumRawData::read($in, $valueListSize);
$this->enums[] = CommandEnumRawData::read($in);
}

$this->chainedSubCommandData = [];
Expand Down Expand Up @@ -231,9 +230,8 @@ protected function encodePayload(ByteBufferWriter $out) : void{
}

VarInt::writeUnsignedInt($out, count($this->enums));
$valueListSize = count($this->enumValues);
foreach($this->enums as $enum){
$enum->write($out, $valueListSize);
$enum->write($out);
}

VarInt::writeUnsignedInt($out, count($this->chainedSubCommandData));
Expand Down
70 changes: 70 additions & 0 deletions src/ClientboundDataStorePacket.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
<?php

/*
* This file is part of BedrockProtocol.
* Copyright (C) 2014-2022 PocketMine Team <https://github.com/pmmp/BedrockProtocol>
*
* BedrockProtocol is free software: you can redistribute it and/or modify
* it under the terms of the GNU Lesser General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*/

declare(strict_types=1);

namespace pocketmine\network\mcpe\protocol;

use pmmp\encoding\ByteBufferReader;
use pmmp\encoding\ByteBufferWriter;
use pmmp\encoding\VarInt;
use pocketmine\network\mcpe\protocol\types\DataStore;
use pocketmine\network\mcpe\protocol\types\DataStoreChange;
use pocketmine\network\mcpe\protocol\types\DataStoreRemoval;
use pocketmine\network\mcpe\protocol\types\DataStoreType;
use pocketmine\network\mcpe\protocol\types\DataStoreUpdate;
use function count;

class ClientboundDataStorePacket extends DataPacket{
public const NETWORK_ID = ProtocolInfo::CLIENTBOUND_DATA_STORE_PACKET;

/**
* @var DataStore[]
* @phpstan-var list<DataStore>
*/
public array $values = [];

/**
* @generate-create-func
* @param DataStore[] $values
* @phpstan-param list<DataStore> $values
*/
public static function create(array $values) : self{
$result = new self;
$result->values = $values;
return $result;
}

protected function decodePayload(ByteBufferReader $in) : void{
$this->values = [];
for($i = 0, $len = VarInt::readUnsignedInt($in); $i < $len; ++$i){
$this->values[] = match(VarInt::readUnsignedInt($in)){
DataStoreType::UPDATE => DataStoreUpdate::read($in),
DataStoreType::CHANGE => DataStoreChange::read($in),
DataStoreType::REMOVAL => DataStoreRemoval::read($in),
default => throw new PacketDecodeException("Unknown DataStore type"),
};
}
}

protected function encodePayload(ByteBufferWriter $out) : void{
VarInt::writeUnsignedInt($out, count($this->values));
foreach($this->values as $value){
VarInt::writeUnsignedInt($out, $value->getTypeId());
$value->write($out);
}
}

public function handle(PacketHandlerInterface $handler) : bool{
return $handler->handleClientboundDataStore($this);
}
}
87 changes: 14 additions & 73 deletions src/ClientboundDebugRendererPacket.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,103 +16,44 @@

use pmmp\encoding\ByteBufferReader;
use pmmp\encoding\ByteBufferWriter;
use pmmp\encoding\LE;
use pocketmine\math\Vector3;
use pocketmine\network\mcpe\protocol\serializer\CommonTypes;
use pocketmine\network\mcpe\protocol\types\DebugMarkerData;

class ClientboundDebugRendererPacket extends DataPacket implements ClientboundPacket{
public const NETWORK_ID = ProtocolInfo::CLIENTBOUND_DEBUG_RENDERER_PACKET;

public const TYPE_CLEAR = 1;
public const TYPE_ADD_CUBE = 2;
public const TYPE_CLEAR = "cleardebugmarkers";
public const TYPE_ADD_CUBE = "adddebugmarkercube";

private int $type;
private string $type;
private ?DebugMarkerData $data = null;

//TODO: if more types are added, we'll probably want to make a separate data type and interfaces
private string $text;
private Vector3 $position;
private float $red;
private float $green;
private float $blue;
private float $alpha;
private int $durationMillis;

private static function base(int $type) : self{
private static function base(string $type) : self{
$result = new self;
$result->type = $type;
return $result;
}

public static function clear() : self{ return self::base(self::TYPE_CLEAR); }

public static function addCube(string $text, Vector3 $position, float $red, float $green, float $blue, float $alpha, int $durationMillis) : self{
public static function addCube(DebugMarkerData $data) : self{
$result = self::base(self::TYPE_ADD_CUBE);
$result->text = $text;
$result->position = $position;
$result->red = $red;
$result->green = $green;
$result->blue = $blue;
$result->alpha = $alpha;
$result->durationMillis = $durationMillis;
$result->data = $data;
return $result;
}

public function getType() : int{ return $this->type; }

public function getText() : string{ return $this->text; }

public function getPosition() : Vector3{ return $this->position; }

public function getRed() : float{ return $this->red; }

public function getGreen() : float{ return $this->green; }
public function getType() : string{ return $this->type; }

public function getBlue() : float{ return $this->blue; }

public function getAlpha() : float{ return $this->alpha; }

public function getDurationMillis() : int{ return $this->durationMillis; }
public function getData() : ?DebugMarkerData{ return $this->data; }

protected function decodePayload(ByteBufferReader $in) : void{
$this->type = LE::readUnsignedInt($in);

switch($this->type){
case self::TYPE_CLEAR:
//NOOP
break;
case self::TYPE_ADD_CUBE:
$this->text = CommonTypes::getString($in);
$this->position = CommonTypes::getVector3($in);
$this->red = LE::readFloat($in);
$this->green = LE::readFloat($in);
$this->blue = LE::readFloat($in);
$this->alpha = LE::readFloat($in);
$this->durationMillis = LE::readUnsignedLong($in);
break;
default:
throw new PacketDecodeException("Unknown type " . $this->type);
}
$this->type = CommonTypes::getString($in);
$this->data = CommonTypes::readOptional($in, DebugMarkerData::read(...));
}

protected function encodePayload(ByteBufferWriter $out) : void{
LE::writeUnsignedInt($out, $this->type);

switch($this->type){
case self::TYPE_CLEAR:
//NOOP
break;
case self::TYPE_ADD_CUBE:
CommonTypes::putString($out, $this->text);
CommonTypes::putVector3($out, $this->position);
LE::writeFloat($out, $this->red);
LE::writeFloat($out, $this->green);
LE::writeFloat($out, $this->blue);
LE::writeFloat($out, $this->alpha);
LE::writeUnsignedLong($out, $this->durationMillis);
break;
default:
throw new \InvalidArgumentException("Unknown type " . $this->type);
}
CommonTypes::putString($out, $this->type);
CommonTypes::writeOptional($out, $this->data, fn(ByteBufferWriter $out, DebugMarkerData $data) => $data->write($out));
}

public function handle(PacketHandlerInterface $handler) : bool{
Expand Down
34 changes: 15 additions & 19 deletions src/CommandOutputPacket.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,10 @@

namespace pocketmine\network\mcpe\protocol;

use pmmp\encoding\Byte;
use pmmp\encoding\ByteBufferReader;
use pmmp\encoding\ByteBufferWriter;
use pmmp\encoding\DataDecodeException;
use pmmp\encoding\LE;
use pmmp\encoding\VarInt;
use pocketmine\network\mcpe\protocol\serializer\CommonTypes;
use pocketmine\network\mcpe\protocol\types\command\CommandOriginData;
Expand All @@ -27,30 +27,28 @@
class CommandOutputPacket extends DataPacket implements ClientboundPacket{
public const NETWORK_ID = ProtocolInfo::COMMAND_OUTPUT_PACKET;

public const TYPE_LAST = 1;
public const TYPE_SILENT = 2;
public const TYPE_ALL = 3;
public const TYPE_DATA_SET = 4;
public const TYPE_LAST = "lastoutput";
public const TYPE_SILENT = "silent";
public const TYPE_ALL = "alloutput";
public const TYPE_DATA_SET = "dataset";

public CommandOriginData $originData;
public int $outputType;
public string $outputType;
public int $successCount;
/** @var CommandOutputMessage[] */
public array $messages = [];
public string $unknownString;
public ?string $data;

protected function decodePayload(ByteBufferReader $in) : void{
$this->originData = CommonTypes::getCommandOriginData($in);
$this->outputType = Byte::readUnsigned($in);
$this->successCount = VarInt::readUnsignedInt($in);
$this->outputType = CommonTypes::getString($in);
$this->successCount = LE::readUnsignedInt($in);

for($i = 0, $size = VarInt::readUnsignedInt($in); $i < $size; ++$i){
$this->messages[] = $this->getCommandMessage($in);
}

if($this->outputType === self::TYPE_DATA_SET){
$this->unknownString = CommonTypes::getString($in);
}
$this->data = CommonTypes::readOptional($in, CommonTypes::getString(...));
}

/**
Expand All @@ -59,8 +57,8 @@ protected function decodePayload(ByteBufferReader $in) : void{
protected function getCommandMessage(ByteBufferReader $in) : CommandOutputMessage{
$message = new CommandOutputMessage();

$message->isInternal = CommonTypes::getBool($in);
$message->messageId = CommonTypes::getString($in);
$message->isInternal = CommonTypes::getBool($in);

for($i = 0, $size = VarInt::readUnsignedInt($in); $i < $size; ++$i){
$message->parameters[] = CommonTypes::getString($in);
Expand All @@ -71,22 +69,20 @@ protected function getCommandMessage(ByteBufferReader $in) : CommandOutputMessag

protected function encodePayload(ByteBufferWriter $out) : void{
CommonTypes::putCommandOriginData($out, $this->originData);
Byte::writeUnsigned($out, $this->outputType);
VarInt::writeUnsignedInt($out, $this->successCount);
CommonTypes::putString($out, $this->outputType);
LE::writeUnsignedInt($out, $this->successCount);

VarInt::writeUnsignedInt($out, count($this->messages));
foreach($this->messages as $message){
$this->putCommandMessage($message, $out);
}

if($this->outputType === self::TYPE_DATA_SET){
CommonTypes::putString($out, $this->unknownString);
}
CommonTypes::writeOptional($out, $this->data, CommonTypes::putString(...));
}

protected function putCommandMessage(CommandOutputMessage $message, ByteBufferWriter $out) : void{
CommonTypes::putBool($out, $message->isInternal);
CommonTypes::putString($out, $message->messageId);
CommonTypes::putBool($out, $message->isInternal);

VarInt::writeUnsignedInt($out, count($message->parameters));
foreach($message->parameters as $parameter){
Expand Down
Loading