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

Make entity fight back

Discussion in 'Development' started by RyanShaw, May 28, 2018.

  1. RyanShaw

    RyanShaw Witch

    Messages:
    69
    I want to have it so when an entity like a zombie for example is hit by a player the entity walks towards the player and attacks them back. I spawn the entity like so
    PHP:
            $nbt = new CompoundTag('', [
        
    'Pos' => new ListTag('Pos', [
            new 
    DoubleTag(''253),
            new 
    DoubleTag(''4),
            new 
    DoubleTag(''253)
        ]),
        
    'Motion' => new ListTag('Motion', [
            new 
    DoubleTag(''0),
            new 
    DoubleTag(''0),
            new 
    DoubleTag(''0)
        ]),
        
    'Rotation' => new ListTag('Rotation', [
            new 
    FloatTag(''lcg_value() * 360),
            new 
    FloatTag(''0)
        ]),
    ]);

            
    $entity Entity::createEntity(Entity::ZOMBIE$this->getServer()->getDefaultLevel(), $nbt);       
            
    $entity->spawnToAll();
    Can anyone teach me how to achieve this behavior? Any help at all is appreciated :p
     
  2. KYUMA

    KYUMA Silverfish

    Messages:
    16
    GitHub:
    Ky75
    Try this:
    Code:
    $nbt = $this->makeNBT("Human", $player);
                $human = Entity::createEntity("Human", $player->getLevel(), $nbt);
                $human->setMaxHealth(20);
                $human->setHealth(20);
                $human->setScale(1);
                $human->setNameTag($player->getName());
                $human->setNameTagVisible(true);
                $human->spawnToAll();
    
    makeNBT function:
    Code:
    private function makeNBT($type, Player $player) {
            $nbt = new CompoundTag;
            $nbt->Pos = new ListTag("Pos", [
                new DoubleTag(0, $player->getX()),
                new DoubleTag(1, $player->getY() + 2),
                new DoubleTag(2, $player->getZ())
            ]);
            $nbt->Motion = new ListTag("Motion", [
                new DoubleTag(0, 0),
                new DoubleTag(1, 0),
                new DoubleTag(2, 0)
            ]);
            $nbt->Rotation = new ListTag("Rotation", [
                new FloatTag(0, $player->getYaw()),
                new FloatTag(1, $player->getPitch())
            ]);
            if($type === "Human") {
                $player->saveNBT();
                $nbt->Inventory = clone $player->namedtag->Inventory;
                $nbt->Skin = new CompoundTag("Skin", ["Data" => new StringTag("Data", $player->getSkinData()), "Name" => new StringTag("Name", $player->getSkinId())]);
            }
            return $nbt;
        }
    
    NOTE: change Human if you want :)
     
  3. xXNiceAssassinlo YT

    xXNiceAssassinlo YT Zombie Pigman

    Messages:
    499
    GitHub:
    xXNiceYT
    Thats slapper
    PHP:
    $nbt Entity::createBaseNBT($playernull22);
    $npc = new Zombie($player->getLevel(), $nbt);
    $npc->spawnToAll();
     
    HyperxXxHound and Muqsit like this.
  4. RyanShaw

    RyanShaw Witch

    Messages:
    69
    You two both don't seem to understand what i'm asking for, I know how to spawn a entity of my choice but what I would like to know is how do I get this entity to attack a player back once they've aggravated it.

     
    Last edited: May 28, 2018
  5. Muqsit

    Muqsit Chicken

    Messages:
    1,548
    GitHub:
    muqsit
    PHP:
    /** @var Entity $damager */
    /** @var Entity $entity */

    $damage 1.2;
    $ev = new EntityDamageByEntityEvent($damager$entityEntityDamageEvent::CAUSE_ENTITY_ATTACK$damage);

    $entity->attack($ev);

    if(!
    $ev->isCancelled()){
        
    //no plugins cancelled the EntityDamageEvent
    }
     
    RyanShaw likes this.
  6. RyanShaw

    RyanShaw Witch

    Messages:
    69
    Hmm i've tried this method earlier like this
    PHP:
       public function entity(EntityDamageEvent $event){

            if(
    $event instanceof EntityDamageByEntityEvent){

                
    $damager $event->getDamager();

                
    $entity $event->getEntity();

                if(
    $damager instanceof Player){

                    
    $entity->attack(new EntityDamageByEntityEvent($damager$entityEntityDamageEvent::CAUSE_ENTITY_ATTACK1));
                }
            }
        } 
    And I get this error

    PHP:
    28.05 22:28:21 [ServerServer thread/DEBUG #2 Crates/src/Ryan/Crates/Main(57): pocketmine\entity\Living->attack(pocketmine\event\entity\EntityDamageByEntityEvent object) 28.05 22:28:21 [Server] Server thread/DEBUG #1 src/pocketmine/entity/Living(524): pocketmine\entity\Entity->attack(pocketmine\event\entity\EntityDamageByEntityEvent object) 28.05 22:28:21 [Server] Server thread/DEBUG #0 src/pocketmine/entity/Entity(896): pocketmine\plugin\PluginManager->callEvent(pocketmine\event\entity\EntityDamageByEntityEvent object) 28.05 22:28:21 [Server] Server thread/CRITICAL RuntimeException: "Recursive event call detected (reached max depth of 50 calls)" (EXCEPTION) in "src/pocketmine/plugin/PluginManager" at line 685 28.05 22:28:21 [Server] Server thread/CRITICAL Could not pass event 'pocketmine\event\entity\EntityDamageByEntityEvent' to 'Crates v3.0.0-1': Recursive event call detected (reached max depth of 50 calls) on Ryan\Crates\Main
    Is it still possible to make the zombie attack the player using this method or is there some other route I need to take? :oops:
     
  7. Marabou

    Marabou Baby Zombie

    Messages:
    137
    GitHub:
    wiligangster
    Try:
    PHP:
    $entity->attack(new EntityDamageEvent($damagerEntityDamageEvent::CAUSE_ENTITY_ATTACK1));
    because the function Entity::attack() is instead EntityDamageEvent and not EntityDamageByEntityEvent.

    as you can see here:
    https://github.com/pmmp/PocketMine-...3b5cacc/src/pocketmine/entity/Entity.php#L895
     
    RyanShaw likes this.
  8. RyanShaw

    RyanShaw Witch

    Messages:
    69
    ohh I see. I've made that change and it did get rid of all the errors I showed earlier, but the zombie I spawned and hit doesn't hit me back and there's no errors on console about it... Any idea's anyone?
     
  9. Marabou

    Marabou Baby Zombie

    Messages:
    137
    GitHub:
    wiligangster
    please send me your code for look at what is happening clearly.
     
  10. RyanShaw

    RyanShaw Witch

    Messages:
    69
    Yeah no problem
    PHP:
    <?php

    namespace Ryan;

    use 
    pocketmine\event\Listener;
    use 
    pocketmine\plugin\PluginBase;
    use 
    pocketmine\nbt\tag\CompoundTag;
    use 
    pocketmine\nbt\tag\DoubleTag;
    use 
    pocketmine\nbt\tag\FloatTag;
    use 
    pocketmine\nbt\tag\ListTag;
    use 
    pocketmine\Player;
    use 
    pocketmine\event\entity\EntityDamageEvent;
    use 
    pocketmine\entity\Entity;
    use 
    pocketmine\event\entity\EntityDamageByEntityEvent;

    class 
    Main extends PluginBase implements Listener {
      
            public function 
    onEnable() {
              
      
    $this->getServer()->getPluginManager()->registerEvents($this$this);

            
    $nbt = new CompoundTag('', [
        
    'Pos' => new ListTag('Pos', [
            new 
    DoubleTag(''253),
            new 
    DoubleTag(''4),
            new 
    DoubleTag(''253)
        ]),
        
    'Motion' => new ListTag('Motion', [
            new 
    DoubleTag(''0),
            new 
    DoubleTag(''0),
            new 
    DoubleTag(''0)
        ]),
        
    'Rotation' => new ListTag('Rotation', [
            new 
    FloatTag(''lcg_value() * 360),
            new 
    FloatTag(''0)
        ]),
    ]);
            
    $entity Entity::createEntity(Entity::ZOMBIE$this->getServer()->getDefaultLevel(), $nbt);      
            
    $entity->setHealth(20);
            
    $entity->spawnToAll();
        }
      
        public function 
    entity(EntityDamageEvent $event){

            if(
    $event instanceof EntityDamageByEntityEvent){

                
    $damager $event->getDamager();

                
    $entity $event->getEntity();

                if(
    $damager instanceof Player){

                
    $entity->attack(new EntityDamageEvent($damagerEntityDamageEvent::CAUSE_ENTITY_ATTACK1));
                }
            }
        }
    }
     
  11. Marabou

    Marabou Baby Zombie

    Messages:
    137
    GitHub:
    wiligangster
    Hmmm

    Try:
    PHP:
    $damager->attack(new EntityDamageEvent($entityEntityDamageEvent::CAUSE_ENTITY_ATTACK1));
    It is the damager who attacks and not the entity I am wrong, it should work now ;)
     
    Last edited: May 30, 2018
  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.