Skip to content

Commit

Permalink
Add 1.21.2 BC support
Browse files Browse the repository at this point in the history
  • Loading branch information
dries-c committed Aug 14, 2024
1 parent 2381d00 commit 618ba32
Show file tree
Hide file tree
Showing 22 changed files with 185 additions and 46 deletions.
8 changes: 6 additions & 2 deletions src/ChangeDimensionPacket.php
Original file line number Diff line number Diff line change
Expand Up @@ -41,14 +41,18 @@ protected function decodePayload(PacketSerializer $in) : void{
$this->dimension = $in->getVarInt();
$this->position = $in->getVector3();
$this->respawn = $in->getBool();
$this->loadingScreenId = $in->readOptional(fn() => $in->getLInt());
if($in->getProtocolId() >= ProtocolInfo::PROTOCOL_1_21_20){
$this->loadingScreenId = $in->readOptional(fn() => $in->getLInt());
}
}

protected function encodePayload(PacketSerializer $out) : void{
$out->putVarInt($this->dimension);
$out->putVector3($this->position);
$out->putBool($this->respawn);
$out->writeOptional($this->loadingScreenId, $out->putLInt(...));
if($out->getProtocolId() >= ProtocolInfo::PROTOCOL_1_21_20){
$out->writeOptional($this->loadingScreenId, $out->putLInt(...));
}
}

