From 0a3ecfdae9bee568722e393e26ee5b97d07394a0 Mon Sep 17 00:00:00 2001 From: "Dylan K. Taylor" Date: Wed, 25 Jan 2023 18:53:11 +0000 Subject: [PATCH] Clean up terminology around block state IDs and their handling --- src/block/Block.php | 4 ++-- src/block/InfestedStone.php | 2 +- ...Factory.php => RuntimeBlockStateRegistry.php} | 9 ++++++--- src/block/tile/FlowerPot.php | 4 ++-- .../convert/BlockObjectToStateSerializer.php | 4 ++-- src/data/bedrock/item/ItemDeserializer.php | 4 ++-- src/entity/EntityFactory.php | 4 ++-- src/entity/object/FallingBlock.php | 4 ++-- src/item/ItemBlock.php | 4 ++-- src/item/ItemBlockWallOrFloor.php | 6 +++--- src/world/Explosion.php | 6 +++--- src/world/SimpleChunkManager.php | 6 +++--- src/world/World.php | 16 ++++++++-------- src/world/format/Chunk.php | 8 ++++---- src/world/format/SubChunk.php | 4 ++-- src/world/generator/Flat.php | 2 +- src/world/generator/hell/Nether.php | 6 +++--- src/world/generator/normal/Normal.php | 6 +++--- src/world/generator/populator/GroundCover.php | 10 +++++----- src/world/light/BlockLightUpdate.php | 4 ++-- src/world/light/LightPopulationTask.php | 4 ++-- src/world/light/LightUpdate.php | 2 +- src/world/light/SkyLightUpdate.php | 6 +++--- tests/phpunit/block/BlockTest.php | 4 ++-- .../block/regenerate_consistency_check.php | 6 +++--- .../convert/BlockSerializerDeserializerTest.php | 4 ++-- .../item/ItemSerializerDeserializerTest.php | 4 ++-- .../mcpe/convert/RuntimeBlockMappingTest.php | 4 ++-- tests/phpunit/world/format/ChunkTest.php | 6 +++--- tests/phpunit/world/format/SubChunkTest.php | 6 +++--- 30 files changed, 81 insertions(+), 78 deletions(-) rename src/block/{BlockFactory.php => RuntimeBlockStateRegistry.php} (93%) diff --git a/src/block/Block.php b/src/block/Block.php index 2e8faf82f..5bbddb0c0 100644 --- a/src/block/Block.php +++ b/src/block/Block.php @@ -97,7 +97,7 @@ class Block{ * Returns the full blockstate ID of this block. This is a compact way of representing a blockstate used to store * blocks in chunks at runtime. * - * This ID can be used to later obtain a copy of this block using {@link BlockFactory::fromStateId()}. + * This ID can be used to later obtain a copy of this block using {@link RuntimeBlockStateRegistry::fromStateId()}. */ public function getStateId() : int{ return ($this->getTypeId() << self::INTERNAL_STATE_DATA_BITS) | $this->computeStateData(); @@ -216,7 +216,7 @@ class Block{ */ public function writeStateToWorld() : void{ $world = $this->position->getWorld(); - $world->getOrLoadChunkAtPosition($this->position)->setFullBlock($this->position->x & Chunk::COORD_MASK, $this->position->y, $this->position->z & Chunk::COORD_MASK, $this->getStateId()); + $world->getOrLoadChunkAtPosition($this->position)->setBlockStateId($this->position->x & Chunk::COORD_MASK, $this->position->y, $this->position->z & Chunk::COORD_MASK, $this->getStateId()); $tileType = $this->idInfo->getTileClass(); $oldTile = $world->getTile($this->position); diff --git a/src/block/InfestedStone.php b/src/block/InfestedStone.php index eb10b38ad..deaa013fc 100644 --- a/src/block/InfestedStone.php +++ b/src/block/InfestedStone.php @@ -35,7 +35,7 @@ final class InfestedStone extends Opaque{ } public function getImitatedBlock() : Block{ - return BlockFactory::getInstance()->fromStateId($this->imitated); + return RuntimeBlockStateRegistry::getInstance()->fromStateId($this->imitated); } public function getDropsForCompatibleTool(Item $item) : array{ diff --git a/src/block/BlockFactory.php b/src/block/RuntimeBlockStateRegistry.php similarity index 93% rename from src/block/BlockFactory.php rename to src/block/RuntimeBlockStateRegistry.php index 9c17e0c45..5aef7006f 100644 --- a/src/block/BlockFactory.php +++ b/src/block/RuntimeBlockStateRegistry.php @@ -32,10 +32,13 @@ use pocketmine\world\light\LightUpdate; use function min; /** - * Manages deserializing block types from their legacy blockIDs and metadata. - * This is primarily needed for loading chunks from disk. + * Blocks are stored as state IDs in chunks at runtime (it would waste far too much memory to represent every block as + * an object). This class maps block state IDs to their corresponding block objects when reading blocks from chunks at + * runtime. + * + * @internal Plugin devs shouldn't need to interact with this class at all, unless registering a new block type. */ -class BlockFactory{ +class RuntimeBlockStateRegistry{ use SingletonTrait; /** diff --git a/src/block/tile/FlowerPot.php b/src/block/tile/FlowerPot.php index 9ef15053a..d6c2c58fb 100644 --- a/src/block/tile/FlowerPot.php +++ b/src/block/tile/FlowerPot.php @@ -25,7 +25,7 @@ namespace pocketmine\block\tile; use pocketmine\block\Air; use pocketmine\block\Block; -use pocketmine\block\BlockFactory; +use pocketmine\block\RuntimeBlockStateRegistry; use pocketmine\data\bedrock\block\BlockStateDeserializeException; use pocketmine\data\bedrock\block\BlockStateNames; use pocketmine\data\SavedDataLoadingException; @@ -67,7 +67,7 @@ class FlowerPot extends Spawnable{ }catch(BlockStateDeserializeException $e){ throw new SavedDataLoadingException("Error deserializing plant for flower pot: " . $e->getMessage(), 0, $e); } - $this->setPlant(BlockFactory::getInstance()->fromStateId($blockStateId)); + $this->setPlant(RuntimeBlockStateRegistry::getInstance()->fromStateId($blockStateId)); } } diff --git a/src/data/bedrock/block/convert/BlockObjectToStateSerializer.php b/src/data/bedrock/block/convert/BlockObjectToStateSerializer.php index 04c56e536..6e7497c0d 100644 --- a/src/data/bedrock/block/convert/BlockObjectToStateSerializer.php +++ b/src/data/bedrock/block/convert/BlockObjectToStateSerializer.php @@ -32,7 +32,6 @@ use pocketmine\block\Bed; use pocketmine\block\Beetroot; use pocketmine\block\Bell; use pocketmine\block\Block; -use pocketmine\block\BlockFactory; use pocketmine\block\BoneBlock; use pocketmine\block\BrewingStand; use pocketmine\block\BrownMushroomBlock; @@ -107,6 +106,7 @@ use pocketmine\block\RedstoneOre; use pocketmine\block\RedstoneRepeater; use pocketmine\block\RedstoneTorch; use pocketmine\block\RedstoneWire; +use pocketmine\block\RuntimeBlockStateRegistry; use pocketmine\block\Sapling; use pocketmine\block\SeaPickle; use pocketmine\block\SimplePillar; @@ -197,7 +197,7 @@ final class BlockObjectToStateSerializer implements BlockStateSerializer{ public function serialize(int $stateId) : BlockStateData{ //TODO: singleton usage not ideal //TODO: we may want to deduplicate cache entries to avoid wasting memory - return $this->cache[$stateId] ??= $this->serializeBlock(BlockFactory::getInstance()->fromStateId($stateId)); + return $this->cache[$stateId] ??= $this->serializeBlock(RuntimeBlockStateRegistry::getInstance()->fromStateId($stateId)); } /** diff --git a/src/data/bedrock/item/ItemDeserializer.php b/src/data/bedrock/item/ItemDeserializer.php index bde907d4e..f4f43e3cb 100644 --- a/src/data/bedrock/item/ItemDeserializer.php +++ b/src/data/bedrock/item/ItemDeserializer.php @@ -24,7 +24,7 @@ declare(strict_types=1); namespace pocketmine\data\bedrock\item; use pocketmine\block\Block; -use pocketmine\block\BlockFactory; +use pocketmine\block\RuntimeBlockStateRegistry; use pocketmine\data\bedrock\block\BlockStateDeserializeException; use pocketmine\data\bedrock\block\BlockStateDeserializer; use pocketmine\data\bedrock\block\convert\UnsupportedBlockStateException; @@ -76,7 +76,7 @@ final class ItemDeserializer{ } //TODO: worth caching this or not? - return BlockFactory::getInstance()->fromStateId($block)->asItem(); + return RuntimeBlockStateRegistry::getInstance()->fromStateId($block)->asItem(); } $id = $data->getName(); if(!isset($this->deserializers[$id])){ diff --git a/src/entity/EntityFactory.php b/src/entity/EntityFactory.php index 0a6839cc1..d8d189cff 100644 --- a/src/entity/EntityFactory.php +++ b/src/entity/EntityFactory.php @@ -26,7 +26,7 @@ namespace pocketmine\entity; use DaveRandom\CallbackValidator\CallbackType; use DaveRandom\CallbackValidator\ParameterType; use DaveRandom\CallbackValidator\ReturnType; -use pocketmine\block\BlockFactory; +use pocketmine\block\RuntimeBlockStateRegistry; use pocketmine\data\bedrock\LegacyEntityIdToStringIdMap; use pocketmine\data\bedrock\PotionTypeIdMap; use pocketmine\data\bedrock\PotionTypeIds; @@ -112,7 +112,7 @@ final class EntityFactory{ }, ['XPOrb', 'minecraft:xp_orb']); $this->register(FallingBlock::class, function(World $world, CompoundTag $nbt) : FallingBlock{ - return new FallingBlock(Helper::parseLocation($nbt, $world), FallingBlock::parseBlockNBT(BlockFactory::getInstance(), $nbt), $nbt); + return new FallingBlock(Helper::parseLocation($nbt, $world), FallingBlock::parseBlockNBT(RuntimeBlockStateRegistry::getInstance(), $nbt), $nbt); }, ['FallingSand', 'minecraft:falling_block']); $this->register(ItemEntity::class, function(World $world, CompoundTag $nbt) : ItemEntity{ diff --git a/src/entity/object/FallingBlock.php b/src/entity/object/FallingBlock.php index 15b7aeac9..301d38b64 100644 --- a/src/entity/object/FallingBlock.php +++ b/src/entity/object/FallingBlock.php @@ -24,7 +24,7 @@ declare(strict_types=1); namespace pocketmine\entity\object; use pocketmine\block\Block; -use pocketmine\block\BlockFactory; +use pocketmine\block\RuntimeBlockStateRegistry; use pocketmine\block\utils\Fallable; use pocketmine\data\bedrock\block\BlockStateDeserializeException; use pocketmine\data\SavedDataLoadingException; @@ -66,7 +66,7 @@ class FallingBlock extends Entity{ protected function getInitialGravity() : float{ return 0.04; } - public static function parseBlockNBT(BlockFactory $factory, CompoundTag $nbt) : Block{ + public static function parseBlockNBT(RuntimeBlockStateRegistry $factory, CompoundTag $nbt) : Block{ //TODO: 1.8+ save format $blockDataUpgrader = GlobalBlockStateHandlers::getUpgrader(); diff --git a/src/item/ItemBlock.php b/src/item/ItemBlock.php index eb476de88..1a005286d 100644 --- a/src/item/ItemBlock.php +++ b/src/item/ItemBlock.php @@ -24,7 +24,7 @@ declare(strict_types=1); namespace pocketmine\item; use pocketmine\block\Block; -use pocketmine\block\BlockFactory; +use pocketmine\block\RuntimeBlockStateRegistry; use pocketmine\block\VanillaBlocks; use pocketmine\data\runtime\RuntimeDataReader; use pocketmine\data\runtime\RuntimeDataWriter; @@ -59,7 +59,7 @@ final class ItemBlock extends Item{ public function getBlock(?int $clickedFace = null) : Block{ //TODO: HACKY MESS, CLEAN IT UP - $factory = BlockFactory::getInstance(); + $factory = RuntimeBlockStateRegistry::getInstance(); if(!$factory->isRegistered($this->blockTypeId)){ return VanillaBlocks::AIR(); } diff --git a/src/item/ItemBlockWallOrFloor.php b/src/item/ItemBlockWallOrFloor.php index bd1213b34..c20c73a1d 100644 --- a/src/item/ItemBlockWallOrFloor.php +++ b/src/item/ItemBlockWallOrFloor.php @@ -24,7 +24,7 @@ declare(strict_types=1); namespace pocketmine\item; use pocketmine\block\Block; -use pocketmine\block\BlockFactory; +use pocketmine\block\RuntimeBlockStateRegistry; use pocketmine\math\Axis; use pocketmine\math\Facing; @@ -40,9 +40,9 @@ class ItemBlockWallOrFloor extends Item{ public function getBlock(?int $clickedFace = null) : Block{ if($clickedFace !== null && Facing::axis($clickedFace) !== Axis::Y){ - return BlockFactory::getInstance()->fromStateId($this->wallVariant); + return RuntimeBlockStateRegistry::getInstance()->fromStateId($this->wallVariant); } - return BlockFactory::getInstance()->fromStateId($this->floorVariant); + return RuntimeBlockStateRegistry::getInstance()->fromStateId($this->floorVariant); } public function getFuelTime() : int{ diff --git a/src/world/Explosion.php b/src/world/Explosion.php index 6bd72fb6f..acc806f99 100644 --- a/src/world/Explosion.php +++ b/src/world/Explosion.php @@ -24,7 +24,7 @@ declare(strict_types=1); namespace pocketmine\world; use pocketmine\block\Block; -use pocketmine\block\BlockFactory; +use pocketmine\block\RuntimeBlockStateRegistry; use pocketmine\block\TNT; use pocketmine\block\VanillaBlocks; use pocketmine\entity\Entity; @@ -81,7 +81,7 @@ class Explosion{ return false; } - $blockFactory = BlockFactory::getInstance(); + $blockFactory = RuntimeBlockStateRegistry::getInstance(); $mRays = $this->rays - 1; for($i = 0; $i < $this->rays; ++$i){ @@ -112,7 +112,7 @@ class Explosion{ continue; } - $state = $this->subChunkExplorer->currentSubChunk->getFullBlock($vBlockX & SubChunk::COORD_MASK, $vBlockY & SubChunk::COORD_MASK, $vBlockZ & SubChunk::COORD_MASK); + $state = $this->subChunkExplorer->currentSubChunk->getBlockStateId($vBlockX & SubChunk::COORD_MASK, $vBlockY & SubChunk::COORD_MASK, $vBlockZ & SubChunk::COORD_MASK); $blastResistance = $blockFactory->blastResistance[$state] ?? 0; if($blastResistance >= 0){ diff --git a/src/world/SimpleChunkManager.php b/src/world/SimpleChunkManager.php index 85849c450..221adbe89 100644 --- a/src/world/SimpleChunkManager.php +++ b/src/world/SimpleChunkManager.php @@ -24,7 +24,7 @@ declare(strict_types=1); namespace pocketmine\world; use pocketmine\block\Block; -use pocketmine\block\BlockFactory; +use pocketmine\block\RuntimeBlockStateRegistry; use pocketmine\block\VanillaBlocks; use pocketmine\utils\Limits; use pocketmine\world\format\Chunk; @@ -41,14 +41,14 @@ class SimpleChunkManager implements ChunkManager{ public function getBlockAt(int $x, int $y, int $z) : Block{ if($this->isInWorld($x, $y, $z) && ($chunk = $this->getChunk($x >> Chunk::COORD_BIT_SIZE, $z >> Chunk::COORD_BIT_SIZE)) !== null){ - return BlockFactory::getInstance()->fromStateId($chunk->getFullBlock($x & Chunk::COORD_MASK, $y, $z & Chunk::COORD_MASK)); + return RuntimeBlockStateRegistry::getInstance()->fromStateId($chunk->getBlockStateId($x & Chunk::COORD_MASK, $y, $z & Chunk::COORD_MASK)); } return VanillaBlocks::AIR(); } public function setBlockAt(int $x, int $y, int $z, Block $block) : void{ if(($chunk = $this->getChunk($x >> Chunk::COORD_BIT_SIZE, $z >> Chunk::COORD_BIT_SIZE)) !== null){ - $chunk->setFullBlock($x & Chunk::COORD_MASK, $y, $z & Chunk::COORD_MASK, $block->getStateId()); + $chunk->setBlockStateId($x & Chunk::COORD_MASK, $y, $z & Chunk::COORD_MASK, $block->getStateId()); }else{ throw new \InvalidArgumentException("Cannot set block at coordinates x=$x,y=$y,z=$z, terrain is not loaded or out of bounds"); } diff --git a/src/world/World.php b/src/world/World.php index e6f240614..15ea9f787 100644 --- a/src/world/World.php +++ b/src/world/World.php @@ -28,8 +28,8 @@ namespace pocketmine\world; use pocketmine\block\Air; use pocketmine\block\Block; -use pocketmine\block\BlockFactory; use pocketmine\block\BlockTypeIds; +use pocketmine\block\RuntimeBlockStateRegistry; use pocketmine\block\tile\Spawnable; use pocketmine\block\tile\Tile; use pocketmine\block\tile\TileFactory; @@ -533,7 +533,7 @@ class World implements ChunkManager{ if($blockStateData === null){ continue; } - $block = BlockFactory::getInstance()->fromStateId(GlobalBlockStateHandlers::getDeserializer()->deserialize($blockStateData)); + $block = RuntimeBlockStateRegistry::getInstance()->fromStateId(GlobalBlockStateHandlers::getDeserializer()->deserialize($blockStateData)); }else{ //TODO: we probably ought to log an error here continue; @@ -544,7 +544,7 @@ class World implements ChunkManager{ } } - foreach(BlockFactory::getInstance()->getAllKnownStates() as $state){ + foreach(RuntimeBlockStateRegistry::getInstance()->getAllKnownStates() as $state){ $dontTickName = $dontTickBlocks[$state->getTypeId()] ?? null; if($dontTickName === null && $state->ticksRandomly()){ $this->randomTickBlocks[$state->getStateId()] = true; @@ -1295,7 +1295,7 @@ class World implements ChunkManager{ $entity->onRandomUpdate(); } - $blockFactory = BlockFactory::getInstance(); + $blockFactory = RuntimeBlockStateRegistry::getInstance(); foreach($chunk->getSubChunks() as $Y => $subChunk){ if(!$subChunk->isEmptyFast()){ $k = 0; @@ -1309,7 +1309,7 @@ class World implements ChunkManager{ $z = ($k >> (SubChunk::COORD_BIT_SIZE * 2)) & SubChunk::COORD_MASK; $k >>= (SubChunk::COORD_BIT_SIZE * 3); - $state = $subChunk->getFullBlock($x, $y, $z); + $state = $subChunk->getBlockStateId($x, $y, $z); if(isset($this->randomTickBlocks[$state])){ $block = $blockFactory->fromStateId($state); @@ -1609,7 +1609,7 @@ class World implements ChunkManager{ return; } - $blockFactory = BlockFactory::getInstance(); + $blockFactory = RuntimeBlockStateRegistry::getInstance(); $this->timings->doBlockSkyLightUpdates->startTiming(); if($this->skyLightUpdate === null){ $this->skyLightUpdate = new SkyLightUpdate(new SubChunkExplorer($this), $blockFactory->lightFilter, $blockFactory->blocksDirectSkyLight); @@ -1732,7 +1732,7 @@ class World implements ChunkManager{ $chunk = $this->chunks[$chunkHash] ?? null; if($chunk !== null){ - $block = BlockFactory::getInstance()->fromStateId($chunk->getFullBlock($x & Chunk::COORD_MASK, $y, $z & Chunk::COORD_MASK)); + $block = RuntimeBlockStateRegistry::getInstance()->fromStateId($chunk->getBlockStateId($x & Chunk::COORD_MASK, $y, $z & Chunk::COORD_MASK)); }else{ $addToCache = false; $block = VanillaBlocks::AIR(); @@ -2371,7 +2371,7 @@ class World implements ChunkManager{ $localY = $tilePosition->getFloorY(); $localZ = $tilePosition->getFloorZ() & Chunk::COORD_MASK; - $newBlock = BlockFactory::getInstance()->fromStateId($chunk->getFullBlock($localX, $localY, $localZ)); + $newBlock = RuntimeBlockStateRegistry::getInstance()->fromStateId($chunk->getBlockStateId($localX, $localY, $localZ)); $expectedTileClass = $newBlock->getIdInfo()->getTileClass(); if( $expectedTileClass === null || //new block doesn't expect a tile diff --git a/src/world/format/Chunk.php b/src/world/format/Chunk.php index 7c55b6292..fd0b83a7b 100644 --- a/src/world/format/Chunk.php +++ b/src/world/format/Chunk.php @@ -93,15 +93,15 @@ class Chunk{ * * @return int bitmap, (id << 4) | meta */ - public function getFullBlock(int $x, int $y, int $z) : int{ - return $this->getSubChunk($y >> SubChunk::COORD_BIT_SIZE)->getFullBlock($x, $y & SubChunk::COORD_MASK, $z); + public function getBlockStateId(int $x, int $y, int $z) : int{ + return $this->getSubChunk($y >> SubChunk::COORD_BIT_SIZE)->getBlockStateId($x, $y & SubChunk::COORD_MASK, $z); } /** * Sets the blockstate at the given coordinate by internal ID. */ - public function setFullBlock(int $x, int $y, int $z, int $block) : void{ - $this->getSubChunk($y >> SubChunk::COORD_BIT_SIZE)->setFullBlock($x, $y & SubChunk::COORD_MASK, $z, $block); + public function setBlockStateId(int $x, int $y, int $z, int $block) : void{ + $this->getSubChunk($y >> SubChunk::COORD_BIT_SIZE)->setBlockStateId($x, $y & SubChunk::COORD_MASK, $z, $block); $this->terrainDirtyFlags |= self::DIRTY_FLAG_BLOCKS; } diff --git a/src/world/format/SubChunk.php b/src/world/format/SubChunk.php index 5dc221514..3f7e943e3 100644 --- a/src/world/format/SubChunk.php +++ b/src/world/format/SubChunk.php @@ -69,14 +69,14 @@ class SubChunk{ */ public function getEmptyBlockId() : int{ return $this->emptyBlockId; } - public function getFullBlock(int $x, int $y, int $z) : int{ + public function getBlockStateId(int $x, int $y, int $z) : int{ if(count($this->blockLayers) === 0){ return $this->emptyBlockId; } return $this->blockLayers[0]->get($x, $y, $z); } - public function setFullBlock(int $x, int $y, int $z, int $block) : void{ + public function setBlockStateId(int $x, int $y, int $z, int $block) : void{ if(count($this->blockLayers) === 0){ $this->blockLayers[] = new PalettedBlockArray($this->emptyBlockId); } diff --git a/src/world/generator/Flat.php b/src/world/generator/Flat.php index 9536cde03..01efaff07 100644 --- a/src/world/generator/Flat.php +++ b/src/world/generator/Flat.php @@ -77,7 +77,7 @@ class Flat extends Generator{ for($Z = 0; $Z < SubChunk::EDGE_LENGTH; ++$Z){ for($X = 0; $X < SubChunk::EDGE_LENGTH; ++$X){ - $subchunk->setFullBlock($X, $y, $Z, $id); + $subchunk->setBlockStateId($X, $y, $Z, $id); } } } diff --git a/src/world/generator/hell/Nether.php b/src/world/generator/hell/Nether.php index e00b22015..665d9452e 100644 --- a/src/world/generator/hell/Nether.php +++ b/src/world/generator/hell/Nether.php @@ -85,16 +85,16 @@ class Nether extends Generator{ for($y = 0; $y < 128; ++$y){ if($y === 0 || $y === 127){ - $chunk->setFullBlock($x, $y, $z, $bedrock); + $chunk->setBlockStateId($x, $y, $z, $bedrock); continue; } $noiseValue = (abs($this->emptyHeight - $y) / $this->emptyHeight) * $this->emptyAmplitude - $noise[$x][$z][$y]; $noiseValue -= 1 - $this->density; if($noiseValue > 0){ - $chunk->setFullBlock($x, $y, $z, $netherrack); + $chunk->setBlockStateId($x, $y, $z, $netherrack); }elseif($y <= $this->waterHeight){ - $chunk->setFullBlock($x, $y, $z, $stillLava); + $chunk->setBlockStateId($x, $y, $z, $stillLava); } } } diff --git a/src/world/generator/normal/Normal.php b/src/world/generator/normal/Normal.php index 107d147e9..a750fc894 100644 --- a/src/world/generator/normal/Normal.php +++ b/src/world/generator/normal/Normal.php @@ -194,15 +194,15 @@ class Normal extends Generator{ for($y = 0; $y < 128; ++$y){ if($y === 0){ - $chunk->setFullBlock($x, $y, $z, $bedrock); + $chunk->setBlockStateId($x, $y, $z, $bedrock); continue; } $noiseValue = $noise[$x][$z][$y] - 1 / $smoothHeight * ($y - $smoothHeight - $minSum); if($noiseValue > 0){ - $chunk->setFullBlock($x, $y, $z, $stone); + $chunk->setBlockStateId($x, $y, $z, $stone); }elseif($y <= $this->waterHeight){ - $chunk->setFullBlock($x, $y, $z, $stillWater); + $chunk->setBlockStateId($x, $y, $z, $stillWater); } } } diff --git a/src/world/generator/populator/GroundCover.php b/src/world/generator/populator/GroundCover.php index 16a1d95a0..1ce0c9fca 100644 --- a/src/world/generator/populator/GroundCover.php +++ b/src/world/generator/populator/GroundCover.php @@ -23,9 +23,9 @@ declare(strict_types=1); namespace pocketmine\world\generator\populator; -use pocketmine\block\BlockFactory; use pocketmine\block\BlockTypeIds; use pocketmine\block\Liquid; +use pocketmine\block\RuntimeBlockStateRegistry; use pocketmine\utils\Random; use pocketmine\world\biome\BiomeRegistry; use pocketmine\world\ChunkManager; @@ -37,7 +37,7 @@ class GroundCover implements Populator{ public function populate(ChunkManager $world, int $chunkX, int $chunkZ, Random $random) : void{ $chunk = $world->getChunk($chunkX, $chunkZ); - $factory = BlockFactory::getInstance(); + $factory = RuntimeBlockStateRegistry::getInstance(); $biomeRegistry = BiomeRegistry::getInstance(); for($x = 0; $x < Chunk::EDGE_LENGTH; ++$x){ for($z = 0; $z < Chunk::EDGE_LENGTH; ++$z){ @@ -51,7 +51,7 @@ class GroundCover implements Populator{ $startY = 127; for(; $startY > 0; --$startY){ - if(!$factory->fromStateId($chunk->getFullBlock($x, $startY, $z))->isTransparent()){ + if(!$factory->fromStateId($chunk->getBlockStateId($x, $startY, $z))->isTransparent()){ break; } } @@ -59,7 +59,7 @@ class GroundCover implements Populator{ $endY = $startY - count($cover); for($y = $startY; $y > $endY && $y >= 0; --$y){ $b = $cover[$startY - $y]; - $id = $factory->fromStateId($chunk->getFullBlock($x, $y, $z)); + $id = $factory->fromStateId($chunk->getBlockStateId($x, $y, $z)); if($id->getTypeId() === BlockTypeIds::AIR && $b->isSolid()){ break; } @@ -67,7 +67,7 @@ class GroundCover implements Populator{ continue; } - $chunk->setFullBlock($x, $y, $z, $b->getStateId()); + $chunk->setBlockStateId($x, $y, $z, $b->getStateId()); } } } diff --git a/src/world/light/BlockLightUpdate.php b/src/world/light/BlockLightUpdate.php index cc8964fbe..b7e0aa9e3 100644 --- a/src/world/light/BlockLightUpdate.php +++ b/src/world/light/BlockLightUpdate.php @@ -50,7 +50,7 @@ class BlockLightUpdate extends LightUpdate{ public function recalculateNode(int $x, int $y, int $z) : void{ if($this->subChunkExplorer->moveTo($x, $y, $z) !== SubChunkExplorerStatus::INVALID){ - $block = $this->subChunkExplorer->currentSubChunk->getFullBlock($x & SubChunk::COORD_MASK, $y & SubChunk::COORD_MASK, $z & SubChunk::COORD_MASK); + $block = $this->subChunkExplorer->currentSubChunk->getBlockStateId($x & SubChunk::COORD_MASK, $y & SubChunk::COORD_MASK, $z & SubChunk::COORD_MASK); $this->setAndUpdateLight($x, $y, $z, max($this->lightEmitters[$block] ?? 0, $this->getHighestAdjacentLight($x, $y, $z) - ($this->lightFilters[$block] ?? self::BASE_LIGHT_FILTER))); } } @@ -83,7 +83,7 @@ class BlockLightUpdate extends LightUpdate{ for($x = 0; $x < SubChunk::EDGE_LENGTH; ++$x){ for($z = 0; $z < SubChunk::EDGE_LENGTH; ++$z){ for($y = 0; $y < SubChunk::EDGE_LENGTH; ++$y){ - $light = $this->lightEmitters[$subChunk->getFullBlock($x, $y, $z)] ?? 0; + $light = $this->lightEmitters[$subChunk->getBlockStateId($x, $y, $z)] ?? 0; if($light > 0){ $this->setAndUpdateLight( $baseX + $x, diff --git a/src/world/light/LightPopulationTask.php b/src/world/light/LightPopulationTask.php index 2acccbf8f..5aa0ead65 100644 --- a/src/world/light/LightPopulationTask.php +++ b/src/world/light/LightPopulationTask.php @@ -23,7 +23,7 @@ declare(strict_types=1); namespace pocketmine\world\light; -use pocketmine\block\BlockFactory; +use pocketmine\block\RuntimeBlockStateRegistry; use pocketmine\scheduler\AsyncTask; use pocketmine\world\format\Chunk; use pocketmine\world\format\io\FastChunkSerializer; @@ -57,7 +57,7 @@ class LightPopulationTask extends AsyncTask{ $manager = new SimpleChunkManager(World::Y_MIN, World::Y_MAX); $manager->setChunk(0, 0, $chunk); - $blockFactory = BlockFactory::getInstance(); + $blockFactory = RuntimeBlockStateRegistry::getInstance(); foreach([ "Block" => new BlockLightUpdate(new SubChunkExplorer($manager), $blockFactory->lightFilter, $blockFactory->light), "Sky" => new SkyLightUpdate(new SubChunkExplorer($manager), $blockFactory->lightFilter, $blockFactory->blocksDirectSkyLight), diff --git a/src/world/light/LightUpdate.php b/src/world/light/LightUpdate.php index 83c99e5a4..d40e68e05 100644 --- a/src/world/light/LightUpdate.php +++ b/src/world/light/LightUpdate.php @@ -191,7 +191,7 @@ abstract class LightUpdate{ $ly = $y & SubChunk::COORD_MASK; $lz = $z & SubChunk::COORD_MASK; $current = $lightArray->get($lx, $ly, $lz); - $potentialLight = $newAdjacentLevel - ($this->lightFilters[$this->subChunkExplorer->currentSubChunk->getFullBlock($lx, $ly, $lz)] ?? self::BASE_LIGHT_FILTER); + $potentialLight = $newAdjacentLevel - ($this->lightFilters[$this->subChunkExplorer->currentSubChunk->getBlockStateId($lx, $ly, $lz)] ?? self::BASE_LIGHT_FILTER); if($current < $potentialLight){ $lightArray->set($lx, $ly, $lz, $potentialLight); diff --git a/src/world/light/SkyLightUpdate.php b/src/world/light/SkyLightUpdate.php index d77f1de5b..501563482 100644 --- a/src/world/light/SkyLightUpdate.php +++ b/src/world/light/SkyLightUpdate.php @@ -66,7 +66,7 @@ class SkyLightUpdate extends LightUpdate{ $chunk = $this->subChunkExplorer->currentChunk; $oldHeightMap = $chunk->getHeightMap($x & Chunk::COORD_MASK, $z & Chunk::COORD_MASK); - $source = $this->subChunkExplorer->currentSubChunk->getFullBlock($x & SubChunk::COORD_MASK, $y & SubChunk::COORD_MASK, $z & SubChunk::COORD_MASK); + $source = $this->subChunkExplorer->currentSubChunk->getBlockStateId($x & SubChunk::COORD_MASK, $y & SubChunk::COORD_MASK, $z & SubChunk::COORD_MASK); $yPlusOne = $y + 1; @@ -194,7 +194,7 @@ class SkyLightUpdate extends LightUpdate{ $result->set($x, $z, World::Y_MIN); }else{ for(; $y >= World::Y_MIN; --$y){ - if(isset($directSkyLightBlockers[$chunk->getFullBlock($x, $y, $z)])){ + if(isset($directSkyLightBlockers[$chunk->getBlockStateId($x, $y, $z)])){ $result->set($x, $z, $y + 1); break; } @@ -221,7 +221,7 @@ class SkyLightUpdate extends LightUpdate{ return World::Y_MIN; } for(; $y >= World::Y_MIN; --$y){ - if(isset($directSkyLightBlockers[$chunk->getFullBlock($x, $y, $z)])){ + if(isset($directSkyLightBlockers[$chunk->getBlockStateId($x, $y, $z)])){ break; } } diff --git a/tests/phpunit/block/BlockTest.php b/tests/phpunit/block/BlockTest.php index 7404b15e3..3f6dbac95 100644 --- a/tests/phpunit/block/BlockTest.php +++ b/tests/phpunit/block/BlockTest.php @@ -33,11 +33,11 @@ use const SORT_STRING; class BlockTest extends TestCase{ - /** @var BlockFactory */ + /** @var RuntimeBlockStateRegistry */ private $blockFactory; public function setUp() : void{ - $this->blockFactory = new BlockFactory(); + $this->blockFactory = new RuntimeBlockStateRegistry(); } /** diff --git a/tests/phpunit/block/regenerate_consistency_check.php b/tests/phpunit/block/regenerate_consistency_check.php index 0db6d3382..9930c54f0 100644 --- a/tests/phpunit/block/regenerate_consistency_check.php +++ b/tests/phpunit/block/regenerate_consistency_check.php @@ -22,17 +22,17 @@ declare(strict_types=1); use pocketmine\block\Block; -use pocketmine\block\BlockFactory; +use pocketmine\block\RuntimeBlockStateRegistry; use pocketmine\utils\AssumptionFailedError; require dirname(__DIR__, 3) . '/vendor/autoload.php'; /* This script needs to be re-run after any intentional blockfactory change (adding or removing a block state). */ -$factory = new \pocketmine\block\BlockFactory(); +$factory = new \pocketmine\block\RuntimeBlockStateRegistry(); $remaps = []; $new = []; -foreach(BlockFactory::getInstance()->getAllKnownStates() as $index => $block){ +foreach(RuntimeBlockStateRegistry::getInstance()->getAllKnownStates() as $index => $block){ if($index !== $block->getStateId()){ throw new AssumptionFailedError("State index should always match state ID"); } diff --git a/tests/phpunit/data/bedrock/block/convert/BlockSerializerDeserializerTest.php b/tests/phpunit/data/bedrock/block/convert/BlockSerializerDeserializerTest.php index f69b4d010..71d4135ea 100644 --- a/tests/phpunit/data/bedrock/block/convert/BlockSerializerDeserializerTest.php +++ b/tests/phpunit/data/bedrock/block/convert/BlockSerializerDeserializerTest.php @@ -26,8 +26,8 @@ namespace pocketmine\data\bedrock\block\convert; use PHPUnit\Framework\TestCase; use pocketmine\block\BaseBanner; use pocketmine\block\Bed; -use pocketmine\block\BlockFactory; use pocketmine\block\BlockTypeIds; +use pocketmine\block\RuntimeBlockStateRegistry; use pocketmine\block\Skull; use pocketmine\data\bedrock\block\BlockStateDeserializeException; use pocketmine\data\bedrock\block\BlockStateSerializeException; @@ -43,7 +43,7 @@ final class BlockSerializerDeserializerTest extends TestCase{ } public function testAllKnownBlockStatesSerializableAndDeserializable() : void{ - foreach(BlockFactory::getInstance()->getAllKnownStates() as $block){ + foreach(RuntimeBlockStateRegistry::getInstance()->getAllKnownStates() as $block){ try{ $blockStateData = $this->serializer->serializeBlock($block); }catch(BlockStateSerializeException $e){ diff --git a/tests/phpunit/data/bedrock/item/ItemSerializerDeserializerTest.php b/tests/phpunit/data/bedrock/item/ItemSerializerDeserializerTest.php index a7617735c..b3945d0f6 100644 --- a/tests/phpunit/data/bedrock/item/ItemSerializerDeserializerTest.php +++ b/tests/phpunit/data/bedrock/item/ItemSerializerDeserializerTest.php @@ -24,7 +24,7 @@ declare(strict_types=1); namespace pocketmine\data\bedrock\item; use PHPUnit\Framework\TestCase; -use pocketmine\block\BlockFactory; +use pocketmine\block\RuntimeBlockStateRegistry; use pocketmine\item\VanillaItems; use pocketmine\world\format\io\GlobalBlockStateHandlers; @@ -60,7 +60,7 @@ final class ItemSerializerDeserializerTest extends TestCase{ } public function testAllVanillaBlocksSerializableAndDeserializable() : void{ - foreach(BlockFactory::getInstance()->getAllKnownStates() as $block){ + foreach(RuntimeBlockStateRegistry::getInstance()->getAllKnownStates() as $block){ $item = $block->asItem(); if($item->isNull()){ continue; diff --git a/tests/phpunit/network/mcpe/convert/RuntimeBlockMappingTest.php b/tests/phpunit/network/mcpe/convert/RuntimeBlockMappingTest.php index 581735503..9e7287da2 100644 --- a/tests/phpunit/network/mcpe/convert/RuntimeBlockMappingTest.php +++ b/tests/phpunit/network/mcpe/convert/RuntimeBlockMappingTest.php @@ -24,7 +24,7 @@ declare(strict_types=1); namespace pocketmine\network\mcpe\convert; use PHPUnit\Framework\TestCase; -use pocketmine\block\BlockFactory; +use pocketmine\block\RuntimeBlockStateRegistry; class RuntimeBlockMappingTest extends TestCase{ @@ -32,7 +32,7 @@ class RuntimeBlockMappingTest extends TestCase{ * @doesNotPerformAssertions */ public function testAllBlockStatesSerialize() : void{ - foreach(BlockFactory::getInstance()->getAllKnownStates() as $state){ + foreach(RuntimeBlockStateRegistry::getInstance()->getAllKnownStates() as $state){ RuntimeBlockMapping::getInstance()->toRuntimeId($state->getStateId()); } } diff --git a/tests/phpunit/world/format/ChunkTest.php b/tests/phpunit/world/format/ChunkTest.php index a8cca71b2..d612ce9d2 100644 --- a/tests/phpunit/world/format/ChunkTest.php +++ b/tests/phpunit/world/format/ChunkTest.php @@ -29,16 +29,16 @@ class ChunkTest extends TestCase{ public function testClone() : void{ $chunk = new Chunk([], false); - $chunk->setFullBlock(0, 0, 0, 1); + $chunk->setBlockStateId(0, 0, 0, 1); $chunk->setBiomeId(0, 0, 0, 1); $chunk->setHeightMap(0, 0, 1); $chunk2 = clone $chunk; - $chunk2->setFullBlock(0, 0, 0, 2); + $chunk2->setBlockStateId(0, 0, 0, 2); $chunk2->setBiomeId(0, 0, 0, 2); $chunk2->setHeightMap(0, 0, 2); - self::assertNotSame($chunk->getFullBlock(0, 0, 0), $chunk2->getFullBlock(0, 0, 0)); + self::assertNotSame($chunk->getBlockStateId(0, 0, 0), $chunk2->getBlockStateId(0, 0, 0)); self::assertNotSame($chunk->getBiomeId(0, 0, 0), $chunk2->getBiomeId(0, 0, 0)); self::assertNotSame($chunk->getHeightMap(0, 0), $chunk2->getHeightMap(0, 0)); } diff --git a/tests/phpunit/world/format/SubChunkTest.php b/tests/phpunit/world/format/SubChunkTest.php index 3b7861051..cdb440147 100644 --- a/tests/phpunit/world/format/SubChunkTest.php +++ b/tests/phpunit/world/format/SubChunkTest.php @@ -34,17 +34,17 @@ class SubChunkTest extends TestCase{ public function testClone() : void{ $sub1 = new SubChunk(0, [], new PalettedBlockArray(BiomeIds::OCEAN)); - $sub1->setFullBlock(0, 0, 0, 1); + $sub1->setBlockStateId(0, 0, 0, 1); $sub1->getBlockLightArray()->set(0, 0, 0, 1); $sub1->getBlockSkyLightArray()->set(0, 0, 0, 1); $sub2 = clone $sub1; - $sub2->setFullBlock(0, 0, 0, 2); + $sub2->setBlockStateId(0, 0, 0, 2); $sub2->getBlockLightArray()->set(0, 0, 0, 2); $sub2->getBlockSkyLightArray()->set(0, 0, 0, 2); - self::assertNotSame($sub1->getFullBlock(0, 0, 0), $sub2->getFullBlock(0, 0, 0)); + self::assertNotSame($sub1->getBlockStateId(0, 0, 0), $sub2->getBlockStateId(0, 0, 0)); self::assertNotSame($sub1->getBlockLightArray()->get(0, 0, 0), $sub2->getBlockLightArray()->get(0, 0, 0)); self::assertNotSame($sub1->getBlockSkyLightArray()->get(0, 0, 0), $sub2->getBlockSkyLightArray()->get(0, 0, 0)); }