Solved config help

Discussion in 'Plugin Development' started by HenkDeKipGaming, Jul 3, 2015.

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

    HenkDeKipGaming

    Hey, i want to check if something is anywhere in the config file.
    so this doesn't seem to work:
    Code:
    if(!getConfig().contains("test")){
    sender.sendMessage("test is not there")
    } else {
    sender.sendMessage("test is there")
    }
    
    i want to check if something is in the config file, even if it's like this:
    Code:
    Players:
      Groups:
        PlayerName1: test
        PlayerName1: testing
        PlayerName1: lol
    
    how to do this?
     
  2. Offline

    webbhead

    Use:
    if (config.getString("test") == null) {
    Its not there
    } else {
    its there
    }
     
  3. Offline

    HenkDeKipGaming

    no, that doesn't work.
    it allways says it's not there
     
  4. Offline

    webbhead

    Well if
    test: "string"
    Isn't there it will be null.

    What is your config?

    EDIT: Then do
    if (config.getString("Players.Groups.PlayerName1") == null) {
    its not there
    } else {
    its there

    I though't "test" was the path to the string. When you do config.getString("") inside getString you need the path to string so: config.getString("path.to.string");
     
  5. This won't work, as FileConfiguration#contains(String):
    - Checks if the PATH (each path is unique) exists, not the KEY (which there can be multiple of).
    - Does not check values, which is what you want.

    For what you want to achieve, you can loop through every entry in the configuration (fairly inefficient, so why do you even want to do something like this? Can you give a reason for why you even want to check if a value exists?) and check if the value is equal to "test".

    Code:
    private boolean containsValue(Object value) {
        for (Map.Entry<String, Object> configEntry : getConfig().getValues(true).entrySet()) { // Loop through EVERY config key and value in the configuration.
            if (configEntry.getValue().equals(value)) return true;
        }
    }
    
    This may or may not work. If it does not work, change "Object value" to "String value" and then change "configEntry.getValue()" to "configEntry.getValue().toString()". I was trying to make this more generic for use.

    Also, since this is a utility method, you might as well make it static and then add in another parameter for the FileConfiguration, and use that FileConfiguration object instead of getConfig(). Then when calling the static method, input in getConfig().
     
    CodePlaysMinecraft likes this.
  6. Offline

    webbhead

    I thought he was trying to see if it existed. Haha thanks this helped me, and I think he is just trying to test. Considering: "test", "testing", "lol"
     
  7. Offline

    HenkDeKipGaming

    @webbhead
    @KingFaris11
    no, i tried to show you guys what i wanted xD

    but i'm making a pluginw here you can create factions, and i want to check if anyone has allready created the faction or not. so if the faction is created the config looks like this:
    Code:
    Guilds:
      Leaders:
        HenkDeKipGaming: lol
    
    so HenkDeKipGaming is in the faction "lol"
    and if someone else makes the faction "lol" it should stop him.
    am i clear now? :)

    greetings!
     
  8. Yes, I understand what you want now. My method above will work, but in the first place, how you're handling guilds is poor, no offence.

    I'd have every Guild in the configuration the other way, like this for example:
    Code:
    Guilds:
      Test:
        Leader: HenkDeKipGaming
        Members:
        - webbhead
      CoolPeople:
        Leader: KingFaris10
        Members: []
    
    Then it'll be more efficient, as you can do things like:
    Code:
    if (!this.getConfig().contains("Guilds.Lol")) {
    
    } else {
    
    }
    
    There'll be no need for my method in my previous post, which is fairly inefficient.

    As he said above, I knew he was trying to make a plugin that does something similar to his example. It was an example configuration, not an example plugin. And you're welcome =)

    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: Jun 12, 2016
  9. Offline

    BurnyDaKath

    Using .getString() for that is easy.

    Code:
    if (config.getString("Guilds.Leaders." + player.getName()) == "lol"/*or how you want to get the name, maybe args[0]*/){
          //player already has "lol" in config
    } else {
          //player doesn't have "lol" in config
    }
     
  10. Offline

    HenkDeKipGaming

    it's not about the palyer, it's about if any of the players in Guilds.Leaders has that behind his name...

    thanks
     
  11. Offline

    BurnyDaKath

  12. Offline

    HenkDeKipGaming

    omg, i didn't think about that xD

    thanks!

    -SOLVED!
     
  13. I'm done.

    Firstly, you completely ignored what I said.

    Secondly, you're using the bad practice @BurnyDaKath has told you, of using the "==" operator for Objects (Strings) and not using String#equals method.

    Thirdly, what he suggested wouldn't even work if swapping the Guild Name and Player Name. Why?

    Because, if your config looks like this:
    Code:
    Guilds:
      Leaders:
        HenkDeKipGaming: lol
    
    And you use: if (config.contains("Guilds.Leaders." + guildName))
    It'd always return "false" unless a guild name is a player name.

    Unless your configuration is actually, "HenkDeKipGaming" is the guild name and "lol" is the player username? That's a bit weird...
     
  14. Offline

    BurnyDaKath

    I always forget about ".equals("")", but i think it is useless until you need ".equalsIgnoreCase("")".
     
  15. No. "==" is a reference check, and the same text in a String can have a different reference thus will return false if using "==" but .equals() is a value check, and the same text in a String always has the same value.

    More information: http://perso.ensta-paristech.fr/~diam/java/online/notes-java/data/expressions/22compareobjects.html

    You should only use "==" on a primitive (int, boolean, float, short, byte, double, long), checking if null, or an enum (as there is only ever one instance of an enum constant in a JVM).
     
  16. Offline

    HenkDeKipGaming

    well i'm really sorry i ignored you.
    but i'm just beginning and i find it really hard to understand the code you sent me.
    by swapping them, my problem is solved. the config now looks messy but everything works fine, so yeah...
    i'm sorry for ignoring you! didn't mean it like that!
    i appreciate your answers! but i want to understand everything i make and what you did was to hard for me to understand

    Greetings!
     
  17. It's fine, you could've just told me and I would've explained. Eventually you'll realise how messy your config and code is, but that time will come once you've learnt more =)
     
Thread Status:
Not open for further replies.

Share This Page