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

@ -40,14 +40,17 @@ use function max;
class SnowLayer extends Flowable implements Fallable{
use FallableTrait;
protected int $layers = 1;
public const MIN_LAYERS = 1;
public const MAX_LAYERS = 8;
protected int $layers = self::MIN_LAYERS;
protected function writeStateToMeta() : int{
return $this->layers - 1;
}
public function readStateFromData(int $id, int $stateMeta) : void{
$this->layers = BlockDataSerializer::readBoundedInt("layers", $stateMeta + 1, 1, 8);
$this->layers = BlockDataSerializer::readBoundedInt("layers", $stateMeta + 1, self::MIN_LAYERS, self::MAX_LAYERS);
}
public function getStateBitmask() : int{
@ -58,15 +61,15 @@ class SnowLayer extends Flowable implements Fallable{
/** @return $this */
public function setLayers(int $layers) : self{
if($layers < 1 || $layers > 8){
throw new \InvalidArgumentException("Layers must be in range 1-8");
if($layers < self::MIN_LAYERS || $layers > self::MAX_LAYERS){
throw new \InvalidArgumentException("Layers must be in range " . self::MIN_LAYERS . " ... " . self::MAX_LAYERS);
}
$this->layers = $layers;
return $this;
}
public function canBeReplaced() : bool{
return $this->layers < 8;
return $this->layers < self::MAX_LAYERS;
}
/**
@ -78,12 +81,12 @@ class SnowLayer extends Flowable implements Fallable{
}
private function canBeSupportedBy(Block $b) : bool{
return $b->isSolid() || ($b instanceof SnowLayer && $b->isSameType($this) && $b->layers === 8);
return $b->isSolid() || ($b instanceof SnowLayer && $b->isSameType($this) && $b->layers === self::MAX_LAYERS);
}
public function place(BlockTransaction $tx, Item $item, Block $blockReplace, Block $blockClicked, int $face, Vector3 $clickVector, ?Player $player = null) : bool{
if($blockReplace instanceof SnowLayer){
if($blockReplace->layers >= 8){
if($blockReplace->layers >= self::MAX_LAYERS){
return false;
}
$this->layers = $blockReplace->layers + 1;