diff --git a/src/API/EntityAPI.php b/src/API/EntityAPI.php index a953a91a..95f55ead 100644 --- a/src/API/EntityAPI.php +++ b/src/API/EntityAPI.php @@ -217,9 +217,12 @@ class EntityAPI{ for($chunkX = $minChunkX; $chunkX <= $maxChunkX; ++$chunkX){ for($chunkZ = $minChunkZ; $chunkZ <= $maxChunkZ; ++$chunkZ){ $ind = "$chunkX $chunkZ"; - foreach($center->level->entityListPositioned[$ind] ?? [] as $entid){ - if($this->entities[$entid] instanceof Entity && ($class === false || $this->entities[$entid]->class == $class)){ + foreach($center->level->entityListPositioned[$ind] ?? [] as $ind2 => $entid){ + if(isset($this->entities[$entid]) && $this->entities[$entid] instanceof Entity && ($class === false || $this->entities[$entid]->class == $class)){ $ents[$entid] = $this->entities[$entid]; + }else if(!isset($this->entities[$entid])){ + ConsoleAPI::debug("Removing entity from level array at index $ind/$ind2: $entid"); + unset($center->level->entityListPositioned[$ind][$ind2]); } } } diff --git a/src/entity/Entity.php b/src/entity/Entity.php index a52f1fd6..0342eef1 100644 --- a/src/entity/Entity.php +++ b/src/entity/Entity.php @@ -1029,26 +1029,6 @@ class Entity extends Position $player->dataPacket($pk); $this->player->sendArmor($player); break; - case ENTITY_ITEM: - $pk = new AddItemEntityPacket(); - $pk->eid = $this->eid; - $pk->x = $this->x; - $pk->y = $this->y; - $pk->z = $this->z; - $pk->yaw = $this->yaw; - $pk->pitch = $this->pitch; - $pk->roll = 0; - $pk->item = BlockAPI::getItem($this->type, $this->meta, $this->stack); - $pk->metadata = $this->getMetadata(); - $player->dataPacket($pk); - - $pk = new SetEntityMotionPacket(); - $pk->eid = $this->eid; - $pk->speedX = $this->speedX; - $pk->speedY = $this->speedY; - $pk->speedZ = $this->speedZ; - $player->dataPacket($pk); - break; case ENTITY_FALLING: $pk = new AddEntityPacket(); $pk->eid = $this->eid; diff --git a/src/entity/object/ItemEntity.php b/src/entity/object/ItemEntity.php index 4992d57f..194bd731 100644 --- a/src/entity/object/ItemEntity.php +++ b/src/entity/object/ItemEntity.php @@ -3,6 +3,7 @@ class ItemEntity extends Entity{ const TYPE = "itemSpecial"; const CLASS_TYPE = ENTITY_ITEM; + public static $searchRadiusX = 0.5, $searchRadiusY = 0.0, $searchRadiusZ = 0.5; public $meta, $stack; @@ -30,18 +31,69 @@ class ItemEntity extends Entity{ if($this->delayBeforePickup > 0) --$this->delayBeforePickup; } + public function searchForOtherItemsNearby(){ + $ents = $this->level->getEntitiesInAABBOfType($this->boundingBox->expand(self::$searchRadiusX, self::$searchRadiusY, self::$searchRadiusZ), ENTITY_ITEM); + + foreach($ents as $e){ + $this->tryCombining($e); + } + + } + + public function spawn($player) + { + $pk = new AddItemEntityPacket(); + $pk->eid = $this->eid; + $pk->x = $this->x; + $pk->y = $this->y; + $pk->z = $this->z; + $pk->yaw = $this->yaw; + $pk->pitch = $this->pitch; + $pk->roll = 0; + $pk->item = BlockAPI::getItem($this->type, $this->meta, $this->stack); + $pk->metadata = $this->getMetadata(); + $player->dataPacket($pk); + + $pk = new SetEntityMotionPacket(); + $pk->eid = $this->eid; + $pk->speedX = $this->speedX; + $pk->speedY = $this->speedY; + $pk->speedZ = $this->speedZ; + $player->dataPacket($pk); + } + + + public function tryCombining(Entity $another){ + + if($another->eid == $this->eid) return false; + + if(!$another->closed && !$this->closed){ + if($another->type == $this->type && $another->meta == $this->meta){ + if(($another->stack + $this->stack) > 64) return false; //TODO dynamic stack size + + $another->stack += $this->stack; + $another->age = min($this->age, $another->age); + $this->close(); + //TODO respawn another entity? + } + } + } + public function updateEntityMovement(){ $this->speedY -= 0.04; $this->noClip = false; //TODO pushOutofBlocks $this->move($this->speedX, $this->speedY, $this->speedZ); $var1 = (int)$this->x != (int)$this->lastX || (int)$this->y != (int)$this->lastY || (int)$this->z != (int)$this->lastZ; - if($var1/* || $this->ticksExisted % 25 == 0*/){ //TODO ticksExisted(should be alternative in nc already) + + if($var1 || $this->counter % 25 == 0/* || $this->ticksExisted % 25 == 0*/){ //TODO ticksExisted(should be alternative in nc already) //TODO check material - //TODO search for other items + $this->searchForOtherItemsNearby(); //not in vanilla 0.8.1 } + if($this->closed) return; + $friction = 0.98; if($this->onGround){ @@ -56,6 +108,7 @@ class ItemEntity extends Entity{ if($this->onGround) $this->speedY *= -0.5; - //TODO ++age; despawn after age >= 6000; + ++$this->age; + //TODO despawn after age >= 6000 ?; } } diff --git a/src/world/Level.php b/src/world/Level.php index 7029b0ee..e794a354 100644 --- a/src/world/Level.php +++ b/src/world/Level.php @@ -355,8 +355,6 @@ class Level{ } public function onTick(PocketMinecraftServer $server, $currentTime){ - safe_var_dump($this->players); - console($this->getName()); if(!$this->stopTime) ++$this->time; for($cX = 0; $cX < 16; ++$cX){ for($cZ = 0; $cZ < 16; ++$cZ){ @@ -473,6 +471,28 @@ class Level{ return false; } + public function getEntitiesInAABBOfType(AxisAlignedBB $bb, $class){ + $minChunkX = ((int)($bb->minX)) >> 4; + $minChunkZ = ((int)($bb->minZ)) >> 4; + $maxChunkX = ((int)($bb->maxX)) >> 4; + $maxChunkZ = ((int)($bb->maxZ)) >> 4; + $ents = []; + console("nya $minChunkX $minChunkZ $maxChunkX $maxChunkZ"); + for($chunkX = $minChunkX; $chunkX <= $maxChunkX; ++$chunkX){ + for($chunkZ = $minChunkZ; $chunkZ <= $maxChunkZ; ++$chunkZ){ + $ind = "$chunkX $chunkZ"; + foreach($this->entityListPositioned[$ind] ?? [] as $ind2 => $entid){ + if(isset($this->entityList[$entid]) && $this->entityList[$entid] instanceof Entity && $this->entityList[$entid]->class === $class && $this->entityList[$entid]->boundingBox->intersectsWith($bb)){ + $ents[$entid] = $this->entityList[$entid]; + }else if(!isset($this->entityList[$entid])){ + ConsoleAPI::debug("Removing entity from level array at index $ind/$ind2: $entid"); + unset($this->entityListPositioned[$ind][$ind2]); + } + } + } + } + return $ents; + } public function getEntitiesInAABB(AxisAlignedBB $bb){ $minChunkX = ((int)($bb->minX)) >> 4; @@ -484,9 +504,12 @@ class Level{ for($chunkX = $minChunkX; $chunkX <= $maxChunkX; ++$chunkX){ for($chunkZ = $minChunkZ; $chunkZ <= $maxChunkZ; ++$chunkZ){ $ind = "$chunkX $chunkZ"; - foreach($this->level->entityListPositioned[$ind] ?? [] as $entid){ - if($this->entityList[$entid] instanceof Entity && $this->entityList[$entid]->boundingBox->intersectsWith($bb)){ + foreach($this->entityListPositioned[$ind] ?? [] as $ind2 => $entid){ + if(isset($this->entityList[$entid]) && $this->entityList[$entid] instanceof Entity && $this->entityList[$entid]->boundingBox->intersectsWith($bb)){ $ents[$entid] = $this->entityList[$entid]; + }else if(!isset($this->entityList[$entid])){ + ConsoleAPI::debug("Removing entity from level array at index $ind/$ind2: $entid"); + unset($this->entityListPositioned[$ind][$ind2]); } } }