PocketMine-MP 5.25.1 git-694aa17cc916a954b10fe12721c81b1dc73eecd5
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 count;
32use function min;
33
42 use SingletonTrait;
43
44 public const COLLISION_CUSTOM = 0;
45 public const COLLISION_CUBE = 1;
46 public const COLLISION_NONE = 2;
47 public const COLLISION_MAY_OVERFLOW = 3;
48
53 private array $fullList = [];
54
60 private array $typeIndex = [];
61
66 public array $light = [];
71 public array $lightFilter = [];
76 public array $blocksDirectSkyLight = [];
81 public array $blastResistance = [];
82
88 public array $collisionInfo = [];
89
90 public function __construct(){
91 foreach(VanillaBlocks::getAll() as $block){
92 $this->register($block);
93 }
94 }
95
102 public function register(Block $block) : void{
103 $typeId = $block->getTypeId();
104
105 if(isset($this->typeIndex[$typeId])){
106 throw new \InvalidArgumentException("Block ID $typeId is already used by another block");
107 }
108
109 $this->typeIndex[$typeId] = clone $block;
110
111 foreach($block->generateStatePermutations() as $v){
112 $this->fillStaticArrays($v->getStateId(), $v);
113 }
114 }
115
122 private static function overridesBlockMethod(\Closure $closure) : bool{
123 $declarer = (new \ReflectionFunction($closure))->getClosureScopeClass();
124 return $declarer !== null && $declarer->getName() !== Block::class;
125 }
126
136 private static function calculateCollisionInfo(Block $block) : int{
137 if(
138 self::overridesBlockMethod($block->getModelPositionOffset(...)) ||
139 self::overridesBlockMethod($block->readStateFromWorld(...))
140 ){
141 //getModelPositionOffset() might cause AABBs to shift outside the cell
142 //readStateFromWorld() might cause overflow in ways we can't predict just by looking at known states
143 //TODO: excluding overriders of readStateFromWorld() also excludes blocks with tiles that don't do anything
144 //weird with their AABBs, but for now this is the best we can do.
145 return self::COLLISION_MAY_OVERFLOW;
146 }
147
148 //TODO: this could blow up if any recalculateCollisionBoxes() uses the world
149 //it shouldn't, but that doesn't mean that custom blocks won't...
150 $boxes = $block->getCollisionBoxes();
151 if(count($boxes) === 0){
152 return self::COLLISION_NONE;
153 }
154
155 if(
156 count($boxes) === 1 &&
157 $boxes[0]->minX === 0.0 &&
158 $boxes[0]->minY === 0.0 &&
159 $boxes[0]->minZ === 0.0 &&
160 $boxes[0]->maxX === 1.0 &&
161 $boxes[0]->maxY === 1.0 &&
162 $boxes[0]->maxZ === 1.0
163 ){
164 return self::COLLISION_CUBE;
165 }
166
167 foreach($boxes as $box){
168 if(
169 $box->minX < 0 || $box->maxX > 1 ||
170 $box->minY < 0 || $box->maxY > 1 ||
171 $box->minZ < 0 || $box->maxZ > 1
172 ){
173 return self::COLLISION_MAY_OVERFLOW;
174 }
175 }
176
177 return self::COLLISION_CUSTOM;
178 }
179
180 private function fillStaticArrays(int $index, Block $block) : void{
181 $fullId = $block->getStateId();
182 if($index !== $fullId){
183 throw new AssumptionFailedError("Cannot fill static arrays for an invalid blockstate");
184 }else{
185 $this->fullList[$index] = $block;
186 $this->blastResistance[$index] = $block->getBreakInfo()->getBlastResistance();
187 $this->light[$index] = $block->getLightLevel();
188 $this->lightFilter[$index] = min(15, $block->getLightFilter() + LightUpdate::BASE_LIGHT_FILTER);
189 if($block->blocksDirectSkyLight()){
190 $this->blocksDirectSkyLight[$index] = true;
191 }
192
193 $this->collisionInfo[$index] = self::calculateCollisionInfo($block);
194 }
195 }
196
197 public function fromStateId(int $stateId) : Block{
198 if($stateId < 0){
199 throw new \InvalidArgumentException("Block state ID cannot be negative");
200 }
201 if(isset($this->fullList[$stateId])) { //hot
202 $block = clone $this->fullList[$stateId];
203 }else{
204 $typeId = $stateId >> Block::INTERNAL_STATE_DATA_BITS;
205 $stateData = ($stateId ^ $typeId) & Block::INTERNAL_STATE_DATA_MASK;
206 $block = new UnknownBlock(new BID($typeId), new BlockTypeInfo(BreakInfo::instant()), $stateData);
207 }
208
209 return $block;
210 }
211
216 public function getAllKnownStates() : array{
217 return $this->fullList;
218 }
219}