Bukkit.getServer().getWorld(args[0]); always returns null !

Discussion in 'Plugin Development' started by general656, Sep 1, 2014.

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

    general656

    Heyy beautiful forum !

    I wanted to make a plugin which will teleport the player to another world . The code is this :
    Code:java
    1. if(cmd.getName().equalsIgnoreCase("universe")){
    2. Player player = (Player) sender;
    3.  
    4. if(args.length == 1){
    5.  
    6. World world = Bukkit.getServer().getWorld(args[0]);
    7.  
    8. if(world != null){
    9. Location lc = world.getSpawnLocation();
    10. player.teleport(lc);
    11. return true;
    12. }
    13. else if(world == null){
    14. player.sendMessage("Invalid world ! World: " + args[0]);
    15.  
    16. return true;
    17. }
    18. }
    19.  
    20. else
    21. return false;
    22. }


    [​IMG]
    ---------------------
    [​IMG]

    It always returns "null" but why ?
     
  2. Offline

    pedrxd1

    World never are going to be null, if you like check if a world is load, you will check if the world is on the worldlist
     
  3. Offline

    fireblast709

    general656 what is line 43? Print out a list of names using Bukkit.getWorlds().
     
  4. Offline

    general656

    Code:java
    1. if(args.length == 1){
    2.  
    3. boolean worldExist = Bukkit.getServer().getWorlds().contains(args[0]);
    4. World world = Bukkit.getServer().getWorld(args[0]);
    5.  
    6. if(worldExist == true){
    7. Location lc = world.getSpawnLocation();
    8. player.teleport(lc);
    9. return true;
    10. }
    11. else if(worldExist == false){
    12. player.sendMessage("Invalid world ! World: " + args[0]);
    13.  
    14. return true;
    15. }
    16. }


    I tried that , and it returns false again . :/
    fireblast709 (Without exception , so I don't have to give you the line 43 , if you want it it's "if(worldExist == true){")
     
  5. Offline

    Necrodoom

    general656 a boolean cannot NPE.
    Paste full class and world list.
     
  6. Offline

    fireblast709

    general656 I'm pretty sure a List of Worlds won't contain a String :p. Also I meant in the code where the exception that you posted in the first post occurred.
     
    pedrxd1 likes this.
  7. Offline

    pedrxd1

    Code:java
    1. private static boolean checkWorld(World w)
    2. {
    3. return Bukkit.getServer().getWorlds().contains(w);
    4. }
    5.  
     
  8. Offline

    general656

    Ahm , yeah I thought about "Unloaded" worlds again but , even this :
    Code:java
    1. else if(worldExist == false){
    2. player.sendMessage("Invalid world ! World: " + args[0]);
    3. player.sendMessage("Available Worlds : ");
    4. for(World w : getServer().getWorlds()) {
    5. player.sendMessage(w.toString());
    6. }
    7.  
    8. return true;
    9. }

    [​IMG]

    The whole command section :
    Code:java
    1. if(cmd.getName().equalsIgnoreCase("universe")){
    2. Player player = (Player) sender;
    3.  
    4. if(args.length == 1){
    5.  
    6. boolean worldExist = Bukkit.getServer().getWorlds().contains(args[0]);
    7. World world = Bukkit.getServer().getWorld(args[0]);
    8. //World worldList = (World) Bukkit.getServer().getWorlds();
    9.  
    10. if(worldExist == true){
    11. Location lc = world.getSpawnLocation();
    12. player.teleport(lc);
    13. return true;
    14. }
    15. else if(worldExist == false){
    16. player.sendMessage("Invalid world ! World: " + args[0]);
    17. player.sendMessage("Available Worlds : ");
    18. for(World w : getServer().getWorlds()) {
    19. player.sendMessage(w.toString());
    20. }
    21.  
    22. return true;
    23. }
    24. }
    25.  
    26. else
    27. return false;
    28. }
     
  9. Offline

    fireblast709

    general656
     
  10. Offline

    general656

    Fixed it :D I thought it should take a string ! :p
    Code:java
    1. World world = Bukkit.getServer().getWorld(args[0]);
    2. boolean worldExist = Bukkit.getServer().getWorlds().contains(world);

    fireblast709 But how do I load worlds ?
     
  11. Offline

    fireblast709

  12. Offline

    general656

    fireblast709
    Code:java
    1. if(cmd.getName().equalsIgnoreCase("worldload")){
    2. Player player = (Player) sender;
    3. if(args.length == 1){
    4. World world = Bukkit.getServer().getWorld(args[0]);
    5. if(Bukkit.getServer().getWorlds().contains(world) != true ){
    6. WorldCreator wc = new WorldCreator(args[0]);
    7. //wc.create() that doesn't exist
    8. player.sendMessage("World" + args[0] + "Loaded ! ");
    9. return true;
    10. }
    11. else{
    12. player.sendMessage("World" + args[0] + "already loaded !");
    13. return false;
    14. }
    15.  
    16. }
    17. else
    18. return false;
    19. }


    Well there is not a method called "create()" . So how shoud I use WorldCreator to load a world and be in that list ?
    [​IMG]
    I don't want to create a new world , I want to load it , just in case :) .
     
  13. Offline

    fireblast709

    general656 createWorld, my bad :p. After you load a world it should appear in the list.
     
  14. Offline

    general656

    fireblast709
    Thank you duude :D Fixed it ! I don't even have to call an instance of WorldCreator . I can use it as a static method :)
    it's :
    Code:java
    1. WorldCreator.name(args[0]).createWorld();
     
Thread Status:
Not open for further replies.

Share This Page