mirror of
https://github.com/pmmp/PocketMine-MP.git
synced 2025-08-16 00:00:04 +00:00
BlockStateUpgrader: All but removed dependency on BlockStateData
This commit is contained in:
parent
cc17e68072
commit
5139800e13
@ -75,6 +75,8 @@ final class BlockStateUpgrader{
|
|||||||
|
|
||||||
public function upgrade(BlockStateData $blockStateData) : BlockStateData{
|
public function upgrade(BlockStateData $blockStateData) : BlockStateData{
|
||||||
$version = $blockStateData->getVersion();
|
$version = $blockStateData->getVersion();
|
||||||
|
$name = $blockStateData->getName();
|
||||||
|
$states = $blockStateData->getStates();
|
||||||
foreach($this->upgradeSchemas as $resultVersion => $schemaList){
|
foreach($this->upgradeSchemas as $resultVersion => $schemaList){
|
||||||
/*
|
/*
|
||||||
* Sometimes Mojang made changes without bumping the version ID.
|
* Sometimes Mojang made changes without bumping the version ID.
|
||||||
@ -91,57 +93,54 @@ final class BlockStateUpgrader{
|
|||||||
}
|
}
|
||||||
|
|
||||||
foreach($schemaList as $schema){
|
foreach($schemaList as $schema){
|
||||||
$blockStateData = $this->applySchema($schema, $blockStateData);
|
[$name, $states] = $this->applySchema($schema, $name, $states);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
if($this->outputVersion > $version){
|
return new BlockStateData($name, $states, $this->outputVersion);
|
||||||
//always update the version number of the blockstate, even if it didn't change - this is needed for
|
|
||||||
//external tools
|
|
||||||
$blockStateData = new BlockStateData($blockStateData->getName(), $blockStateData->getStates(), $this->outputVersion);
|
|
||||||
}
|
|
||||||
return $blockStateData;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
private function applySchema(BlockStateUpgradeSchema $schema, BlockStateData $blockStateData) : BlockStateData{
|
/**
|
||||||
$newStateData = $this->applyStateRemapped($schema, $blockStateData);
|
* @param Tag[] $states
|
||||||
if($newStateData !== null){
|
* @phpstan-param array<string, Tag> $states
|
||||||
return $newStateData;
|
*
|
||||||
|
* @return (string|Tag[])[]
|
||||||
|
* @phpstan-return array{0: string, 1: array<string, Tag>}
|
||||||
|
*/
|
||||||
|
private function applySchema(BlockStateUpgradeSchema $schema, string $oldName, array $states) : array{
|
||||||
|
$remapped = $this->applyStateRemapped($schema, $oldName, $states);
|
||||||
|
if($remapped !== null){
|
||||||
|
return $remapped;
|
||||||
}
|
}
|
||||||
|
|
||||||
$oldName = $blockStateData->getName();
|
|
||||||
$states = $blockStateData->getStates();
|
|
||||||
|
|
||||||
if(isset($schema->renamedIds[$oldName]) && isset($schema->flattenedProperties[$oldName])){
|
if(isset($schema->renamedIds[$oldName]) && isset($schema->flattenedProperties[$oldName])){
|
||||||
//TODO: this probably ought to be validated when the schema is constructed
|
//TODO: this probably ought to be validated when the schema is constructed
|
||||||
throw new AssumptionFailedError("Both renamedIds and flattenedProperties are set for the same block ID \"$oldName\" - don't know what to do");
|
throw new AssumptionFailedError("Both renamedIds and flattenedProperties are set for the same block ID \"$oldName\" - don't know what to do");
|
||||||
}
|
}
|
||||||
if(isset($schema->renamedIds[$oldName])){
|
if(isset($schema->renamedIds[$oldName])){
|
||||||
$newName = $schema->renamedIds[$oldName] ?? null;
|
$newName = $schema->renamedIds[$oldName];
|
||||||
}elseif(isset($schema->flattenedProperties[$oldName])){
|
}elseif(isset($schema->flattenedProperties[$oldName])){
|
||||||
[$newName, $states] = $this->applyPropertyFlattened($schema->flattenedProperties[$oldName], $oldName, $states);
|
[$newName, $states] = $this->applyPropertyFlattened($schema->flattenedProperties[$oldName], $oldName, $states);
|
||||||
}else{
|
}else{
|
||||||
$newName = null;
|
$newName = $oldName;
|
||||||
}
|
}
|
||||||
|
|
||||||
$stateChanges = 0;
|
$states = $this->applyPropertyAdded($schema, $oldName, $states);
|
||||||
|
$states = $this->applyPropertyRemoved($schema, $oldName, $states);
|
||||||
|
$states = $this->applyPropertyRenamedOrValueChanged($schema, $oldName, $states);
|
||||||
|
$states = $this->applyPropertyValueChanged($schema, $oldName, $states);
|
||||||
|
|
||||||
$states = $this->applyPropertyAdded($schema, $oldName, $states, $stateChanges);
|
return [$newName, $states];
|
||||||
$states = $this->applyPropertyRemoved($schema, $oldName, $states, $stateChanges);
|
|
||||||
$states = $this->applyPropertyRenamedOrValueChanged($schema, $oldName, $states, $stateChanges);
|
|
||||||
$states = $this->applyPropertyValueChanged($schema, $oldName, $states, $stateChanges);
|
|
||||||
|
|
||||||
if($newName !== null || $stateChanges > 0){
|
|
||||||
return new BlockStateData($newName ?? $oldName, $states, $schema->getVersionId());
|
|
||||||
}
|
|
||||||
|
|
||||||
return $blockStateData;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
private function applyStateRemapped(BlockStateUpgradeSchema $schema, BlockStateData $blockStateData) : ?BlockStateData{
|
/**
|
||||||
$oldName = $blockStateData->getName();
|
* @param Tag[] $oldState
|
||||||
$oldState = $blockStateData->getStates();
|
* @phpstan-param array<string, Tag> $oldState
|
||||||
|
*
|
||||||
|
* @return (string|Tag[])[]|null
|
||||||
|
* @phpstan-return array{0: string, 1: array<string, Tag>}|null
|
||||||
|
*/
|
||||||
|
private function applyStateRemapped(BlockStateUpgradeSchema $schema, string $oldName, array $oldState) : ?array{
|
||||||
if(isset($schema->remappedStates[$oldName])){
|
if(isset($schema->remappedStates[$oldName])){
|
||||||
foreach($schema->remappedStates[$oldName] as $remap){
|
foreach($schema->remappedStates[$oldName] as $remap){
|
||||||
if(count($remap->oldState) > count($oldState)){
|
if(count($remap->oldState) > count($oldState)){
|
||||||
@ -168,7 +167,7 @@ final class BlockStateUpgrader{
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
return new BlockStateData($newName, $newState, $schema->getVersionId());
|
return [$newName, $newState];
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -182,11 +181,10 @@ final class BlockStateUpgrader{
|
|||||||
* @return Tag[]
|
* @return Tag[]
|
||||||
* @phpstan-return array<string, Tag>
|
* @phpstan-return array<string, Tag>
|
||||||
*/
|
*/
|
||||||
private function applyPropertyAdded(BlockStateUpgradeSchema $schema, string $oldName, array $states, int &$stateChanges) : array{
|
private function applyPropertyAdded(BlockStateUpgradeSchema $schema, string $oldName, array $states) : array{
|
||||||
if(isset($schema->addedProperties[$oldName])){
|
if(isset($schema->addedProperties[$oldName])){
|
||||||
foreach(Utils::stringifyKeys($schema->addedProperties[$oldName]) as $propertyName => $value){
|
foreach(Utils::stringifyKeys($schema->addedProperties[$oldName]) as $propertyName => $value){
|
||||||
if(!isset($states[$propertyName])){
|
if(!isset($states[$propertyName])){
|
||||||
$stateChanges++;
|
|
||||||
$states[$propertyName] = $value;
|
$states[$propertyName] = $value;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -202,13 +200,10 @@ final class BlockStateUpgrader{
|
|||||||
* @return Tag[]
|
* @return Tag[]
|
||||||
* @phpstan-return array<string, Tag>
|
* @phpstan-return array<string, Tag>
|
||||||
*/
|
*/
|
||||||
private function applyPropertyRemoved(BlockStateUpgradeSchema $schema, string $oldName, array $states, int &$stateChanges) : array{
|
private function applyPropertyRemoved(BlockStateUpgradeSchema $schema, string $oldName, array $states) : array{
|
||||||
if(isset($schema->removedProperties[$oldName])){
|
if(isset($schema->removedProperties[$oldName])){
|
||||||
foreach($schema->removedProperties[$oldName] as $propertyName){
|
foreach($schema->removedProperties[$oldName] as $propertyName){
|
||||||
if(isset($states[$propertyName])){
|
unset($states[$propertyName]);
|
||||||
$stateChanges++;
|
|
||||||
unset($states[$propertyName]);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -234,12 +229,11 @@ final class BlockStateUpgrader{
|
|||||||
* @return Tag[]
|
* @return Tag[]
|
||||||
* @phpstan-return array<string, Tag>
|
* @phpstan-return array<string, Tag>
|
||||||
*/
|
*/
|
||||||
private function applyPropertyRenamedOrValueChanged(BlockStateUpgradeSchema $schema, string $oldName, array $states, int &$stateChanges) : array{
|
private function applyPropertyRenamedOrValueChanged(BlockStateUpgradeSchema $schema, string $oldName, array $states) : array{
|
||||||
if(isset($schema->renamedProperties[$oldName])){
|
if(isset($schema->renamedProperties[$oldName])){
|
||||||
foreach(Utils::stringifyKeys($schema->renamedProperties[$oldName]) as $oldPropertyName => $newPropertyName){
|
foreach(Utils::stringifyKeys($schema->renamedProperties[$oldName]) as $oldPropertyName => $newPropertyName){
|
||||||
$oldValue = $states[$oldPropertyName] ?? null;
|
$oldValue = $states[$oldPropertyName] ?? null;
|
||||||
if($oldValue !== null){
|
if($oldValue !== null){
|
||||||
$stateChanges++;
|
|
||||||
unset($states[$oldPropertyName]);
|
unset($states[$oldPropertyName]);
|
||||||
|
|
||||||
//If a value remap is needed, we need to do it here, since we won't be able to locate the property
|
//If a value remap is needed, we need to do it here, since we won't be able to locate the property
|
||||||
@ -260,16 +254,13 @@ final class BlockStateUpgrader{
|
|||||||
* @return Tag[]
|
* @return Tag[]
|
||||||
* @phpstan-return array<string, Tag>
|
* @phpstan-return array<string, Tag>
|
||||||
*/
|
*/
|
||||||
private function applyPropertyValueChanged(BlockStateUpgradeSchema $schema, string $oldName, array $states, int &$stateChanges) : array{
|
private function applyPropertyValueChanged(BlockStateUpgradeSchema $schema, string $oldName, array $states) : array{
|
||||||
if(isset($schema->remappedPropertyValues[$oldName])){
|
if(isset($schema->remappedPropertyValues[$oldName])){
|
||||||
foreach(Utils::stringifyKeys($schema->remappedPropertyValues[$oldName]) as $oldPropertyName => $remappedValues){
|
foreach(Utils::stringifyKeys($schema->remappedPropertyValues[$oldName]) as $oldPropertyName => $remappedValues){
|
||||||
$oldValue = $states[$oldPropertyName] ?? null;
|
$oldValue = $states[$oldPropertyName] ?? null;
|
||||||
if($oldValue !== null){
|
if($oldValue !== null){
|
||||||
$newValue = $this->locateNewPropertyValue($schema, $oldName, $oldPropertyName, $oldValue);
|
$newValue = $this->locateNewPropertyValue($schema, $oldName, $oldPropertyName, $oldValue);
|
||||||
if($newValue !== $oldValue){
|
$states[$oldPropertyName] = $newValue;
|
||||||
$stateChanges++;
|
|
||||||
$states[$oldPropertyName] = $newValue;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user