PocketMine-MP 5.21.2 git-b2aa6396c3cc2cafdd815eacc360e1ad89599899
Loading...
Searching...
No Matches
RuntimeBlockStateRegistry.php
1<?php
2
3/*
4 *
5 * ____ _ _ __ __ _ __ __ ____
6 * | _ \ ___ ___| | _____| |_| \/ (_)_ __ ___ | \/ | _ \
7 * | |_) / _ \ / __| |/ / _ \ __| |\/| | | '_ \ / _ \_____| |\/| | |_) |
8 * | __/ (_) | (__| < __/ |_| | | | | | | | __/_____| | | | __/
9 * |_| \___/ \___|_|\_\___|\__|_| |_|_|_| |_|\___| |_| |_|_|
10 *
11 * This program is free software: you can redistribute it and/or modify
12 * it under the terms of the GNU Lesser General Public License as published by
13 * the Free Software Foundation, either version 3 of the License, or
14 * (at your option) any later version.
15 *
16 * @author PocketMine Team
17 * @link http://www.pocketmine.net/
18 *
19 *
20 */
21
22declare(strict_types=1);
23
24namespace pocketmine\block;
25
26use pocketmine\block\BlockBreakInfo as BreakInfo;
29use pocketmine\utils\SingletonTrait;
31use function min;
32
41 use SingletonTrait;
42
47 private array $fullList = [];
48
54 private array $typeIndex = [];
55
60 public array $light = [];
65 public array $lightFilter = [];
70 public array $blocksDirectSkyLight = [];
75 public array $blastResistance = [];
76
77 public function __construct(){
78 foreach(VanillaBlocks::getAll() as $block){
79 $this->register($block);
80 }
81 }
82
89 public function register(Block $block) : void{
90 $typeId = $block->getTypeId();
91
92 if(isset($this->typeIndex[$typeId])){
93 throw new \InvalidArgumentException("Block ID $typeId is already used by another block");
94 }
95
96 $this->typeIndex[$typeId] = clone $block;
97
98 foreach($block->generateStatePermutations() as $v){
99 $this->fillStaticArrays($v->getStateId(), $v);
100 }
101 }
102
103 private function fillStaticArrays(int $index, Block $block) : void{
104 $fullId = $block->getStateId();
105 if($index !== $fullId){
106 throw new AssumptionFailedError("Cannot fill static arrays for an invalid blockstate");
107 }else{
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;
114 }
115 }
116 }
117
118 public function fromStateId(int $stateId) : Block{
119 if($stateId < 0){
120 throw new \InvalidArgumentException("Block state ID cannot be negative");
121 }
122 if(isset($this->fullList[$stateId])) { //hot
123 $block = clone $this->fullList[$stateId];
124 }else{
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);
128 }
129
130 return $block;
131 }
132
136 public function getAllKnownStates() : array{
137 return $this->fullList;
138 }
139}