Skip to content

Commit

Permalink
Squashed commit of the following:
Browse files Browse the repository at this point in the history
commit 9422c97
Author: IvanCraft623 <[email protected]>
Date:   Tue Aug 13 19:22:20 2024 -0500

    CS fix

commit 3b0e82e
Author: Dries C <[email protected]>
Date:   Wed Aug 14 02:07:07 2024 +0200

    Fix unread byte in InventoryTransactionPacket

commit b6b1ccf
Author: Dries C <[email protected]>
Date:   Tue Aug 13 16:32:28 2024 +0200

    Protocol changes for 1.21.20

commit a871e12
Author: Tobias Grether <[email protected]>
Date:   Tue Aug 13 20:25:54 2024 +0200

    Update field order and fix enum values in CorrectPlayerMovePredictionPacket (pmmp#259)

commit 5db8d10
Author: Dylan K. Taylor <[email protected]>
Date:   Tue Aug 13 17:21:12 2024 +0100

    Make use of new autogen features in PlayerAuthInputPacket

commit a61b555
Author: Dylan K. Taylor <[email protected]>
Date:   Tue Aug 13 17:20:20 2024 +0100

    Allow any static function to have a @generate-create-func tag
    this allows the creation of internal auto-generated constructors that aren't called create(), which is useful if the public create() wants to do extra validation without losing autogen capability.
  • Loading branch information
dries-c committed Aug 14, 2024
1 parent 7739e2d commit 2381d00
Show file tree
Hide file tree
Showing 48 changed files with 739 additions and 129 deletions.
17 changes: 16 additions & 1 deletion src/CameraInstructionPacket.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,22 +19,27 @@
use pocketmine\network\mcpe\protocol\types\CacheableNbt;
use pocketmine\network\mcpe\protocol\types\camera\CameraFadeInstruction;
use pocketmine\network\mcpe\protocol\types\camera\CameraSetInstruction;
use pocketmine\network\mcpe\protocol\types\camera\CameraTargetInstruction;

class CameraInstructionPacket extends DataPacket implements ClientboundPacket{
public const NETWORK_ID = ProtocolInfo::CAMERA_INSTRUCTION_PACKET;

private ?CameraSetInstruction $set;
private ?bool $clear;
private ?CameraFadeInstruction $fade;
private ?CameraTargetInstruction $target;
private ?bool $removeTarget;

/**
* @generate-create-func
*/
public static function create(?CameraSetInstruction $set, ?bool $clear, ?CameraFadeInstruction $fade) : self{
public static function create(?CameraSetInstruction $set, ?bool $clear, ?CameraFadeInstruction $fade, ?CameraTargetInstruction $target, ?bool $removeTarget) : self{
$result = new self;
$result->set = $set;
$result->clear = $clear;
$result->fade = $fade;
$result->target = $target;
$result->removeTarget = $removeTarget;
return $result;
}

Expand All @@ -44,11 +49,17 @@ public function getClear() : ?bool{ return $this->clear; }

public function getFade() : ?CameraFadeInstruction{ return $this->fade; }

public function getTarget() : ?CameraTargetInstruction{ return $this->target; }

protected function decodePayload(PacketSerializer $in) : void{
if($in->getProtocolId() >= ProtocolInfo::PROTOCOL_1_20_30){
$this->set = $in->readOptional(fn() => CameraSetInstruction::read($in));
$this->clear = $in->readOptional($in->getBool(...));
$this->fade = $in->readOptional(fn() => CameraFadeInstruction::read($in));
if($in->getProtocolId() >= ProtocolInfo::PROTOCOL_1_21_20){
$this->target = $in->readOptional(fn() => CameraTargetInstruction::read($in));
$this->removeTarget = $in->readOptional($in->getBool(...));
}
}else{
$this->fromNBT($in->getNbtCompoundRoot());
}
Expand All @@ -69,6 +80,10 @@ protected function encodePayload(PacketSerializer $out) : void{
$out->writeOptional($this->set, fn(CameraSetInstruction $v) => $v->write($out));
$out->writeOptional($this->clear, $out->putBool(...));
$out->writeOptional($this->fade, fn(CameraFadeInstruction $v) => $v->write($out));
if($out->getProtocolId() >= ProtocolInfo::PROTOCOL_1_21_20){
$out->writeOptional($this->target, fn(CameraTargetInstruction $v) => $v->write($out));
$out->writeOptional($this->removeTarget, $out->putBool(...));
}
}else{
$data = new CacheableNbt($this->toNBT());
$out->put($data->getEncodedNbt());
Expand Down
6 changes: 5 additions & 1 deletion src/ChangeDimensionPacket.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,28 +23,32 @@ class ChangeDimensionPacket extends DataPacket implements ClientboundPacket{
public int $dimension;
public Vector3 $position;
public bool $respawn = false;
private ?int $loadingScreenId = null;

/**
* @generate-create-func
*/
public static function create(int $dimension, Vector3 $position, bool $respawn) : self{
public static function create(int $dimension, Vector3 $position, bool $respawn, ?int $loadingScreenId) : self{
$result = new self;
$result->dimension = $dimension;
$result->position = $position;
$result->respawn = $respawn;
$result->loadingScreenId = $loadingScreenId;
return $result;
}

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());
}

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

public function handle(PacketHandlerInterface $handler) : bool{
Expand Down
2 changes: 1 addition & 1 deletion src/ClientboundCloseFormPacket.php
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,6 @@ protected function encodePayload(PacketSerializer $out) : void{
}

public function handle(PacketHandlerInterface $handler) : bool{
return $handler->handleCloseForm($this);
return $handler->handleClientboundCloseForm($this);
}
}
42 changes: 33 additions & 9 deletions src/CorrectPlayerMovePredictionPacket.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,34 +14,45 @@

namespace pocketmine\network\mcpe\protocol;

use pocketmine\math\Vector2;
use pocketmine\math\Vector3;
use pocketmine\network\mcpe\protocol\serializer\PacketSerializer;

class CorrectPlayerMovePredictionPacket extends DataPacket implements ClientboundPacket{
public const NETWORK_ID = ProtocolInfo::CORRECT_PLAYER_MOVE_PREDICTION_PACKET;

public const PREDICTION_TYPE_VEHICLE = 0;
public const PREDICTION_TYPE_PLAYER = 1;
public const PREDICTION_TYPE_PLAYER = 0;
public const PREDICTION_TYPE_VEHICLE = 1;

private Vector3 $position;
private Vector3 $delta;
private bool $onGround;
private int $tick;
private int $predictionType;
private ?Vector2 $vehicleRotation;

/**
* @generate-create-func
*/
public static function create(Vector3 $position, Vector3 $delta, bool $onGround, int $tick, int $predictionType) : self{
private static function internalCreate(Vector3 $position, Vector3 $delta, bool $onGround, int $tick, int $predictionType, ?Vector2 $vehicleRotation) : self{
$result = new self;
$result->position = $position;
$result->delta = $delta;
$result->onGround = $onGround;
$result->tick = $tick;
$result->predictionType = $predictionType;
$result->vehicleRotation = $vehicleRotation;
return $result;
}

public static function create(Vector3 $position, Vector3 $delta, bool $onGround, int $tick, int $predictionType, ?Vector2 $vehicleRotation) : self{
if($predictionType === self::PREDICTION_TYPE_VEHICLE && $vehicleRotation === null){
throw new \LogicException("CorrectPlayerMovePredictionPackets with type VEHICLE require a vehicleRotation to be provided");
}

return self::internalCreate($position, $delta, $onGround, $tick, $predictionType, $vehicleRotation);
}

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

public function getDelta() : Vector3{ return $this->delta; }
Expand All @@ -52,24 +63,37 @@ public function getTick() : int{ return $this->tick; }

public function getPredictionType() : int{ return $this->predictionType; }

public function getVehicleRotation() : ?Vector2{ return $this->vehicleRotation; }

protected function decodePayload(PacketSerializer $in) : void{
if($in->getProtocolId() >= ProtocolInfo::PROTOCOL_1_20_60){
$this->predictionType = $in->getByte();
}
$this->position = $in->getVector3();
$this->delta = $in->getVector3();
if($in->getProtocolId() >= ProtocolInfo::PROTOCOL_1_20_60 && $this->predictionType === self::PREDICTION_TYPE_VEHICLE){
$this->vehicleRotation = new Vector2($in->getFloat(), $in->getFloat());
}
$this->onGround = $in->getBool();
$this->tick = $in->getUnsignedVarLong();
if($in->getProtocolId() >= ProtocolInfo::PROTOCOL_1_20_60){
$this->predictionType = $in->getByte();
}
}

protected function encodePayload(PacketSerializer $out) : void{
if($out->getProtocolId() >= ProtocolInfo::PROTOCOL_1_20_60){
$out->putByte($this->predictionType);
}
$out->putVector3($this->position);
$out->putVector3($this->delta);
if($out->getProtocolId() >= ProtocolInfo::PROTOCOL_1_20_60 && $this->predictionType === self::PREDICTION_TYPE_VEHICLE){
if($this->vehicleRotation === null){ // this should never be the case
throw new \LogicException("CorrectPlayerMovePredictionPackets with type VEHICLE require a vehicleRotation to be provided");
}

$out->putFloat($this->vehicleRotation->getX());
$out->putFloat($this->vehicleRotation->getY());
}
$out->putBool($this->onGround);
$out->putUnsignedVarLong($this->tick);
if($out->getProtocolId() >= ProtocolInfo::PROTOCOL_1_20_60){
$out->putByte($this->predictionType);
}
}

public function handle(PacketHandlerInterface $handler) : bool{
Expand Down
44 changes: 44 additions & 0 deletions src/CurrentStructureFeaturePacket.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
<?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 pocketmine\network\mcpe\protocol\serializer\PacketSerializer;

class CurrentStructureFeaturePacket extends DataPacket implements ClientboundPacket{
public const NETWORK_ID = ProtocolInfo::CURRENT_STRUCTURE_FEATURE_PACKET;

public string $currentStructureFeature;

/**
* @generate-create-func
*/
public static function create(string $currentStructureFeature) : self{
$result = new self;
$result->currentStructureFeature = $currentStructureFeature;
return $result;
}

protected function decodePayload(PacketSerializer $in) : void{
$this->currentStructureFeature = $in->getString();
}

protected function encodePayload(PacketSerializer $out) : void{
$out->putString($this->currentStructureFeature);
}

public function handle(PacketHandlerInterface $handler) : bool{
return $handler->handleCurrentStructureFeature($this);
}
}
20 changes: 14 additions & 6 deletions src/DisconnectPacket.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,14 +21,16 @@ class DisconnectPacket extends DataPacket implements ClientboundPacket, Serverbo

public int $reason; //TODO: add constants / enum
public ?string $message;
public ?string $filteredMessage;

/**
* @generate-create-func
*/
public static function create(int $reason, ?string $message) : self{
public static function create(int $reason, ?string $message, ?string $filteredMessage) : self{
$result = new self;
$result->reason = $reason;
$result->message = $message;
$result->filteredMessage = $filteredMessage;
return $result;
}

Expand All @@ -40,17 +42,23 @@ protected function decodePayload(PacketSerializer $in) : void{
if($in->getProtocolId() >= ProtocolInfo::PROTOCOL_1_20_40){
$this->reason = $in->getVarInt();
}
$hideDisconnectionScreen = $in->getBool();
$this->message = $hideDisconnectionScreen ? null : $in->getString();
$skipMessage = $in->getBool();
$this->message = $skipMessage ? null : $in->getString();
if($in->getProtocolId() >= ProtocolInfo::PROTOCOL_1_21_20){
$this->filteredMessage = $skipMessage ? null : $in->getString();
}
}

protected function encodePayload(PacketSerializer $out) : void{
if($out->getProtocolId() >= ProtocolInfo::PROTOCOL_1_20_40){
$out->putVarInt($this->reason);
}
$out->putBool($this->message === null);
if($this->message !== null){
$out->putString($this->message);
$out->putBool($skipMessage = $this->message === null && $this->filteredMessage === null);
if(!$skipMessage){
$out->putString($this->message ?? "");
if($out->getProtocolId() >= ProtocolInfo::PROTOCOL_1_21_20){
$out->putString($this->filteredMessage ?? "");
}
}
}

Expand Down
8 changes: 7 additions & 1 deletion src/EditorNetworkPacket.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,27 +23,33 @@
class EditorNetworkPacket extends DataPacket implements ClientboundPacket, ServerboundPacket{
public const NETWORK_ID = ProtocolInfo::EDITOR_NETWORK_PACKET;

private bool $isRouteToManager;
/** @phpstan-var CacheableNbt<\pocketmine\nbt\tag\CompoundTag> */
private CacheableNbt $payload;

/**
* @generate-create-func
* @phpstan-param CacheableNbt<\pocketmine\nbt\tag\CompoundTag> $payload
*/
public static function create(CacheableNbt $payload) : self{
public static function create(bool $isRouteToManager, CacheableNbt $payload) : self{
$result = new self;
$result->isRouteToManager = $isRouteToManager;
$result->payload = $payload;
return $result;
}

/** @phpstan-return CacheableNbt<\pocketmine\nbt\tag\CompoundTag> */
public function getPayload() : CacheableNbt{ return $this->payload; }

public function isRouteToManager() : bool{ return $this->isRouteToManager; }

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

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

Expand Down
6 changes: 5 additions & 1 deletion src/InventoryContentPacket.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,15 +24,17 @@ class InventoryContentPacket extends DataPacket implements ClientboundPacket{
public int $windowId;
/** @var ItemStackWrapper[] */
public array $items = [];
public int $dynamicContainerId;

/**
* @generate-create-func
* @param ItemStackWrapper[] $items
*/
public static function create(int $windowId, array $items) : self{
public static function create(int $windowId, array $items, int $dynamicContainerId) : self{
$result = new self;
$result->windowId = $windowId;
$result->items = $items;
$result->dynamicContainerId = $dynamicContainerId;
return $result;
}

Expand All @@ -42,6 +44,7 @@ protected function decodePayload(PacketSerializer $in) : void{
for($i = 0; $i < $count; ++$i){
$this->items[] = $in->getItemStackWrapper();
}
$this->dynamicContainerId = $in->getUnsignedVarInt();
}

protected function encodePayload(PacketSerializer $out) : void{
Expand All @@ -50,6 +53,7 @@ protected function encodePayload(PacketSerializer $out) : void{
foreach($this->items as $item){
$out->putItemStackWrapper($item);
}
$out->putUnsignedVarInt($this->dynamicContainerId);
}

public function handle(PacketHandlerInterface $handler) : bool{
Expand Down
6 changes: 5 additions & 1 deletion src/InventorySlotPacket.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,27 +23,31 @@ class InventorySlotPacket extends DataPacket implements ClientboundPacket{
public int $windowId;
public int $inventorySlot;
public ItemStackWrapper $item;
public int $dynamicContainerId;

/**
* @generate-create-func
*/
public static function create(int $windowId, int $inventorySlot, ItemStackWrapper $item) : self{
public static function create(int $windowId, int $inventorySlot, ItemStackWrapper $item, int $dynamicContainerId) : self{
$result = new self;
$result->windowId = $windowId;
$result->inventorySlot = $inventorySlot;
$result->item = $item;
$result->dynamicContainerId = $dynamicContainerId;
return $result;
}

protected function decodePayload(PacketSerializer $in) : void{
$this->windowId = $in->getUnsignedVarInt();
$this->inventorySlot = $in->getUnsignedVarInt();
$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);
$out->putItemStackWrapper($this->item);
}

Expand Down
Loading

0 comments on commit 2381d00

Please sign in to comment.