LegacyBlockStateMapper: provide a way to add custom upgrade mappings

this will be needed by plugin developers to upgrade old custom blocks from PM4.
This commit is contained in:
Dylan K. Taylor 2022-07-07 19:44:16 +01:00
parent c67e42a723
commit 56e6a55645
No known key found for this signature in database
GPG Key ID: 8927471A91CAFD3D
2 changed files with 23 additions and 0 deletions

View File

@ -81,4 +81,15 @@ abstract class LegacyToStringBidirectionalIdMap{
public function getStringToLegacyMap() : array{
return $this->stringToLegacy;
}
public function add(string $string, int $legacy) : void{
if(isset($this->legacyToString[$legacy])){
throw new \InvalidArgumentException("Legacy ID $legacy is already mapped to string " . $this->legacyToString[$legacy]);
}
if(isset($this->stringToLegacy[$string])){
throw new \InvalidArgumentException("String ID $string is already mapped to legacy ID " . $this->stringToLegacy[$string]);
}
$this->legacyToString[$legacy] = $string;
$this->stringToLegacy[$string] = $legacy;
}
}

View File

@ -52,6 +52,18 @@ final class LegacyBlockStateMapper{
return $this->fromStringIdMeta($stringId, $meta);
}
/**
* Adds a mapping of legacy block ID and meta to modern blockstate data. This may be needed for upgrading data from
* stored custom blocks from older versions of PocketMine-MP.
*/
public function addMapping(string $stringId, int $intId, int $meta, BlockStateData $stateData) : void{
if(isset($this->mappingTable[$stringId][$meta])){
throw new \InvalidArgumentException("A mapping for $stringId:$meta already exists");
}
$this->mappingTable[$stringId][$meta] = $stateData;
$this->legacyNumericIdMap->add($intId, $stringId);
}
public static function loadFromString(string $data, LegacyBlockIdToStringIdMap $idMap, BlockStateUpgrader $blockStateUpgrader) : self{
$mappingTable = [];