Hit block legacy metadata with the biggest nuke you've ever seen

This commit completely revamps the way that blocks are represented in memory at runtime.

Instead of being represented by legacy Mojang block IDs and metadata, which are dated, limited and unchangeable, we now use custom PM block IDs, which are generated from VanillaBlocks.
This means we have full control of how they are assigned, which opens the doors to finally addressing inconsistencies like glazed terracotta, stripped logs handling, etc.

To represent state, BlockDataReader and BlockDataWriter have been introduced, and are used by blocks with state information to pack said information into a binary form that can be stored on a chunk at runtime.
Conceptually it's pretty similar to legacy metadata, but the actual format shares no resemblance whatsoever to legacy metadata, and is fully controlled by PM.
This means that the 'state data' may change in serialization format at any time, so it should **NOT** be stored on disk or in a config.

In the future, this will be improved using more auto-generated code and attributes, instead of hand-baked decodeState() and encodeState(). For now, this opens the gateway to a significant expansion of features.
It's not ideal, but it's a big step forwards.
This commit is contained in:
Dylan K. Taylor
2022-06-24 23:19:37 +01:00
parent be2fe160b3
commit f24f2d9ca9
149 changed files with 2234 additions and 2650 deletions

View File

@ -58,7 +58,7 @@ final class BlockStateToBlockObjectDeserializer implements BlockStateDeserialize
}
public function deserialize(BlockStateData $stateData) : int{
return $this->deserializeBlock($stateData)->getFullId();
return $this->deserializeBlock($stateData)->getStateId();
}
/** @phpstan-param \Closure(Reader) : Block $c */

View File

@ -52,7 +52,7 @@ final class LegacyBlockStateMapper{
return $this->fromStringIdMeta($stringId, $meta);
}
public static function loadFromString(string $data, LegacyBlockIdToStringIdMap $idMap) : self{
public static function loadFromString(string $data, LegacyBlockIdToStringIdMap $idMap, BlockStateUpgrader $blockStateUpgrader) : self{
$mappingTable = [];
$legacyStateMapReader = new BinaryStream($data);
@ -64,7 +64,7 @@ final class LegacyBlockStateMapper{
$offset = $legacyStateMapReader->getOffset();
$state = $nbtReader->read($legacyStateMapReader->getBuffer(), $offset)->mustGetCompoundTag();
$legacyStateMapReader->setOffset($offset);
$mappingTable[$id][$meta] = BlockStateData::fromNbt($state);
$mappingTable[$id][$meta] = $blockStateUpgrader->upgrade(BlockStateData::fromNbt($state));
}
return new self($mappingTable, $idMap);

View File

@ -191,7 +191,7 @@ final class ItemSerializer{
*/
private function standardBlock(Block $block) : Data{
try{
$blockStateData = $this->blockStateSerializer->serialize($block->getFullId());
$blockStateData = $this->blockStateSerializer->serialize($block->getStateId());
}catch(BlockStateSerializeException $e){
throw new ItemTypeSerializeException($e->getMessage(), 0, $e);
}

View File

@ -104,8 +104,6 @@ final class ItemDataUpgrader{
if($blockStateData === null){
throw new SavedDataLoadingException("Expected a blockstate to be associated with this block");
}
//the block data upgrader returns states from 1.18.10, which need to be updated to the current version the usual way
$blockStateData = $this->blockDataUpgrader->getBlockStateUpgrader()->upgrade($blockStateData);
}else{
//probably a standard item
$blockStateData = null;