Solved Checking a list in config.yml

Discussion in 'Plugin Development' started by Markyroson, Jun 7, 2015.

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

    Markyroson

    Hello, I am wondering how I go about checking if something exists within a list stored in the config.yml file, if it does then I want to remove that item, if it doesn't then I would tell the player so and not do anything else. I think I have figured out how to remove an item from the list but I am not sure how to check if it exists before trying to remove it (because if it doesn't exist that will cause some problems).

    Any ideas?
     
  2. Offline

    Zombie_Striker

    @Markyroson
    Do you have any code? If so, then why did you not post it?

    Loop through the file and see if it contains what you're looking for, or if it's a list use List#contains(Object);
     
  3. Offline

    Markyroson

    sorry, the code for the section in question is:

    Code:
     if(args[0].equalsIgnoreCase("remove")) {
              //TODO: Need to set it up so that if the player isn't in list it tells the sender and does not attempt to remove it.
              if(args[1].equalsIgnoreCase("owner") || args[1].equalsIgnoreCase("owner")) {
                  //remove player (arg[2]) from list
                  List<String> removeList = (List<String>)plugin.getConfig().getList("Owner");
                  removeList.remove(args[2]);
                  plugin.saveConfig();
                  plugin.reloadConfig();
                  p.sendMessage(args[2] + " was removed from the Owners list.");
              }
              else if(args[1].equalsIgnoreCase("admin") || args[1].equalsIgnoreCase("administrator")) {
                  List<String> removeList = (List<String>)plugin.getConfig().getList("Admin");
                  removeList.remove(args[2]);
                  plugin.saveConfig();
                  plugin.reloadConfig();
                  p.sendMessage(args[2] + " was removed from the Admin list.");
              }
              else if(args[1].equalsIgnoreCase("mod") || args[1].equalsIgnoreCase("moderator")) {
                  //remove player (arg[2]) from list
                  List<String> removeList = (List<String>)plugin.getConfig().getList("Mod");
                  removeList.remove(args[2]);
                  plugin.saveConfig();
                  plugin.reloadConfig();
                  p.sendMessage(args[2] + " was removed from the Moderator list.");
              }
          }
    and I have figured out how to remove the player (the method above of doing it works fine), it is just how to check if the player is in the list before trying to remove them. It doesn't generate any errors or break anything doing it if they are not (I tested that too) but it will just tell the player they were removed even if they werent there in the first place. What I want to do is have it check if the list contains the player in question, if not then tell them that the player is not in the list and don't do anything further, if so then proceed with above.
     
  4. Offline

    GrimReaper52498

    @Markyroson

    Not sure if you've got it figured out or not but:


    Code:
     if(args[0].equalsIgnoreCase("remove")) {
     
              if(args[1].equalsIgnoreCase("owner") || args[1].equalsIgnoreCase("owner")) {
                  //remove player (arg[2]) from list
                  List<String> removeList = (List<String>)plugin.getConfig().getList("Owner");
             if(removeList.contains(args[2])){
                  removeList.remove(args[2]);
              }else{
               //Not in the list
              }
                  plugin.saveConfig();
                  plugin.reloadConfig();
                  p.sendMessage(args[2] + " was removed from the Owners list.");
              }
     
  5. wat
    1. if(args[1].equalsIgnoreCase("owner") || args[1].equalsIgnoreCase("owner")) {
     
  6. Offline

    GrimReaper52498

  7. Offline

    Markyroson

    That is one of the evils of copying&pasting haha. Thank you for catching that though ;-) @TheGamesHawk2001
    I was wondering if that would have worked or not and was going to try it out tonight, now I know it does. Thank you! :)

    I will mark this thread as "resolved" once I confirm it working at home on my PC ;-)


    EDIT: It did work. Thank you all for your help on this and sorry that I did not post the code initially (an oversight on my part...okay, okay, I'll tell the truth, I just forgot, that's all ;):p)
     
    Last edited: Jun 8, 2015
Thread Status:
Not open for further replies.

Share This Page