Last active
July 20, 2022 20:38
-
-
Save DavyCraft648/5b539753ba4446e3241e8800db1a49e2 to your computer and use it in GitHub Desktop.
Register legacy mappings from pm4
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| <?php | |
| namespace DavyCraft648\RegisterLegacyMappings; | |
| use pocketmine\data\bedrock\block\upgrade\LegacyBlockIdToStringIdMap; | |
| use pocketmine\network\mcpe\convert\RuntimeBlockMapping; | |
| use pocketmine\plugin\PluginBase; | |
| use pocketmine\world\format\io\GlobalBlockStateHandlers; | |
| use function array_flip; | |
| /** | |
| * @name RegisterLegacyMappings | |
| * @version 0.0.1 | |
| * @main DavyCraft648\RegisterLegacyMappings\RegisterLegacyMappings | |
| * @api 5.0.0 | |
| */ | |
| class RegisterLegacyMappings extends PluginBase { | |
| protected function onLoad(): void { | |
| self::registerLegacyMappings(); | |
| } | |
| public static function registerLegacyMappings(): void { | |
| $nameToLegacyMap = array_flip(LegacyBlockIdToStringIdMap::getInstance()->getLegacyToStringMap()); | |
| $blockDataUpgrader = GlobalBlockStateHandlers::getUpgrader(); | |
| $legacyBlockStateMapper = $blockDataUpgrader->getBlockIdMetaUpgrader(); | |
| $prop = new \ReflectionProperty($legacyBlockStateMapper, "mappingTable"); | |
| $prop->setAccessible(true); | |
| $mappingTable = $prop->getValue($legacyBlockStateMapper); | |
| /** @var int[] $metaMap */ | |
| $metaMap = []; | |
| $blockStateDictionary = RuntimeBlockMapping::getInstance()->getBlockStateDictionary(); | |
| foreach ($blockStateDictionary->getStates() as $entry) { | |
| $state = $entry->getStateData(); | |
| $name = $state->getName(); | |
| $legacyId = $nameToLegacyMap[$name] ?? null; | |
| if ($legacyId === null) { | |
| continue; | |
| } | |
| $meta = isset($metaMap[$name]) ? ++$metaMap[$name] : $metaMap[$name] = 0; | |
| if (!isset($mappingTable[$name][$meta])) { | |
| $legacyBlockStateMapper->addIdMetaToStateMapping($name, $meta, $state); | |
| $mappingTable[$name][$meta] = $state; | |
| } | |
| } | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment