0
0
mirror of https://github.com/pmmp/BedrockProtocol.git synced 2025-02-23 08:06:20 +00:00
BedrockProtocol/src/LevelEventGenericPacket.php
Dylan K. Taylor 93bf272d34 LevelEventGenericPacket: fixed encoding
closes #77, after all this time
2023-09-20 17:50:10 +01:00

67 lines
1.9 KiB
PHP

<?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\nbt\NBT;
use pocketmine\nbt\NbtDataException;
use pocketmine\nbt\tag\Tag;
use pocketmine\network\mcpe\protocol\serializer\NetworkNbtSerializer;
use pocketmine\network\mcpe\protocol\serializer\PacketSerializer;
class LevelEventGenericPacket extends DataPacket implements ClientboundPacket{
public const NETWORK_ID = ProtocolInfo::LEVEL_EVENT_GENERIC_PACKET;
private int $eventId;
private Tag $eventData;
/**
* @generate-create-func
*/
public static function create(int $eventId, Tag $eventData) : self{
$result = new self;
$result->eventId = $eventId;
$result->eventData = $eventData;
return $result;
}
public function getEventId() : int{
return $this->eventId;
}
public function getEventData() : Tag{
return $this->eventData;
}
protected function decodePayload(PacketSerializer $in) : void{
$this->eventId = $in->getVarInt();
$offset = $in->getOffset();
try{
$this->eventData = (new NetworkNbtSerializer())->readHeadless($in->getBuffer(), NBT::TAG_Compound, $offset);
}catch(NbtDataException $e){
throw PacketDecodeException::wrap($e);
}
$in->setOffset($offset);
}
protected function encodePayload(PacketSerializer $out) : void{
$out->putVarInt($this->eventId);
$out->put((new NetworkNbtSerializer())->writeHeadless($this->eventData));
}
public function handle(PacketHandlerInterface $handler) : bool{
return $handler->handleLevelEventGeneric($this);
}
}