From b9ec63f01666db349f730a3e23c33217da5bf50a Mon Sep 17 00:00:00 2001 From: Shoghi Cervantes Date: Wed, 28 May 2014 14:41:09 +0200 Subject: [PATCH] Fixed getting Entities by area --- src/pocketmine/Player.php | 5 +++-- src/pocketmine/entity/Entity.php | 31 ------------------------------- src/pocketmine/level/Level.php | 31 +++++++++++++++++++++++++++++++ 3 files changed, 34 insertions(+), 33 deletions(-) diff --git a/src/pocketmine/Player.php b/src/pocketmine/Player.php index d89f895da..1b1b2778f 100644 --- a/src/pocketmine/Player.php +++ b/src/pocketmine/Player.php @@ -1203,7 +1203,7 @@ class Player extends Human implements CommandSender, InventoryHolder, IPlayer{ return true; } $hasUpdate = $this->entityBaseTick(); - foreach($this->getNearbyEntities($this->boundingBox->expand(3, 3, 3)) as $entity){ + foreach($this->getLevel()->getCollidingEntities($this->boundingBox->expand(1.5, 1, 1.5), $this) as $entity){ if($entity instanceof DroppedItem){ if($entity->dead !== true and $entity->getPickupDelay() <= 0){ $item = $entity->getItem(); @@ -1227,6 +1227,8 @@ class Player extends Human implements CommandSender, InventoryHolder, IPlayer{ break; } + $this->inventory->addItem(clone $item); + $pk = new TakeItemEntityPacket; $pk->eid = 0; $pk->target = $entity->getID(); @@ -1236,7 +1238,6 @@ class Player extends Human implements CommandSender, InventoryHolder, IPlayer{ $pk->eid = $this->getID(); $pk->target = $entity->getID(); $this->server->broadcastPacket($entity->getViewers(), $pk); - $this->inventory->addItem(clone $item); $entity->kill(); } } diff --git a/src/pocketmine/entity/Entity.php b/src/pocketmine/entity/Entity.php index 7b5ef5b4c..12fcd1704 100644 --- a/src/pocketmine/entity/Entity.php +++ b/src/pocketmine/entity/Entity.php @@ -591,37 +591,6 @@ abstract class Entity extends Position implements Metadatable{ $this->fallDistance = 0; } - /** - * Returns the entities near the current one inside the AxisAlignedBB - * - * @param AxisAlignedBB $bb - * - * @return Entity[] - */ - public function getNearbyEntities(AxisAlignedBB $bb){ - - $nearby = []; - - $minX = ($bb->minX - 2) >> 4; - $maxX = ($bb->maxX + 2) >> 4; - $minZ = ($bb->minZ - 2) >> 4; - $maxZ = ($bb->maxZ + 2) >> 4; - - for($x = $minX; $x <= $maxX; ++$x){ - for($z = $minZ; $z <= $maxZ; ++$z){ - if($this->getLevel()->isChunkLoaded($x, $z)){ - foreach($this->getLevel()->getChunkEntities($x, $z) as $ent){ - if($ent !== $this and $ent->boundingBox->intersectsWith($this->boundingBox)){ - $nearby[] = $ent; - } - } - } - } - } - - return $nearby; - } - public function move($dx, $dy, $dz){ //$collision = []; //$this->checkBlockCollision($collision); diff --git a/src/pocketmine/level/Level.php b/src/pocketmine/level/Level.php index bb43f4d70..e30ce755c 100644 --- a/src/pocketmine/level/Level.php +++ b/src/pocketmine/level/Level.php @@ -929,6 +929,37 @@ class Level{ return $this->entities; } + /** + * Returns the entities near the current one inside the AxisAlignedBB + * + * @param AxisAlignedBB $bb + * @param Entity $entity + * + * @return Entity[] + */ + public function getCollidingEntities(AxisAlignedBB $bb, Entity $entity = null){ + $nearby = []; + + $minX = ($bb->minX - 2) >> 4; + $maxX = ($bb->maxX + 2) >> 4; + $minZ = ($bb->minZ - 2) >> 4; + $maxZ = ($bb->maxZ + 2) >> 4; + + for($x = $minX; $x <= $maxX; ++$x){ + for($z = $minZ; $z <= $maxZ; ++$z){ + if($this->isChunkLoaded($x, $z)){ + foreach($this->getChunkEntities($x, $z) as $ent){ + if($ent !== $entity and $ent->boundingBox->intersectsWith($bb)){ + $nearby[] = $ent; + } + } + } + } + } + + return $nearby; + } + public function addEntity(Entity $entity){ //TODO: chunkIndex $this->entities[$entity->getID()] = $entity;