diff --git a/src/entity/Entity.php b/src/entity/Entity.php index 06edc47e3..2f286bd82 100644 --- a/src/entity/Entity.php +++ b/src/entity/Entity.php @@ -1219,32 +1219,41 @@ abstract class Entity{ $this->onGround = ($wantedY != $dy and $wantedY < 0); } + /** + * Yields all the blocks whose full-cube areas are intersected by the entity's AABB. + * + * @phpstan-return \Generator + */ + protected function getBlocksIntersected(float $inset) : \Generator{ + $minX = (int) floor($this->boundingBox->minX + $inset); + $minY = (int) floor($this->boundingBox->minY + $inset); + $minZ = (int) floor($this->boundingBox->minZ + $inset); + $maxX = (int) floor($this->boundingBox->maxX - $inset); + $maxY = (int) floor($this->boundingBox->maxY - $inset); + $maxZ = (int) floor($this->boundingBox->maxZ - $inset); + + $world = $this->getWorld(); + + for($z = $minZ; $z <= $maxZ; ++$z){ + for($x = $minX; $x <= $maxX; ++$x){ + for($y = $minY; $y <= $maxY; ++$y){ + yield $world->getBlockAt($x, $y, $z); + } + } + } + } + /** * @return Block[] */ protected function getBlocksAroundWithEntityInsideActions() : array{ if($this->blocksAround === null){ - $inset = 0.001; //Offset against floating-point errors - - $minX = (int) floor($this->boundingBox->minX + $inset); - $minY = (int) floor($this->boundingBox->minY + $inset); - $minZ = (int) floor($this->boundingBox->minZ + $inset); - $maxX = (int) floor($this->boundingBox->maxX - $inset); - $maxY = (int) floor($this->boundingBox->maxY - $inset); - $maxZ = (int) floor($this->boundingBox->maxZ - $inset); - $this->blocksAround = []; - $world = $this->getWorld(); - - for($z = $minZ; $z <= $maxZ; ++$z){ - for($x = $minX; $x <= $maxX; ++$x){ - for($y = $minY; $y <= $maxY; ++$y){ - $block = $world->getBlockAt($x, $y, $z); - if($block->hasEntityCollision()){ - $this->blocksAround[] = $block; - } - } + $inset = 0.001; //Offset against floating-point errors + foreach($this->getBlocksIntersected($inset) as $block){ + if($block->hasEntityCollision()){ + $this->blocksAround[] = $block; } } }