[SOLVED] Teleporting to the nether and the end with no portals

Discussion in 'Plugin Development' started by SnowGears, Aug 13, 2012.

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

    SnowGears

    Is it possible to teleport to the nether by typing a command like /teleport nether or /teleport theend?
    This is going to be crucial for my plugin and I am not sure how I would go about finding the location to teleport to in the other world. Thanks in advance!
     
  2. Offline

    zajacmp3

    It is possible.

    I will show you some code from my plugin that may become handy in that:
    Code:
        public static void preparePlayerPosition(int position, String playername) {
             World world = Bukkit.getServer().getWorld(Integer.toString(actualMap));
             if(world!=null){
                     Location location = Bukkit.getPlayer(playername).getLocation();
                     location.setWorld(world);
                     location.setX(config.getDouble("MapConfiguration."+actualMap+"."+(position+1)+".X"));
                     location.setY(config.getDouble("MapConfiguration."+actualMap+"."+(position+1)+".Y"));
                     location.setZ(config.getDouble("MapConfiguration."+actualMap+"."+(position+1)+".Z"));
                     Bukkit.getPlayer(playername).teleport(location);
             }
             else System.out.println("World Is null!");
            
        }
     
  3. Offline

    Timr

    Code:
    public boolean onCommand(CommandSender sender, Command cmd, String commandLabel, String[] args) {
        if(cmd.getName().equalsIgnoreCase("teleport")) {
            if(args.length <= 0) {
                sender.sendMessage("Syntax error!");
                return true;
            }
            if(args.length < 2) {
                sender.sendMessage("Syntax error!");
                return true;
            }
            if(!(sender instanceof Player)) {
                sender.sendMessage("I can't teleport console..");
                return true;
            }
            
            Player p = (Player) sender;
            
            if(args[1].equalsIgnoreCase("nether") {
                World nether = Bukkit.getWorld("NETHER_WORLD_NAME");
                p.teleport(nether.getSpawnLocation());
                sender.sendMessage("You've been teleported to the nether. Enjoy your stay!");
                return true;                
            }
            
            if(args[1].equalsIgnoreCase("end") {
                World end = Bukkit.getWorld("END_WORLD_NAME");
                p.teleport(end.getSpawnLocation());
                sender.sendMessage("You've been teleported to the end. Enjoy your stay!");
                return true;                
            }
        }        
        return false;
    }
    Something like this should work. Take note of the missing variables on line 19 and 26, these are where you would place the name of the nether world and end world. (IE world_nether, world_theend). I would suggest something config based for this, as hard coded things like that are prone to failure.
     
    Tooner101 likes this.
  4. Offline

    SnowGears

    Would this sometimes teleport the player into a wall though? Because it doesn't check if the location blocks are air. And also, this code would work if I typed
    World nether = Bukkit.getServer().getWorld(nether) ? Thanks

    So basically I can get the spawn location of the nether or the end by nether.getSpawnLocation()? That makes things a little easier. And thanks for the advice. I will make the world names in the config file

    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: May 27, 2016
  5. Offline

    zajacmp3

    Yes it would work but only if you typed "nether" it is a String remember and of course this world must be loaded to send some one there. I had a problem in what I could not teleport anyone to other world like world 1 world 2 world 3 cause it was not loaded. I used API for it from Multiverse to get it loaded before I teleported someone.
    It will not teleport anyone through wall if the X, Y, Z choosen by you are not in wall :)
    I know the exact spot where I want a player to go so that is not a problem for me.
     
  6. Offline

    SnowGears

    But if I just wanted the end and the nether I would not have to load in the API correct?
     
  7. Offline

    Timr

    Indeed you can.
     
  8. Offline

    whitehooder

    Take note that not all worlds are named the same, so you would probably need to use some function to get the world the end.
     
  9. Offline

    SnowGears

    Timr
    I tried this code and then a ton of pig zombie ghosts are just glitching up and down in the sky. I took a picture.
    [​IMG]

    I also tried going to the nether first and then back. It takes me to the screen like I am in the nether for a second or so but then I appear back at the main spawn with pig zombie ghosts everywhere...

    This is the code I am using
    Code:
    World nether = Bukkit.getWorld("world_nether");
    player.teleport(nether.getSpawnLocation());
    
    Please help if you can. I don't know what else to try.

    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: May 27, 2016
  10. Offline

    SnowGears

    If anyone has any ideas I would love to hear them because I am stuck at a complete standstill right now and would love to continue to work on this but I need to solve this issue. Thanks in advance
     
  11. Offline

    whitehooder

    You could try to create a fake player standing in the ends spawnpoint, then teleport the other player to the fake player. Or you could for some reason do nether.getSpawnLocation()... some way of getting the world and location again. Just debugging that can solve this im afraid of. But it seems like a bug.
     
  12. Offline

    SnowGears

    How would I create a fake player. I might try that.
     
  13. Offline

    whitehooder

    Could it be as easy as:
    Player p = null;
    p.teleport(getServer().getWorld("world_nether").getSpawnLocation());
    ?

    A little more safe (I believe):
    Code:
            World world = null;
            Player fakep = null;
            for (World w : getServer().getWorlds()) {
                if (w.getWorldType().getName().contains("_nether")) {
                    world = w;
                }
            }
            try {
                fakep.teleport(world.getSpawnLocation());
            } catch(Exception ex) {}
           
            if (fakep.getWorld().getName().contains("_nether")) {
                //Teleport the player (not the fake one) to the nether spwan here. Example:
                player.teleport(fakep);
            }
    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: May 27, 2016
  14. Offline

    SnowGears

    Thanks I will try this

    If someone can please try to test this to see if it works for them that would be awesome. I just keep getting pig zombie ghosts everywhere in my world when I try to teleport to the nether with no portal.

    Code I am using currently is this:

    World nether = Bukkit.getWorld("world_nether");
    Location loc = nether.getSpawnLocation();
    player.teleport(loc);

    Please let me know. Thanks

    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: May 27, 2016
  15. Offline

    whitehooder

    I can try when I get home. Probably 6 more hours :(
     
  16. Offline

    SnowGears

    Okay thats fine. Just let me know how it goes. Thanks
     
  17. Offline

    whitehooder

    I've tested it now with this code (no big difference) and did not see anything like your problem. Try with this code then (again no big difference) :
    Code:
    public boolean onCommand(CommandSender sender, Command cmd, String label, String[] args) {
            if (cmd.getName().equalsIgnoreCase("nether")) {
                if (sender instanceof Player) {
                    World nether = Bukkit.getWorld("world_nether");
                    Location loc = nether.getSpawnLocation();
                    ((Player) sender).teleport(loc);
                    return true;
                }
            }
            return false;
        }
     
  18. Offline

    SnowGears

    Okay I will try it and let you know in a few minutes. Thanks
     
  19. Offline

    ZeusAllMighty11

    I have an off-topic question about null

    Player p = null;

    That still defines Player as p, but it doesn't void it for being null right?
    So if I did p.sendMessage("test"); it sends, though I made it null?
     
  20. Offline

    Firefly

    No no no no. You can't use ANY methods on a null object, that's when you get a NullPointerException.

    Also, I think your problem with teleporting and "glitching" has to do with the server not keeping that world's spawn in memory. This can be fixed by using a few methods from the API. Try that.

    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: May 27, 2016
    ZeusAllMighty11 likes this.
  21. Offline

    SnowGears

    whitehooder

    The code with command /nether works fine but I cannot get my respawn code to work. It still makes pig zombie ghosts everywhere....

    This is my code I need to get to work. Any help would be great. Thanks.
    Code:
    @EventHandler
    public void respawn(PlayerRespawnEvent event){
                   final Player player = event.getPlayer();
                   player.sendMessage("Hades has summoned you to the underworld!");
                   World nether = Bukkit.getWorld("world_nether");
                   Location loc = nether.getSpawnLocation();
                   player.teleport(loc);
    }
    
     
  22. Offline

    whitehooder

    This error is a little weird. Maybe its because that the event is triggered before the player actually spawns. Try scheduling the teleport with 5 ticks or so, just to let the player spawn before teleporting. Good luck
     
  23. Offline

    SnowGears

    Okay thanks I will try that. Thanks for all of your help!
     
  24. Offline

    whitehooder

    :)
     
  25. Offline

    SnowGears

    That worked. Changed it to solved. Thanks very much
     
Thread Status:
Not open for further replies.

Share This Page