PocketMine-MP 5.35.1 git-e32e836dad793a3a3c8ddd8927c00e112b1e576a
Loading...
Searching...
No Matches
BlockIdMetaUpgrader.php
1<?php
2
3/*
4 *
5 * ____ _ _ __ __ _ __ __ ____
6 * | _ \ ___ ___| | _____| |_| \/ (_)_ __ ___ | \/ | _ \
7 * | |_) / _ \ / __| |/ / _ \ __| |\/| | | '_ \ / _ \_____| |\/| | |_) |
8 * | __/ (_) | (__| < __/ |_| | | | | | | | __/_____| | | | __/
9 * |_| \___/ \___|_|\_\___|\__|_| |_|_|_| |_|\___| |_| |_|_|
10 *
11 * This program is free software: you can redistribute it and/or modify
12 * it under the terms of the GNU Lesser General Public License as published by
13 * the Free Software Foundation, either version 3 of the License, or
14 * (at your option) any later version.
15 *
16 * @author PocketMine Team
17 * @link http://www.pocketmine.net/
18 *
19 *
20 */
21
22declare(strict_types=1);
23
24namespace pocketmine\data\bedrock\block\upgrade;
25
26use pmmp\encoding\ByteBufferReader;
27use pmmp\encoding\DataDecodeException;
28use pmmp\encoding\VarInt;
32
41 public function __construct(
42 private array $mappingTable,
43 private LegacyBlockIdToStringIdMap $legacyNumericIdMap
44 ){}
45
49 public function fromStringIdMeta(string $id, int $meta) : BlockStateData{
50 return $this->mappingTable[$id][$meta] ??
51 $this->mappingTable[$id][0] ??
52 throw new BlockStateDeserializeException("Unknown legacy block string ID $id");
53 }
54
58 public function fromIntIdMeta(int $id, int $meta) : BlockStateData{
59 $stringId = $this->legacyNumericIdMap->legacyToString($id);
60 if($stringId === null){
61 throw new BlockStateDeserializeException("Unknown legacy block numeric ID $id");
62 }
63 return $this->fromStringIdMeta($stringId, $meta);
64 }
65
70 public function addIntIdToStringIdMapping(int $intId, string $stringId) : void{
71 $this->legacyNumericIdMap->add($stringId, $intId);
72 }
73
78 public function addIdMetaToStateMapping(string $stringId, int $meta, BlockStateData $stateData) : void{
79 if(isset($this->mappingTable[$stringId][$meta])){
80 throw new \InvalidArgumentException("A mapping for $stringId:$meta already exists");
81 }
82 $this->mappingTable[$stringId][$meta] = $stateData;
83 }
84
85 public static function loadFromString(string $data, LegacyBlockIdToStringIdMap $idMap, BlockStateUpgrader $blockStateUpgrader) : self{
86 $mappingTable = [];
87
88 $legacyStateMapReader = new ByteBufferReader($data);
89 $nbtReader = new LittleEndianNbtSerializer();
90
91 $idCount = VarInt::readUnsignedInt($legacyStateMapReader);
92 for($idIndex = 0; $idIndex < $idCount; $idIndex++){
93 $id = $legacyStateMapReader->readByteArray(VarInt::readUnsignedInt($legacyStateMapReader));
94
95 $metaCount = VarInt::readUnsignedInt($legacyStateMapReader);
96 for($metaIndex = 0; $metaIndex < $metaCount; $metaIndex++){
97 $meta = VarInt::readUnsignedInt($legacyStateMapReader);
98
99 $offset = $legacyStateMapReader->getOffset();
100 $state = $nbtReader->read($legacyStateMapReader->getData(), $offset)->mustGetCompoundTag();
101 $legacyStateMapReader->setOffset($offset);
102 $mappingTable[$id][$meta] = $blockStateUpgrader->upgrade(BlockStateData::fromNbt($state));
103 }
104 }
105 if($legacyStateMapReader->getUnreadLength() > 0){
106 throw new DataDecodeException("Unexpected trailing data in legacy state map data");
107 }
108
109 return new self($mappingTable, $idMap);
110 }
111}
__construct(private array $mappingTable, private LegacyBlockIdToStringIdMap $legacyNumericIdMap)
addIdMetaToStateMapping(string $stringId, int $meta, BlockStateData $stateData)