From 9655cb819d85192c0e6f4ca9c9538dd81f56fc1d Mon Sep 17 00:00:00 2001 From: "Dylan K. Taylor" Date: Thu, 15 Apr 2021 19:41:03 +0100 Subject: [PATCH] World: Remove duplicated code --- src/world/World.php | 33 +++++++++++---------------------- 1 file changed, 11 insertions(+), 22 deletions(-) diff --git a/src/world/World.php b/src/world/World.php index b0798a18a..27da4673c 100644 --- a/src/world/World.php +++ b/src/world/World.php @@ -1293,9 +1293,9 @@ class World implements ChunkManager{ } /** - * Returns the highest block light level available in the positions adjacent to the specified block coordinates. + * @phpstan-param \Closure(int $x, int $y, int $z) : int $lightGetter */ - public function getHighestAdjacentPotentialBlockSkyLight(int $x, int $y, int $z) : int{ + private function getHighestAdjacentLight(int $x, int $y, int $z, \Closure $lightGetter) : int{ $max = 0; foreach([ [$x + 1, $y, $z], @@ -1312,11 +1312,18 @@ class World implements ChunkManager{ ){ continue; } - $max = max($max, $this->getPotentialBlockSkyLightAt($x1, $y1, $z1)); + $max = max($max, $lightGetter($x1, $y1, $z1)); } return $max; } + /** + * Returns the highest block light level available in the positions adjacent to the specified block coordinates. + */ + public function getHighestAdjacentPotentialBlockSkyLight(int $x, int $y, int $z) : int{ + return $this->getHighestAdjacentLight($x, $y, $z, \Closure::fromCallable([$this, 'getPotentialBlockSkyLightAt'])); + } + /** * Returns the highest block sky light available in the positions adjacent to the given coordinates, adjusted for * the world's current time of day and weather conditions. @@ -1329,25 +1336,7 @@ class World implements ChunkManager{ * Returns the highest block light level available in the positions adjacent to the specified block coordinates. */ public function getHighestAdjacentBlockLight(int $x, int $y, int $z) : int{ - $max = 0; - foreach([ - [$x + 1, $y, $z], - [$x - 1, $y, $z], - [$x, $y + 1, $z], - [$x, $y - 1, $z], - [$x, $y, $z + 1], - [$x, $y, $z - 1] - ] as [$x1, $y1, $z1]){ - if( - !$this->isInWorld($x1, $y1, $z1) || - ($chunk = $this->getChunk($x1 >> 4, $z1 >> 4)) === null || - $chunk->isLightPopulated() !== true - ){ - continue; - } - $max = max($max, $this->getBlockLightAt($x1, $y1, $z1)); - } - return $max; + return $this->getHighestAdjacentLight($x, $y, $z, \Closure::fromCallable([$this, 'getBlockLightAt'])); } private function executeQueuedLightUpdates() : void{