From a6b36d6c3cc83c4d02320d332c7dfbbeba0d34dc Mon Sep 17 00:00:00 2001 From: "Dylan K. Taylor" Date: Thu, 2 Nov 2023 15:32:22 +0000 Subject: [PATCH] CropGrowthHelper: avoid unnecessary checks --- src/block/utils/CropGrowthHelper.php | 27 ++++++++++++++++----------- 1 file changed, 16 insertions(+), 11 deletions(-) diff --git a/src/block/utils/CropGrowthHelper.php b/src/block/utils/CropGrowthHelper.php index 2f2cc1ed3..446c00887 100644 --- a/src/block/utils/CropGrowthHelper.php +++ b/src/block/utils/CropGrowthHelper.php @@ -66,7 +66,8 @@ final class CropGrowthHelper{ $xRow = false; $zRow = false; - $diagonalRow = false; + $improperArrangement = false; + for($x = -1; $x <= 1; $x++){ for($z = -1; $z <= 1; $z++){ if($x === 0 && $z === 0){ @@ -74,23 +75,27 @@ final class CropGrowthHelper{ } $nextFarmland = $world->getBlockAt($baseX + $x, $baseY - 1, $baseZ + $z); - if($nextFarmland instanceof Farmland){ - $result += $nextFarmland->getWetness() > 0 ? self::ADJACENT_HYDRATED_FARMLAND_BONUS : self::ADJACENT_DRY_FARMLAND_BONUS; + if(!$nextFarmland instanceof Farmland){ + continue; } - $nextCrop = $world->getBlockAt($baseX + $x, $baseY, $baseZ + $z); - if($nextCrop->hasSameTypeId($block)){ - match(0){ - $x => $xRow = true, - $z => $zRow = true, - default => $diagonalRow = true, - }; + $result += $nextFarmland->getWetness() > 0 ? self::ADJACENT_HYDRATED_FARMLAND_BONUS : self::ADJACENT_DRY_FARMLAND_BONUS; + + if(!$improperArrangement){ + $nextCrop = $world->getBlockAt($baseX + $x, $baseY, $baseZ + $z); + if($nextCrop->hasSameTypeId($block)){ + match(0){ + $x => $zRow ? $improperArrangement = true : $xRow = true, + $z => $xRow ? $improperArrangement = true : $zRow = true, + default => $improperArrangement = true, + }; + } } } } //crops can be arranged in rows, but the rows must not cross and must be spaced apart by at least one block - if(($xRow && $zRow) || $diagonalRow){ + if($improperArrangement){ $result /= self::IMPROPER_ARRANGEMENT_DIVISOR; }