BlockStateUpgrader: extract state remap to its own function

This commit is contained in:
Dylan K. Taylor 2024-03-06 12:56:49 +00:00
parent dbc7105e5b
commit ac8dbf8640
No known key found for this signature in database
GPG Key ID: 8927471A91CAFD3D

View File

@ -69,44 +69,14 @@ final class BlockStateUpgrader{
//didn't always bump the blockstate version when changing it :(
continue;
}
$oldName = $blockStateData->getName();
$oldState = $blockStateData->getStates();
if(isset($schema->remappedStates[$oldName])){
foreach($schema->remappedStates[$oldName] as $remap){
if(count($remap->oldState) > count($oldState)){
//match criteria has more requirements than we have state properties
continue; //try next state
}
foreach(Utils::stringifyKeys($remap->oldState) as $k => $v){
if(!isset($oldState[$k]) || !$oldState[$k]->equals($v)){
continue 2; //try next state
}
}
if(is_string($remap->newName)){
$newName = $remap->newName;
}else{
$flattenedValue = $oldState[$remap->newName->flattenedProperty] ?? null;
if($flattenedValue instanceof StringTag){
$newName = sprintf("%s%s%s", $remap->newName->prefix, $flattenedValue->getValue(), $remap->newName->suffix);
unset($oldState[$remap->newName->flattenedProperty]);
}else{
//flattened property is not a TAG_String, so this transformation is not applicable
continue;
}
}
$newState = $remap->newState;
foreach($remap->copiedState as $stateName){
if(isset($oldState[$stateName])){
$newState[$stateName] = $oldState[$stateName];
}
}
$blockStateData = new BlockStateData($newName, $newState, $resultVersion);
continue 2; //try next schema
}
$newStateData = $this->applyStateRemapped($schema, $blockStateData);
if($newStateData !== null){
$blockStateData = $newStateData;
continue;
}
$oldName = $blockStateData->getName();
$newName = $schema->renamedIds[$oldName] ?? null;
$stateChanges = 0;
@ -131,6 +101,49 @@ final class BlockStateUpgrader{
return $blockStateData;
}
private function applyStateRemapped(BlockStateUpgradeSchema $schema, BlockStateData $blockStateData) : ?BlockStateData{
$oldName = $blockStateData->getName();
$oldState = $blockStateData->getStates();
if(isset($schema->remappedStates[$oldName])){
foreach($schema->remappedStates[$oldName] as $remap){
if(count($remap->oldState) > count($oldState)){
//match criteria has more requirements than we have state properties
continue; //try next state
}
foreach(Utils::stringifyKeys($remap->oldState) as $k => $v){
if(!isset($oldState[$k]) || !$oldState[$k]->equals($v)){
continue 2; //try next state
}
}
if(is_string($remap->newName)){
$newName = $remap->newName;
}else{
$flattenedValue = $oldState[$remap->newName->flattenedProperty] ?? null;
if($flattenedValue instanceof StringTag){
$newName = sprintf("%s%s%s", $remap->newName->prefix, $flattenedValue->getValue(), $remap->newName->suffix);
unset($oldState[$remap->newName->flattenedProperty]);
}else{
//flattened property is not a TAG_String, so this transformation is not applicable
continue;
}
}
$newState = $remap->newState;
foreach($remap->copiedState as $stateName){
if(isset($oldState[$stateName])){
$newState[$stateName] = $oldState[$stateName];
}
}
return new BlockStateData($newName, $newState, $schema->getVersionId());
}
}
return null;
}
/**
* @param Tag[] $states
* @phpstan-param array<string, Tag> $states