Solved Bukkit.getServer().getWorld(worldname) returns null?

Discussion in 'Plugin Development' started by blackwolf12333, Jul 10, 2012.

Thread Status:
Not open for further replies.
  1. Hello, what i am trying to do is roll something back from a log file, it worked fine:p but i wanted it to be a bit less laggy, so i tried getting it all in a thread, also works now i think.
    But now i have the problem when i try to rollback one line from the file, which is a block place event in this case, but all the other events that are logged have the same problem.
    I have to get the World, which i do this way:
    Code:
    Bukkit.getServer().getWorld(worldname)
    the worldname is a variable, and i checked it is correct, but still the world object it returns is null.
    I was wondering, does that happen because i use this function in a thread?
    I use the bukkit scheduler btw.

    I hope you guys can help me with this, the source of my whole plugin can be found here:
    GriefLog

    Thanks in advance, blackwolf12333
     
  2. Offline

    Vynlar

    Try checking if each of the components are null. Like, Bukkit, and the Server. To see if it is the variable, or the higher components. Also, which file in the source is the line located in? I'd rather not look through everything.
     
  3. Oh yeah sorry, forgot that, look at the package: tk.blackwolf12333.grieflog.rollback
    both files are almost the same, but the problem occurs in both of the files so, in the rollback function you will find the variable "world" which is a global variable btw.
    But i have to go now, i'll be back in 2 or 3 houres.

    greetz blackwolf12333
     
  4. Bukkit and getServer() will never return null. The only reason Bukkit.getServer().getWorld(worldname) would return null is if there is no world with the specified name. It might be if it's in a bad thread (Async)
     
    justcool393 likes this.
  5. The world it there, i checked, looping through the List<World> from Bukkit.getServer().getWorlds().
    And second, i use a syncrepeating thread for the rollback.
     
  6. Offline

    Firefly

    I get this problem too when a world isn't generated by the server, or if you use a single player world. It's as if the server doesn't recognize it as a world. I'm interested to see of Theres a solution. Also, if I were to loop through every world, the world that is null won't be in the list.
     
  7. I know, but the world has the name world, and that world was the only world in the list, so it does exist, but it i try to get it it returns null, which is really weird i think
     
  8. It returns null if it can't match the string to a world, maybe do a System.out.println(worldname); to check if the world name is correct.
     
  9. Offline

    Deathmarine

    Or you can get hardcore and define more parameters for your search with worlds bukkit already knows exist.
    Code:
     
    public World findWorld(String string){
    Iterator<World> worlds = plugin.getServer().getWorlds().iterator();
    while(worlds.hasNext()){
    World world = worlds.next();
    if(world.getName().equalsIgnoreCase(string)){
    return world;
    }
    }
    return null;
    }
    
     
  10. Deathmarine That's pointless, it's exactly what Bukkit does
     
  11. As i already said in the first post, i tried that, it is correct.
     
  12. Offline

    Deathmarine

    Its probably a case issue.
     
  13. No, i looked at craftbukkit's source, it sets the worldname to lowercase so case is also not the problem:(
    I was thinking that this might be caused by the fact that i use the scheduler for this, i don't hope it is, but you never know...

    Ok, i tried going hardcore, didn't work:(

    Alright, so i did some debugging...
    What i found was that when i use the hardcore method, and i check the names, they are not equal, when i print the names, they are equal, here is the code:
    Code:
    public World getWorld(String name) {
        Iterator<World> worlds = plugin.getServer().getWorlds().iterator();
        while(worlds.hasNext()) {
            World w = worlds.next();
            String wName = w.getName().toLowerCase();
            System.out.print(wName);
            System.out.print(name); // this is the variable passed in to the method
            // these print the name: world, i only have one world so...
            // now i check if they are equal, this doesn't output anything...
            if(wName.equals(name.toLowerCase()) {
                  System.out.print("debug"); // just to test if the function even gets here
                  return w;
            }
        }
     
        return player.getWorld(); // player is a global variable in this case
    }
    I really wonder why this doesn't work, the names are equal, yet the function equals(String) says they aren't equal..
    Am i doing something wrong?

    greetz blackwolf12333

    Ok now i am pretty sure, this is caused by the fact that somehow, i can't use getWorld(String name) in a SyncRepeatingTask, i haven't tried yet with other tasks, but i guess that will end up the same...
    if that does end up the same as it does now, i am gonna report it as a bug in bukkit/craftbukkit.

    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: May 26, 2016
  14. mayby some spaces after the name, thinked about that, also print the lenghth of the string out
     
  15. I'll try thanks
     
  16. Offline

    Deathmarine

    I agree with ferrybig try to add .trim() to both of your strings and .equalsIgnoreCase(AnotherString)
     
  17. Ok, it is a bit weird, but that worked, now i have to find out why there are spaces in the names, and which name it is...but i will be able to do that myself i hope:)
    Thanks so much:)
    greetz blackwolf12333
     
  18. the name that has the bigger length has spaces
     
  19. Yeah, i found it, the problem was that i forgot that the name was on the end of a line, and i split that line to a String[] by .split(" "), which didn't remove the \n at the end of the line...
     
  20. Offline

    Sheepii

    Is the world loaded? if it is why not just make a command sender while you're in the world. /whatistheworld
    Code:
            @Override
            public boolean onCommand(CommandSender sender, Command cmd, String commandLabel, String[] args)
            {
                    if(commandLabel.equalsIgnoreCase("worldname") && (args.length == 0))
                    {
                      sender.sendMessage(ColoredChat.Green + sender.getLocation().getWorld() + " is the world you're in.")
                    }
            }
    
    Not sure if this will work.
     
  21. Guys, this thread is old you know... and it is solved already:p
     
Thread Status:
Not open for further replies.

Share This Page