From 43f4e8223df9b4b8236fd30b79895fd8a9a335c8 Mon Sep 17 00:00:00 2001 From: AkmalFairuz Date: Sat, 21 Sep 2024 20:28:20 +0700 Subject: [PATCH] Fix wrong packet --- src/InventoryContentPacket.php | 26 +++++++-------- src/InventorySlotPacket.php | 26 +++++++-------- src/types/inventory/ContainerUIIds.php | 32 ------------------- src/types/inventory/FullContainerName.php | 13 +++++--- .../InventoryTransactionChangedSlotsHack.php | 4 +-- .../stackrequest/ItemStackRequestSlotInfo.php | 14 ++++---- .../ItemStackResponseContainerInfo.php | 14 ++++---- 7 files changed, 47 insertions(+), 82 deletions(-) diff --git a/src/InventoryContentPacket.php b/src/InventoryContentPacket.php index d13f6d21..ad51ad41 100644 --- a/src/InventoryContentPacket.php +++ b/src/InventoryContentPacket.php @@ -47,14 +47,12 @@ protected function decodePayload(PacketSerializer $in) : void{ for($i = 0; $i < $count; ++$i){ $this->items[] = ItemStackWrapper::read($in); } - if($in->getProtocol() >= ProtocolInfo::PROTOCOL_712){ - if($in->getProtocol() >= ProtocolInfo::PROTOCOL_729){ - $this->containerName = FullContainerName::read($in); - $this->dynamicContainerSize = $in->getUnsignedVarInt(); - }else { - $dynamicId = $in->getUnsignedVarInt(); - $this->containerName = new FullContainerName(0, $dynamicId); - } + if($in->getProtocol() >= ProtocolInfo::PROTOCOL_729){ + $this->containerName = FullContainerName::read($in); + $this->dynamicContainerSize = $in->getUnsignedVarInt(); + }elseif($in->getProtocol() >= ProtocolInfo::PROTOCOL_712){ + $dynamicId = $in->getUnsignedVarInt(); + $this->containerName = new FullContainerName(0, $dynamicId); } } @@ -64,13 +62,11 @@ protected function encodePayload(PacketSerializer $out) : void{ foreach($this->items as $item){ $item->write($out); } - if($out->getProtocol() >= ProtocolInfo::PROTOCOL_712){ - if($out->getProtocol() >= ProtocolInfo::PROTOCOL_729){ - $this->containerName->write($out); - $out->putUnsignedVarInt($this->dynamicContainerSize); - }else{ - $out->putUnsignedVarInt($this->containerName->getDynamicId() ?? 0); - } + if($out->getProtocol() >= ProtocolInfo::PROTOCOL_729){ + $this->containerName->write($out); + $out->putUnsignedVarInt($this->dynamicContainerSize); + }elseif($out->getProtocol() >= ProtocolInfo::PROTOCOL_712){ + $out->putUnsignedVarInt($this->containerName->getDynamicId() ?? 0); } } diff --git a/src/InventorySlotPacket.php b/src/InventorySlotPacket.php index 4eb7aeed..8bc94f26 100644 --- a/src/InventorySlotPacket.php +++ b/src/InventorySlotPacket.php @@ -43,14 +43,12 @@ public static function create(int $windowId, int $inventorySlot, FullContainerNa protected function decodePayload(PacketSerializer $in) : void{ $this->windowId = $in->getUnsignedVarInt(); $this->inventorySlot = $in->getUnsignedVarInt(); - if($in->getProtocol() >= ProtocolInfo::PROTOCOL_712){ - if($in->getProtocol() >= ProtocolInfo::PROTOCOL_729){ - $this->containerName = FullContainerName::read($in); - $this->dynamicContainerSize = $in->getUnsignedVarInt(); - }else{ - $dynamicId = $this->containerName->getDynamicId(); - $this->containerName = new FullContainerName(0, $dynamicId); - } + if($in->getProtocol() >= ProtocolInfo::PROTOCOL_729){ + $this->containerName = FullContainerName::read($in); + $this->dynamicContainerSize = $in->getUnsignedVarInt(); + }elseif($in->getProtocol() >= ProtocolInfo::PROTOCOL_712){ + $dynamicId = $this->containerName->getDynamicId(); + $this->containerName = new FullContainerName(0, $dynamicId); } $this->item = ItemStackWrapper::read($in); } @@ -58,13 +56,11 @@ protected function decodePayload(PacketSerializer $in) : void{ protected function encodePayload(PacketSerializer $out) : void{ $out->putUnsignedVarInt($this->windowId); $out->putUnsignedVarInt($this->inventorySlot); - if($out->getProtocol() >= ProtocolInfo::PROTOCOL_712){ - if($out->getProtocol() >= ProtocolInfo::PROTOCOL_729){ - $this->containerName->write($out); - $out->putUnsignedVarInt($this->dynamicContainerSize); - }else{ - $out->putUnsignedVarInt($this->containerName->getDynamicId() ?? 0); - } + if($out->getProtocol() >= ProtocolInfo::PROTOCOL_729){ + $this->containerName->write($out); + $out->putUnsignedVarInt($this->dynamicContainerSize); + }elseif($out->getProtocol() >= ProtocolInfo::PROTOCOL_712){ + $out->putUnsignedVarInt($this->containerName->getDynamicId() ?? 0); } $this->item->write($out); } diff --git a/src/types/inventory/ContainerUIIds.php b/src/types/inventory/ContainerUIIds.php index 863dd310..be48b607 100644 --- a/src/types/inventory/ContainerUIIds.php +++ b/src/types/inventory/ContainerUIIds.php @@ -14,9 +14,6 @@ namespace pocketmine\network\mcpe\protocol\types\inventory; -use pocketmine\network\mcpe\protocol\ProtocolInfo; -use pocketmine\network\mcpe\protocol\serializer\PacketSerializer; - final class ContainerUIIds{ private function __construct(){ @@ -87,33 +84,4 @@ private function __construct(){ public const SMITHING_TABLE_TEMPLATE = 61; public const CRAFTER = 62; public const DYNAMIC = 63; - - public static function write(PacketSerializer $out, int $containerId, bool $legacy = false) : void{ - if($out->getProtocol() >= ProtocolInfo::PROTOCOL_712 && !$legacy){ - (new FullContainerName($containerId))->write($out); - return; - } - if($out->getProtocol() < ProtocolInfo::PROTOCOL_560){ - if($containerId > self::RECIPE_BOOK){ - $containerId--; - }elseif($containerId === self::RECIPE_BOOK){ - throw new \InvalidArgumentException("Invalid container ID for protocol version " . $out->getProtocol()); - } - } - - $out->putByte($containerId); - } - - public static function read(PacketSerializer $in, bool $legacy = false) : int{ - if($in->getProtocol() >= ProtocolInfo::PROTOCOL_712 && !$legacy){ - return FullContainerName::read($in)->getContainerId(); - } - $containerId = $in->getByte(); - - if($in->getProtocol() < ProtocolInfo::PROTOCOL_560 && $containerId >= self::RECIPE_BOOK){ - $containerId++; - } - - return $containerId; - } } diff --git a/src/types/inventory/FullContainerName.php b/src/types/inventory/FullContainerName.php index 8053dcb5..4da5e965 100644 --- a/src/types/inventory/FullContainerName.php +++ b/src/types/inventory/FullContainerName.php @@ -29,16 +29,19 @@ public function getDynamicId() : ?int{ return $this->dynamicId; } public static function read(PacketSerializer $in) : self{ $containerId = $in->getByte(); - $dynamicId = $in->getProtocol() >= ProtocolInfo::PROTOCOL_729 ? - $in->readOptional($in->getLInt(...)) : $in->getLInt(); - return new self($containerId, $dynamicId); + if($in->getProtocol() >= ProtocolInfo::PROTOCOL_729){ + $dynamicId = $in->readOptional($in->getLInt(...)); + }elseif($in->getProtocol() >= ProtocolInfo::PROTOCOL_712){ + $dynamicId = $in->getLInt(); + } + return new self($containerId, $dynamicId ?? null); } public function write(PacketSerializer $out) : void{ $out->putByte($this->containerId); - if($out->getProtocol() >= ProtocolInfo::PROTOCOL_729) { + if($out->getProtocol() >= ProtocolInfo::PROTOCOL_729){ $out->writeOptional($this->dynamicId, $out->putLInt(...)); - }else{ + }elseif($out->getProtocol() >= ProtocolInfo::PROTOCOL_712){ $out->putLInt($this->dynamicId ?? 0); } } diff --git a/src/types/inventory/InventoryTransactionChangedSlotsHack.php b/src/types/inventory/InventoryTransactionChangedSlotsHack.php index 30c08226..329d7415 100644 --- a/src/types/inventory/InventoryTransactionChangedSlotsHack.php +++ b/src/types/inventory/InventoryTransactionChangedSlotsHack.php @@ -32,7 +32,7 @@ public function getContainerId() : int{ return $this->containerId; } public function getChangedSlotIndexes() : array{ return $this->changedSlotIndexes; } public static function read(PacketSerializer $in) : self{ - $containerId = ContainerUIIds::read($in, true); + $containerId = $in->getByte(); $changedSlots = []; for($i = 0, $len = $in->getUnsignedVarInt(); $i < $len; ++$i){ $changedSlots[] = $in->getByte(); @@ -41,7 +41,7 @@ public static function read(PacketSerializer $in) : self{ } public function write(PacketSerializer $out) : void{ - ContainerUIIds::write($out, $this->containerId, true); + $out->putByte($this->containerId); $out->putUnsignedVarInt(count($this->changedSlotIndexes)); foreach($this->changedSlotIndexes as $index){ $out->putByte($index); diff --git a/src/types/inventory/stackrequest/ItemStackRequestSlotInfo.php b/src/types/inventory/stackrequest/ItemStackRequestSlotInfo.php index b28c2b08..7e1cbfad 100644 --- a/src/types/inventory/stackrequest/ItemStackRequestSlotInfo.php +++ b/src/types/inventory/stackrequest/ItemStackRequestSlotInfo.php @@ -15,30 +15,32 @@ namespace pocketmine\network\mcpe\protocol\types\inventory\stackrequest; use pocketmine\network\mcpe\protocol\serializer\PacketSerializer; -use pocketmine\network\mcpe\protocol\types\inventory\ContainerUIIds; +use pocketmine\network\mcpe\protocol\types\inventory\FullContainerName; final class ItemStackRequestSlotInfo{ public function __construct( - private int $containerId, + private FullContainerName $containerName, private int $slotId, private int $stackId ){} - public function getContainerId() : int{ return $this->containerId; } + public function getContainerId() : int{ return $this->containerName->getContainerId(); } + + public function getContainerName() : FullContainerName{ return $this->containerName; } public function getSlotId() : int{ return $this->slotId; } public function getStackId() : int{ return $this->stackId; } public static function read(PacketSerializer $in) : self{ - $containerId = ContainerUIIds::read($in); + $containerName = FullContainerName::read($in); $slotId = $in->getByte(); $stackId = $in->readGenericTypeNetworkId(); - return new self($containerId, $slotId, $stackId); + return new self($containerName, $slotId, $stackId); } public function write(PacketSerializer $out) : void{ - ContainerUIIds::write($out, $this->containerId); + $this->containerName->write($out); $out->putByte($this->slotId); $out->writeGenericTypeNetworkId($this->stackId); } diff --git a/src/types/inventory/stackresponse/ItemStackResponseContainerInfo.php b/src/types/inventory/stackresponse/ItemStackResponseContainerInfo.php index d95dcf79..ea4d2f5f 100644 --- a/src/types/inventory/stackresponse/ItemStackResponseContainerInfo.php +++ b/src/types/inventory/stackresponse/ItemStackResponseContainerInfo.php @@ -14,9 +14,7 @@ namespace pocketmine\network\mcpe\protocol\types\inventory\stackresponse; -use pocketmine\network\mcpe\protocol\ProtocolInfo; use pocketmine\network\mcpe\protocol\serializer\PacketSerializer; -use pocketmine\network\mcpe\protocol\types\inventory\ContainerUIIds; use pocketmine\network\mcpe\protocol\types\inventory\FullContainerName; use function count; @@ -25,26 +23,28 @@ final class ItemStackResponseContainerInfo{ * @param ItemStackResponseSlotInfo[] $slots */ public function __construct( - private int $containerId, + private FullContainerName $containerName, private array $slots ){} - public function getContainerId() : int{ return $this->containerId; } + public function getContainerId() : int{ return $this->containerName->getContainerId(); } + + public function getContainerName() : FullContainerName{ return $this->containerName; } /** @return ItemStackResponseSlotInfo[] */ public function getSlots() : array{ return $this->slots; } public static function read(PacketSerializer $in) : self{ - $containerId = ContainerUIIds::read($in); + $containerName = FullContainerName::read($in); $slots = []; for($i = 0, $len = $in->getUnsignedVarInt(); $i < $len; ++$i){ $slots[] = ItemStackResponseSlotInfo::read($in); } - return new self($containerId, $slots); + return new self($containerName, $slots); } public function write(PacketSerializer $out) : void{ - ContainerUIIds::write($out, $this->containerId); + $this->containerName->write($out); $out->putUnsignedVarInt(count($this->slots)); foreach($this->slots as $slot){ $slot->write($out);