*/ private array $fullList = []; /** * Index of default states for every block type * @var Block[] * @phpstan-var array */ private array $typeIndex = []; /** * @var int[] * @phpstan-var array */ public array $light = []; /** * @var int[] * @phpstan-var array */ public array $lightFilter = []; /** * @var true[] * @phpstan-var array */ public array $blocksDirectSkyLight = []; /** * @var float[] * @phpstan-var array */ public array $blastResistance = []; public function __construct(){ foreach(VanillaBlocks::getAll() as $block){ $this->register($block); } } /** * Maps a block type's state permutations to its corresponding state IDs. This is necessary for the block to be * recognized when fetching it by its state ID from chunks at runtime. * * @throws \InvalidArgumentException if the desired block type ID is already registered */ public function register(Block $block) : void{ $typeId = $block->getTypeId(); if(isset($this->typeIndex[$typeId])){ throw new \InvalidArgumentException("Block ID $typeId is already used by another block"); } $this->typeIndex[$typeId] = clone $block; foreach($block->generateStatePermutations() as $v){ $this->fillStaticArrays($v->getStateId(), $v); } } private function fillStaticArrays(int $index, Block $block) : void{ $fullId = $block->getStateId(); if($index !== $fullId){ throw new AssumptionFailedError("Cannot fill static arrays for an invalid blockstate"); }else{ $this->fullList[$index] = $block; $this->blastResistance[$index] = $block->getBreakInfo()->getBlastResistance(); $this->light[$index] = $block->getLightLevel(); $this->lightFilter[$index] = min(15, $block->getLightFilter() + LightUpdate::BASE_LIGHT_FILTER); if($block->blocksDirectSkyLight()){ $this->blocksDirectSkyLight[$index] = true; } } } public function fromStateId(int $stateId) : Block{ if($stateId < 0){ throw new \InvalidArgumentException("Block state ID cannot be negative"); } if(isset($this->fullList[$stateId])) { //hot $block = clone $this->fullList[$stateId]; }else{ $typeId = $stateId >> Block::INTERNAL_STATE_DATA_BITS; $stateData = ($stateId ^ $typeId) & Block::INTERNAL_STATE_DATA_MASK; $block = new UnknownBlock(new BID($typeId), new BlockTypeInfo(BreakInfo::instant()), $stateData); } return $block; } /** * @return Block[] */ public function getAllKnownStates() : array{ return $this->fullList; } }