Skip to content

Commit

Permalink
Fix wrong packet
Browse files Browse the repository at this point in the history
  • Loading branch information
AkmalFairuz committed Sep 21, 2024
1 parent 233938b commit 43f4e82
Show file tree
Hide file tree
Showing 7 changed files with 47 additions and 82 deletions.
26 changes: 11 additions & 15 deletions src/InventoryContentPacket.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
}

Expand All @@ -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);
}
}

Expand Down
26 changes: 11 additions & 15 deletions src/InventorySlotPacket.php
Original file line number Diff line number Diff line change
Expand Up @@ -43,28 +43,24 @@ 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);
}

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);
}
Expand Down
32 changes: 0 additions & 32 deletions src/types/inventory/ContainerUIIds.php
Original file line number Diff line number Diff line change
Expand Up @@ -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(){
Expand Down Expand Up @@ -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;
}
}
13 changes: 8 additions & 5 deletions src/types/inventory/FullContainerName.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
}
Expand Down
4 changes: 2 additions & 2 deletions src/types/inventory/InventoryTransactionChangedSlotsHack.php
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand All @@ -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);
Expand Down
14 changes: 8 additions & 6 deletions src/types/inventory/stackrequest/ItemStackRequestSlotInfo.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand All @@ -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);
Expand Down

0 comments on commit 43f4e82

Please sign in to comment.