PocketMine-MP 5.35.1 git-e32e836dad793a3a3c8ddd8927c00e112b1e576a
Loading...
Searching...
No Matches
DimensionDataPacket.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;
23use function count;
24
29 public const NETWORK_ID = ProtocolInfo::DIMENSION_DATA_PACKET;
30
35 private array $definitions;
36
42 public static function create(array $definitions) : self{
43 $result = new self;
44 $result->definitions = $definitions;
45 return $result;
46 }
47
52 public function getDefinitions() : array{ return $this->definitions; }
53
54 protected function decodePayload(ByteBufferReader $in) : void{
55 $this->definitions = [];
56
57 for($i = 0, $count = VarInt::readUnsignedInt($in); $i < $count; $i++){
58 $dimensionNameId = CommonTypes::getString($in);
59 $dimensionData = DimensionData::read($in);
60
61 if(isset($this->definitions[$dimensionNameId])){
62 throw new PacketDecodeException("Repeated dimension data for key \"$dimensionNameId\"");
63 }
64 if($dimensionNameId !== DimensionNameIds::OVERWORLD && $dimensionNameId !== DimensionNameIds::NETHER && $dimensionNameId !== DimensionNameIds::THE_END){
65 throw new PacketDecodeException("Invalid dimension name ID \"$dimensionNameId\"");
66 }
67 $this->definitions[$dimensionNameId] = $dimensionData;
68 }
69 }
70
71 protected function encodePayload(ByteBufferWriter $out) : void{
72 VarInt::writeUnsignedInt($out, count($this->definitions));
73
74 foreach($this->definitions as $dimensionNameId => $definition){
75 CommonTypes::putString($out, (string) $dimensionNameId); //@phpstan-ignore-line
76 $definition->write($out);
77 }
78 }
79
80 public function handle(PacketHandlerInterface $handler) : bool{
81 return $handler->handleDimensionData($this);
82 }
83}