[Tutorial] Teleporting a player on horseback

Discussion in 'Resources' started by Cryptite, Sep 20, 2013.

Thread Status:
Not open for further replies.
  1. Offline

    Cryptite

    This has perhaps been solved and/or posted elsewhere, but I didn't find anything very quickly when I searched. That said, here's a quick snippet of code to handle teleporting a player on horseback.

    I use this in conjuction with a regular teleport (I add some sound effects and particles for effect on my server), hence the if statement. Anyway, hope this helps some people!

    Code:java
    1. if (player.isInsideVehicle() && player.getVehicle() instanceof Horse) {
    2.  
    3. //Grab the horse from the entity returned by getVehicle
    4. Horse horse = (Horse) player.getVehicle();
    5.  
    6. //Eject the player before we do the warp, otherwise bad stuff.
    7. horse.eject();
    8.  
    9. //Teleport both the horse AND the player to the destination
    10. horse.teleport(loc);
    11. player.teleport(loc);
    12.  
    13. //Once arrived, remount the player with setPassenger.
    14. horse.setPassenger(player);
    15. } else {
    16. player.teleport(loc);
    17. }
     
    DevRosemberg and LaxWasHere like this.
  2. Offline

    Ultimate_n00b

    Since you can ride other things (pigs & minecarts for example) could you cast player.getVehicle() to Vehicle then do everything?
     
  3. Offline

    Cryptite

    Yes I would assume so. The main reason I tested for Horses in my example was because I handle the end destination for those on a horse differently, but abstracting it to deal with all entities/vehicles should theoretically work.
     
    Ultimate_n00b likes this.
  4. Offline

    Ultimate_n00b

    Another question, if you just teleport the horse then set its passenger to the player, wouldn't it teleport the player to the horse?
     
  5. Offline

    Cryptite

    There seems to be an odd bug wherein using setPassenger over long distances causes your horse to go invisible. I read this on a variety of horse-teleport related posts, hence teleporting both individually. It makes for a smoother teleport as well, I found.
     
    Ultimate_n00b likes this.
Thread Status:
Not open for further replies.

Share This Page