PocketMine-MP 5.21.2 git-a6534ecbbbcf369264567d27e5ed70f7f5be9816
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
31
40 public function __construct(
41 private array $mappingTable,
42 private LegacyBlockIdToStringIdMap $legacyNumericIdMap
43 ){}
44
48 public function fromStringIdMeta(string $id, int $meta) : BlockStateData{
49 return $this->mappingTable[$id][$meta] ??
50 $this->mappingTable[$id][0] ??
51 throw new BlockStateDeserializeException("Unknown legacy block string ID $id");
52 }
53
57 public function fromIntIdMeta(int $id, int $meta) : BlockStateData{
58 $stringId = $this->legacyNumericIdMap->legacyToString($id);
59 if($stringId === null){
60 throw new BlockStateDeserializeException("Unknown legacy block numeric ID $id");
61 }
62 return $this->fromStringIdMeta($stringId, $meta);
63 }
64
69 public function addIntIdToStringIdMapping(int $intId, string $stringId) : void{
70 $this->legacyNumericIdMap->add($stringId, $intId);
71 }
72
77 public function addIdMetaToStateMapping(string $stringId, int $meta, BlockStateData $stateData) : void{
78 if(isset($this->mappingTable[$stringId][$meta])){
79 throw new \InvalidArgumentException("A mapping for $stringId:$meta already exists");
80 }
81 $this->mappingTable[$stringId][$meta] = $stateData;
82 }
83
84 public static function loadFromString(string $data, LegacyBlockIdToStringIdMap $idMap, BlockStateUpgrader $blockStateUpgrader) : self{
85 $mappingTable = [];
86
87 $legacyStateMapReader = new BinaryStream($data);
88 $nbtReader = new LittleEndianNbtSerializer();
89
90 $idCount = $legacyStateMapReader->getUnsignedVarInt();
91 for($idIndex = 0; $idIndex < $idCount; $idIndex++){
92 $id = $legacyStateMapReader->get($legacyStateMapReader->getUnsignedVarInt());
93
94 $metaCount = $legacyStateMapReader->getUnsignedVarInt();
95 for($metaIndex = 0; $metaIndex < $metaCount; $metaIndex++){
96 $meta = $legacyStateMapReader->getUnsignedVarInt();
97
98 $offset = $legacyStateMapReader->getOffset();
99 $state = $nbtReader->read($legacyStateMapReader->getBuffer(), $offset)->mustGetCompoundTag();
100 $legacyStateMapReader->setOffset($offset);
101 $mappingTable[$id][$meta] = $blockStateUpgrader->upgrade(BlockStateData::fromNbt($state));
102 }
103 }
104 if(!$legacyStateMapReader->feof()){
105 throw new BinaryDataException("Unexpected trailing data in legacy state map data");
106 }
107
108 return new self($mappingTable, $idMap);
109 }
110}
__construct(private array $mappingTable, private LegacyBlockIdToStringIdMap $legacyNumericIdMap)
addIdMetaToStateMapping(string $stringId, int $meta, BlockStateData $stateData)