Send a player to a custom location upon respawn

Discussion in 'Plugin Development' started by gamerguy115, Apr 17, 2015.

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

    gamerguy115

    I have been trying to develop a plugin that allows the player to set a custom position for a spawn. A part of the plugin allows the player to teleport to that location upon respawn. I heard that you have to make the plugin wait one tick before actually teleporting, or else it won't work. Here is my code, I am not sure what to do. It is not working.

    Code:
        public void onRespawn(final PlayerRespawnEvent e){
            Player p = e.getPlayer();
           
            FileConfiguration cfg = getConfig();
           
            String world = cfg.getString("spawn.world");
            double x = cfg.getDouble("spawn.x");
            double y = cfg.getDouble("spawn.y");
            double z = cfg.getDouble("spawn.z");
            double yaw = cfg.getDouble("spawn.yaw");
            double pitch = cfg.getDouble("spawn.pitch");
           
            final Location spawn = new Location(Bukkit.getWorld(world), x, y, z);
            spawn.setYaw((float) yaw);
            spawn.setPitch((float) pitch);       
           
            if(p.getWorld().getName().equalsIgnoreCase("world")) {
                if(getConfig().getBoolean("TPToSpawnOnDeath") == true) {
                    int t = Bukkit.getScheduler().scheduleSyncDelayedTask(this, new Runnable() {
                        public void run() {
                            e.setRespawnLocation(spawn);
                        }
                    }, 1);
               
                }
               
            }
        }
    Here is my original code, without the Schedulers.

    Code:
        public void onRespawn(final PlayerRespawnEvent e){
            Player p = e.getPlayer();
           
            FileConfiguration cfg = getConfig();
           
            String world = cfg.getString("spawn.world");
            double x = cfg.getDouble("spawn.x");
            double y = cfg.getDouble("spawn.y");
            double z = cfg.getDouble("spawn.z");
            double yaw = cfg.getDouble("spawn.yaw");
            double pitch = cfg.getDouble("spawn.pitch");
           
            final Location spawn = new Location(Bukkit.getWorld(world), x, y, z);
            spawn.setYaw((float) yaw);
            spawn.setPitch((float) pitch);       
           
            if(p.getWorld().getName().equalsIgnoreCase("world")) {
                if(getConfig().getBoolean("TPToSpawnOnDeath") == true) {
                    e.setRespawnLocation(spawn);           
                }
               
            }
        }
    Thanks in advance! :)
     
  2. Offline

    teej107

    @gamerguy115 All event changes should happen when it is fired. It will do no good once the event is finished.
     
  3. Offline

    gamerguy115

    How would I be able to do that? I am fairly new to Bukkit development. Can you send me some code?
     
  4. Offline

    teej107

    Scheduling a task always happens at the next available tick or later. By the time the code in your scheduled task has run, it'll be too late since the event has already been processed and finished with.
     
  5. Offline

    dsouzamatt

    @gamerguy115 Don't bother changing the respawn location, just teleport the player to spawn after 1 tick.
     
  6. Offline

    gamerguy115

    @dsouzamatt How would I do that? I know that I can put p.teleport(spawn);, but how do I make it wait a tick?
     
  7. Offline

    Zombie_Striker

    Maybe the problem is that you're only waiting one tick. Try setting the DelayedTask to 5L or 20L.
     
  8. .teleport(location)
     
  9. Offline

    teej107

  10. Offline

    gamerguy115

  11. Offline

    teej107

  12. Offline

    gamerguy115

    I am so stupid... the problem was that I had to add @EventHandler before the event... I didn't even need the BukkitRunnable anyway. Thanks for the help anyway! :p
     
Thread Status:
Not open for further replies.

Share This Page