diff --git a/src/block/Anvil.php b/src/block/Anvil.php index 48fade268..5a97384c5 100644 --- a/src/block/Anvil.php +++ b/src/block/Anvil.php @@ -39,7 +39,11 @@ class Anvil extends Transparent implements Fallable{ use FallableTrait; use HorizontalFacingTrait; - private int $damage = 0; + public const UNDAMAGED = 0; + public const SLIGHTLY_DAMAGED = 1; + public const VERY_DAMAGED = 2; + + private int $damage = self::UNDAMAGED; protected function writeStateToMeta() : int{ return BlockDataSerializer::writeLegacyHorizontalFacing($this->facing) | ($this->damage << 2); @@ -47,7 +51,7 @@ class Anvil extends Transparent implements Fallable{ public function readStateFromData(int $id, int $stateMeta) : void{ $this->facing = BlockDataSerializer::readLegacyHorizontalFacing($stateMeta & 0x3); - $this->damage = BlockDataSerializer::readBoundedInt("damage", $stateMeta >> 2, 0, 2); + $this->damage = BlockDataSerializer::readBoundedInt("damage", $stateMeta >> 2, self::UNDAMAGED, self::VERY_DAMAGED); } public function getStateBitmask() : int{ @@ -62,8 +66,8 @@ class Anvil extends Transparent implements Fallable{ /** @return $this */ public function setDamage(int $damage) : self{ - if($damage < 0 || $damage > 2){ - throw new \InvalidArgumentException("Damage must be in range 0-2"); + if($damage < self::UNDAMAGED || $damage > self::VERY_DAMAGED){ + throw new \InvalidArgumentException("Damage must be in range " . self::UNDAMAGED . " ... " . self::VERY_DAMAGED); } $this->damage = $damage; return $this; diff --git a/src/block/Beetroot.php b/src/block/Beetroot.php index 51d05688e..8f8c25d3d 100644 --- a/src/block/Beetroot.php +++ b/src/block/Beetroot.php @@ -30,7 +30,7 @@ use function mt_rand; class Beetroot extends Crops{ public function getDropsForCompatibleTool(Item $item) : array{ - if($this->age >= 7){ + if($this->age >= self::MAX_AGE){ return [ VanillaItems::BEETROOT(), VanillaItems::BEETROOT_SEEDS()->setCount(mt_rand(0, 3)) diff --git a/src/block/Cactus.php b/src/block/Cactus.php index 2802c4966..815d8bcef 100644 --- a/src/block/Cactus.php +++ b/src/block/Cactus.php @@ -36,6 +36,7 @@ use pocketmine\player\Player; use pocketmine\world\BlockTransaction; class Cactus extends Transparent{ + public const MAX_AGE = 15; protected int $age = 0; @@ -44,7 +45,7 @@ class Cactus extends Transparent{ } public function readStateFromData(int $id, int $stateMeta) : void{ - $this->age = BlockDataSerializer::readBoundedInt("age", $stateMeta, 0, 15); + $this->age = BlockDataSerializer::readBoundedInt("age", $stateMeta, 0, self::MAX_AGE); } public function getStateBitmask() : int{ @@ -55,8 +56,8 @@ class Cactus extends Transparent{ /** @return $this */ public function setAge(int $age) : self{ - if($age < 0 || $age > 15){ - throw new \InvalidArgumentException("Age must be in range 0-15"); + if($age < 0 || $age > self::MAX_AGE){ + throw new \InvalidArgumentException("Age must be in range 0 ... " . self::MAX_AGE); } $this->age = $age; return $this; @@ -101,7 +102,7 @@ class Cactus extends Transparent{ public function onRandomTick() : void{ if(!$this->getSide(Facing::DOWN)->isSameType($this)){ - if($this->age === 15){ + if($this->age === self::MAX_AGE){ for($y = 1; $y < 3; ++$y){ if(!$this->position->getWorld()->isInWorld($this->position->x, $this->position->y + $y, $this->position->z)){ break; diff --git a/src/block/Cake.php b/src/block/Cake.php index e9f82943f..3ac75147c 100644 --- a/src/block/Cake.php +++ b/src/block/Cake.php @@ -35,6 +35,7 @@ use pocketmine\player\Player; use pocketmine\world\BlockTransaction; class Cake extends Transparent implements FoodSource{ + public const MAX_BITES = 6; protected int $bites = 0; @@ -43,7 +44,7 @@ class Cake extends Transparent implements FoodSource{ } public function readStateFromData(int $id, int $stateMeta) : void{ - $this->bites = BlockDataSerializer::readBoundedInt("bites", $stateMeta, 0, 6); + $this->bites = BlockDataSerializer::readBoundedInt("bites", $stateMeta, 0, self::MAX_BITES); } public function getStateBitmask() : int{ @@ -66,8 +67,8 @@ class Cake extends Transparent implements FoodSource{ /** @return $this */ public function setBites(int $bites) : self{ - if($bites < 0 || $bites > 6){ - throw new \InvalidArgumentException("Bites must be in range 0-6"); + if($bites < 0 || $bites > self::MAX_BITES){ + throw new \InvalidArgumentException("Bites must be in range 0 ... " . self::MAX_BITES); } $this->bites = $bites; return $this; @@ -118,7 +119,7 @@ class Cake extends Transparent implements FoodSource{ public function getResidue(){ $clone = clone $this; $clone->bites++; - if($clone->bites > 6){ + if($clone->bites > self::MAX_BITES){ $clone = VanillaBlocks::AIR(); } return $clone; diff --git a/src/block/Carrot.php b/src/block/Carrot.php index 612b967f6..9b94fad78 100644 --- a/src/block/Carrot.php +++ b/src/block/Carrot.php @@ -31,7 +31,7 @@ class Carrot extends Crops{ public function getDropsForCompatibleTool(Item $item) : array{ return [ - VanillaItems::CARROT()->setCount($this->age >= 7 ? mt_rand(1, 4) : 1) + VanillaItems::CARROT()->setCount($this->age >= self::MAX_AGE ? mt_rand(1, 4) : 1) ]; } diff --git a/src/block/CocoaBlock.php b/src/block/CocoaBlock.php index ef19356e8..c33fcfe7e 100644 --- a/src/block/CocoaBlock.php +++ b/src/block/CocoaBlock.php @@ -41,6 +41,8 @@ use function mt_rand; class CocoaBlock extends Transparent{ use HorizontalFacingTrait; + public const MAX_AGE = 2; + protected int $age = 0; protected function writeStateToMeta() : int{ @@ -49,7 +51,7 @@ class CocoaBlock extends Transparent{ public function readStateFromData(int $id, int $stateMeta) : void{ $this->facing = Facing::opposite(BlockDataSerializer::readLegacyHorizontalFacing($stateMeta & 0x03)); - $this->age = BlockDataSerializer::readBoundedInt("age", $stateMeta >> 2, 0, 2); + $this->age = BlockDataSerializer::readBoundedInt("age", $stateMeta >> 2, 0, self::MAX_AGE); } public function getStateBitmask() : int{ @@ -60,8 +62,8 @@ class CocoaBlock extends Transparent{ /** @return $this */ public function setAge(int $age) : self{ - if($age < 0 || $age > 2){ - throw new \InvalidArgumentException("Age must be in range 0-2"); + if($age < 0 || $age > self::MAX_AGE){ + throw new \InvalidArgumentException("Age must be in range 0 ... " . self::MAX_AGE); } $this->age = $age; return $this; @@ -121,7 +123,7 @@ class CocoaBlock extends Transparent{ } private function grow() : bool{ - if($this->age < 2){ + if($this->age < self::MAX_AGE){ $block = clone $this; $block->age++; $ev = new BlockGrowEvent($this, $block); @@ -136,7 +138,7 @@ class CocoaBlock extends Transparent{ public function getDropsForCompatibleTool(Item $item) : array{ return [ - VanillaItems::COCOA_BEANS()->setCount($this->age === 2 ? mt_rand(2, 3) : 1) + VanillaItems::COCOA_BEANS()->setCount($this->age === self::MAX_AGE ? mt_rand(2, 3) : 1) ]; } diff --git a/src/block/Crops.php b/src/block/Crops.php index 054fed254..1979ce31f 100644 --- a/src/block/Crops.php +++ b/src/block/Crops.php @@ -34,6 +34,7 @@ use pocketmine\world\BlockTransaction; use function mt_rand; abstract class Crops extends Flowable{ + public const MAX_AGE = 7; protected int $age = 0; @@ -42,7 +43,7 @@ abstract class Crops extends Flowable{ } public function readStateFromData(int $id, int $stateMeta) : void{ - $this->age = BlockDataSerializer::readBoundedInt("age", $stateMeta, 0, 7); + $this->age = BlockDataSerializer::readBoundedInt("age", $stateMeta, 0, self::MAX_AGE); } public function getStateBitmask() : int{ @@ -53,8 +54,8 @@ abstract class Crops extends Flowable{ /** @return $this */ public function setAge(int $age) : self{ - if($age < 0 || $age > 7){ - throw new \InvalidArgumentException("Age must be in range 0-7"); + if($age < 0 || $age > self::MAX_AGE){ + throw new \InvalidArgumentException("Age must be in range 0 ... " . self::MAX_AGE); } $this->age = $age; return $this; @@ -69,11 +70,11 @@ abstract class Crops extends Flowable{ } public function onInteract(Item $item, int $face, Vector3 $clickVector, ?Player $player = null) : bool{ - if($this->age < 7 && $item instanceof Fertilizer){ + if($this->age < self::MAX_AGE && $item instanceof Fertilizer){ $block = clone $this; $block->age += mt_rand(2, 5); - if($block->age > 7){ - $block->age = 7; + if($block->age > self::MAX_AGE){ + $block->age = self::MAX_AGE; } $ev = new BlockGrowEvent($this, $block); @@ -100,7 +101,7 @@ abstract class Crops extends Flowable{ } public function onRandomTick() : void{ - if($this->age < 7 && mt_rand(0, 2) === 1){ + if($this->age < self::MAX_AGE && mt_rand(0, 2) === 1){ $block = clone $this; ++$block->age; $ev = new BlockGrowEvent($this, $block); diff --git a/src/block/Farmland.php b/src/block/Farmland.php index 81f6a43e0..cdb7c816c 100644 --- a/src/block/Farmland.php +++ b/src/block/Farmland.php @@ -33,6 +33,7 @@ use pocketmine\math\Facing; use function lcg_value; class Farmland extends Transparent{ + public const MAX_WETNESS = 7; protected int $wetness = 0; //"moisture" blockstate property in PC @@ -41,7 +42,7 @@ class Farmland extends Transparent{ } public function readStateFromData(int $id, int $stateMeta) : void{ - $this->wetness = BlockDataSerializer::readBoundedInt("wetness", $stateMeta, 0, 7); + $this->wetness = BlockDataSerializer::readBoundedInt("wetness", $stateMeta, 0, self::MAX_WETNESS); } public function getStateBitmask() : int{ @@ -52,8 +53,8 @@ class Farmland extends Transparent{ /** @return $this */ public function setWetness(int $wetness) : self{ - if($wetness < 0 || $wetness > 7){ - throw new \InvalidArgumentException("Wetness must be in range 0-7"); + if($wetness < 0 || $wetness > self::MAX_WETNESS){ + throw new \InvalidArgumentException("Wetness must be in range 0 ... " . self::MAX_WETNESS); } $this->wetness = $wetness; return $this; @@ -84,8 +85,8 @@ class Farmland extends Transparent{ }else{ $this->position->getWorld()->setBlock($this->position, VanillaBlocks::DIRT()); } - }elseif($this->wetness < 7){ - $this->wetness = 7; + }elseif($this->wetness < self::MAX_WETNESS){ + $this->wetness = self::MAX_WETNESS; $this->position->getWorld()->setBlock($this->position, $this, false); } } diff --git a/src/block/Fire.php b/src/block/Fire.php index 1b73882b0..d6919d408 100644 --- a/src/block/Fire.php +++ b/src/block/Fire.php @@ -41,6 +41,7 @@ use function min; use function mt_rand; class Fire extends Flowable{ + public const MAX_AGE = 15; protected int $age = 0; @@ -49,7 +50,7 @@ class Fire extends Flowable{ } public function readStateFromData(int $id, int $stateMeta) : void{ - $this->age = BlockDataSerializer::readBoundedInt("age", $stateMeta, 0, 15); + $this->age = BlockDataSerializer::readBoundedInt("age", $stateMeta, 0, self::MAX_AGE); } public function getStateBitmask() : int{ @@ -60,8 +61,8 @@ class Fire extends Flowable{ /** @return $this */ public function setAge(int $age) : self{ - if($age < 0 || $age > 15){ - throw new \InvalidArgumentException("Age must be in range 0-15"); + if($age < 0 || $age > self::MAX_AGE){ + throw new \InvalidArgumentException("Age must be in range 0 ... " . self::MAX_AGE); } $this->age = $age; return $this; @@ -114,7 +115,7 @@ class Fire extends Flowable{ $down = $this->getSide(Facing::DOWN); $result = null; - if($this->age < 15 && mt_rand(0, 2) === 0){ + if($this->age < self::MAX_AGE && mt_rand(0, 2) === 0){ $this->age++; $result = $this; } @@ -122,7 +123,7 @@ class Fire extends Flowable{ if(!$down->burnsForever()){ //TODO: check rain - if($this->age === 15){ + if($this->age === self::MAX_AGE){ if(!$down->isFlammable() && mt_rand(0, 3) === 3){ //1/4 chance to extinguish $canSpread = false; $result = VanillaBlocks::AIR(); @@ -183,7 +184,7 @@ class Fire extends Flowable{ $spreadedFire = false; if(mt_rand(0, $this->age + 9) < 5){ //TODO: check rain $fire = clone $this; - $fire->age = min(15, $fire->age + (mt_rand(0, 4) >> 2)); + $fire->age = min(self::MAX_AGE, $fire->age + (mt_rand(0, 4) >> 2)); $spreadedFire = $this->spreadBlock($block, $fire); } if(!$spreadedFire){ @@ -243,7 +244,7 @@ class Fire extends Flowable{ if($maxChance > 0 && mt_rand(0, $randomBound - 1) <= $maxChance){ $new = clone $this; - $new->age = min(15, $this->age + (mt_rand(0, 4) >> 2)); + $new->age = min(self::MAX_AGE, $this->age + (mt_rand(0, 4) >> 2)); $this->spreadBlock($block, $new); } } diff --git a/src/block/FrostedIce.php b/src/block/FrostedIce.php index 217263e92..c74dfc92c 100644 --- a/src/block/FrostedIce.php +++ b/src/block/FrostedIce.php @@ -28,11 +28,12 @@ use pocketmine\event\block\BlockMeltEvent; use function mt_rand; class FrostedIce extends Ice{ + public const MAX_AGE = 3; protected int $age = 0; public function readStateFromData(int $id, int $stateMeta) : void{ - $this->age = BlockDataSerializer::readBoundedInt("age", $stateMeta, 0, 3); + $this->age = BlockDataSerializer::readBoundedInt("age", $stateMeta, 0, self::MAX_AGE); } protected function writeStateToMeta() : int{ @@ -47,8 +48,8 @@ class FrostedIce extends Ice{ /** @return $this */ public function setAge(int $age) : self{ - if($age < 0 || $age > 3){ - throw new \InvalidArgumentException("Age must be in range 0-3"); + if($age < 0 || $age > self::MAX_AGE){ + throw new \InvalidArgumentException("Age must be in range 0 ... " . self::MAX_AGE); } $this->age = $age; return $this; @@ -105,7 +106,7 @@ class FrostedIce extends Ice{ * @return bool Whether the ice was destroyed. */ private function tryMelt() : bool{ - if($this->age >= 3){ + if($this->age >= self::MAX_AGE){ $ev = new BlockMeltEvent($this, VanillaBlocks::WATER()); $ev->call(); if(!$ev->isCancelled()){ diff --git a/src/block/Liquid.php b/src/block/Liquid.php index 46e9c487d..3bff3fca7 100644 --- a/src/block/Liquid.php +++ b/src/block/Liquid.php @@ -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); diff --git a/src/block/NetherWartPlant.php b/src/block/NetherWartPlant.php index c6b441915..0a809c950 100644 --- a/src/block/NetherWartPlant.php +++ b/src/block/NetherWartPlant.php @@ -33,6 +33,7 @@ use pocketmine\world\BlockTransaction; use function mt_rand; class NetherWartPlant extends Flowable{ + public const MAX_AGE = 3; protected int $age = 0; @@ -41,7 +42,7 @@ class NetherWartPlant extends Flowable{ } public function readStateFromData(int $id, int $stateMeta) : void{ - $this->age = BlockDataSerializer::readBoundedInt("age", $stateMeta, 0, 3); + $this->age = BlockDataSerializer::readBoundedInt("age", $stateMeta, 0, self::MAX_AGE); } public function getStateBitmask() : int{ @@ -52,8 +53,8 @@ class NetherWartPlant extends Flowable{ /** @return $this */ public function setAge(int $age) : self{ - if($age < 0 || $age > 3){ - throw new \InvalidArgumentException("Age must be in range 0-3"); + if($age < 0 || $age > self::MAX_AGE){ + throw new \InvalidArgumentException("Age must be in range 0 ..." . self::MAX_AGE); } $this->age = $age; return $this; @@ -79,7 +80,7 @@ class NetherWartPlant extends Flowable{ } public function onRandomTick() : void{ - if($this->age < 3 && mt_rand(0, 10) === 0){ //Still growing + if($this->age < self::MAX_AGE && mt_rand(0, 10) === 0){ //Still growing $block = clone $this; $block->age++; $ev = new BlockGrowEvent($this, $block); @@ -92,7 +93,7 @@ class NetherWartPlant extends Flowable{ public function getDropsForCompatibleTool(Item $item) : array{ return [ - $this->asItem()->setCount($this->age === 3 ? mt_rand(2, 4) : 1) + $this->asItem()->setCount($this->age === self::MAX_AGE ? mt_rand(2, 4) : 1) ]; } } diff --git a/src/block/Potato.php b/src/block/Potato.php index 1bfda0d5c..af1443cae 100644 --- a/src/block/Potato.php +++ b/src/block/Potato.php @@ -31,9 +31,9 @@ class Potato extends Crops{ public function getDropsForCompatibleTool(Item $item) : array{ $result = [ - VanillaItems::POTATO()->setCount($this->age >= 7 ? mt_rand(1, 5) : 1) + VanillaItems::POTATO()->setCount($this->age >= self::MAX_AGE ? mt_rand(1, 5) : 1) ]; - if($this->age >= 7 && mt_rand(0, 49) === 0){ + if($this->age >= self::MAX_AGE && mt_rand(0, 49) === 0){ $result[] = VanillaItems::POISONOUS_POTATO(); } return $result; diff --git a/src/block/RedstoneRepeater.php b/src/block/RedstoneRepeater.php index b0fb793fd..839393248 100644 --- a/src/block/RedstoneRepeater.php +++ b/src/block/RedstoneRepeater.php @@ -37,9 +37,12 @@ class RedstoneRepeater extends Flowable{ use HorizontalFacingTrait; use PoweredByRedstoneTrait; + public const MIN_DELAY = 1; + public const MAX_DELAY = 4; + protected BlockIdentifierFlattened $idInfoFlattened; - protected int $delay = 1; + protected int $delay = self::MIN_DELAY; public function __construct(BlockIdentifierFlattened $idInfo, string $name, BlockBreakInfo $breakInfo){ $this->idInfoFlattened = $idInfo; @@ -52,7 +55,7 @@ class RedstoneRepeater extends Flowable{ public function readStateFromData(int $id, int $stateMeta) : void{ $this->facing = BlockDataSerializer::readLegacyHorizontalFacing($stateMeta & 0x03); - $this->delay = BlockDataSerializer::readBoundedInt("delay", ($stateMeta >> 2) + 1, 1, 4); + $this->delay = BlockDataSerializer::readBoundedInt("delay", ($stateMeta >> 2) + 1, self::MIN_DELAY, self::MAX_DELAY); $this->powered = $id === $this->idInfoFlattened->getSecondId(); } @@ -68,8 +71,8 @@ class RedstoneRepeater extends Flowable{ /** @return $this */ public function setDelay(int $delay) : self{ - if($delay < 1 || $delay > 4){ - throw new \InvalidArgumentException("Delay must be in range 1-4"); + if($delay < self::MIN_DELAY || $delay > self::MAX_DELAY){ + throw new \InvalidArgumentException("Delay must be in range " . self::MIN_DELAY . " ... " . self::MAX_DELAY); } $this->delay = $delay; return $this; @@ -95,8 +98,8 @@ class RedstoneRepeater extends Flowable{ } public function onInteract(Item $item, int $face, Vector3 $clickVector, ?Player $player = null) : bool{ - if(++$this->delay > 4){ - $this->delay = 1; + if(++$this->delay > self::MAX_DELAY){ + $this->delay = self::MIN_DELAY; } $this->position->getWorld()->setBlock($this->position, $this); return true; diff --git a/src/block/SeaPickle.php b/src/block/SeaPickle.php index fb931f004..f32107a80 100644 --- a/src/block/SeaPickle.php +++ b/src/block/SeaPickle.php @@ -30,8 +30,10 @@ use pocketmine\player\Player; use pocketmine\world\BlockTransaction; class SeaPickle extends Transparent{ + public const MIN_COUNT = 1; + public const MAX_COUNT = 4; - protected int $count = 1; + protected int $count = self::MIN_COUNT; protected bool $underwater = false; public function readStateFromData(int $id, int $stateMeta) : void{ @@ -51,8 +53,8 @@ class SeaPickle extends Transparent{ /** @return $this */ public function setCount(int $count) : self{ - if($count < 1 || $count > 4){ - throw new \InvalidArgumentException("Count must be in range 1-4"); + if($count < self::MIN_COUNT || $count > self::MAX_COUNT){ + throw new \InvalidArgumentException("Count must be in range " . self::MIN_COUNT . " ... " . self::MAX_COUNT); } $this->count = $count; return $this; @@ -83,12 +85,12 @@ class SeaPickle extends Transparent{ public function canBePlacedAt(Block $blockReplace, Vector3 $clickVector, int $face, bool $isClickedBlock) : bool{ //TODO: proper placement logic (needs a supporting face below) - return ($blockReplace instanceof SeaPickle && $blockReplace->count < 4) || parent::canBePlacedAt($blockReplace, $clickVector, $face, $isClickedBlock); + return ($blockReplace instanceof SeaPickle && $blockReplace->count < self::MAX_COUNT) || parent::canBePlacedAt($blockReplace, $clickVector, $face, $isClickedBlock); } public function place(BlockTransaction $tx, Item $item, Block $blockReplace, Block $blockClicked, int $face, Vector3 $clickVector, ?Player $player = null) : bool{ $this->underwater = false; //TODO: implement this once we have new water logic in place - if($blockReplace instanceof SeaPickle && $blockReplace->count < 4){ + if($blockReplace instanceof SeaPickle && $blockReplace->count < self::MAX_COUNT){ $this->count = $blockReplace->count + 1; } diff --git a/src/block/Skull.php b/src/block/Skull.php index b403924cf..84542635b 100644 --- a/src/block/Skull.php +++ b/src/block/Skull.php @@ -36,12 +36,14 @@ use function assert; use function floor; class Skull extends Flowable{ + public const MIN_ROTATION = 0; + public const MAX_ROTATION = 15; protected SkullType $skullType; protected int $facing = Facing::NORTH; protected bool $noDrops = false; - protected int $rotation = 0; //TODO: split this into floor skull and wall skull handling + protected int $rotation = self::MIN_ROTATION; //TODO: split this into floor skull and wall skull handling public function __construct(BlockIdentifier $idInfo, string $name, BlockBreakInfo $breakInfo){ $this->skullType = SkullType::SKELETON(); //TODO: this should be a parameter @@ -105,8 +107,8 @@ class Skull extends Flowable{ /** @return $this */ public function setRotation(int $rotation) : self{ - if($rotation < 0 || $rotation > 15){ - throw new \InvalidArgumentException("Rotation must be a value between 0 and 15"); + if($rotation < self::MIN_ROTATION || $rotation > self::MAX_ROTATION){ + throw new \InvalidArgumentException("Rotation must be in range " . self::MIN_ROTATION . " ... " . self::MAX_ROTATION); } $this->rotation = $rotation; return $this; diff --git a/src/block/SnowLayer.php b/src/block/SnowLayer.php index 015733a76..be5acb6dc 100644 --- a/src/block/SnowLayer.php +++ b/src/block/SnowLayer.php @@ -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; diff --git a/src/block/Stem.php b/src/block/Stem.php index e223b18be..c7fd444af 100644 --- a/src/block/Stem.php +++ b/src/block/Stem.php @@ -35,7 +35,7 @@ abstract class Stem extends Crops{ public function onRandomTick() : void{ if(mt_rand(0, 2) === 1){ - if($this->age < 7){ + if($this->age < self::MAX_AGE){ $block = clone $this; ++$block->age; $ev = new BlockGrowEvent($this, $block); diff --git a/src/block/Sugarcane.php b/src/block/Sugarcane.php index d2c13308a..b90efa77e 100644 --- a/src/block/Sugarcane.php +++ b/src/block/Sugarcane.php @@ -33,6 +33,7 @@ use pocketmine\player\Player; use pocketmine\world\BlockTransaction; class Sugarcane extends Flowable{ + public const MAX_AGE = 15; protected int $age = 0; @@ -41,7 +42,7 @@ class Sugarcane extends Flowable{ } public function readStateFromData(int $id, int $stateMeta) : void{ - $this->age = BlockDataSerializer::readBoundedInt("age", $stateMeta, 0, 15); + $this->age = BlockDataSerializer::readBoundedInt("age", $stateMeta, 0, self::MAX_AGE); } public function getStateBitmask() : int{ @@ -76,8 +77,8 @@ class Sugarcane extends Flowable{ /** @return $this */ public function setAge(int $age) : self{ - if($age < 0 || $age > 15){ - throw new \InvalidArgumentException("Age must be in range 0-15"); + if($age < 0 || $age > self::MAX_AGE){ + throw new \InvalidArgumentException("Age must be in range 0 ... " . self::MAX_AGE); } $this->age = $age; return $this; @@ -108,7 +109,7 @@ class Sugarcane extends Flowable{ public function onRandomTick() : void{ if(!$this->getSide(Facing::DOWN)->isSameType($this)){ - if($this->age === 15){ + if($this->age === self::MAX_AGE){ $this->grow(); }else{ ++$this->age; diff --git a/src/block/Wheat.php b/src/block/Wheat.php index 5261e38ec..8dabf8c00 100644 --- a/src/block/Wheat.php +++ b/src/block/Wheat.php @@ -30,7 +30,7 @@ use function mt_rand; class Wheat extends Crops{ public function getDropsForCompatibleTool(Item $item) : array{ - if($this->age >= 7){ + if($this->age >= self::MAX_AGE){ return [ VanillaItems::WHEAT(), VanillaItems::WHEAT_SEEDS()->setCount(mt_rand(0, 3))