Solved Teleport doesn't work

Discussion in 'Plugin Development' started by MieskeB, Jun 19, 2020.

Thread Status:
Not open for further replies.
  1. I've tried to teleport the players and change their location. So that player p1 teleports to player p2 and p2 teleports to p1. I've done that using the following code:

    Code:
    Location lp1 = p1.getLocation();
    Location lp2 = p2.getLocation();
    sendAll(lp1.getWorld() + "x: " + lp1.getBlockX() + "y: " + lp1.getBlockY() + "z: " + lp1.getBlockZ());
    sendAll(lp2.getWorld() + "x: " + lp2.getBlockX() + "y: " + lp2.getBlockY() + "z: " + lp2.getBlockZ());
    teleport(p1, lp2);
    teleport(p2, lp1);
    Code:
    private void teleport(Player player, Location location) {
        player.teleport(location);
        player.sendMessage("Teleport completed!");
    }
    It does display the right locations in the chat, but the teleport doesn't seem to work. Also any code writed after these lines doesn't work, but it doesn't display any error messages. What am I doing wrong?

    A problem can be that the method is called from another thread. The full class code can be found here:
    Code:
    package nl.my.plugin;
    
    import org.bukkit.Bukkit;
    import org.bukkit.Location;
    import org.bukkit.entity.Player;
    
    import java.util.concurrent.Executors;
    import java.util.concurrent.ScheduledExecutorService;
    import java.util.concurrent.ScheduledFuture;
    import java.util.concurrent.TimeUnit;
    
    public class EveryMinute {
        private final ScheduledExecutorService scheduler = Executors.newScheduledThreadPool(1);
    
        public void everyFiveMinutes() {
            final Runnable teleporter = new Runnable() {
                @Override
                public void run() {
                    Environment.getInstance().setSeconds(Environment.getInstance().getSeconds() - 5);
                    if (Environment.getInstance().getSeconds() == 0) {
                        Environment.getInstance().setSeconds(300);
                        Player p1 = null;
                        Player p2 = null;
                        for (Player player : Bukkit.getOnlinePlayers()) {
                            if (p1 == null) {
                                p1 = player;
                            }
                            else {
                                p2 = player;
                            }
                        }
                        Location lp1 = p1.getLocation();
                        Location lp2 = p2.getLocation();
                        sendAll(lp1.getWorld() + "x: " + lp1.getBlockX() + "y: " + lp1.getBlockY() + "z: " + lp1.getBlockZ());
                        sendAll(lp2.getWorld() + "x: " + lp2.getBlockX() + "y: " + lp2.getBlockY() + "z: " + lp2.getBlockZ());
                        teleport(p1, lp2);
                        teleport(p2, lp1);
                    }
                    if (Environment.getInstance().getSeconds() % 60 == 0) {
                        sendAll("Only " + Environment.getInstance().getSeconds() / 60 + " minutes left!");
                    }
                    if (Environment.getInstance().getSeconds() < 30) {
                        sendAll("Only " + Environment.getInstance().getSeconds() + " seconds left!");
                    }
                }
            };
            final ScheduledFuture<?> teleportHandle = scheduler.scheduleAtFixedRate(teleporter, 5, 5, TimeUnit.SECONDS);
        }
    
        private synchronized void teleport(Player player, Location location) {
            player.teleport(location);
            player.sendMessage("Teleport completed!");
        }
    
        private void sendAll(String message) {
            for(Player player : Bukkit.getOnlinePlayers()) {
                player.sendMessage(message);
            }
        }
    }
    
    EDIT: added teleport method and extra things that should be considered.
     
    Last edited: Jun 19, 2020
  2. Offline

    timtower Administrator Administrator Moderator

    @MieskeB Please post your teleport method.
     
  3. @timtower Oops forgot it, I've updated the question.

    Code:
    private synchronized void teleport(Player player, Location location) {
        player.teleport(location);
        player.sendMessage("Teleport completed!");
    }
     
  4. Offline

    KarimAKL

    @MieskeB Why don't you use a BukkitRunnable?
     
  5. Offline

    timtower Administrator Administrator Moderator

  6. @KarimAKL I didn't know this existed, I will try now

    It worked! Thank you very much!
     
    Last edited: Jun 19, 2020
    KarimAKL likes this.
Thread Status:
Not open for further replies.

Share This Page