47 private array $fullList = [];
54 private array $typeIndex = [];
60 public array $light = [];
65 public array $lightFilter = [];
70 public array $blocksDirectSkyLight = [];
75 public array $blastResistance = [];
77 public function __construct(){
79 $this->
register($block);
89 public function register(
Block $block) : void{
90 $typeId = $block->getTypeId();
92 if(isset($this->typeIndex[$typeId])){
93 throw new \InvalidArgumentException(
"Block ID $typeId is already used by another block");
96 $this->typeIndex[$typeId] = clone $block;
98 foreach($block->generateStatePermutations() as $v){
99 $this->fillStaticArrays($v->getStateId(), $v);
103 private function fillStaticArrays(
int $index, Block $block) : void{
104 $fullId = $block->getStateId();
105 if($index !== $fullId){
108 $this->fullList[$index] = $block;
109 $this->blastResistance[$index] = $block->getBreakInfo()->getBlastResistance();
110 $this->light[$index] = $block->getLightLevel();
111 $this->lightFilter[$index] = min(15, $block->getLightFilter() + LightUpdate::BASE_LIGHT_FILTER);
112 if($block->blocksDirectSkyLight()){
113 $this->blocksDirectSkyLight[$index] =
true;
118 public function fromStateId(
int $stateId) : Block{
120 throw new \InvalidArgumentException(
"Block state ID cannot be negative");
122 if(isset($this->fullList[$stateId])) {
123 $block = clone $this->fullList[$stateId];
125 $typeId = $stateId >> Block::INTERNAL_STATE_DATA_BITS;
126 $stateData = ($stateId ^ $typeId) & Block::INTERNAL_STATE_DATA_MASK;
127 $block =
new UnknownBlock(
new BID($typeId),
new BlockTypeInfo(BreakInfo::instant()), $stateData);
137 return $this->fullList;