Solved Make Arguments Case Insensitive

Discussion in 'Plugin Development' started by YoungCerly, May 30, 2016.

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

    YoungCerly

    Hey bukkit! I've made a voucher plugin, everything is finished, however when you give a player a voucher, it is case sensitive, how would I go about fixing this?

    For example;
    If the config looks like this:

    PHP:
    Vouchers:
      
    Money:
        
    IDPAPER
        Name
    '&a&l+ $10,000'
        
    Lore:
          - 
    '&7&oRight click to receive $10,000'
        
    Command'eco give {NAME} 10000'
        
    Message'&a&lVOUCHERS &8> &7You have received 10000'
    The player would have to type /voucher give YoungCerly Money
    How could I make it to where the capitalisation doesn't matter?
    So they could type /voucher give YoungCerly money or /voucher give YoungCerly MoNeY
     
  2. Offline

    I Al Istannen

    @YoungCerly
    Loop through all the keys in Vouchers and check if the key equalsIgnoreCase to "Money". Or save the key directly in lower case/upper case (just stay consistent) in your config and then convert the argument to lower/upper case.
     
  3. Offline

    Zombie_Striker

    @YoungCerly
    If you know that player has joined before, you can use "Bukkit.getOfflinePlayer(String)" to get the offline player's instances regardless of case. From there, you would use OfflinePlayer#getName() to return the exact name with the correct capitalization.
     
  4. Offline

    YoungCerly

    The player part isn't the issue, I should've been more clear.

    Okay, so this is what I have for my code;

    Code:
    for (String str : plugin.getConfig().getConfigurationSection("Vouchers").getKeys(false)) {
                        ArrayList<String> keys = new ArrayList<>();
                        keys.add(str.toUpperCase());
                        if (!keys.contains(args[2].toUpperCase())) {
                            p.sendMessage(ChatColor.RED + "Keys: " + ChatColor.GRAY + keys);
                            p.sendMessage(ChatColor.RED + "Args: " + ChatColor.GRAY + args[2]);
                        } else {
                            p.sendMessage(str);
                        }
                    }
    This is the message it's displaying in game.
    [​IMG]

    This is the config;

    PHP:
    ######################
    #      Vouchers      #
    # Made By YoungCerly #
    ######################

    #Settings
    Send-Messagetrue

    #Vouchers
    Vouchers:
      
    Money:
        
    IDPAPER
        Name
    '&a&l+ $10,000'
        
    Lore:
          - 
    '&7&oRight click to receive $10,000'
        
    Command'eco give {NAME} 10000'
        
    Message'&a&lVOUCHERS &8> &7You have received 10000'

    #Messages
    Unknown-Player'&a&lVOUCHERS &8> &7Could not find the player {PLAYER}'
    Unknown-Voucher'&a&lVOUCHERS &8> &7Could not find the voucher {VOUCHER}'
    Usage:
      - 
    '&7&l&m--------------------------------------------'
      
    '&2&lGiving Vouchers:'
      
    '  &7To give a voucher type &a/voucher give <name> <voucher>'
      
    '&7&l&m--------------------------------------------'
    It's saying that args[2] is in the arraylist and that it isnt.
    I don't know what the issue is
     
  5. Offline

    Zombie_Striker

    @YoungCerly
    It looks as though you only have two arguments instead of three. Remember: the first argument starts as 0 (e.g. arg[0]), so the second argument is args[1].
     
  6. Offline

    YoungCerly

    I don't think that's the issue,
    args[0] = give
    args[1] = player
    args[2] = voucher
    There's 3 arguments
     
  7. Offline

    Zombie_Striker

    @YoungCerly
    My mistake. Your real issue is this:
    You are testing if the keys contain a the string "MONEY", which is all capital. What you have in the config is "Money" which only has the first letter capitalized. The .contains method is CaSe Sensitive. To solve this problem: loop through all the keys and test if each individual string equalsIgnoreCase args[2].
     
  8. Offline

    I Al Istannen

    @Zombie_Striker
    "keys.add(str.toUpperCase());"
    He converted both to upper case. Just in a quite strange way... .


    @YoungCerly
    You loop through all keys. You don't need to add the string to an arraylist. The arraylist is deleted every iteration and only one item added, which means it is quite useless. Just check if str.equalsIgnoreCase args[2].
    The iusse may be, that you send the message on every iteration. So if you have two keys, it will be false for one. You could only send the message if it is found.
     
    MasterDoctor likes this.
  9. Offline

    Xerox262

    @YoungCerly Print str aswell so you can see what it's trying to compare it to then post a screenshot of that too?
     
  10. Offline

    YoungCerly

    I ended up solving the issue if anyone else is having the same issue this is what I did.
    Step 1: I created an arraylist, and added all of the keys into it but I made them all uppercase using
    Code:
    ArrayList<String> vouchers = new ArrayList<String>
      for (String parent : plugin.getConfig().getConfigurationSection("Vouchers").getKeys(false)) {
        vouchers.add(parent.toUpperCase());
    }
    Step 2: I checked if my arraylist contained args[2] in all uppercased letters using
    Code:
    if (vouchers.contains(args[2].toUpperCase())) {
     
    }
    Step 3: I checked if the keys were equal to args[2] (ignoring case) using
    Code:
    if (parent.equalsIgnoreCase(args[2])) {
      /*
       * You can use parent to get a certain value
       * For example;
       * String name = plugin.getConfig().getString("Vouchers." + parent + ".Name");
       */
    }
    Now I know I could of completely skipped step 2 and just used the check on step 3, however I wanted step 2 so I could send the player a message saying that their voucher wasn't actually a voucher in the config.

    And that's how I did it, so far it's working out well for me.
     
Thread Status:
Not open for further replies.

Share This Page