mirror of
https://github.com/pmmp/PocketMine-MP.git
synced 2025-04-21 00:07:30 +00:00
Replace hardcoded block metadata shifts and masks with constants
we might want to make these bigger than 4 bits in the future.
This commit is contained in:
parent
c22f793521
commit
61c59be299
@ -48,6 +48,8 @@ use function dechex;
|
||||
use const PHP_INT_MAX;
|
||||
|
||||
class Block{
|
||||
public const INTERNAL_METADATA_BITS = 4;
|
||||
public const INTERNAL_METADATA_MASK = ~(~0 << self::INTERNAL_METADATA_BITS);
|
||||
|
||||
protected BlockIdentifier $idInfo;
|
||||
protected string $fallbackName;
|
||||
@ -90,7 +92,7 @@ class Block{
|
||||
* @internal
|
||||
*/
|
||||
public function getFullId() : int{
|
||||
return ($this->getId() << 4) | $this->getMeta();
|
||||
return ($this->getId() << self::INTERNAL_METADATA_BITS) | $this->getMeta();
|
||||
}
|
||||
|
||||
public function asItem() : Item{
|
||||
|
@ -895,7 +895,7 @@ class BlockFactory{
|
||||
throw new \InvalidArgumentException("Block registration " . get_class($block) . " has states which conflict with other blocks");
|
||||
}
|
||||
|
||||
$index = ($id << 4) | $m;
|
||||
$index = ($id << Block::INTERNAL_METADATA_BITS) | $m;
|
||||
|
||||
$v = clone $block;
|
||||
try{
|
||||
@ -915,7 +915,7 @@ class BlockFactory{
|
||||
}
|
||||
|
||||
public function remap(int $id, int $meta, Block $block) : void{
|
||||
$index = ($id << 4) | $meta;
|
||||
$index = ($id << Block::INTERNAL_METADATA_BITS) | $meta;
|
||||
if($this->isRegistered($id, $meta)){
|
||||
$existing = $this->fullList[$index];
|
||||
if($existing !== null && $existing->getFullId() === $index){
|
||||
@ -924,7 +924,7 @@ class BlockFactory{
|
||||
//if it's not a match, this was already remapped for some reason; remapping overwrites are OK
|
||||
}
|
||||
}
|
||||
$this->fillStaticArrays(($id << 4) | $meta, $block);
|
||||
$this->fillStaticArrays(($id << Block::INTERNAL_METADATA_BITS) | $meta, $block);
|
||||
}
|
||||
|
||||
private function fillStaticArrays(int $index, Block $block) : void{
|
||||
@ -943,14 +943,14 @@ class BlockFactory{
|
||||
* Deserializes a block from the provided legacy ID and legacy meta.
|
||||
*/
|
||||
public function get(int $id, int $meta) : Block{
|
||||
if($meta < 0 or $meta > 0xf){
|
||||
if($meta < 0 or $meta > (1 << Block::INTERNAL_METADATA_BITS)){
|
||||
throw new \InvalidArgumentException("Block meta value $meta is out of bounds");
|
||||
}
|
||||
|
||||
/** @var Block|null $block */
|
||||
$block = null;
|
||||
try{
|
||||
$index = ($id << 4) | $meta;
|
||||
$index = ($id << Block::INTERNAL_METADATA_BITS) | $meta;
|
||||
if($this->fullList[$index] !== null){
|
||||
$block = clone $this->fullList[$index];
|
||||
}
|
||||
@ -966,14 +966,14 @@ class BlockFactory{
|
||||
}
|
||||
|
||||
public function fromFullBlock(int $fullState) : Block{
|
||||
return $this->get($fullState >> 4, $fullState & 0xf);
|
||||
return $this->get($fullState >> Block::INTERNAL_METADATA_BITS, $fullState & Block::INTERNAL_METADATA_MASK);
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns whether a specified block state is already registered in the block factory.
|
||||
*/
|
||||
public function isRegistered(int $id, int $meta = 0) : bool{
|
||||
$b = $this->fullList[($id << 4) | $meta];
|
||||
$b = $this->fullList[($id << Block::INTERNAL_METADATA_BITS) | $meta];
|
||||
return $b !== null and !($b instanceof UnknownBlock);
|
||||
}
|
||||
|
||||
|
@ -23,6 +23,7 @@ declare(strict_types=1);
|
||||
|
||||
namespace pocketmine\network\mcpe\convert;
|
||||
|
||||
use pocketmine\block\Block;
|
||||
use pocketmine\block\BlockLegacyIds;
|
||||
use pocketmine\data\bedrock\LegacyBlockIdToStringIdMap;
|
||||
use pocketmine\nbt\tag\CompoundTag;
|
||||
@ -110,7 +111,7 @@ final class RuntimeBlockMapping{
|
||||
}
|
||||
|
||||
public function toRuntimeId(int $internalStateId) : int{
|
||||
return $this->legacyToRuntimeMap[$internalStateId] ?? $this->legacyToRuntimeMap[BlockLegacyIds::INFO_UPDATE << 4];
|
||||
return $this->legacyToRuntimeMap[$internalStateId] ?? $this->legacyToRuntimeMap[BlockLegacyIds::INFO_UPDATE << Block::INTERNAL_METADATA_BITS];
|
||||
}
|
||||
|
||||
public function fromRuntimeId(int $runtimeId) : int{
|
||||
@ -118,8 +119,8 @@ final class RuntimeBlockMapping{
|
||||
}
|
||||
|
||||
private function registerMapping(int $staticRuntimeId, int $legacyId, int $legacyMeta) : void{
|
||||
$this->legacyToRuntimeMap[($legacyId << 4) | $legacyMeta] = $staticRuntimeId;
|
||||
$this->runtimeToLegacyMap[$staticRuntimeId] = ($legacyId << 4) | $legacyMeta;
|
||||
$this->legacyToRuntimeMap[($legacyId << Block::INTERNAL_METADATA_BITS) | $legacyMeta] = $staticRuntimeId;
|
||||
$this->runtimeToLegacyMap[$staticRuntimeId] = ($legacyId << Block::INTERNAL_METADATA_BITS) | $legacyMeta;
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -26,6 +26,7 @@ declare(strict_types=1);
|
||||
|
||||
namespace pocketmine\world\format;
|
||||
|
||||
use pocketmine\block\Block;
|
||||
use pocketmine\block\BlockLegacyIds;
|
||||
use pocketmine\block\tile\Tile;
|
||||
use pocketmine\data\bedrock\BiomeIds;
|
||||
@ -86,7 +87,7 @@ class Chunk{
|
||||
$this->subChunks = new \SplFixedArray(Chunk::MAX_SUBCHUNKS);
|
||||
|
||||
foreach($this->subChunks as $y => $null){
|
||||
$this->subChunks[$y] = $subChunks[$y] ?? new SubChunk(BlockLegacyIds::AIR << 4, []);
|
||||
$this->subChunks[$y] = $subChunks[$y] ?? new SubChunk(BlockLegacyIds::AIR << Block::INTERNAL_METADATA_BITS, []);
|
||||
}
|
||||
|
||||
$val = ($this->subChunks->getSize() * 16);
|
||||
@ -366,7 +367,7 @@ class Chunk{
|
||||
throw new \InvalidArgumentException("Invalid subchunk Y coordinate $y");
|
||||
}
|
||||
|
||||
$this->subChunks[$y] = $subChunk ?? new SubChunk(BlockLegacyIds::AIR << 4, []);
|
||||
$this->subChunks[$y] = $subChunk ?? new SubChunk(BlockLegacyIds::AIR << Block::INTERNAL_METADATA_BITS, []);
|
||||
$this->setDirtyFlag(self::DIRTY_FLAG_TERRAIN, true);
|
||||
}
|
||||
|
||||
|
@ -23,6 +23,7 @@ declare(strict_types=1);
|
||||
|
||||
namespace pocketmine\world\format\io\leveldb;
|
||||
|
||||
use pocketmine\block\Block;
|
||||
use pocketmine\block\BlockLegacyIds;
|
||||
use pocketmine\data\bedrock\LegacyBlockIdToStringIdMap;
|
||||
use pocketmine\nbt\LittleEndianNbtSerializer;
|
||||
@ -176,7 +177,7 @@ class LevelDB extends BaseWorldProvider implements WritableWorldProvider{
|
||||
//we really need a proper state fixer, but this is a pressing issue.
|
||||
$data = 0;
|
||||
}
|
||||
$palette[] = ($id << 4) | $data;
|
||||
$palette[] = ($id << Block::INTERNAL_METADATA_BITS) | $data;
|
||||
}
|
||||
|
||||
//TODO: exceptions
|
||||
@ -219,9 +220,9 @@ class LevelDB extends BaseWorldProvider implements WritableWorldProvider{
|
||||
$blockId = $value & 0xff;
|
||||
$blockData = ($value >> 8) & 0xf;
|
||||
if(!isset($extraDataLayers[$ySub])){
|
||||
$extraDataLayers[$ySub] = new PalettedBlockArray(BlockLegacyIds::AIR << 4);
|
||||
$extraDataLayers[$ySub] = new PalettedBlockArray(BlockLegacyIds::AIR << Block::INTERNAL_METADATA_BITS);
|
||||
}
|
||||
$extraDataLayers[$ySub]->set($x, $y, $z, ($blockId << 4) | $blockData);
|
||||
$extraDataLayers[$ySub]->set($x, $y, $z, ($blockId << Block::INTERNAL_METADATA_BITS) | $blockData);
|
||||
}
|
||||
|
||||
return $extraDataLayers;
|
||||
@ -301,14 +302,14 @@ class LevelDB extends BaseWorldProvider implements WritableWorldProvider{
|
||||
$storages[] = $convertedLegacyExtraData[$y];
|
||||
}
|
||||
|
||||
$subChunks[$y] = new SubChunk(BlockLegacyIds::AIR << 4, $storages);
|
||||
$subChunks[$y] = new SubChunk(BlockLegacyIds::AIR << Block::INTERNAL_METADATA_BITS, $storages);
|
||||
break;
|
||||
case 1: //paletted v1, has a single blockstorage
|
||||
$storages = [$this->deserializePaletted($binaryStream)];
|
||||
if(isset($convertedLegacyExtraData[$y])){
|
||||
$storages[] = $convertedLegacyExtraData[$y];
|
||||
}
|
||||
$subChunks[$y] = new SubChunk(BlockLegacyIds::AIR << 4, $storages);
|
||||
$subChunks[$y] = new SubChunk(BlockLegacyIds::AIR << Block::INTERNAL_METADATA_BITS, $storages);
|
||||
break;
|
||||
case 8:
|
||||
//legacy extradata layers intentionally ignored because they aren't supposed to exist in v8
|
||||
@ -319,7 +320,7 @@ class LevelDB extends BaseWorldProvider implements WritableWorldProvider{
|
||||
for($k = 0; $k < $storageCount; ++$k){
|
||||
$storages[] = $this->deserializePaletted($binaryStream);
|
||||
}
|
||||
$subChunks[$y] = new SubChunk(BlockLegacyIds::AIR << 4, $storages);
|
||||
$subChunks[$y] = new SubChunk(BlockLegacyIds::AIR << Block::INTERNAL_METADATA_BITS, $storages);
|
||||
}
|
||||
break;
|
||||
default:
|
||||
@ -362,7 +363,7 @@ class LevelDB extends BaseWorldProvider implements WritableWorldProvider{
|
||||
if(isset($convertedLegacyExtraData[$yy])){
|
||||
$storages[] = $convertedLegacyExtraData[$yy];
|
||||
}
|
||||
$subChunks[$yy] = new SubChunk(BlockLegacyIds::AIR << 4, $storages);
|
||||
$subChunks[$yy] = new SubChunk(BlockLegacyIds::AIR << Block::INTERNAL_METADATA_BITS, $storages);
|
||||
}
|
||||
|
||||
try{
|
||||
@ -446,9 +447,9 @@ class LevelDB extends BaseWorldProvider implements WritableWorldProvider{
|
||||
$tags = [];
|
||||
foreach($palette as $p){
|
||||
$tags[] = new TreeRoot(CompoundTag::create()
|
||||
->setString("name", $idMap->legacyToString($p >> 4) ?? "minecraft:info_update")
|
||||
->setInt("oldid", $p >> 4) //PM only (debugging), vanilla doesn't have this
|
||||
->setShort("val", $p & 0xf));
|
||||
->setString("name", $idMap->legacyToString($p >> Block::INTERNAL_METADATA_BITS) ?? "minecraft:info_update")
|
||||
->setInt("oldid", $p >> Block::INTERNAL_METADATA_BITS) //PM only (debugging), vanilla doesn't have this
|
||||
->setShort("val", $p & Block::INTERNAL_METADATA_MASK));
|
||||
}
|
||||
|
||||
$subStream->put((new LittleEndianNbtSerializer())->writeMultiple($tags));
|
||||
|
@ -23,6 +23,7 @@ declare(strict_types=1);
|
||||
|
||||
namespace pocketmine\world\format\io\region;
|
||||
|
||||
use pocketmine\block\Block;
|
||||
use pocketmine\block\BlockLegacyIds;
|
||||
use pocketmine\nbt\tag\CompoundTag;
|
||||
use pocketmine\world\format\io\SubChunkConverter;
|
||||
@ -32,7 +33,7 @@ class Anvil extends RegionWorldProvider{
|
||||
use LegacyAnvilChunkTrait;
|
||||
|
||||
protected function deserializeSubChunk(CompoundTag $subChunk) : SubChunk{
|
||||
return new SubChunk(BlockLegacyIds::AIR << 4, [SubChunkConverter::convertSubChunkYZX(
|
||||
return new SubChunk(BlockLegacyIds::AIR << Block::INTERNAL_METADATA_BITS, [SubChunkConverter::convertSubChunkYZX(
|
||||
self::readFixedSizeByteArray($subChunk, "Blocks", 4096),
|
||||
self::readFixedSizeByteArray($subChunk, "Data", 2048)
|
||||
)]);
|
||||
|
@ -23,6 +23,7 @@ declare(strict_types=1);
|
||||
|
||||
namespace pocketmine\world\format\io\region;
|
||||
|
||||
use pocketmine\block\Block;
|
||||
use pocketmine\block\BlockLegacyIds;
|
||||
use pocketmine\nbt\BigEndianNbtSerializer;
|
||||
use pocketmine\nbt\NbtDataException;
|
||||
@ -63,7 +64,7 @@ class McRegion extends RegionWorldProvider{
|
||||
$fullData = self::readFixedSizeByteArray($chunk, "Data", 16384);
|
||||
|
||||
for($y = 0; $y < 8; ++$y){
|
||||
$subChunks[$y] = new SubChunk(BlockLegacyIds::AIR << 4, [SubChunkConverter::convertSubChunkFromLegacyColumn($fullIds, $fullData, $y)]);
|
||||
$subChunks[$y] = new SubChunk(BlockLegacyIds::AIR << Block::INTERNAL_METADATA_BITS, [SubChunkConverter::convertSubChunkFromLegacyColumn($fullIds, $fullData, $y)]);
|
||||
}
|
||||
|
||||
$makeBiomeArray = function(string $biomeIds) : BiomeArray{
|
||||
|
@ -23,6 +23,7 @@ declare(strict_types=1);
|
||||
|
||||
namespace pocketmine\world\format\io\region;
|
||||
|
||||
use pocketmine\block\Block;
|
||||
use pocketmine\block\BlockLegacyIds;
|
||||
use pocketmine\nbt\tag\CompoundTag;
|
||||
use pocketmine\world\format\io\SubChunkConverter;
|
||||
@ -36,7 +37,7 @@ class PMAnvil extends RegionWorldProvider{
|
||||
use LegacyAnvilChunkTrait;
|
||||
|
||||
protected function deserializeSubChunk(CompoundTag $subChunk) : SubChunk{
|
||||
return new SubChunk(BlockLegacyIds::AIR << 4, [SubChunkConverter::convertSubChunkXZY(
|
||||
return new SubChunk(BlockLegacyIds::AIR << Block::INTERNAL_METADATA_BITS, [SubChunkConverter::convertSubChunkXZY(
|
||||
self::readFixedSizeByteArray($subChunk, "Blocks", 4096),
|
||||
self::readFixedSizeByteArray($subChunk, "Data", 2048)
|
||||
)]);
|
||||
|
@ -36,14 +36,14 @@ $new = array_map(
|
||||
);
|
||||
foreach($old as $k => $name){
|
||||
if(!isset($new[$k])){
|
||||
echo "Removed state for $name (" . ($k >> 4) . ":" . ($k & 0xf) . ")\n";
|
||||
echo "Removed state for $name (" . ($k >> \pocketmine\block\Block::INTERNAL_METADATA_BITS) . ":" . ($k & \pocketmine\block\Block::INTERNAL_METADATA_MASK) . ")\n";
|
||||
}
|
||||
}
|
||||
foreach($new as $k => $name){
|
||||
if(!isset($old[$k])){
|
||||
echo "Added state for $name (" . ($k >> 4) . ":" . ($k & 0xf) . ")\n";
|
||||
echo "Added state for $name (" . ($k >> \pocketmine\block\Block::INTERNAL_METADATA_BITS) . ":" . ($k & \pocketmine\block\Block::INTERNAL_METADATA_MASK) . ")\n";
|
||||
}elseif($old[$k] !== $name){
|
||||
echo "Name changed (" . ($k >> 4) . ":" . ($k & 0xf) . "): " . $old[$k] . " -> " . $name . "\n";
|
||||
echo "Name changed (" . ($k >> \pocketmine\block\Block::INTERNAL_METADATA_BITS) . ":" . ($k & \pocketmine\block\Block::INTERNAL_METADATA_MASK) . "): " . $old[$k] . " -> " . $name . "\n";
|
||||
}
|
||||
}
|
||||
file_put_contents(__DIR__ . '/block_factory_consistency_check.json', json_encode(
|
||||
|
Loading…
x
Reference in New Issue
Block a user