PocketMine-MP 5.15.1 git-5ef247620a7c6301a849b54e5ef1009217729fc8
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
20use function count;
21
26 public const NETWORK_ID = ProtocolInfo::DIMENSION_DATA_PACKET;
27
32 private array $definitions;
33
39 public static function create(array $definitions) : self{
40 $result = new self;
41 $result->definitions = $definitions;
42 return $result;
43 }
44
49 public function getDefinitions() : array{ return $this->definitions; }
50
51 protected function decodePayload(PacketSerializer $in) : void{
52 $this->definitions = [];
53
54 for($i = 0, $count = $in->getUnsignedVarInt(); $i < $count; $i++){
55 $dimensionNameId = $in->getString();
56 $dimensionData = DimensionData::read($in);
57
58 if(isset($this->definitions[$dimensionNameId])){
59 throw new PacketDecodeException("Repeated dimension data for key \"$dimensionNameId\"");
60 }
61 if($dimensionNameId !== DimensionNameIds::OVERWORLD && $dimensionNameId !== DimensionNameIds::NETHER && $dimensionNameId !== DimensionNameIds::THE_END){
62 throw new PacketDecodeException("Invalid dimension name ID \"$dimensionNameId\"");
63 }
64 $this->definitions[$dimensionNameId] = $dimensionData;
65 }
66 }
67
68 protected function encodePayload(PacketSerializer $out) : void{
69 $out->putUnsignedVarInt(count($this->definitions));
70
71 foreach($this->definitions as $dimensionNameId => $definition){
72 $out->putString((string) $dimensionNameId); //@phpstan-ignore-line
73 $definition->write($out);
74 }
75 }
76
77 public function handle(PacketHandlerInterface $handler) : bool{
78 return $handler->handleDimensionData($this);
79 }
80}