public function handle(PacketHandlerInterface $handler) : bool{
Expand Down
8 changes: 6 additions & 2 deletions src/EditorNetworkPacket.php
Original file line number Diff line number Diff line change
Expand Up @@ -44,12 +44,16 @@ public function getPayload() : CacheableNbt{ return $this->payload; }
public function isRouteToManager() : bool{ return $this->isRouteToManager; }

protected function decodePayload(PacketSerializer $in) : void{
$this->isRouteToManager = $in->getBool();
if($in->getProtocolId() >= ProtocolInfo::PROTOCOL_1_21_20){
$this->isRouteToManager = $in->getBool();
}
$this->payload = new CacheableNbt($in->getNbtCompoundRoot());
}

protected function encodePayload(PacketSerializer $out) : void{
$out->putBool($this->isRouteToManager);
if($out->getProtocolId() >= ProtocolInfo::PROTOCOL_1_21_20){
$out->putBool($this->isRouteToManager);
}
$out->put($this->payload->getEncodedNbt());
}

Expand Down
8 changes: 6 additions & 2 deletions src/InventoryContentPacket.php
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,9 @@ protected function decodePayload(PacketSerializer $in) : void{
for($i = 0; $i < $count; ++$i){
$this->items[] = $in->getItemStackWrapper();
}
$this->dynamicContainerId = $in->getUnsignedVarInt();
if($in->getProtocolId() >= ProtocolInfo::PROTOCOL_1_21_20){
$this->dynamicContainerId = $in->getUnsignedVarInt();
}
}

protected function encodePayload(PacketSerializer $out) : void{
Expand All @@ -53,7 +55,9 @@ protected function encodePayload(PacketSerializer $out) : void{
foreach($this->items as $item){
$out->putItemStackWrapper($item);
}
$out->putUnsignedVarInt($this->dynamicContainerId);
if($out->getProtocolId() >= ProtocolInfo::PROTOCOL_1_21_20){
$out->putUnsignedVarInt($this->dynamicContainerId);
}
}

public function handle(PacketHandlerInterface $handler) : bool{
Expand Down
8 changes: 6 additions & 2 deletions src/InventorySlotPacket.php
Original file line number Diff line number Diff line change
Expand Up @@ -40,14 +40,18 @@ public static function create(int $windowId, int $inventorySlot, ItemStackWrappe
protected function decodePayload(PacketSerializer $in) : void{
$this->windowId = $in->getUnsignedVarInt();
$this->inventorySlot = $in->getUnsignedVarInt();
$this->dynamicContainerId = $in->getUnsignedVarInt();
if($in->getProtocolId() >= ProtocolInfo::PROTOCOL_1_21_20){
$this->dynamicContainerId = $in->getUnsignedVarInt();
}
$this->item = $in->getItemStackWrapper();
}

protected function encodePayload(PacketSerializer $out) : void{
$out->putUnsignedVarInt($this->windowId);
$out->putUnsignedVarInt($this->inventorySlot);
$out->putUnsignedVarInt($this->dynamicContainerId);
if($out->getProtocolId() >= ProtocolInfo::PROTOCOL_1_21_20){
$out->putUnsignedVarInt($this->dynamicContainerId);
}
$out->putItemStackWrapper($this->item);
}

Expand Down
8 changes: 6 additions & 2 deletions src/MobArmorEquipmentPacket.php
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,9 @@ protected function decodePayload(PacketSerializer $in) : void{
$this->chest = $in->getItemStackWrapper();
$this->legs = $in->getItemStackWrapper();
$this->feet = $in->getItemStackWrapper();
$this->body = $in->getItemStackWrapper();
if($in->getProtocolId() >= ProtocolInfo::PROTOCOL_1_21_20){
$this->body = $in->getItemStackWrapper();
}
}

protected function encodePayload(PacketSerializer $out) : void{
Expand All @@ -58,7 +60,9 @@ protected function encodePayload(PacketSerializer $out) : void{
$out->putItemStackWrapper($this->chest);
$out->putItemStackWrapper($this->legs);
$out->putItemStackWrapper($this->feet);
$out->putItemStackWrapper($this->body);
if($out->getProtocolId() >= ProtocolInfo::PROTOCOL_1_21_20){
$out->putItemStackWrapper($this->body);
}
}

public function handle(PacketHandlerInterface $handler) : bool{
Expand Down
10 changes: 7 additions & 3 deletions src/PlayerArmorDamagePacket.php
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,9 @@ protected function decodePayload(PacketSerializer $in) : void{
$this->chestSlotDamage = $this->maybeReadDamage($flags, self::FLAG_CHEST, $in);
$this->legsSlotDamage = $this->maybeReadDamage($flags, self::FLAG_LEGS, $in);
$this->feetSlotDamage = $this->maybeReadDamage($flags, self::FLAG_FEET, $in);
$this->bodySlotDamage = $this->maybeReadDamage($flags, self::FLAG_BODY, $in);
if($in->getProtocolId() >= ProtocolInfo::PROTOCOL_1_21_20){
$this->bodySlotDamage = $this->maybeReadDamage($flags, self::FLAG_BODY, $in);
}
}

private function composeFlag(?int $field, int $flag) : int{
Expand All @@ -87,14 +89,16 @@ protected function encodePayload(PacketSerializer $out) : void{
$this->composeFlag($this->chestSlotDamage, self::FLAG_CHEST) |
$this->composeFlag($this->legsSlotDamage, self::FLAG_LEGS) |
$this->composeFlag($this->feetSlotDamage, self::FLAG_FEET) |
$this->composeFlag($this->bodySlotDamage, self::FLAG_BODY)
($out->getProtocolId() >= ProtocolInfo::PROTOCOL_1_21_20 ? $this->composeFlag($this->bodySlotDamage, self::FLAG_BODY) : 0)
);

$this->maybeWriteDamage($this->headSlotDamage, $out);
$this->maybeWriteDamage($this->chestSlotDamage, $out);
$this->maybeWriteDamage($this->legsSlotDamage, $out);
$this->maybeWriteDamage($this->feetSlotDamage, $out);
$this->maybeWriteDamage($this->bodySlotDamage, $out);
if($out->getProtocolId() >= ProtocolInfo::PROTOCOL_1_21_20){
$this->maybeWriteDamage($this->bodySlotDamage, $out);
}
}

public function handle(PacketHandlerInterface $handler) : bool{
Expand Down
8 changes: 6 additions & 2 deletions src/SetTitlePacket.php
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,9 @@ protected function decodePayload(PacketSerializer $in) : void{
$this->fadeOutTime = $in->getVarInt();
$this->xuid = $in->getString();
$this->platformOnlineId = $in->getString();
$this->filteredTitleText = $in->getString();
if($in->getProtocolId() >= ProtocolInfo::PROTOCOL_1_21_20){
$this->filteredTitleText = $in->getString();
}
}

protected function encodePayload(PacketSerializer $out) : void{
Expand All @@ -82,7 +84,9 @@ protected function encodePayload(PacketSerializer $out) : void{
$out->putVarInt($this->fadeOutTime);
$out->putString($this->xuid);
$out->putString($this->platformOnlineId);
$out->putString($this->filteredTitleText);
if($out->getProtocolId() >= ProtocolInfo::PROTOCOL_1_21_20){
$out->putString($this->filteredTitleText);
}
}

public function handle(PacketHandlerInterface $handler) : bool{
Expand Down
8 changes: 6 additions & 2 deletions src/StopSoundPacket.php
Original file line number Diff line number Diff line change
Expand Up @@ -37,13 +37,17 @@ public static function create(string $soundName, bool $stopAll, bool $stopLegacy
protected function decodePayload(PacketSerializer $in) : void{
$this->soundName = $in->getString();
$this->stopAll = $in->getBool();
$this->stopLegacyMusic = $in->getBool();
if($in->getProtocolId() >= ProtocolInfo::PROTOCOL_1_21_20){
$this->stopLegacyMusic = $in->getBool();
}
}

protected function encodePayload(PacketSerializer $out) : void{
$out->putString($this->soundName);
$out->putBool($this->stopAll);
$out->putBool($this->stopLegacyMusic);
if($out->getProtocolId() >= ProtocolInfo::PROTOCOL_1_21_20){
$out->putBool($this->stopLegacyMusic);
}
}

public function handle(PacketHandlerInterface $handler) : bool{
Expand Down
11 changes: 8 additions & 3 deletions src/serializer/PacketSerializer.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
use pocketmine\nbt\tag\CompoundTag;
use pocketmine\nbt\TreeRoot;
use pocketmine\network\mcpe\protocol\PacketDecodeException;
use pocketmine\network\mcpe\protocol\ProtocolInfo;
use pocketmine\network\mcpe\protocol\types\BlockPosition;
use pocketmine\network\mcpe\protocol\types\BoolGameRule;
use pocketmine\network\mcpe\protocol\types\command\CommandOriginData;
Expand Down Expand Up @@ -648,8 +649,10 @@ public function getEntityLink() : EntityLink{
$type = $this->getByte();
$immediate = $this->getBool();
$causedByRider = $this->getBool();
$vehicleAngularVelocity = $this->getLFloat();
return new EntityLink($fromActorUniqueId, $toActorUniqueId, $type, $immediate, $causedByRider, $vehicleAngularVelocity);
if($this->getProtocolId() >= ProtocolInfo::PROTOCOL_1_21_20){
$vehicleAngularVelocity = $this->getLFloat();
}
return new EntityLink($fromActorUniqueId, $toActorUniqueId, $type, $immediate, $causedByRider, $vehicleAngularVelocity ?? 0.0);
}

public function putEntityLink(EntityLink $link) : void{
Expand All @@ -658,7 +661,9 @@ public function putEntityLink(EntityLink $link) : void{
$this->putByte($link->type);
$this->putBool($link->immediate);
$this->putBool($link->causedByRider);
$this->putLFloat($link->vehicleAngularVelocity);
if($this->getProtocolId() >= ProtocolInfo::PROTOCOL_1_21_20){
$this->putLFloat($link->vehicleAngularVelocity);
}
}

/**
Expand Down
2 changes: 1 addition & 1 deletion src/types/camera/CameraPreset.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,9 @@

namespace pocketmine\network\mcpe\protocol\types\camera;

use pocketmine\math\Vector2;
use pocketmine\nbt\tag\CompoundTag;
use pocketmine\network\mcpe\protocol\ProtocolInfo;
use pocketmine\math\Vector2;
use pocketmine\network\mcpe\protocol\serializer\PacketSerializer;

final class CameraPreset{
Expand Down
11 changes: 8 additions & 3 deletions src/types/inventory/FullContainerName.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@

namespace pocketmine\network\mcpe\protocol\types\inventory;

use pocketmine\network\mcpe\protocol\ProtocolInfo;
use pocketmine\network\mcpe\protocol\serializer\PacketSerializer;

final class FullContainerName{
Expand All @@ -28,12 +29,16 @@ public function getDynamicId() : int{ return $this->dynamicId; }

public static function read(PacketSerializer $in) : self{
$containerId = $in->getByte();
$dynamicId = $in->getLInt();
return new self($containerId, $dynamicId);
if($in->getProtocolId() >= ProtocolInfo::PROTOCOL_1_21_20){
$dynamicId = $in->getLInt();
}
return new self($containerId, $dynamicId ?? 0);
}

public function write(PacketSerializer $out) : void{
$out->putByte($this->containerId);
$out->putLInt($this->dynamicId);
if($out->getProtocolId() >= ProtocolInfo::PROTOCOL_1_21_20){
$out->putLInt($this->dynamicId);
}
}
}
17 changes: 13 additions & 4 deletions src/types/inventory/UseItemTransactionData.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@

use pocketmine\math\Vector3;
use pocketmine\network\mcpe\protocol\InventoryTransactionPacket;
use pocketmine\network\mcpe\protocol\ProtocolInfo;
use pocketmine\network\mcpe\protocol\serializer\PacketSerializer;
use pocketmine\network\mcpe\protocol\types\BlockPosition;
use pocketmine\network\mcpe\protocol\types\GetTypeIdFromConstTrait;
Expand Down Expand Up @@ -78,28 +79,36 @@ public function getClientInteractPrediction() : PredictedResult{ return $this->c

protected function decodeData(PacketSerializer $stream) : void{
$this->actionType = $stream->getUnsignedVarInt();
$this->triggerType = TriggerType::fromPacket($stream->getUnsignedVarInt());
if($stream->getProtocolId() >= ProtocolInfo::PROTOCOL_1_21_20){
$this->triggerType = TriggerType::fromPacket($stream->getUnsignedVarInt());
}
$this->blockPosition = $stream->getBlockPosition();
$this->face = $stream->getVarInt();
$this->hotbarSlot = $stream->getVarInt();
$this->itemInHand = $stream->getItemStackWrapper();
$this->playerPosition = $stream->getVector3();
$this->clickPosition = $stream->getVector3();
$this->blockRuntimeId = $stream->getUnsignedVarInt();
$this->clientInteractPrediction = PredictedResult::fromPacket($stream->getUnsignedVarInt());
if($stream->getProtocolId() >= ProtocolInfo::PROTOCOL_1_21_20){
$this->clientInteractPrediction = PredictedResult::fromPacket($stream->getUnsignedVarInt());
}
}

protected function encodeData(PacketSerializer $stream) : void{
$stream->putUnsignedVarInt($this->actionType);
$stream->putUnsignedVarInt($this->triggerType->value);
if($stream->getProtocolId() >= ProtocolInfo::PROTOCOL_1_21_20){
$stream->putUnsignedVarInt($this->triggerType->value);
}
$stream->putBlockPosition($this->blockPosition);
$stream->putVarInt($this->face);
$stream->putVarInt($this->hotbarSlot);
$stream->putItemStackWrapper($this->itemInHand);
$stream->putVector3($this->playerPosition);
$stream->putVector3($this->clickPosition);
$stream->putUnsignedVarInt($this->blockRuntimeId);
$stream->putUnsignedVarInt($this->clientInteractPrediction->value);
if($stream->getProtocolId() >= ProtocolInfo::PROTOCOL_1_21_20){
$stream->putUnsignedVarInt($this->clientInteractPrediction->value);
}
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@

namespace pocketmine\network\mcpe\protocol\types\inventory\stackrequest;

use pocketmine\network\mcpe\protocol\ProtocolInfo;
use pocketmine\network\mcpe\protocol\serializer\PacketSerializer;
use pocketmine\network\mcpe\protocol\types\GetTypeIdFromConstTrait;
use pocketmine\network\mcpe\protocol\types\recipe\RecipeIngredient;
Expand Down Expand Up @@ -53,18 +54,22 @@ public function getIngredients() : array{ return $this->ingredients; }

public static function read(PacketSerializer $in) : self{
$recipeId = $in->readRecipeNetId();
$repetitions2 = $in->getByte();
if($in->getProtocolId() >= ProtocolInfo::PROTOCOL_1_21_20){
$repetitions2 = $in->getByte();
}
$repetitions = $in->getByte();
$ingredients = [];
for($i = 0, $count = $in->getByte(); $i < $count; ++$i){
$ingredients[] = $in->getRecipeIngredient();
}
return new self($recipeId, $repetitions2, $repetitions, $ingredients);
return new self($recipeId, $repetitions2 ?? 0, $repetitions, $ingredients);
}

public function write(PacketSerializer $out) : void{
$out->writeRecipeNetId($this->recipeId);
$out->putByte($this->repetitions2);
if($out->getProtocolId() >= ProtocolInfo::PROTOCOL_1_21_20){
$out->putByte($this->repetitions2);
}
$out->putByte($this->repetitions);
$out->putByte(count($this->ingredients));
foreach($this->ingredients as $ingredient){
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@

namespace pocketmine\network\mcpe\protocol\types\inventory\stackrequest;

use pocketmine\network\mcpe\protocol\ProtocolInfo;
use pocketmine\network\mcpe\protocol\serializer\PacketSerializer;
use pocketmine\network\mcpe\protocol\types\GetTypeIdFromConstTrait;

Expand All @@ -36,12 +37,16 @@ public function getRepetitions() : int { return $this->repetitions; }

public static function read(PacketSerializer $in) : self{
$recipeId = $in->readRecipeNetId();
$repetitions = $in->getByte();
return new self($recipeId, $repetitions);
if($in->getProtocolId() >= ProtocolInfo::PROTOCOL_1_21_20){
$repetitions = $in->getByte();
}
return new self($recipeId, $repetitions ?? 0);
}

public function write(PacketSerializer $out) : void{
$out->writeRecipeNetId($this->recipeId);
$out->putByte($this->repetitions);
if($out->getProtocolId() >= ProtocolInfo::PROTOCOL_1_21_20){
$out->putByte($this->repetitions);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@

namespace pocketmine\network\mcpe\protocol\types\inventory\stackrequest;

use pocketmine\network\mcpe\protocol\ProtocolInfo;
use pocketmine\network\mcpe\protocol\serializer\PacketSerializer;
use pocketmine\network\mcpe\protocol\types\GetTypeIdFromConstTrait;

Expand All @@ -36,12 +37,16 @@ public function getRepetitions() : int { return $this->repetitions; }

public static function read(PacketSerializer $in) : self{
$creativeItemId = $in->readCreativeItemNetId();
$repetitions = $in->getByte();
return new self($creativeItemId, $repetitions);
if($in->getProtocolId() >= ProtocolInfo::PROTOCOL_1_21_20){
$repetitions = $in->getByte();
}
return new self($creativeItemId, $repetitions ?? 0);
}

public function write(PacketSerializer $out) : void{
$out->writeCreativeItemNetId($this->creativeItemId);
$out->putByte($this->repetitions);
if($out->getProtocolId() >= ProtocolInfo::PROTOCOL_1_21_20){
$out->putByte($this->repetitions);
}
}
}
Loading

0 comments on commit 618ba32

Please sign in to comment.