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

Solved Riding Entities

Discussion in 'Development' started by Sandertv, Apr 8, 2017.

  1. Sandertv

    Sandertv Zombie Pigman Poggit Reviewer

    Messages:
    786
    GitHub:
    Sandertv
    Hey guys!
    I'm working on a pet plugin, (which will be public soon) and am trying to implement riding the pets. Now, I sort of know how to link entities to eachother, but because the fields aren't yet added in PocketMine, I'm not entirely sure how. I've made two previous attempts, which both didn't seem to do anything.

    The following code is inside of an abstract entity class, which extends the Creature and implements the Rideable classes. The code gets executed on EntityDamageByEntityEvent, if the owner of the pet hits the pet with a saddle. The message of this get's shown, so the code does get executed correctly.

    PHP:
            /**
         * @param Player $player
         */
        
    public function setRider(Player $player) {
            
    $this->ridden true;
            
    $this->rider $player->getName();

            
    $pk = new SetEntityLinkPacket();
            
    $pk->from $this->getId();
            
    $pk->to $player->getId();
            
    $pk->type 1;
            
    $this->server->broadcastPacket($this->level->getPlayers(), $pk);
        }

        public function 
    throwRiderOff() {
            
    $pk = new SetEntityLinkPacket();
            
    $pk->from $this->getPetOwner()->getId();
            
    $pk->to $this->getId();
            
    $pk->type 0;
            
    $this->ridden false;
            
    $this->rider null;
        }

        
    /**
         * @return Player
         */
        
    public function getRider(): Player {
            return 
    $this->getLevel()->getServer()->getPlayer($this->rider);
        }

        
    /**
         * @return bool
         */
        
    public function isRidden(): bool {
            return 
    $this->ridden;
        }

    The second time I've switched around some things, because I'm not sure what has to go where.

    PHP:
           /**
         * @param Player $player
         */
        
    public function setRider(Player $player) {
            
    $this->ridden true;
            
    $this->rider $player->getName();

            
    $pk = new SetEntityLinkPacket();
            
    $pk->from $player->getId();
            
    $pk->to $this->getId();
            
    $pk->type 1;
            
    $this->server->broadcastPacket($this->level->getPlayers(), $pk);
        }

        public function 
    throwRiderOff() {
            
    $pk = new SetEntityLinkPacket();
            
    $pk->from $this->getPetOwner()->getId();
            
    $pk->to $this->getId();
            
    $pk->type 0;
            
    $this->ridden false;
            
    $this->rider null;
        }

        
    /**
         * @return Player
         */
        
    public function getRider(): Player {
            return 
    $this->getLevel()->getServer()->getPlayer($this->rider);
        }

        
    /**
         * @return bool
         */
        
    public function isRidden(): bool {
            return 
    $this->ridden;
        }
    I've also attempted to change the Packet type, but this didn't seem to have a different behaviour either. I assume type 1 is for riding.

    I know doing this is still kinda hacky, but I'm willing to do so to make riding it work. If anybody here has an idea what I'm doing wrong, or how it should be, please tell me. I'd greately appreciate it.

    Thanks in advance!
     
  2. Sandertv

    Sandertv Zombie Pigman Poggit Reviewer

    Messages:
    786
    GitHub:
    Sandertv
    Some extra information: I've tried the code on any entity, with the thought in mind that it would only work on rideable animals such as horses and pigs, (I don't know, Mojang likes to hard code things sometimes) maybe even only if they have a saddle on. If anybody knows about that, feel free to tell me please. :)
     
  3. Sandertv

    Sandertv Zombie Pigman Poggit Reviewer

    Messages:
    786
    GitHub:
    Sandertv
    May I just ping @dktapps, in case he knows anything about it?
     
  4. dktapps

    dktapps Administrator Staff Member PMMP Team

    Messages:
    774
    GitHub:
    dktapps
    Off the hop, you seem to have more idea than I do. I haven't messed with most of the entities stuff.
     
    jasonwynn10 and XCodeMCPE like this.
  5. corytortoise

    corytortoise Skeleton

    Messages:
    825
    GitHub:
    corytortoise
    I don't know how willing he would be to help you, but @mal0ne_23 may know a bit about this.
     
  6. Sandertv

    Sandertv Zombie Pigman Poggit Reviewer

    Messages:
    786
    GitHub:
    Sandertv
    I'll take a peek at MiNET, see if I can get anything useful there :p
     
  7. Dog2puppy

    Dog2puppy Slime

    Messages:
    94
    GitHub:
    colesquared
    AvengeTech was able to do this. But instead of the player riding the other player, the player is riding a squid, and the squid is teleported to the person that your riding.
     
  8. Sandertv

    Sandertv Zombie Pigman Poggit Reviewer

    Messages:
    786
    GitHub:
    Sandertv
    Update: I got it to work! After some messing around, it worked, using this code:
    PHP:
    /**
    * @param Player $player
    */
    public function setRider(Player $player) {
       
    $this->ridden true;
       
    $this->rider $player->getName();

       
    $pk = new SetEntityLinkPacket();
       
    $pk->to $player->getId();
       
    $pk->from $this->getId();
       
    $pk->type self::STATE_SITTING;
       
    $this->server->broadcastPacket($this->level->getPlayers(), $pk);

       
    $pk = new SetEntityLinkPacket();
       
    $pk->to 0;
       
    $pk->from $this->getId();
       
    $pk->type self::STATE_SITTING;
       
    $player->dataPacket($pk);
    }

    public function 
    throwRiderOff() {
       
    $pk = new SetEntityLinkPacket();
       
    $pk->from $this->getId();
       
    $pk->to $this->getPetOwner()->getId();
       
    $pk->type self::STATE_STANDING;
       
    $this->ridden false;
       
    $this->rider null;
       
    $this->server->broadcastPacket($this->level->getPlayers(), $pk);

       
    $pk = new SetEntityLinkPacket();
       
    $pk->from $this->getPetOwner()->getId();
       
    $pk->to 0;
       
    $pk->type self::STATE_STANDING;
       
    $this->getPetOwner()->dataPacket($pk);
    }

    /**
    * @return Player
    */
    public function getRider() {
       return 
    $this->getLevel()->getServer()->getPlayer($this->rider);
    }

    /**
    * @return bool
    */
    public function isRidden(): bool {
       return 
    $this->ridden;
    }
    The sending to the rider seems to be necessary for the rider to show up riding.

    I'm now facing a new issue however. This new issue is that plays are positioned UNDER the entity ridden, instead of on top of it. Is there any way to fix this, you know about? Workarounds are welcome too, as this seems to be an MCPE thing.
     
    Jack Noordhuis likes this.
  9. dktapps

    dktapps Administrator Staff Member PMMP Team

    Messages:
    774
    GitHub:
    dktapps
    You need to look at the seat position metadata fields. I forget which one it is, might be 58.
     
    jasonwynn10 and Sandertv like this.
  10. Sandertv

    Sandertv Zombie Pigman Poggit Reviewer

    Messages:
    786
    GitHub:
    Sandertv
    Awesome, thanks!
     
  11. Sandertv

    Sandertv Zombie Pigman Poggit Reviewer

    Messages:
    786
    GitHub:
    Sandertv
    Tried a bit with 58, 57, 59, but no success with those. Is it the one that requires a Vector3F? Or...
     
  12. BalAnce

    BalAnce Silverfish

    Messages:
    22
    GitHub:
    YaBoiBalAnce
    where you put it at
     
  13. moska

    moska Baby Zombie

    Messages:
    105
    GitHub:
    supermaxalex
    I found it.
    PHP:
    const DATA_RIDE_POSITION 57//Try with 58 too
    $player->setDataProperty(Entity::DATA_TYPE_VECTOR3Fself::/*or Entity::*/DATA_RIDE_POSITION, [-0.022.30.19]);
     
  14. Sandertv

    Sandertv Zombie Pigman Poggit Reviewer

    Messages:
    786
    GitHub:
    Sandertv
    I'll give it a try. Not sure why it wouldn't have worked earlier.
     
    xLeakDev Enzo likes this.
  15. KairusDarkSeeker

    KairusDarkSeeker Spider Jockey

    Messages:
    25
    GitHub:
    kairusdarkseeker
    Why not just use
    PHP:
    $player->linkEntity($entity);
     
  16. Sandertv

    Sandertv Zombie Pigman Poggit Reviewer

    Messages:
    786
    GitHub:
    Sandertv
    Because I'm not using a spoon.
     
    jasonwynn10 likes this.
  17. KairusDarkSeeker

    KairusDarkSeeker Spider Jockey

    Messages:
    25
    GitHub:
    kairusdarkseeker
    Well I'm using a fork
     
  18. Sandertv

    Sandertv Zombie Pigman Poggit Reviewer

    Messages:
    786
    GitHub:
    Sandertv
    And I'm not, plus, these are PocketMine forums, so solutions for forks/spoons of PocketMine will not fix the problem. Like I mentioned above, I already got it working. I just need to find the metadata ID for changing the seat position.
     
    jasonwynn10 and HimbeersaftLP like this.
  19. TheDiamondYT

    TheDiamondYT Zombie

    Messages:
    298
    GitHub:
    TheDiamondYT1
    We don't support forks.
     
    jasonwynn10 and HimbeersaftLP like this.
  20. KairusDarkSeeker

    KairusDarkSeeker Spider Jockey

    Messages:
    25
    GitHub:
    kairusdarkseeker
    Well PMMP supports the function
    PHP:
    $player->linkEntity();
     
    Last edited: Apr 9, 2017
  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.