From 457660235e69668b0c92b484e619c7ed2082cd5e Mon Sep 17 00:00:00 2001 From: "Dylan K. Taylor" Date: Mon, 6 Nov 2023 16:02:57 +0000 Subject: [PATCH] Crops must have access to a light level of at least 9 --- src/block/utils/CropGrowthHelper.php | 12 ++++++++- src/world/World.php | 38 ++++++++++++++++++++++------ 2 files changed, 41 insertions(+), 9 deletions(-) diff --git a/src/block/utils/CropGrowthHelper.php b/src/block/utils/CropGrowthHelper.php index 446c00887..b5fc31362 100644 --- a/src/block/utils/CropGrowthHelper.php +++ b/src/block/utils/CropGrowthHelper.php @@ -36,6 +36,8 @@ final class CropGrowthHelper{ private const IMPROPER_ARRANGEMENT_DIVISOR = 2; + private const MIN_LIGHT_LEVEL = 9; + private function __construct(){ //NOOP } @@ -102,9 +104,17 @@ final class CropGrowthHelper{ return $result; } + public static function hasEnoughLight(Block $block, int $minLevel = self::MIN_LIGHT_LEVEL) : bool{ + $position = $block->getPosition(); + $world = $position->getWorld(); + + //crop growth is not affected by time of day since 1.11 or so + return $world->getPotentialLight($position) >= $minLevel; + } + public static function canGrow(Block $block) : bool{ //while it may be tempting to use mt_rand(0, 25) < multiplier, this would make crops grow a bit faster than //vanilla in most cases due to the remainder of 25 / multiplier not being discarded - return mt_rand(0, (int) (25 / self::calculateMultiplier($block))) === 0; + return mt_rand(0, (int) (25 / self::calculateMultiplier($block))) === 0 && self::hasEnoughLight($block); } } diff --git a/src/world/World.php b/src/world/World.php index 3e1131272..59b02be1a 100644 --- a/src/world/World.php +++ b/src/world/World.php @@ -1666,16 +1666,16 @@ class World implements ChunkManager{ } /** - * Returns the highest available level of any type of light at the given coordinates, adjusted for the current - * weather and time of day. + * Returns the highest level of any type of light at the given coordinates, adjusted for the current weather and + * time of day. */ public function getFullLight(Vector3 $pos) : int{ return $this->getFullLightAt($pos->x, $pos->y, $pos->z); } /** - * Returns the highest available level of any type of light at the given coordinates, adjusted for the current - * weather and time of day. + * Returns the highest level of any type of light at the given coordinates, adjusted for the current weather and + * time of day. */ public function getFullLightAt(int $x, int $y, int $z) : int{ $skyLight = $this->getRealBlockSkyLightAt($x, $y, $z); @@ -1687,18 +1687,40 @@ class World implements ChunkManager{ } /** - * Returns the highest available level of any type of light at, or adjacent to, the given coordinates, adjusted for - * the current weather and time of day. + * Returns the highest level of any type of light at, or adjacent to, the given coordinates, adjusted for the + * current weather and time of day. */ public function getHighestAdjacentFullLightAt(int $x, int $y, int $z) : int{ return $this->getHighestAdjacentLight($x, $y, $z, $this->getFullLightAt(...)); } + /** + * Returns the highest potential level of any type of light at the target coordinates. + * This is not affected by weather or time of day. + */ + public function getPotentialLight(Vector3 $pos) : int{ + return $this->getPotentialLightAt($pos->x, $pos->y, $pos->z); + } + + /** + * Returns the highest potential level of any type of light at the target coordinates. + * This is not affected by weather or time of day. + */ + public function getPotentialLightAt(int $x, int $y, int $z) : int{ + return max($this->getPotentialBlockSkyLightAt($x, $y, $z), $this->getBlockLightAt($x, $y, $z)); + } + + /** + * Returns the highest potential level of any type of light at, or adjacent to, the given coordinates. + * This is not affected by weather or time of day. + */ + public function getHighestAdjacentPotentialLightAt(int $x, int $y, int $z) : int{ + return $this->getHighestAdjacentLight($x, $y, $z, $this->getPotentialLightAt(...)); + } + /** * Returns the highest potential level of sky light at the target coordinates, regardless of the time of day or * weather conditions. - * You usually don't want to use this for vanilla gameplay logic; prefer the real sky light instead. - * @see World::getRealBlockSkyLightAt() * * @return int 0-15 */