Teleport player into other world

Discussion in 'Plugin Development' started by Xge, Feb 11, 2019 at 10:26 AM.

  1. Offline

    Xge

    I want to teleport a player into another world. I'm doing this:
    Code:
    Bukkit.getServer().createWorld(new WorldCreator("Desert"));
    
    p.teleport(new Location(Bukkit.getServer().getWorld("Desert"), 250,50,1300));
    
    Its only working in my JoinEvents.
    But when i launch this f.e. in public void tpWorld(Player p), its not working..
     
  2. @Xge
    What part of this is not working? Does the world generate? Does the player get teleported somewhere, even if it is not that world? Are there any errors in the console when this happens?
     
  3. Offline

    Xge

    There aren't any errors. The player don't get teleported, but when i use my command(/desert), which is exactly the same code, its working.
     
  4. @Xge
    Just realized you were probably running the code inside the join event, and most likely were not waiting one tick. Because events are called before the action is actually done (that is why some events can be canceled), the player is technically not fully connected as a player in the world when the event is called, so you can't actually teleport the player right then.

    Fix this by creating a BukkitRunnable and waiting atleast one tick before teleporting the player.

    Also, side note, consider creating the world in the onEnable. Since there can be only one world with that name, the world can be generated right when the plugin is enabled, so there isn't a lag spike caused by creating the world when the player first joins.
     

Share This Page