I can't teleport a player on respawn

Discussion in 'Plugin Development' started by willeb96, Jun 20, 2012.

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

    willeb96

    Code:
    @EventHandler
    public void onPlayerRespawn(PlayerRespawnEvent event) {
        if (game_in_progress == true) {
            event.getPlayer().sendMessage("The game is still progress, so you will be taken to the spectator room");
            tpToSpec(event.getPlayer());
        }
    }
    
    I get the message in chat and I know that tpToSpec() works, so why doesn't this work?
    Thanks for any help =)
     
  2. Offline

    desht

    Try deferring the teleport by a tick with a sync delayed task.
     
  3. Offline

    willeb96

    I'm all new to Java, I have no idea what you're talking about :/
     
  4. Offline

    desht

    OK, Bukkit includes a scheduler, which allows tasks to be scheduled to run in the future (possibly repeating, but in your case you just need a one-off). Have a read of http://wiki.bukkit.org/Scheduler_Programming

    Try a method like this:
    PHP:
    @EventHandler
    public void onPlayerRespawn(final PlayerRespawnEvent event) {
        if (
    game_in_progress == true) {
            
    event.getPlayer().sendMessage("The game is still progress, so you will be taken to the spectator room");
            
    Bukkit.getScheduler().scheduleSyncDelayedTask(yourPlugin, new Runnable() {
               @
    Override
               
    public void run() { tpToSpec(event.getPlayer()); }
            });
        }
    }
    You will need an instance of your plugin (I called it yourPlugin) there. If your event handler is in your main plugin class, can just pass this, otherwise you'll need to let your listener class know the plugin instance somehow - passing it to the listener constructor is the easiest way.
     
    JD_Guy17 likes this.
  5. Offline

    willeb96

    Thanks a lot man, it works now! Sure will look into the scheduler thing, that seems really cool :D
     
Thread Status:
Not open for further replies.

Share This Page