PocketMine-MP 5.35.1 git-e32e836dad793a3a3c8ddd8927c00e112b1e576a
Loading...
Searching...
No Matches
BiomeDefinitionListPacket.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 array_map;
24use function count;
25
27 public const NETWORK_ID = ProtocolInfo::BIOME_DEFINITION_LIST_PACKET;
28
33 private array $definitionData = [];
38 private array $strings = [];
39
47 public static function create(array $definitionData, array $strings) : self{
48 $result = new self;
49 $result->definitionData = $definitionData;
50 $result->strings = $strings;
51 return $result;
52 }
53
57 public static function fromDefinitions(array $definitions) : self{
62 $stringIndexLookup = [];
63 $strings = [];
64 $addString = function(string $string) use (&$stringIndexLookup, &$strings) : int{
65 if(isset($stringIndexLookup[$string])){
66 return $stringIndexLookup[$string];
67 }
68
69 $stringIndexLookup[$string] = count($stringIndexLookup);
70 $strings[] = $string;
71 return $stringIndexLookup[$string];
72 };
73
74 $definitionData = array_map(fn(BiomeDefinitionEntry $entry) => new BiomeDefinitionData(
75 $addString($entry->getBiomeName()),
76 $entry->getId(),
77 $entry->getTemperature(),
78 $entry->getDownfall(),
79 $entry->getFoliageSnow(),
80 $entry->getDepth(),
81 $entry->getScale(),
82 $entry->getMapWaterColor(),
83 $entry->hasRain(),
84 $entry->getTags() === null ? null : array_map($addString, $entry->getTags()),
85 $entry->getChunkGenData(),
86 ), $definitions);
87
88 return self::create($definitionData, $strings);
89 }
90
94 private function locateString(int $index) : string{
95 return $this->strings[$index] ?? throw new PacketDecodeException("Unknown string index $index");
96 }
97
106 public function buildDefinitionsFromData() : array{
107 return array_map(fn(BiomeDefinitionData $data) => new BiomeDefinitionEntry(
108 $this->locateString($data->getNameIndex()),
109 $data->getId(),
110 $data->getTemperature(),
111 $data->getDownfall(),
112 $data->getFoliageSnow(),
113 $data->getDepth(),
114 $data->getScale(),
115 $data->getMapWaterColor(),
116 $data->hasRain(),
117 ($tagIndexes = $data->getTagIndexes()) === null ? null : array_map($this->locateString(...), $tagIndexes),
118 $data->getChunkGenData(),
119 ), $this->definitionData);
120 }
121
122 protected function decodePayload(ByteBufferReader $in) : void{
123 for($i = 0, $count = VarInt::readUnsignedInt($in); $i < $count; ++$i){
124 $this->definitionData[] = BiomeDefinitionData::read($in);
125 }
126
127 for($i = 0, $count = VarInt::readUnsignedInt($in); $i < $count; ++$i){
128 $this->strings[] = CommonTypes::getString($in);
129 }
130 }
131
132 protected function encodePayload(ByteBufferWriter $out) : void{
133 VarInt::writeUnsignedInt($out, count($this->definitionData));
134 foreach($this->definitionData as $data){
135 $data->write($out);
136 }
137
138 VarInt::writeUnsignedInt($out, count($this->strings));
139 foreach($this->strings as $string){
140 CommonTypes::putString($out, $string);
141 }
142 }
143
144 public function handle(PacketHandlerInterface $handler) : bool{
145 return $handler->handleBiomeDefinitionList($this);
146 }
147}
getTags()