If statement error

Discussion in 'Plugin Development' started by Coopah, Feb 6, 2015.

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

    Coopah

    I'm really confused right now because of this error! How come when I have a else to this statement it will always run the else? If I remove the else it will actually work and proceed to do what it's suppose to.

    So the error is, when it's checking the "if (list.contains(chunk2) {" and it does in fact contain chunk2 it will always skip to the else statement and run that instead. When I delete the else statement it works fine?!?!

    Code:
    Code:
    @EventHandler
        public void onPlayerMove(PlayerMoveEvent e) {
    
            Player p = e.getPlayer();
    
            Chunk chunk = p.getLocation().getChunk();
            String chunk2 = chunk.getWorld().getName() + "/" + chunk.getX() + "/" + chunk.getZ();
            String cName = plugin.getConfig().getString(p.getName() + ".Clan");
    
            if (plugin.getConfig().contains("Claims")) {
    
                for(String string : plugin.getConfig().getConfigurationSection("Claims").getKeys(true)) {
    
                    List<String> list = plugin.getConfig().getStringList("Claims." + string);
    
                    if (p.getLocation().getBlockX() % 16 == 0 || p.getLocation().getBlockY() % 16 == 0) {
    
                        if (list.contains(chunk2)) {
                           
                            if (plugin.getConfig().contains(p.getName())) {                   
    
                                if (!plugin.getConfig().getString(p.getName() + ".Clan").contains(string)) {
    
                                    if (!plugin.getConfig().getString(p.getName() + ".PrevChunk").contains(chunk2)) {
    
                                    plugin.getConfig().set(p.getName() + ".PrevChunk", p.getLocation().getWorld().getName() + "/" + p.getLocation().getBlockX() + "/" + p.getLocation().getBlockY());
                                    p.sendMessage(StringList.pref() + "Territory: " + string);
                                   
                                    }
    
                                    plugin.saveConfig();
                                    return;
    
                                } else if (plugin.getConfig().getString(p.getName() + ".Clan").contains(string)) {
    
                                    if (!plugin.getConfig().getString(p.getName() + ".PrevChunk").contains(chunk2)) {
    
                                        plugin.getConfig().set(p.getName() + ".PrevChunk", chunk.getWorld().getName() + "/" + chunk.getX() + "/" + chunk.getZ());
                                        p.sendMessage(StringList.pref() + "Territory: " + ChatColor.AQUA + string);
    
                                    }
    
                                    plugin.saveConfig();
                                    return;
    
                                } else if (plugin.getConfig().getStringList(cName + ".Allies").contains(string)) {
    
                                    if (!plugin.getConfig().getString(p.getName() + ".PrevChunk").contains(chunk2)) {
    
                                        plugin.getConfig().set(p.getName() + ".PrevChunk", chunk.getWorld().getName() + "/" + chunk.getX() + "/" + chunk.getZ());
                                        p.sendMessage(StringList.pref() + "Territory: " + ChatColor.LIGHT_PURPLE + string);
    
                                    }
    
                                    plugin.saveConfig();
                                    return;
    
                                } else if (plugin.getConfig().getStringList(cName + ".Enemies").contains(string)) {
    
                                    if (!plugin.getConfig().getString(p.getName() + ".PrevChunk").contains(chunk2)) {
    
                                        plugin.getConfig().set(p.getName() + ".PrevChunk", chunk.getWorld().getName() + "/" + chunk.getX() + "/" + chunk.getZ());
                                        p.sendMessage(StringList.pref() + "Territory: " + ChatColor.RED + string);
    
                                    }
    
                                    plugin.saveConfig();
                                    return;
    
                                }
                            }
                        } else {
    
                            if (!plugin.getConfig().contains(p.getName())) {
    
                                plugin.getConfig().set(p.getName(), "");
                                plugin.getConfig().set(p.getName() + ".PrevChunk", p.getLocation().getWorld().getName() + "/" + p.getLocation().getBlockX() + "/" + p.getLocation().getBlockY());
                                p.sendMessage(StringList.pref() + "Territory: " + ChatColor.DARK_GREEN + "Wilderness");
    
                            } else if (plugin.getConfig().getString(p.getName() + ".PrevChunk") == null) {
    
                                plugin.getConfig().set(p.getName() + ".PrevChunk", "");
    
    
                            } else if (!plugin.getConfig().getString(p.getName() + ".PrevChunk").contains(chunk2)) {
    
                                plugin.getConfig().set(p.getName() + ".PrevChunk", null);
                                plugin.getConfig().set(p.getName() + ".PrevChunk", chunk.getWorld().getName() + "/" + chunk.getX() + "/" + chunk.getZ());
                                p.sendMessage(StringList.pref() + "Territory: " + ChatColor.DARK_GREEN + "Wilderness");
    
                            }
    
                            plugin.saveConfig();
                            return;
    
                        }
    
                    }
                }
            }
    
    
        }
     
  2. Offline

    teej107

    code line?
     
  3. Offline

    Coopah

  4. Offline

    teej107

    @Coopah Well it makes so sense that the if part of the statement will work by just removing the else. Put in debug code.
     
  5. Offline

    Coopah

    @teej107
    I have. Whenever I have the else statement it goes straight to there. It's really weird. It just completely skips the if statement.
     
  6. Offline

    teej107

    @Coopah because the if part of the statement is false
     
    Last edited: Feb 6, 2015
  7. Offline

    Coopah

    @teej107
    It isn't! When I remove the else it works perfectly fine.
     
  8. Offline

    teej107

    @Coopah Probably because you are doing something in the else statement to make the if statement false when it runs again!
     
  9. Offline

    Coopah

    @teej107
    As far as I can tell, the if statement isn't even being ran when the else statement is there. I added in test messages to each part and the first part which is just after it checks (if (list.contains(chunk2) {) isn't being ran.
     
  10. Offline

    teej107

     
  11. Offline

    Coopah

    @teej107
    Now why would it be false? It works correctly without the 'else'?
     
  12. Offline

    teej107

    because "list" does not contain "chunk2".

    Is that really meant to be a question?
     
  13. Offline

    Coopah

    @teej107
    How can it possible be false? I'm looking straight at the path of it in the config. I'm not changing anything but once I remove the else and run it. It works perfectly fine. How can it be false when nothing changes. List does contain chunk2.
     
  14. Offline

    teej107

    @Coopah Something in your else is making the if condition false. Does the if part run at least once?
     
  15. Offline

    Coopah

    @teej107
    The if statement doesn't even run... I tried to send a message straight after it checked. Nothing happened. It's basically just skipping past it.

    Perhaps it has something to do with this line?
    Code:
     if (p.getLocation().getBlockX() % 16 == 0 || p.getLocation().getBlockY() % 16 == 0) {
    
     
    Last edited: Feb 6, 2015
  16. Offline

    Rocoty

    That is probably it. Unless you're at an X value that is divisible by 16 (0, 16, 32...) and at a similar height value, it's not going to even check if the list contains the chunk.
     
  17. Offline

    Coopah

    @Rocoty
    I changed it to this:
    Code:
    if (event.getFrom().getChunk().equals(event.getTo().getChunk()) {
    Still the same error though?
     
  18. Offline

    teej107

     
  19. Offline

    Rocoty

    Okay. Add debug prints at EVERY point where it might fail (before and after every if-statement). Also print out the value of chunk2 once you get it. It might not be what you expect.

    Honestly you'd probably find your code a lot easier to debug if it were a bit more tidy. Lots of nested blocks makes the code really difficult to read and follow.
     
  20. Offline

    Coopah

    @Rocoty
    Okay, so it's getting down to the check then going straight to the else. The chunks are correct "world/x/y". I'm puzzled.

    Okay now when I remove the else statement nothing seems to happen...

    Updated code, now when I remove the else statement it works.
    Code:
    @EventHandler
        public void onPlayerMove(PlayerMoveEvent e) {
    
            Player p = e.getPlayer();
    
            Chunk chunk = p.getLocation().getChunk();
            String chunk2 = chunk.getWorld().getName() + "/" + chunk.getX() + "/" + chunk.getZ();
            String cName = plugin.getConfig().getString(p.getName() + ".Clan");
    
            if (plugin.getConfig().contains("Claims")) {
    
                for(String string : plugin.getConfig().getConfigurationSection("Claims").getKeys(true)) {
    
                    List<String> list = plugin.getConfig().getStringList("Claims." + string);
    
                    if (e.getFrom().getChunk().equals(e.getTo().getChunk())) {
                       
                        if (list.contains(chunk2)) {
    
                            if (plugin.getConfig().contains(p.getName())) {                   
    
                                if (!plugin.getConfig().getString(p.getName() + ".Clan").contains(string)) {
    
                                    if (!plugin.getConfig().getString(p.getName() + ".PrevChunk").contains(chunk2)) {
    
                                    plugin.getConfig().set(p.getName() + ".PrevChunk", p.getLocation().getWorld().getName() + "/" + p.getLocation().getBlockX() + "/" + p.getLocation().getBlockY());
                                    p.sendMessage(StringList.pref() + "Territory: " + string);
                                   
                                    }
    
                                    plugin.saveConfig();
                                    return;
    
                                } else if (plugin.getConfig().getString(p.getName() + ".Clan").contains(string)) {
    
                                    if (!plugin.getConfig().getString(p.getName() + ".PrevChunk").contains(chunk2)) {
    
                                        plugin.getConfig().set(p.getName() + ".PrevChunk", chunk.getWorld().getName() + "/" + chunk.getX() + "/" + chunk.getZ());
                                        p.sendMessage(StringList.pref() + "Territory: " + ChatColor.AQUA + string);
    
                                    }
    
                                    plugin.saveConfig();
                                    return;
    
                                } else if (plugin.getConfig().getStringList(cName + ".Allies").contains(string)) {
    
                                    if (!plugin.getConfig().getString(p.getName() + ".PrevChunk").contains(chunk2)) {
    
                                        plugin.getConfig().set(p.getName() + ".PrevChunk", chunk.getWorld().getName() + "/" + chunk.getX() + "/" + chunk.getZ());
                                        p.sendMessage(StringList.pref() + "Territory: " + ChatColor.LIGHT_PURPLE + string);
    
                                    }
    
                                    plugin.saveConfig();
                                    return;
    
                                } else if (plugin.getConfig().getStringList(cName + ".Enemies").contains(string)) {
    
                                    if (!plugin.getConfig().getString(p.getName() + ".PrevChunk").contains(chunk2)) {
    
                                        plugin.getConfig().set(p.getName() + ".PrevChunk", chunk.getWorld().getName() + "/" + chunk.getX() + "/" + chunk.getZ());
                                        p.sendMessage(StringList.pref() + "Territory: " + ChatColor.RED + string);
    
                                    }
    
                                    plugin.saveConfig();
                                    return;
    
                                }
                            }
                        } else if (!list.contains(chunk2)) {
    
                            if (!plugin.getConfig().contains(p.getName())) {
    
                                plugin.getConfig().set(p.getName(), "");
                                plugin.getConfig().set(p.getName() + ".PrevChunk", p.getLocation().getWorld().getName() + "/" + p.getLocation().getBlockX() + "/" + p.getLocation().getBlockY());
                                p.sendMessage(StringList.pref() + "Territory: " + ChatColor.DARK_GREEN + "Wilderness");
    
                            } else if (plugin.getConfig().getString(p.getName() + ".PrevChunk") == null) {
    
                                plugin.getConfig().set(p.getName() + ".PrevChunk", "");
    
                            } else if (!plugin.getConfig().getString(p.getName() + ".PrevChunk").contains(chunk2)) {
    
                                plugin.getConfig().set(p.getName() + ".PrevChunk", null);
                                plugin.getConfig().set(p.getName() + ".PrevChunk", chunk.getWorld().getName() + "/" + chunk.getX() + "/" + chunk.getZ());
                                p.sendMessage(StringList.pref() + "Territory: " + ChatColor.DARK_GREEN + "Wilderness");
    
                            }
    
                            plugin.saveConfig();
                            return;
    
                        }
    
                    }
                }
            }
    
    
        }
    
     
    Last edited: Feb 6, 2015
  21. Offline

    CullanP

    Try converting list to string and sending a message and see what it looks like. Same with chunk2
     
    Last edited: Feb 6, 2015
  22. Offline

    Rocoty

    Basically, what your code does as of now. It checks if the first key in the Claims section to see if the list mapped to it contains the value of chunk2. If this list doesn't contain chunk2 then it goes to the else block. Problem here is that once it gets to the else block you can be certain the code won't continue the loop, because you've put a return statement in there. Try and see what happens if you remove the return statement on line 93.
     
  23. Offline

    Coopah

    @Rocoty
    I removed the returns but the same error occurs. It seems to be only checking the first set of the config. Like you said. When I have 1 clan it will produce the right answer but when there is more then one it just checks the first one then if it's not it skips to the else.
     
  24. Offline

    Rocoty

    Post your updated code?
     
  25. Offline

    Coopah

    @Rocoty
    Code:
    @EventHandler
        public void onPlayerMove(PlayerMoveEvent e) {
    
            Player p = e.getPlayer();
    
            Chunk chunk = p.getLocation().getChunk();
            String chunk2 = chunk.getWorld().getName() + "/" + chunk.getX() + "/" + chunk.getZ();
            String cName = plugin.getConfig().getString(p.getName() + ".Clan");
    
            if (plugin.getConfig().contains("Claims")) {
    
                for(String string : plugin.getConfig().getConfigurationSection("Claims").getKeys(true)) {
    
                    List<String> list = plugin.getConfig().getStringList("Claims." + string);
    
                    if (e.getFrom().getChunk().equals(e.getTo().getChunk())) {
    
                        if (list.contains(chunk2)) {
    
                            if (plugin.getConfig().contains(p.getName())) {                   
    
                                if (plugin.getConfig().getString(p.getName() + ".Clan") == null) {
    
                                    if (!plugin.getConfig().getString(p.getName() + ".PrevChunk").contains(chunk2)) {
    
                                        plugin.getConfig().set(p.getName() + ".PrevChunk", chunk.getWorld().getName() + "/" + chunk.getX() + "/" + chunk.getZ());
                                        p.sendMessage(StringList.pref() + "Territory: " + string);
    
                                    }
    
                                } else if (plugin.getConfig().getString(p.getName() + ".Clan").contains(string)) {
    
                                    if (!plugin.getConfig().getString(p.getName() + ".PrevChunk").contains(chunk2)) {
    
                                        plugin.getConfig().set(p.getName() + ".PrevChunk", chunk.getWorld().getName() + "/" + chunk.getX() + "/" + chunk.getZ());
                                        p.sendMessage(StringList.pref() + "Territory: " + ChatColor.AQUA + string);
    
                                    }
    
                                    plugin.saveConfig();
    
                                } else if (plugin.getConfig().getStringList(cName + ".Allies").contains(string)) {
    
                                    if (!plugin.getConfig().getString(p.getName() + ".PrevChunk").contains(chunk2)) {
    
                                        plugin.getConfig().set(p.getName() + ".PrevChunk", chunk.getWorld().getName() + "/" + chunk.getX() + "/" + chunk.getZ());
                                        p.sendMessage(StringList.pref() + "Territory: " + ChatColor.LIGHT_PURPLE + string);
    
                                    }
    
                                    plugin.saveConfig();
    
                                } else if (plugin.getConfig().getStringList(cName + ".Enemies").contains(string)) {
    
                                    if (!plugin.getConfig().getString(p.getName() + ".PrevChunk").contains(chunk2)) {
    
                                        plugin.getConfig().set(p.getName() + ".PrevChunk", chunk.getWorld().getName() + "/" + chunk.getX() + "/" + chunk.getZ());
                                        p.sendMessage(StringList.pref() + "Territory: " + ChatColor.RED + string);
    
                                    }
    
                                    plugin.saveConfig();
    
                                } else if (!plugin.getConfig().getString(p.getName() + ".Clan").contains(string)) {
    
                                    if (!plugin.getConfig().getString(p.getName() + ".PrevChunk").contains(chunk2)) {
    
                                        plugin.getConfig().set(p.getName() + ".PrevChunk", chunk.getWorld().getName() + "/" + chunk.getX() + "/" + chunk.getZ());
                                        p.sendMessage(StringList.pref() + "Territory: " + string);
    
                                    }
    
                                    plugin.saveConfig();
                                   
                                } else {
    
                                    if (!plugin.getConfig().contains(p.getName())) {
    
                                        plugin.getConfig().set(p.getName(), "");
                                        plugin.getConfig().set(p.getName() + ".PrevChunk", p.getLocation().getWorld().getName() + "/" + p.getLocation().getBlockX() + "/" + p.getLocation().getBlockY());
                                        p.sendMessage(StringList.pref() + "Territory: " + ChatColor.DARK_GREEN + "Wilderness");
    
                                    } else if (plugin.getConfig().getString(p.getName() + ".PrevChunk") == null) {
    
                                        plugin.getConfig().set(p.getName() + ".PrevChunk", "");
    
    
                                    } else if (!plugin.getConfig().getString(p.getName() + ".PrevChunk").contains(chunk2)) {
    
                                        plugin.getConfig().set(p.getName() + ".PrevChunk", chunk.getWorld().getName() + "/" + chunk.getX() + "/" + chunk.getZ());
                                        p.sendMessage(StringList.pref() + "Territory: " + ChatColor.DARK_GREEN + "Wilderness");
    
                                    }
    
                                    plugin.saveConfig();
    
                                }
                            }
                        }
    
                    }
                }
            }
    
    
        }
    
     
  26. Offline

    Rocoty

    And if you put a println at line 13 what is the output? And could you also post the config?
     
  27. Offline

    Coopah

    @Rocoty
    Cofing:
    Code:
    Claims:
      Legacy:
      - world/-2/16
      Testty:
      - world/-3/16
    It is printing two messages, one with the first clan and one with the second.
    Message: "Legacy [world/-2/16]"
    Message 2: "Testty [world/-3/16]"
     
  28. Offline

    Rocoty

    And what does chunk2 print?
     
  29. Offline

    Coopah

    @Rocoty
    chunk2 is the location, (world/x/y)
     
  30. Offline

    Rocoty

    @Coopah When you print it, what is the output?
     
Thread Status:
Not open for further replies.

Share This Page