Question Worlds that exist are null when using getWorld

Discussion in 'Bukkit Help' started by EncodedLua, Jun 28, 2017.

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

    EncodedLua

    I have 2 commands that teleport you to 2 different worlds. The 2 worlds are working world files in the server folder. One is named main_lobby and one is named world. Whenever I restart the server and try to run the commands, it tells me that survivalWorld is null. Every time this happens, I create a new world using Bukkit.createWorld with only a name as an argument and the command works fine until I restart the server again. Why is this?

    EDIT: I also tried looping through Bukkit.getWorlds() and world, world-nether, world-end, and main_lobby were all there.

    Code:
    private Logger logger = Bukkit.getLogger();
        private World lobbyWorld = Bukkit.getWorld("main_lobby");
        private World survivalWorld = Bukkit.getWorld("world");
    
        @Override
        public boolean onCommand(CommandSender sender, Command command, String label, String[] args) {
            Player player = (Player) sender;
       
            switch (command.getName()) {
                case "lobby":
                    sender.sendMessage(ChatColor.GREEN + "Teleporting you to the main lobby...");
                    player.teleport(new Location(lobbyWorld, lobbyWorld.getSpawnLocation().getX(), lobbyWorld.getSpawnLocation().getY(), lobbyWorld.getSpawnLocation().getZ()));
                    return true;
                case "survival":
                    sender.sendMessage(ChatColor.GREEN + "Teleporting you to the survival world...");
                    Location spawnLoc = player.getBedSpawnLocation();
                    player.teleport(new Location(survivalWorld, spawnLoc.getX(), spawnLoc.getY(), spawnLoc.getZ()));
                    return true;
            }
       
            return false;
        }
    
     
    Last edited: Jun 28, 2017
  2. Offline

    Zombie_Striker

    @EncodedLua
    You are getting those worlds before the plugin is even enabled. That is your issue. Try getting those worlds in the onEnable, or if you want to reduce code, in the onCommand.


    Also, you should never need to use loggers. Bukkit already logs your plugins for you, and you can always use System.out if you need to print something to the console.

    On top of that, Do not blidnly cast sender to a Player. The Console, other plugins, and the command blocks can all send commands as well. Do an instanceof check first before casting sender to a player.

    [Edit] Just noticed you are using lobbyworld's spawn location variables to create a new location. Why not just use lobbyWorld.getSpawnLocation() instead of creating a new location?
     
    EncodedLua likes this.
  3. Offline

    EncodedLua

    @Zombie_Striker Thank you very much, I knew it had to be something simple like that.
     
Thread Status:
Not open for further replies.

Share This Page