PocketMine-MP 5.15.1 git-5ef247620a7c6301a849b54e5ef1009217729fc8
BlockTranslator.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\network\mcpe\convert;
25
31
35final class BlockTranslator{
40 private array $networkIdCache = [];
41
43 private BlockStateData $fallbackStateData;
44 private int $fallbackStateId;
45
46 public function __construct(
47 private BlockStateDictionary $blockStateDictionary,
48 private BlockStateSerializer $blockStateSerializer
49 ){
50 $this->fallbackStateData = BlockStateData::current(BlockTypeNames::INFO_UPDATE, []);
51 $this->fallbackStateId = $this->blockStateDictionary->lookupStateIdFromData($this->fallbackStateData) ??
52 throw new AssumptionFailedError(BlockTypeNames::INFO_UPDATE . " should always exist");
53 }
54
55 public function internalIdToNetworkId(int $internalStateId) : int{
56 if(isset($this->networkIdCache[$internalStateId])){
57 return $this->networkIdCache[$internalStateId];
58 }
59
60 try{
61 $blockStateData = $this->blockStateSerializer->serialize($internalStateId);
62
63 $networkId = $this->blockStateDictionary->lookupStateIdFromData($blockStateData);
64 if($networkId === null){
65 throw new AssumptionFailedError("Unmapped blockstate returned by blockstate serializer: " . $blockStateData->toNbt());
66 }
68 //TODO: this will swallow any error caused by invalid block properties; this is not ideal, but it should be
69 //covered by unit tests, so this is probably a safe assumption.
70 $networkId = $this->fallbackStateId;
71 }
72
73 return $this->networkIdCache[$internalStateId] = $networkId;
74 }
75
79 public function internalIdToNetworkStateData(int $internalStateId) : BlockStateData{
80 //we don't directly use the blockstate serializer here - we can't assume that the network blockstate NBT is the
81 //same as the disk blockstate NBT, in case we decide to have different world version than network version (or in
82 //case someone wants to implement multi version).
83 $networkRuntimeId = $this->internalIdToNetworkId($internalStateId);
84
85 return $this->blockStateDictionary->generateDataFromStateId($networkRuntimeId) ?? throw new AssumptionFailedError("We just looked up this state ID, so it must exist");
86 }
87
88 public function getBlockStateDictionary() : BlockStateDictionary{ return $this->blockStateDictionary; }
89
90 public function getFallbackStateData() : BlockStateData{ return $this->fallbackStateData; }
91}
static current(string $name, array $states)