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

Entity follows player

Discussion in 'Development' started by #A6543, Jan 19, 2017.

  1. #A6543

    #A6543 Zombie

    Messages:
    267
    How can I make a entity follows a player?
     
  2. wolfdale

    wolfdale Zombie Pigman

    Messages:
    535
    GitHub:
    diamond-gold
  3. #A6543

    #A6543 Zombie

    Messages:
    267
  4. wolfdale

    wolfdale Zombie Pigman

    Messages:
    535
    GitHub:
    diamond-gold
    Just do
    PHP:
    $speed 1;
    $x $player-> $this->x;
    $y $player-> $this->y;
    $z $player-> $this->z;
    if(
    $x ** $z ** 0.7){
    $motionX 0;
    $motionZ 0;
    }else{
    $diff abs($x) + abs($z);
    $motionX $speed 0.15 * ($x $diff);
    $motionZ $speed 0.15 * ($z $diff);
    }
    $entity->move($motionX$motionY$motionZ);
     
    Marabou likes this.
  5. #A6543

    #A6543 Zombie

    Messages:
    267
    But how can I define the entity? I spawn it with AddEntityPacket (id 5000)
     
  6. wolfdale

    wolfdale Zombie Pigman

    Messages:
    535
    GitHub:
    diamond-gold
    You need to use Entity::createEntity() to be able to use this, if you just want to use packets see SetEntityMotionPacket
     
  7. #A6543

    #A6543 Zombie

    Messages:
    267
    I tried this:
    But it has my skin an is a human
    PHP:
    public function onMove(PlayerMoveEvent $e)
      {
      
    $player $e->getPlayer();
     
    $pk = new MovePlayerPacket();
    $pk->eid 2233;
    $pk->$player->x;
    $pk->$player->y;
    $pk->$player->z;
    $pk->yaw $player->yaw;
    $pk->pitch $player->pitch;
    $player->dataPacket($pk);
      }
    public function 
    spawnPet($player){
    $name $player->getName();
    $text "§l§6Test";
    $pk = new AddPlayerPacket();
                
    $pk->eid 2233;
                
    $pk->uuid UUID::fromRandom();
                
    $pk->$player->x;
                
    $pk->$player->y;
                
    $pk->$player->z;
                
    $pk->speedX 0;
                
    $pk->speedY 0;
                
    $pk->speedZ 0;
                
    $pk->yaw 20;
                
    $pk->pitch = -30;
                
    $pk->item Item::get(0);
        
    $flags 0;
                
    $flags |= << Entity::DATA_FLAG_CAN_SHOW_NAMETAG;
                
    $flags |= << Entity::DATA_FLAG_ALWAYS_SHOW_NAMETAG;
                
    $flags |= << Entity::DATA_FLAG_IMMOBILE;
                
    $pk->metadata = [
                    
    Entity::DATA_FLAGS => [Entity::DATA_TYPE_LONG$flags],
                    
    Entity::DATA_NAMETAG => [Entity::DATA_TYPE_STRING$text],
    Entity::DATA_LEAD_HOLDER_EID => [Entity::DATA_TYPE_LONG12],
                ];
    $player->dataPacket($pk);
    }
     
  8. wolfdale

    wolfdale Zombie Pigman

    Messages:
    535
    GitHub:
    diamond-gold
    I would suggest using Entity::createEntity() to spawn, because using packets is quite hard to manage the 'entities' you spawn
     
  9. #A6543

    #A6543 Zombie

    Messages:
    267
    Ok. And how can I spawn a pig using this?
     
  10. Primus

    Primus Zombie Pigman

    Messages:
    749
    Perhaps we should write the code for you?
     
    imYannic and Sandertv like this.
  11. wolfdale

    wolfdale Zombie Pigman

    Messages:
    535
    GitHub:
    diamond-gold
    PHP:
    $nbt = new CompoundTag("", [
                
    "Pos" => new ListTag("Pos", [
                    new 
    DoubleTag(""$pos->x),
                    new 
    DoubleTag(""$pos->y),
                    new 
    DoubleTag(""$pos->z)
                ]),
                
    "Motion" => new ListTag("Motion", [
                    new 
    DoubleTag(""0),
                    new 
    DoubleTag(""0),
                    new 
    DoubleTag(""0)
                ]),
                
    "Rotation" => new ListTag("Rotation", [
                    new 
    FloatTag(""$source instanceof Location $pos->yaw 0),
                    new 
    FloatTag(""$source instanceof Location $pos->pitch 0)
                ]),
            ]);

    $pig Entity::createEntity("Pig",$chunk,$nbt);#$chunk can be $player->chunk
    $pig->spawnToAll();
     
  12. Primus

    Primus Zombie Pigman

    Messages:
    749
    I found your nanny ^
     
    jasonwynn10 likes this.
  13. #A6543

    #A6543 Zombie

    Messages:
    267
    Mh. This works. But I can't set an id?! And what is $source?
     
  14. #A6543

    #A6543 Zombie

    Messages:
    267
    Does someone know how to set the Id?
     
  15. wolfdale

    wolfdale Zombie Pigman

    Messages:
    535
    GitHub:
    diamond-gold
    You cant set the entityId,$source is supposed to be $pos :p
     
  16. #A6543

    #A6543 Zombie

    Messages:
    267
    Ok. But how can I get the entity in another task or Event?
     
  17. wolfdale

    wolfdale Zombie Pigman

    Messages:
    535
    GitHub:
    diamond-gold
    You save the entityId then use $level->getEntity($id) to get the entity
     
  18. #A6543

    #A6543 Zombie

    Messages:
    267
    Ok. But I didn't set the Id when I spawned the entity?!
     
  19. wolfdale

    wolfdale Zombie Pigman

    Messages:
    535
    GitHub:
    diamond-gold
    The id will be $entity->getId()
     
  20. #A6543

    #A6543 Zombie

    Messages:
    267
    Ok... Is there a way to set the nametag of the entity?
     
  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.