Plugin Help Bukkit.getWorld() returns null after reload?

Discussion in 'Plugin Help/Development/Requests' started by R00t9, Jun 24, 2015.

Thread Status:
Not open for further replies.
  1. Hello, my problem is that the Bukkit.getWorld() returns null, when I reload my server. Does anyone have any idea how to fix this?
    My plugins' code(it's very silly and simple, it's my first plugin)
    Code:
    package com.mscmd.mscmd.MineStrikeCommands;
    
    import java.util.Set;
    
    import org.bukkit.Bukkit;
    import org.bukkit.ChatColor;
    import org.bukkit.Location;
    import org.bukkit.World;
    import org.bukkit.WorldCreator;
    import org.bukkit.command.Command;
    import org.bukkit.command.CommandSender;
    import org.bukkit.entity.Player;
    import org.bukkit.plugin.java.JavaPlugin;
    
    public final class MineStrikeCommands extends JavaPlugin {
        @Override
        public void onEnable() {
            this.saveDefaultConfig();
        }
     
        @Override
        public void onDisable() {
            this.saveConfig();
        }
     
     
        public Location _tempLoc;
        public String _tempWorld;
        public World _tempWorld_World;
        public String _tempLobbyName;
     
        @Override
        public boolean onCommand(CommandSender sender, Command cmd, String commandLabel, String[] args)
        {
            if (cmd.getName().equalsIgnoreCase("ms")) {
                if(args.length == 1 || args.length > 1)
                { 
                    if(args[0].equalsIgnoreCase("setal"))
                    {
                        if(sender instanceof Player)
                        {
                            if(args.length < 2)
                            {
                                sender.sendMessage(ChatColor.RED + "Musis zadat jmeno lobby!");
                                return true;
                             
                            } else {
                                Player player = (Player) sender;
                             
                                _tempLoc = player.getLocation();
                                _tempWorld = player.getWorld().getName();
                                _tempLobbyName = args[1];
                             
                                double _x = _tempLoc.getX();
                                double _y = _tempLoc.getY();
                                double _z = _tempLoc.getZ();
                                double _yaw = _tempLoc.getYaw();
                                double _pitch = _tempLoc.getPitch();
                             
                                this.getConfig().set("Lobbys.Coordinates." + _tempLobbyName + ".world", _tempWorld);
                                this.getConfig().set("Lobbys.Coordinates." + _tempLobbyName + ".x", _x);
                                this.getConfig().set("Lobbys.Coordinates." + _tempLobbyName + ".y", _y);
                                this.getConfig().set("Lobbys.Coordinates." + _tempLobbyName + ".z", _z);
                                this.getConfig().set("Lobbys.Coordinates." + _tempLobbyName + ".yaw", _yaw);
                                this.getConfig().set("Lobbys.Coordinates." + _tempLobbyName + ".pitch", _pitch);
                                this.saveConfig();
                             
                                sender.sendMessage(ChatColor.YELLOW + "Lobby " + _tempLobbyName + " uspesne vytvoreno!");
                             
                                return true;
                            }
                        } else {
                            sender.sendMessage(ChatColor.RED + "Pro pouziti tohoto prikazu musis byt hrac!");
                            return true;
                        }
                     
                    } else if (args[0].equalsIgnoreCase("list")) {
                        Set<String> _lobbys = this.getConfig().getConfigurationSection("Lobbys.Coordinates").getKeys(false);
                        sender.sendMessage(ChatColor.RED + "List vsech lobby: ");
                        for(String s : _lobbys)
                        {
                            sender.sendMessage(ChatColor.GREEN + s);
                        }
                     
                        return true;
                    } else if (args[0].equalsIgnoreCase("delal")){
                        if(args.length < 2)
                        {
                            sender.sendMessage(ChatColor.RED + "Musis zadat jmeno lobby!");
                         
                            return true;
                         
                        } else {
                            if(this.getConfig().getConfigurationSection("Lobbys.Coordinates").contains(args[1]))
                            {
                                this.getConfig().getConfigurationSection("Lobbys.Coordinates").set(args[1], null);
                                this.saveConfig();
                             
                                sender.sendMessage(ChatColor.YELLOW + "Lobby " + args[1] + " uspesne smazano!");
                             
                                return true;
                            } else {
                                sender.sendMessage(ChatColor.RED + "Zadne lobby se jmenem " + args[1] + " neexistuje!");
                             
                                return true;
                            }
                        }
                    } else if (args[0].equalsIgnoreCase("join")){
                        if(sender instanceof Player)
                        {
                            if(args.length < 2)
                            {
                                sender.sendMessage(ChatColor.RED + "Musis zadat jmeno lobby!");
                             
                                return true;
                            } else {
                                if(this.getConfig().getConfigurationSection("Lobbys.Coordinates").contains(args[1]))
                                {
                                    double _x = Double.parseDouble(this.getConfig().getString("Lobbys.Coordinates." + args[1] + ".x"));
                                    double _y = Double.parseDouble(this.getConfig().getString("Lobbys.Coordinates." + args[1] + ".y"));
                                    double _z = Double.parseDouble(this.getConfig().getString("Lobbys.Coordinates." + args[1] + ".z"));
                                    float _yaw = Float.parseFloat(this.getConfig().getString("Lobbys.Coordinates." + args[1] + ".yaw"));
                                    float _pitch = Float.parseFloat(this.getConfig().getString("Lobbys.Coordinates." + args[1] + ".pitch"));
                                 
                                    _tempWorld = this.getConfig().getString("Lobbys.Coordinates." + args[1] + ".world");
                                    _tempWorld_World = Bukkit.getWorld(_tempWorld.trim());
                                    _tempLoc.setWorld(_tempWorld_World); _tempLoc.setX(_x); _tempLoc.setY(_y); _tempLoc.setZ(_z); _tempLoc.setYaw(_yaw); _tempLoc.setPitch(_pitch);
                                 
                                    Player player = (Player) sender;
                                    player.teleport(_tempLoc);
                                 
                                    sender.sendMessage(ChatColor.YELLOW + "Byl jsi uspesne teleportovan do lobby " + args[1]);
                                    return true;
                                 
                                } else {
                                    sender.sendMessage(ChatColor.RED + "Zadne lobby se jmenem " + args[1] + " neexistuje!");
                                    return true;
                                }
                            }
                        } else if (args[0].equalsIgnoreCase("leave")){
                         
                         
                        } else {
                            sender.sendMessage(ChatColor.RED + "Pro pouziti tohoto prikazu musis byt hrac!");
                            return true;
                        }
                    } else {
                        sender.sendMessage(ChatColor.RED + "List vsech prikazu: ");
                        sender.sendMessage(ChatColor.GREEN + "/ms list - Zobrazi vsechna dostupna lobby");
                        sender.sendMessage(ChatColor.GREEN + "/ms setal jmeno_lobby - Nastavi lobby ");
                        return true;
                    }
                } else {
                    sender.sendMessage(ChatColor.RED + "List vsech prikazu: ");
                    sender.sendMessage(ChatColor.GREEN + "/ms list - Zobrazi vsechna dostupna lobby");
                    sender.sendMessage(ChatColor.GREEN + "/ms setal jmeno_lobby - Nastavi lobby ");
                    return true;
                }
            }
            return true;
        }
    }
    In the log, there's NullPointerException at line 130, which is:
    Code:
    player.teleport(_tempLoc);
    But before I reload the server/or restart, it works. I am sure it saves the config and i am sure that it reads the config properly. So please help me...

    Oh sorry, it's not line 130, but 127...
    Code:
    _tempLoc.setWorld(_tempWorld_World); _tempLoc.setX(_x); _tempLoc.setY(_y); _tempLoc.setZ(_z); _tempLoc.setYaw(_yaw); _tempLoc.setPitch(_pitch);
    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: Jun 12, 2016
  2. Offline

    BizarrePlatinum

    @R00t9 is it possible that the world is being unloaded when you reload the server? That can cause it to return null.
     
  3. I don't think so, because it's the default overworld... I am in it when I reload the server so there's no chance this world is not loaded.
    BTW: I saw that you made some simple, but useful plugins. I am dreaming of that :)
     
    Last edited: Jun 24, 2015
Thread Status:
Not open for further replies.

Share This Page