mirror of
https://github.com/pmmp/PocketMine-MP.git
synced 2025-09-07 18:32:55 +00:00
Block: specifying required type/state data bits is no longer required
RuntimeDataSizeCalculator allows calculating the number of required bits from describeType directly, which considerably reduces boilerplate code.
This commit is contained in:
@ -31,6 +31,7 @@ use pocketmine\block\tile\Tile;
|
||||
use pocketmine\block\utils\SupportType;
|
||||
use pocketmine\data\runtime\RuntimeDataDescriber;
|
||||
use pocketmine\data\runtime\RuntimeDataReader;
|
||||
use pocketmine\data\runtime\RuntimeDataSizeCalculator;
|
||||
use pocketmine\data\runtime\RuntimeDataWriter;
|
||||
use pocketmine\entity\Entity;
|
||||
use pocketmine\entity\projectile\Projectile;
|
||||
@ -64,6 +65,17 @@ class Block{
|
||||
/** @var AxisAlignedBB[]|null */
|
||||
protected ?array $collisionBoxes = null;
|
||||
|
||||
/**
|
||||
* @var int[]
|
||||
* @phpstan-var array<string, int>
|
||||
*/
|
||||
private static array $typeDataBits = [];
|
||||
/**
|
||||
* @var int[]
|
||||
* @phpstan-var array<string, int>
|
||||
*/
|
||||
private static array $stateDataBits = [];
|
||||
|
||||
/**
|
||||
* @param string $name English name of the block type (TODO: implement translations)
|
||||
*/
|
||||
@ -114,9 +126,25 @@ class Block{
|
||||
return new ItemBlock($this);
|
||||
}
|
||||
|
||||
public function getRequiredTypeDataBits() : int{ return 0; }
|
||||
final public function getRequiredTypeDataBits() : int{
|
||||
$class = get_class($this);
|
||||
if(isset(self::$typeDataBits[$class])){
|
||||
return self::$typeDataBits[$class];
|
||||
}
|
||||
$calculator = new RuntimeDataSizeCalculator();
|
||||
$this->describeType($calculator);
|
||||
return self::$typeDataBits[$class] = $calculator->getBitsUsed();
|
||||
}
|
||||
|
||||
public function getRequiredStateDataBits() : int{ return 0; }
|
||||
final public function getRequiredStateDataBits() : int{
|
||||
$class = get_class($this);
|
||||
if(isset(self::$stateDataBits[$class])){
|
||||
return self::$stateDataBits[$class];
|
||||
}
|
||||
$calculator = new RuntimeDataSizeCalculator();
|
||||
$this->describeState($calculator);
|
||||
return self::$stateDataBits[$class] = $calculator->getBitsUsed();
|
||||
}
|
||||
|
||||
/**
|
||||
* @internal
|
||||
|
Reference in New Issue
Block a user