PocketMine-MP 5.35.1 git-e32e836dad793a3a3c8ddd8927c00e112b1e576a
Loading...
Searching...
No Matches
LevelEventGenericPacket.php
1<?php
2
3/*
4 * This file is part of BedrockProtocol.
5 * Copyright (C) 2014-2022 PocketMine Team <https://github.com/pmmp/BedrockProtocol>
6 *
7 * BedrockProtocol is free software: you can redistribute it and/or modify
8 * it under the terms of the GNU Lesser General Public License as published by
9 * the Free Software Foundation, either version 3 of the License, or
10 * (at your option) any later version.
11 */
12
13declare(strict_types=1);
14
15namespace pocketmine\network\mcpe\protocol;
16
17use pmmp\encoding\ByteBufferReader;
18use pmmp\encoding\ByteBufferWriter;
19use pmmp\encoding\VarInt;
24
26 public const NETWORK_ID = ProtocolInfo::LEVEL_EVENT_GENERIC_PACKET;
27
28 private int $eventId;
29 private Tag $eventData;
30
34 public static function create(int $eventId, Tag $eventData) : self{
35 $result = new self;
36 $result->eventId = $eventId;
37 $result->eventData = $eventData;
38 return $result;
39 }
40
41 public function getEventId() : int{
42 return $this->eventId;
43 }
44
45 public function getEventData() : Tag{
46 return $this->eventData;
47 }
48
49 protected function decodePayload(ByteBufferReader $in) : void{
50 $this->eventId = VarInt::readSignedInt($in);
51 $offset = $in->getOffset();
52 try{
53 $this->eventData = (new NetworkNbtSerializer())->readHeadless($in->getData(), NBT::TAG_Compound, $offset);
54 }catch(NbtDataException $e){
55 throw PacketDecodeException::wrap($e);
56 }
57 $in->setOffset($offset);
58 }
59
60 protected function encodePayload(ByteBufferWriter $out) : void{
61 VarInt::writeSignedInt($out, $this->eventId);
62 $out->writeByteArray((new NetworkNbtSerializer())->writeHeadless($this->eventData));
63 }
64
65 public function handle(PacketHandlerInterface $handler) : bool{
66 return $handler->handleLevelEventGeneric($this);
67 }
68}