Getting an exact match from a List

Discussion in 'Plugin Development' started by The Gaming Grunts, Mar 22, 2014.

Thread Status:
Not open for further replies.
  1. Hey everyone! So I'm completely "blanking" on this problem. So I have an List<String> that contains different strings (amazing, right?). I have a method and in that method there is a check to see if the inputted string is contained in the List. It seems, however, that as long as at least one word is entered that is contained in the list will make the contains() check return true. For example, the value "allow pvp" is contained in the List. However, if only "allow" were entered, the check will return true, which is not supposed to happen. Is there a way to make it so the check will return true if and only if the inputted string is an exact match? Thanks :)

    Code:java
    1. /**
    2.   * This method is used to edit the default properties for the worlds listed in the "worlds.yml" file
    3.   *
    4.   * @param sender : Who will receive info about the editing process
    5.   * @param world : The name of the world to edit the properties of
    6.   * @param attribute : The attribute that will be edited
    7.   * @param value : Boolean value. Set the attribute to enabled (true) or disabled (false)
    8.   */
    9. public void editDefaultAttribute(CommandSender sender, String world, String attribute, String value){
    10. FileConfiguration c = WorldFileManager.getInstance().getWorlds();
    11.  
    12. if (getAttributesList().contains(attribute)){
    13. c.set(world + "." + attribute, Boolean.parseBoolean(value));
    14. WorldFileManager.getInstance().saveWorlds();
    15. sender.sendMessage(MessageManager.getChatPrefix() + ChatColor.GRAY +
    16. "Updated value of " + ChatColor.AQUA + attribute + ChatColor.GRAY + " to " +
    17. ChatColor.AQUA + value + ChatColor.GRAY + " in the world " + ChatColor.AQUA + world);
    18. }else{
    19. sender.sendMessage(MessageManager.getChatPrefix() + ChatColor.DARK_RED + "Invalid attribute specified (" + attribute + "). Valid attributes: ");
    20. sender.sendMessage(getAttributesList());
    21. }
    22. }
    23.  
    24. /**
    25.   * Gets a list of world attributes that can be modified in the 'worlds.yml' file
    26.   *
    27.   * @return List of world attributes
    28.   */
    29. public String getAttributesList(){
    30. List<String> attrib = Arrays.asList("weather", "allow pvp", "allow hostile mobs",
    31. "allow passive mobs", "allow hunger", "allow health regeneration",
    32. "enable animal spawn limit", "allow daylight cycle");
    33. return attrib.toString().replace("[", "").replace("]", "");
    34. }
     
  2. Offline

    JBoss925

    Well, if you don't want to use string builders or a ton of args and stuff then I would just change it to something like "allow_pvp", if not the second easiest would be if the args[0] is "allow" then check if args[1] is pvp or if args[1] is "hostile" then check if args[2] is "mobs" if not that then look into the string builder.
     
Thread Status:
Not open for further replies.

Share This Page