General reminder for everyone regarding teleports

Discussion in 'Plugin Development' started by bergerkiller, Mar 7, 2013.

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

    bergerkiller

    You can no longer use entity.teleport to teleport entities around freely. You have to take care of players being inside vehicle, and when teleporting any entity, you have to take care of vehicles containing passengers. Someone decided it was useful to disable teleportation of passengers and non-empty vehicles, so you will have to store previous entities yourself.

    If anyone is interested, this is code I use to teleport any type of entity without issues. I am fairly sure a lot of plugins are simply calling player.teleport without realizing that the player could be in a minecart or boat, causing all of it to fail. Ejecting that players before teleporting is REQUIRED.

    NOTE: This code is most likely outdated, the latest code can be found here.
    There are is more code in the CommonPlayer class for dealing with additional code moving.

    Code:
        /**
        * Teleports an entity
        *
        * @param entity to teleport
        * @param to location to teleport to
        */
        public static boolean teleport(final org.bukkit.entity.Entity entity, final Location to) {
            final Entity entityHandle = CommonNMS.getNative(entity);
            final Entity passenger = entityHandle.passenger;
            World newworld = CommonNMS.getNative(to.getWorld());
            WorldUtil.loadChunks(to, 3);
            if (entityHandle.world != newworld && !(entityHandle instanceof EntityPlayer)) {
                if (passenger != null) {
                    entityHandle.passenger = null;
                    passenger.vehicle = null;
                    if (teleport(passenger.getBukkitEntity(), to)) {
                        CommonUtil.nextTick(new Runnable() {
                            public void run() {
                                passenger.setPassengerOf(entityHandle);
                            }
                        });
                    }
                }
     
                // teleport this entity
                entityHandle.world.removeEntity(entityHandle);
                entityHandle.dead = false;
                entityHandle.world = newworld;
                entityHandle.setLocation(to.getX(), to.getY(), to.getZ(), to.getYaw(), to.getPitch());
                entityHandle.world.addEntity(entityHandle);
                return true;
            } else {
                // If in a vehicle, make sure we eject first
                if (entityHandle.vehicle != null) {
                    entityHandle.setPassengerOf(null);
                }
                // If vehicle, eject the passenger first
                if (passenger != null) {
                    passenger.vehicle = null;
                    entityHandle.passenger = null;
                }
                final boolean succ = entity.teleport(to);
                // If there was a passenger, let passenger enter again
                if (passenger != null) {
                    passenger.vehicle = entityHandle;
                    entityHandle.passenger = passenger;
                }
                return succ;
            }
        }
    The code also supports non-player teleportation between worlds, and due to lacking API I was forced to do all this myself using various NMS calls. You may want to replace all of the above with reflection (yay) or include it in a library under several version-bound classes.

    Yes, it was pretty funny to see trains literally freezing the server because teleportation was bugged. Thanks for the great 'fixes' everyone, but I'm not here to rant, so I hope I can be of use instead.
     
  2. Offline

    chasechocolate

  3. Offline

    Rprrr

    Thanks for posting this. :)
    I've discovered this myself recently, but I do know that alot of developers are not aware of it. I believe Essentials also simply uses .teleport().
     
  4. Offline

    bergerkiller

    I...I did not expect such an overwhelming response. Anyway, glad I could be of use. :)
     
    xize and Sayshal like this.
Thread Status:
Not open for further replies.

Share This Page