Solved Help Looping Through worlds

Discussion in 'Plugin Development' started by blok601, Jul 30, 2016.

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

    blok601

    Hi I just have a quick question. This isn't working for me for some reason:
    Code:
                            w = args[0];
                            for (World world : Bukkit.getServer().getWorlds()){
                                p.sendMessage("Debug");
                                if(world.getName().equalsIgnoreCase(w)){
                                    w = args[0];
                                    p.sendMessage(ChatColor.BLUE + "[Nightshade] " + ChatColor.DARK_AQUA + "Starting UHC in world: "  + w);                              
                                }else{
                                    p.sendMessage(ChatColor.RED + "Couldn't find world: " + w);
                                    return;
                                }
                            }
                            BukkitTask r = new BukkitRunnable(){
    
                                @Override
                                public void run() {
                                    p.sendMessage("Debug Message");
                                  
                                }
                              
                            }.runTaskLater(Main.getPlugin(), 20);
    The world that I pass is a legit world but it keeps going to the couldn't find world message. Am I looping through them wrong?

    I am not sure whats wrong, thanks for the help
     
  2. Offline

    AlvinB

    @blok601
    Looks like it should work, have you tried printing out alll the worlds to see if your world is there?
     
  3. Offline

    bwfcwalshy Retired Staff

    @blok601 Why aren't you just getting the world with Server#getWorld(String) and check if that is null, saves the for
     
  4. Offline

    Zombie_Striker

    This is because of a logic error. If the first world it gets is "w", then it will still loop until it is not. If the first world is not "W", then it will stop looking for "w" even though it only checked one world. To fix this, do as @bwfcwalshy said or move the "could not find world" message outside of the for loop.
     
  5. Offline

    Oxyorum

    @blok601 Some observations:

    1) Why are you setting w equal to args[0] before the for loop and inside it. The assignment inside the for loop is superfluous.

    2) If you wanted to keep the code the way it is, and fix the issue that @Zombie_Striker pointed out, you'll want to take the return statement off your else statement, and put a break after the code inside your if statement. That will fix the immediate problem and make sure that the rest of the method code outside the loop executes (return will end the execution of the entire method code, not just your for loop). As for the "could not find world text", putting it outside wont help, since it will execute even if we did actually find the world we are looking for. Keeping it inside will have that text print out for as many worlds as there are on the given server.

    3) @bwfcwalshy is right. Better to just try to grab a world under the name you are looking for (with Server#getWorld()) and do a null check on the result. This way would also make controlling the text output easy.
     
  6. Offline

    blok601

    Thank you all four of you, I will implement the new stuff in my code.

    I will use what @bwfcwalshy said.

    Edit: Solved, thank you everyone
     
Thread Status:
Not open for further replies.

Share This Page