block: added constants for various integer property bounds

This commit is contained in:
Dylan K. Taylor
2022-01-28 21:27:30 +00:00
parent 466b018319
commit 0642364a44
20 changed files with 107 additions and 79 deletions

View File

@ -37,6 +37,7 @@ use pocketmine\world\sound\Sound;
use function lcg_value;
abstract class Liquid extends Transparent{
public const MAX_DECAY = 7;
protected BlockIdentifierFlattened $idInfoFlattened;
@ -62,7 +63,7 @@ abstract class Liquid extends Transparent{
}
public function readStateFromData(int $id, int $stateMeta) : void{
$this->decay = BlockDataSerializer::readBoundedInt("decay", $stateMeta & 0x07, 0, 7);
$this->decay = BlockDataSerializer::readBoundedInt("decay", $stateMeta & 0x07, 0, self::MAX_DECAY);
$this->falling = ($stateMeta & BlockLegacyMetadata::LIQUID_FLAG_FALLING) !== 0;
$this->still = $id === $this->idInfoFlattened->getSecondId();
}
@ -83,6 +84,9 @@ abstract class Liquid extends Transparent{
/** @return $this */
public function setDecay(int $decay) : self{
if($decay < 0 || $decay > self::MAX_DECAY){
throw new \InvalidArgumentException("Decay must be in range 0 ... " . self::MAX_DECAY);
}
$this->decay = $decay;
return $this;
}
@ -279,7 +283,7 @@ abstract class Liquid extends Transparent{
$newDecay = $smallestFlowDecay + $multiplier;
$falling = false;
if($newDecay >= 8 || $smallestFlowDecay < 0){
if($newDecay > self::MAX_DECAY || $smallestFlowDecay < 0){
$newDecay = -1;
}
@ -319,7 +323,7 @@ abstract class Liquid extends Transparent{
$adjacentDecay = $this->decay + $multiplier;
}
if($adjacentDecay < 8){
if($adjacentDecay <= self::MAX_DECAY){
$calculator = new MinimumCostFlowCalculator($this->position->getWorld(), $this->getFlowDecayPerBlock(), \Closure::fromCallable([$this, 'canFlowInto']));
foreach($calculator->getOptimalFlowDirections($this->position->getFloorX(), $this->position->getFloorY(), $this->position->getFloorZ()) as $facing){
$this->flowIntoBlock($world->getBlock($this->position->getSide($facing)), $adjacentDecay, false);