Check name in a file?

Discussion in 'Plugin Development' started by TobyG123, Aug 26, 2012.

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

    TobyG123

    How do you search the contents of a certain file and do an if/else if that string is there or not?

    i.e.

    if (player.getName() isfound in names.yml){
    DO SOMETHING
    }
    else{
    DO SOMETHING ELSE
    }
     
  2. Offline

    macguy8

    Well, Is this file you're talking about a section of the config.yml file, or is it another .yml file? If it's a config file, It's possible, for anything else I'm not to sure.

    To do this in a config file, The code would be:
    Code:
    if (getConfig().isSet("SomeFolder." + player.getName()) && getConfig().get("SomeFolder." + player.getName() + "SomeData") != null) {
    //Do something if they are in the SomeFolder.<TheirName> in the config.yml
    } else {
    //Do something if they aren't in the SomeFolder.<TheirName> in the config.yml
    }
     
     
    
     
  3. Offline

    QuantumDev

    Load a yml config with a string list like:

    Code:
    #Example config
    names:
    - Name1
    - Name2
    - Name3
    Then load the names with config.getStringList("names").

    Then check if the player's name is in that list:

    Code:
    List<String> names = config.getStringList("names");
    if (names.contains(player.getName()) {
        //Do stuff
    }
     
  4. Offline

    macguy8

    QuantumDev
    This will work, but keep in mind there will be no way to keep data under each player, just the player names. (Depends on if his plugin needs to store user-dependent data).

    TobyG123
    If this really matters for the plugin, You'll want to use my way, because you can store data under each player, but if it's just a list of players, etc, His way is more efficent, and a little nicer to edit the data manually.

    Okay, Do you need to store data under each player, or just the player names in the config?

    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: May 28, 2016
  5. Offline

    TobyG123

    Just the player names.
     
  6. Offline

    QuantumDev

    macguy8

    In his pseudo code, he has a file named names.yml, which would lead us to believe he only wants to store player names...
     
  7. Offline

    macguy8

    Oh, I read that wrong

    Okay, It looks like QuantumDev's code would work best for you here

    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: May 28, 2016
  8. Offline

    TobyG123

    Hmm...

    List cannot be resolved to a type.

    Config cannot be resolved

    player cannot be resolved.
     
  9. Offline

    QuantumDev

    TobyG123

    That's not plug and go code, you have to make the player variable, and you have to load a config.

    Import java.util.List for the list issue.
     
Thread Status:
Not open for further replies.

Share This Page