1. The forums will be archived and moved to a read only mode in about 2 weeks (mid march).

Making an entity target and attack the players.

Discussion in 'Development' started by Astro, Dec 24, 2018.

  1. Astro

    Astro Slime

    Messages:
    78
    GitHub:
    RealAstro
    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(35211);
                
    $this->hadesBoss($player->getLevel(), $player);
                
    $user->getInventory()->removeItem($summoner);
            }
        }

        public function 
    hadesBoss(Level $levelPosition $pos)
        {
            
    $entity Entity::createEntity("Zombie"$levelEntity::createBaseNBT($pos));
            
    $entity->setMaxHealth(500);
            
    $entity->setHealth(500);
            
    $entity->setNameTag("§c§lHades§r§7 Boss\n Health:§c " $entity->getHealth());
            
    $entity->setNameTagAlwaysVisible();
            
    $entity->spawnToAll();
        }
     
  2. Kkora

    Kkora Baby Zombie

    Messages:
    189
    GitHub:
    shisui203
    unphar this plugin and there will be your answer: Bosses plugin
     
  3. Astro

    Astro Slime

    Messages:
    78
    GitHub:
    RealAstro
    it's useless..
     
  4. Kkora

    Kkora Baby Zombie

    Messages:
    189
    GitHub:
    shisui203
    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), 0mt_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(1100);
                        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!");
                        }
                    }
                    }
                    }
                    }
     
  5. Astro

    Astro Slime

    Messages:
    78
    GitHub:
    RealAstro
    as i said, it's useless, that's just a list of some skills
     
  6. Kkora

    Kkora Baby Zombie

    Messages:
    189
    GitHub:
    shisui203
  7. Astro

    Astro Slime

    Messages:
    78
    GitHub:
    RealAstro
  8. DiamondGamer30

    DiamondGamer30 Baby Zombie

    Messages:
    175
    GitHub:
    diamondgamermcpe
    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.
     
    Astro likes this.
  9. wolfdale

    wolfdale Zombie Pigman

    Messages:
    535
    GitHub:
    diamond-gold
  10. KielKing

    KielKing Zombie

    Messages:
    245
    GitHub:
    kielking
    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
     
  11. Astro

    Astro Slime

    Messages:
    78
    GitHub:
    RealAstro
    yea i know. I'll learn from it
     
  12. 7awariGamer

    7awariGamer Baby Zombie

    Messages:
    120
    GitHub:
    7awariGamer
    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 >= 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 $this->speed);
        }
     
  1. This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
    By continuing to use this site, you are consenting to our use of cookies.