Player respawn code not working

Discussion in 'Plugin Development' started by CrispyFri, Dec 29, 2013.

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

    CrispyFri

    Hello, I am trying to write code where a player is teleported to the world he/she died in when they respawn. I am using PlayerRespawnEvent like so:

    Code:java
    1. @EventHandler(priority = EventPriority.HIGHEST)
    2. public void onRespawn(PlayerRespawnEvent respawn) {
    3. Player player = respawn.getPlayer();
    4. Location loc = new Location(Bukkit.getServer().getWorld(world), Bukkit.getServer().getWorld(world).getSpawnLocation().getX(), Bukkit.getServer().getWorld(world).getSpawnLocation().getY(), Bukkit.getServer().getWorld(world).getSpawnLocation().getZ());
    5. player.teleport(loc);
    6. log.info("[HeroicPVP] Player "+player.getName()+" respawned, world is "+Bukkit.getServer().getWorld(world)+" (after)");
    7. log.info("[HeroicPVP] Player's world is: "+player.getWorld());
    8. }


    I know the event is being called because the logs are working. The worlds that the infos are returning are valid, as in they are the worlds the player died in. However, the player is not teleported. Instead, the player just spawns in the default/spawn world. Any help with this would be quite appreciated.

    Thanks in advance,
    CrispyFri.
     
  2. Offline

    Ronbo

    CrispyFri
    The event is thrown before the player actually respawns, so basically you're teleporting them before they're alive. Just use the scheduler and run all of that on the next tick.
     
  3. Offline

    CrispyFri

    Ronbo
    Could you be so kind as to show me how to do that? I have little experience with the scheduler.
     
  4. Offline

    BillyGalbreath

    Code:java
    1.  
    2. @EventHandler(priority = EventPriority.HIGHEST)
    3. public void onRespawn(PlayerRespawnEvent respawn) {
    4. final Player player = respawn.getPlayer();
    5. final Location loc = new Location(Bukkit.getServer().getWorld(world), Bukkit.getServer().getWorld(world).getSpawnLocation().getX(), Bukkit.getServer().getWorld(world).getSpawnLocation().getY(), Bukkit.getServer().getWorld(world).getSpawnLocation().getZ());
    6. Bukkit.getScheduler().runTaskLater(this, new Runnable() {
    7. @Override
    8. public void run() {
    9. player.teleport(loc);
    10. log.info("[HeroicPVP] Player "+player.getName()+" respawned, world is "+Bukkit.getServer().getWorld(world)+" (after)");
    11. log.info("[HeroicPVP] Player's world is: "+player.getWorld());
    12. }
    13. }, 1);
    14. }
    15.  


    Edit:

    Not sure why you have that huge long line to get the spawn location. This should suffice:

    Code:java
    1.  
    2. final Location loc = Bukkit.getServer().getWorld(world).getSpawnLocation().clone();
    3.  
     
  5. Offline

    CrispyFri

    Thanks a bunch, that works. Also thanks for fixing the location line, yours is much better.
     
Thread Status:
Not open for further replies.

Share This Page