$upgradeSchemas */ public function __construct(array $upgradeSchemas){ foreach($upgradeSchemas as $priority => $schema){ $this->addSchema($schema, $priority); } } public function addSchema(BlockStateUpgradeSchema $schema, int $priority) : void{ if(isset($this->upgradeSchemas[$schema->getVersionId()][$priority])){ throw new \InvalidArgumentException("Another schema already has this priority"); } $this->upgradeSchemas[$schema->getVersionId()][$priority] = $schema; ksort($this->upgradeSchemas, SORT_NUMERIC); ksort($this->upgradeSchemas[$schema->getVersionId()], SORT_NUMERIC); } public function upgrade(BlockStateData $blockStateData) : BlockStateData{ $oldName = $blockStateData->getName(); $version = $blockStateData->getVersion(); foreach($this->upgradeSchemas as $resultVersion => $schemas){ if($version > $resultVersion){ //even if this is actually the same version, we have to apply it anyway because mojang are dumb and //didn't always bump the blockstate version when changing it :( continue; } foreach($schemas as $schema){ $newName = $schema->renamedIds[$oldName] ?? null; $stateChanges = 0; $states = $blockStateData->getStates(); $states = $this->applyPropertyAdded($schema, $oldName, $states, $stateChanges); $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){ $blockStateData = new BlockStateData($newName ?? $oldName, $states, $resultVersion); //don't break out; we may need to further upgrade the state } } } return $blockStateData; } private function cloneIfNeeded(CompoundTag $states, int &$stateChanges) : CompoundTag{ if($stateChanges === 0){ $states = clone $states; } $stateChanges++; return $states; } private function applyPropertyAdded(BlockStateUpgradeSchema $schema, string $oldName, CompoundTag $states, int &$stateChanges) : CompoundTag{ $newStates = $states; if(isset($schema->addedProperties[$oldName])){ foreach(Utils::stringifyKeys($schema->addedProperties[$oldName]) as $propertyName => $value){ $oldValue = $states->getTag($propertyName); if($oldValue === null){ $newStates = $this->cloneIfNeeded($newStates, $stateChanges); $newStates->setTag($propertyName, $value); } } } return $newStates; } private function applyPropertyRemoved(BlockStateUpgradeSchema $schema, string $oldName, CompoundTag $states, int &$stateChanges) : CompoundTag{ $newStates = $states; if(isset($schema->removedProperties[$oldName])){ foreach($schema->removedProperties[$oldName] as $propertyName){ if($states->getTag($propertyName) !== null){ $newStates = $this->cloneIfNeeded($newStates, $stateChanges); $newStates->removeTag($propertyName); } } } return $newStates; } private function locateNewPropertyValue(BlockStateUpgradeSchema $schema, string $oldName, string $oldPropertyName, Tag $oldValue) : Tag{ if(isset($schema->remappedPropertyValues[$oldName][$oldPropertyName])){ foreach($schema->remappedPropertyValues[$oldName][$oldPropertyName] as $mappedPair){ if($mappedPair->old->equals($oldValue)){ return $mappedPair->new; } } } return $oldValue; } private function applyPropertyRenamedOrValueChanged(BlockStateUpgradeSchema $schema, string $oldName, CompoundTag $states, int &$stateChanges) : CompoundTag{ if(isset($schema->renamedProperties[$oldName])){ foreach(Utils::stringifyKeys($schema->renamedProperties[$oldName]) as $oldPropertyName => $newPropertyName){ $oldValue = $states->getTag($oldPropertyName); if($oldValue !== null){ $states = $this->cloneIfNeeded($states, $stateChanges); $states->removeTag($oldPropertyName); //If a value remap is needed, we need to do it here, since we won't be able to locate the property //after it's been renamed - value remaps are always indexed by old property name for the sake of //being able to do changes in any order. $states->setTag($newPropertyName, $this->locateNewPropertyValue($schema, $oldName, $oldPropertyName, $oldValue)); } } } return $states; } private function applyPropertyValueChanged(BlockStateUpgradeSchema $schema, string $oldName, CompoundTag $states, int &$stateChanges) : CompoundTag{ if(isset($schema->remappedPropertyValues[$oldName])){ foreach(Utils::stringifyKeys($schema->remappedPropertyValues[$oldName]) as $oldPropertyName => $remappedValues){ $oldValue = $states->getTag($oldPropertyName); if($oldValue !== null){ $newValue = $this->locateNewPropertyValue($schema, $oldName, $oldPropertyName, $oldValue); if($newValue !== $oldValue){ $states = $this->cloneIfNeeded($states, $stateChanges); $states->setTag($oldPropertyName, $newValue); } } } } return $states; } }