.contains() returning false when true

Discussion in 'Plugin Development' started by xXMaTTHDXx, Jul 29, 2014.

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

    xXMaTTHDXx

    I have a list of uuid.toStrings() inside my config, they section is Residents, I had added a check to confirm the uuid in the list is the same as the players uuid when executing the command yet when I do it the .contains is returning false therefore not allowing me to continue with the code:

    Code:java
    1. else if(args[0].equalsIgnoreCase("invite")){
    2. @SuppressWarnings("deprecation")
    3. final Player target = Bukkit.getPlayer(args[1]);
    4.  
    5. Town town = plugin.getTown(p);
    6.  
    7.  
    8. for(String s : plugin.getTowns().getKeys(false)){
    9. boolean contains = false;
    10. for(String r : plugin.getTowns().getStringList(s + ".Residents")) {
    11. p.sendMessage(r + (r.equals(p.getUniqueId().toString()) ? " matches " : " does not match ") + p.getUniqueId().toString());
    12. if(r.equalsIgnoreCase(p.getUniqueId().toString())) {
    13. contains = true;
    14. break;
    15. }
    16. }
    17.  
    18. if(!contains) {
    19. p.sendMessage(ChatColor.RED + "You must be of status " + ChatColor.WHITE + "resident " + ChatColor.RED + "to invite a player to a town.");
    20. return true;
    21. }
    22. }
    23. if(target == null){
    24. p.sendMessage(ChatColor.WHITE + args[0] + ChatColor.RED + " is not online at this time!");
    25. return true;
    26. }
    27.  
    28. if(plugin.getTown(target) != null){
    29. p.sendMessage(ChatColor.WHITE + target.getName() + ChatColor.RED + " is already in a town.");
    30. return true;
    31. }
    32.  
    33. if(target.getName() == p.getName()){
    34. p.sendMessage(ChatColor.RED + "You cant invite yourself to a town!");
    35. return true;
    36. }
    37.  
    38.  
    39. accepting.put(target.getName(), p.getName());
    40. target.sendMessage(ChatColor.YELLOW + "You have seconds to accept the request by typing /town accept playerName");
    41. int TaskId = Bukkit.getScheduler().scheduleSyncDelayedTask(plugin.getPlugin(), new Runnable(){
    42.  
    43.  
    44. @Override
    45. public void run() {
    46. accepting.remove(target.getName());
    47. target.sendMessage(ChatColor.RED + "Request canceled");
    48. }
    49.  
    50. }, 20*30);
    51.  
    52. Town t = plugin.getTown(p);
    53.  
    54. t.getMembers().add(target.getUniqueId().toString());
    55.  
    56. }


    Just look at the for() near the top, thanks!
     
  2. Offline

    1Rogue

    All I see is your own defined boolean "contains", not once do I see a call to Collection#contains. Might be a typo in your key location (e.g. Residents vs. residents).
     
  3. Offline

    xXMaTTHDXx


    I had structured my code to be if(plugin.getStringList(s+".Residents").contains(p.getUniqueId().toString()); yet is still gave me the same problem as this code.

    Along with the fact that the message telling if the uuid matches is true.
     
  4. Offline

    lzravanger

    Because your contains variable and if statement are declared inside the for loop, the player must be in every town otherwise they will fail the contains check for that town and return out. Move your contains variable and if statement outside of the for loop and you should be fine.
     
    1Rogue likes this.
Thread Status:
Not open for further replies.

Share This Page