PocketMine-MP 5.28.1 git-88cdc2eb67c40075559c3ef51418b418cd5488e9
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
20use function array_map;
21use function count;
22
24 public const NETWORK_ID = ProtocolInfo::BIOME_DEFINITION_LIST_PACKET;
25
30 private array $definitionData = [];
35 private array $strings = [];
36
44 public static function create(array $definitionData, array $strings) : self{
45 $result = new self;
46 $result->definitionData = $definitionData;
47 $result->strings = $strings;
48 return $result;
49 }
50
54 public static function fromDefinitions(array $definitions) : self{
59 $stringIndexLookup = [];
60 $strings = [];
61 $addString = function(string $string) use (&$stringIndexLookup, &$strings) : int{
62 if(isset($stringIndexLookup[$string])){
63 return $stringIndexLookup[$string];
64 }
65
66 $stringIndexLookup[$string] = count($stringIndexLookup);
67 $strings[] = $string;
68 return $stringIndexLookup[$string];
69 };
70
71 $definitionData = array_map(fn(BiomeDefinitionEntry $entry) => new BiomeDefinitionData(
72 $addString($entry->getBiomeName()),
73 $entry->getId(),
74 $entry->getTemperature(),
75 $entry->getDownfall(),
76 $entry->getRedSporeDensity(),
77 $entry->getBlueSporeDensity(),
78 $entry->getAshDensity(),
79 $entry->getWhiteAshDensity(),
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->getRedSporeDensity(),
113 $data->getBlueSporeDensity(),
114 $data->getAshDensity(),
115 $data->getWhiteAshDensity(),
116 $data->getDepth(),
117 $data->getScale(),
118 $data->getMapWaterColor(),
119 $data->hasRain(),
120 ($tagIndexes = $data->getTagIndexes()) === null ? null : array_map($this->locateString(...), $tagIndexes),
121 $data->getChunkGenData(),
122 ), $this->definitionData);
123 }
124
125 protected function decodePayload(PacketSerializer $in) : void{
126 for($i = 0, $count = $in->getUnsignedVarInt(); $i < $count; ++$i){
127 $this->definitionData[] = BiomeDefinitionData::read($in);
128 }
129
130 for($i = 0, $count = $in->getUnsignedVarInt(); $i < $count; ++$i){
131 $this->strings[] = $in->getString();
132 }
133 }
134
135 protected function encodePayload(PacketSerializer $out) : void{
136 $out->putUnsignedVarInt(count($this->definitionData));
137 foreach($this->definitionData as $data){
138 $data->write($out);
139 }
140
141 $out->putUnsignedVarInt(count($this->strings));
142 foreach($this->strings as $string){
143 $out->putString($string);
144 }
145 }
146
147 public function handle(PacketHandlerInterface $handler) : bool{
148 return $handler->handleBiomeDefinitionList($this);
149 }
150}
getTags()