I'm working on a bosses plugin, but idk how to make the entity target and attack the players. Current code: PHP: public function onTap(PlayerInteractEvent $event) { $user = $event->getPlayer(); $item = $event->getItem(); $player = $event->getPlayer(); if ($item->getId() == 352 && $item->getDamage() == 1) { $summoner = Item::get(352, 1, 1); $this->hadesBoss($player->getLevel(), $player); $user->getInventory()->removeItem($summoner); } } public function hadesBoss(Level $level, Position $pos) { $entity = Entity::createEntity("Zombie", $level, Entity::createBaseNBT($pos)); $entity->setMaxHealth(500); $entity->setHealth(500); $entity->setNameTag("§c§lHades§r§7 Boss\n Health:§c " . $entity->getHealth()); $entity->setNameTagAlwaysVisible(); $entity->spawnToAll(); }
It will attack the attacker:. PHP: public function onEntityDamageEvent(EntityDamageEvent $event){ $player = $event->getEntity(); $x = $player->getX(); $y = $player->getY(); $z = $player->getZ(); $vector = new Vector3(mt_rand(0.10, -0.10), 0, mt_rand(0.10, -0.10)); if($event instanceof EntityDamageByEntityEvent){ $damager = $event->getDamager(); if($damager instanceof Player && $player instanceof Zombie){ $event->setKnockBack(1); if($player->getHealth() > $event->getDamage()){ $damager->sendTip("§8[§c{$player->getHealth()}§8/§a5000§8] §c♥"); $random = mt_rand(1, 100); if($random == 1){ $damager->setHealth($damager->getHealth() - 4); $damager->sendMessage(" §8§a[§eBoss§a] §fYou lost §a4 HP§f"); } if($random == 2){ $player->setHealth($player->getHealth() + 6); $damager->sendMessage(" §8§a[§eBoss§a] §cBoss§a regen §e6 HP§f!"); } if($random == 3){ $damager->setHealth($damager->getHealth() - 3); $damager->sendMessage("§8§a[§eBoss§a] §fYou loss §a4 HP§f!"); } } } } }
My bad. The other plugin will make a human entity that might be the BotPVP: https://github.com/Rex-Tech/MyNPC/tree/master/src/jb
Get the nearest player, check if the player is in a certain range. Cache the player into the entity class. And then you can work with that.
You can take reference from here https://github.com/diamond-gold/MiniBosses/blob/master/src/MiniBosses/Boss.php#L151
look into: https://github.com/RevivalPMMP/Pure...ureentities/entity/monster/walking/Zombie.php which extends: https://github.com/RevivalPMMP/Pure...ureentities/entity/monster/WalkingMonster.php which extends: https://github.com/RevivalPMMP/Pure...valpmmp/pureentities/entity/WalkingEntity.php which extends: https://github.com/RevivalPMMP/Pure...evivalpmmp/pureentities/entity/BaseEntity.php which extends the Creature class of pocketmine please don't copy paste the attack function, because there are other codes on that function in it's extensions which make it work
PHP: public function getTarget(){ return Server::getInstance()->getPlayerExact((string) $this->target); } public function atRandomPosition(){ return $this->getRandomPosition() == null || $this->distance($this->getRandomPosition()) <= 2; } public function getRandomPosition(){ return $this->randomPosition; } public function generateRandomPosition(){ $minX = $this->getFloorX() - 8; $minY = $this->getFloorY() - 8; $minZ = $this->getFloorZ() - 8; $maxX = $minX + 16; $maxY = $minY + 16; $maxZ = $minZ + 16; $level = $this->getLevel(); for($attempts = 0; $attempts < 16; ++$attempts){ $x = mt_rand($minX, $maxX); $y = mt_rand($minY, $maxY); $z = mt_rand($minZ, $maxZ); while($y >= 0 and !$level->getBlockAt($x, $y, $z)->isSolid()){ $y--; } if($y < 0){ continue; } $blockUp = $level->getBlockAt($x, $y + 1, $z); $blockUp2 = $level->getBlockAt($x, $y + 2, $z); if($blockUp->isSolid() or $blockUp instanceof Liquid or $blockUp2->isSolid() or $blockUp2 instanceof Liquid){ continue; } break; } $this->randomPosition = new Vector3($x, $y + 1, $z); } public function getSpeed(){ return ($this->isUnderwater() ? $this->speed / 2 : $this->speed); }