From bb5a3ae02435251b094791231d75f1dbea7c462b Mon Sep 17 00:00:00 2001 From: Dries C Date: Thu, 13 Jun 2024 20:19:54 +0200 Subject: [PATCH] Add v1.20.80 BC support --- src/CodeBuilderSourcePacket.php | 18 ++++++-- src/ContainerClosePacket.php | 8 +++- src/PacketHandlerInterface.php | 2 + src/PacketPool.php | 1 + src/ProtocolInfo.php | 2 +- src/TextPacket.php | 8 +++- src/TickSyncPacket.php | 64 ++++++++++++++++++++++++++++ src/types/LevelSettings.php | 16 ++++--- src/types/recipe/ShapelessRecipe.php | 11 +++-- 9 files changed, 113 insertions(+), 17 deletions(-) create mode 100644 src/TickSyncPacket.php diff --git a/src/CodeBuilderSourcePacket.php b/src/CodeBuilderSourcePacket.php index 3d408b26..f213235f 100644 --- a/src/CodeBuilderSourcePacket.php +++ b/src/CodeBuilderSourcePacket.php @@ -21,15 +21,17 @@ class CodeBuilderSourcePacket extends DataPacket implements ServerboundPacket{ private int $operation; private int $category; + private string $value; private int $codeStatus; /** * @generate-create-func */ - public static function create(int $operation, int $category, int $codeStatus) : self{ + public static function create(int $operation, int $category, string $value, int $codeStatus) : self{ $result = new self; $result->operation = $operation; $result->category = $category; + $result->value = $value; $result->codeStatus = $codeStatus; return $result; } @@ -38,18 +40,28 @@ public function getOperation() : int{ return $this->operation; } public function getCategory() : int{ return $this->category; } + public function getValue() : string{ return $this->value; } + public function getCodeStatus() : int{ return $this->codeStatus; } protected function decodePayload(PacketSerializer $in) : void{ $this->operation = $in->getByte(); $this->category = $in->getByte(); - $this->codeStatus = $in->getByte(); + if($in->getProtocolId() >= ProtocolInfo::PROTOCOL_1_21_0){ + $this->codeStatus = $in->getByte(); + }else{ + $this->value = $in->getString(); + } } protected function encodePayload(PacketSerializer $out) : void{ $out->putByte($this->operation); $out->putByte($this->category); - $out->putByte($this->codeStatus); + if($out->getProtocolId() >= ProtocolInfo::PROTOCOL_1_21_0){ + $out->putByte($this->codeStatus); + }else{ + $out->putString($this->value); + } } public function handle(PacketHandlerInterface $handler) : bool{ diff --git a/src/ContainerClosePacket.php b/src/ContainerClosePacket.php index 36fdd1a5..ff5a7ccb 100644 --- a/src/ContainerClosePacket.php +++ b/src/ContainerClosePacket.php @@ -36,13 +36,17 @@ public static function create(int $windowId, int $windowType, bool $server) : se protected function decodePayload(PacketSerializer $in) : void{ $this->windowId = $in->getByte(); - $this->windowType = $in->getByte(); + if($in->getProtocolId() >= ProtocolInfo::PROTOCOL_1_21_0){ + $this->windowType = $in->getByte(); + } $this->server = $in->getBool(); } protected function encodePayload(PacketSerializer $out) : void{ $out->putByte($this->windowId); - $out->putByte($this->windowType); + if($out->getProtocolId() >= ProtocolInfo::PROTOCOL_1_21_0){ + $out->putByte($this->windowType); + } $out->putBool($this->server); } diff --git a/src/PacketHandlerInterface.php b/src/PacketHandlerInterface.php index 90c5a38f..29996c8e 100644 --- a/src/PacketHandlerInterface.php +++ b/src/PacketHandlerInterface.php @@ -62,6 +62,8 @@ public function handleUpdateBlock(UpdateBlockPacket $packet) : bool; public function handleAddPainting(AddPaintingPacket $packet) : bool; + public function handleTickSync(TickSyncPacket $packet) : bool; + public function handleLevelSoundEventPacketV1(LevelSoundEventPacketV1 $packet) : bool; public function handleLevelEvent(LevelEventPacket $packet) : bool; diff --git a/src/PacketPool.php b/src/PacketPool.php index 16800a2f..47321d23 100644 --- a/src/PacketPool.php +++ b/src/PacketPool.php @@ -55,6 +55,7 @@ public function __construct(){ $this->registerPacket(new PassengerJumpPacket()); $this->registerPacket(new UpdateBlockPacket()); $this->registerPacket(new AddPaintingPacket()); + $this->registerPacket(new TickSyncPacket()); $this->registerPacket(new LevelSoundEventPacketV1()); $this->registerPacket(new LevelEventPacket()); $this->registerPacket(new BlockEventPacket()); diff --git a/src/ProtocolInfo.php b/src/ProtocolInfo.php index b3d7d469..bd088d81 100644 --- a/src/ProtocolInfo.php +++ b/src/ProtocolInfo.php @@ -82,7 +82,7 @@ private function __construct(){ public const PASSENGER_JUMP_PACKET = 0x14; public const UPDATE_BLOCK_PACKET = 0x15; public const ADD_PAINTING_PACKET = 0x16; - + public const TICK_SYNC_PACKET = 0x17; public const LEVEL_SOUND_EVENT_PACKET_V1 = 0x18; public const LEVEL_EVENT_PACKET = 0x19; public const BLOCK_EVENT_PACKET = 0x1a; diff --git a/src/TextPacket.php b/src/TextPacket.php index ceae96ca..8f831974 100644 --- a/src/TextPacket.php +++ b/src/TextPacket.php @@ -126,7 +126,9 @@ protected function decodePayload(PacketSerializer $in) : void{ $this->xboxUserId = $in->getString(); $this->platformChatId = $in->getString(); - $this->filteredMessage = $in->getString(); + if($in->getProtocolId() >= ProtocolInfo::PROTOCOL_1_21_0){ + $this->filteredMessage = $in->getString(); + } } protected function encodePayload(PacketSerializer $out) : void{ @@ -160,7 +162,9 @@ protected function encodePayload(PacketSerializer $out) : void{ $out->putString($this->xboxUserId); $out->putString($this->platformChatId); - $out->putString($this->filteredMessage); + if($out->getProtocolId() >= ProtocolInfo::PROTOCOL_1_21_0){ + $out->putString($this->filteredMessage); + } } public function handle(PacketHandlerInterface $handler) : bool{ diff --git a/src/TickSyncPacket.php b/src/TickSyncPacket.php new file mode 100644 index 00000000..cb6ba987 --- /dev/null +++ b/src/TickSyncPacket.php @@ -0,0 +1,64 @@ + + * + * 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 pocketmine\network\mcpe\protocol\serializer\PacketSerializer; + +class TickSyncPacket extends DataPacket implements ClientboundPacket, ServerboundPacket{ + public const NETWORK_ID = ProtocolInfo::TICK_SYNC_PACKET; + + private int $clientSendTime; + private int $serverReceiveTime; + + /** + * @generate-create-func + */ + private static function create(int $clientSendTime, int $serverReceiveTime) : self{ + $result = new self; + $result->clientSendTime = $clientSendTime; + $result->serverReceiveTime = $serverReceiveTime; + return $result; + } + + public static function request(int $clientTime) : self{ + return self::create($clientTime, 0 /* useless, but always written anyway */); + } + + public static function response(int $clientSendTime, int $serverReceiveTime) : self{ + return self::create($clientSendTime, $serverReceiveTime); + } + + public function getClientSendTime() : int{ + return $this->clientSendTime; + } + + public function getServerReceiveTime() : int{ + return $this->serverReceiveTime; + } + + protected function decodePayload(PacketSerializer $in) : void{ + $this->clientSendTime = $in->getLLong(); + $this->serverReceiveTime = $in->getLLong(); + } + + protected function encodePayload(PacketSerializer $out) : void{ + $out->putLLong($this->clientSendTime); + $out->putLLong($this->serverReceiveTime); + } + + public function handle(PacketHandlerInterface $handler) : bool{ + return $handler->handleTickSync($this); + } +} diff --git a/src/types/LevelSettings.php b/src/types/LevelSettings.php index 4afd72ba..d3e5f67c 100644 --- a/src/types/LevelSettings.php +++ b/src/types/LevelSettings.php @@ -147,9 +147,11 @@ private function internalRead(PacketSerializer $in) : void{ $this->experimentalGameplayOverride = $in->readOptional($in->getBool(...)); $this->chatRestrictionLevel = $in->getByte(); $this->disablePlayerInteractions = $in->getBool(); - $this->serverIdentifier = $in->getString(); - $this->worldIdentifier = $in->getString(); - $this->scenarioIdentifier = $in->getString(); + if($in->getProtocolId() >= ProtocolInfo::PROTOCOL_1_21_0){ + $this->serverIdentifier = $in->getString(); + $this->worldIdentifier = $in->getString(); + $this->scenarioIdentifier = $in->getString(); + } } public function write(PacketSerializer $out) : void{ @@ -207,8 +209,10 @@ public function write(PacketSerializer $out) : void{ $out->writeOptional($this->experimentalGameplayOverride, $out->putBool(...)); $out->putByte($this->chatRestrictionLevel); $out->putBool($this->disablePlayerInteractions); - $out->putString($this->serverIdentifier); - $out->putString($this->worldIdentifier); - $out->putString($this->scenarioIdentifier); + if($out->getProtocolId() >= ProtocolInfo::PROTOCOL_1_21_0){ + $out->putString($this->serverIdentifier); + $out->putString($this->worldIdentifier); + $out->putString($this->scenarioIdentifier); + } } } diff --git a/src/types/recipe/ShapelessRecipe.php b/src/types/recipe/ShapelessRecipe.php index 92ec411f..ddb50138 100644 --- a/src/types/recipe/ShapelessRecipe.php +++ b/src/types/recipe/ShapelessRecipe.php @@ -14,6 +14,7 @@ namespace pocketmine\network\mcpe\protocol\types\recipe; +use pocketmine\network\mcpe\protocol\ProtocolInfo; use pocketmine\network\mcpe\protocol\serializer\PacketSerializer; use pocketmine\network\mcpe\protocol\types\inventory\ItemStack; use Ramsey\Uuid\UuidInterface; @@ -87,11 +88,13 @@ public static function decode(int $recipeType, PacketSerializer $in) : self{ $uuid = $in->getUUID(); $block = $in->getString(); $priority = $in->getVarInt(); - $unlockingRequirement = RecipeUnlockingRequirement::read($in); + if($in->getProtocolId() >= ProtocolInfo::PROTOCOL_1_21_0){ + $unlockingRequirement = RecipeUnlockingRequirement::read($in); + } $recipeNetId = $in->readRecipeNetId(); - return new self($recipeType, $recipeId, $input, $output, $uuid, $block, $priority, $unlockingRequirement, $recipeNetId); + return new self($recipeType, $recipeId, $input, $output, $uuid, $block, $priority, $unlockingRequirement ?? new RecipeUnlockingRequirement(null), $recipeNetId); } public function encode(PacketSerializer $out) : void{ @@ -109,7 +112,9 @@ public function encode(PacketSerializer $out) : void{ $out->putUUID($this->uuid); $out->putString($this->blockName); $out->putVarInt($this->priority); - $this->unlockingRequirement->write($out); + if($out->getProtocolId() >= ProtocolInfo::PROTOCOL_1_21_0){ + $this->unlockingRequirement->write($out); + } $out->writeRecipeNetId($this->recipeNetId); }