Fixed getting Entities by area

This commit is contained in:
Shoghi Cervantes 2014-05-28 14:41:09 +02:00
parent 9aed430fda
commit b9ec63f016
3 changed files with 34 additions and 33 deletions

View File

@ -1203,7 +1203,7 @@ class Player extends Human implements CommandSender, InventoryHolder, IPlayer{
return true; return true;
} }
$hasUpdate = $this->entityBaseTick(); $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 instanceof DroppedItem){
if($entity->dead !== true and $entity->getPickupDelay() <= 0){ if($entity->dead !== true and $entity->getPickupDelay() <= 0){
$item = $entity->getItem(); $item = $entity->getItem();
@ -1227,6 +1227,8 @@ class Player extends Human implements CommandSender, InventoryHolder, IPlayer{
break; break;
} }
$this->inventory->addItem(clone $item);
$pk = new TakeItemEntityPacket; $pk = new TakeItemEntityPacket;
$pk->eid = 0; $pk->eid = 0;
$pk->target = $entity->getID(); $pk->target = $entity->getID();
@ -1236,7 +1238,6 @@ class Player extends Human implements CommandSender, InventoryHolder, IPlayer{
$pk->eid = $this->getID(); $pk->eid = $this->getID();
$pk->target = $entity->getID(); $pk->target = $entity->getID();
$this->server->broadcastPacket($entity->getViewers(), $pk); $this->server->broadcastPacket($entity->getViewers(), $pk);
$this->inventory->addItem(clone $item);
$entity->kill(); $entity->kill();
} }
} }

View File

@ -591,37 +591,6 @@ abstract class Entity extends Position implements Metadatable{
$this->fallDistance = 0; $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){ public function move($dx, $dy, $dz){
//$collision = []; //$collision = [];
//$this->checkBlockCollision($collision); //$this->checkBlockCollision($collision);

View File

@ -929,6 +929,37 @@ class Level{
return $this->entities; 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){ public function addEntity(Entity $entity){
//TODO: chunkIndex //TODO: chunkIndex
$this->entities[$entity->getID()] = $entity; $this->entities[$entity->getID()] = $entity;