1.16.5 Get Key of a Value from YML

Discussion in 'Spigot Plugin Development' started by DrBlender, Apr 25, 2021.

Thread Status:
Not open for further replies.
  1. Hey,

    i need a bit support with YML data. How can I get the lenght of a YML data and how can i get the key of a Value?

    My YML data looks like this:

    Code (Text):
    DrBlender: 23
    With this snippet from me I can get only the Value of a Key...

    Code (Text):
    if (ymlFile.contains(String.valueOf(tmp))){
        do smth
    }
     
  2. SteelPhoenix

    Moderator

    What are you trying to accomplish?
    https://xyproblem.info/

    It's a one-to-many mapping, meaning

    Code (YAML):
    a: 1
    b
    : 1
    is allowed and
    Code (YAML):
    a: 1
    a
    : 2
    is not.

    In the first case, you can't get the key from a given value, because it can either be 'a' or 'b'.
     
  3. Trying to follow the title, if you are using
    Code (Text):
    ymlFile.contains(String.valueOf(tmp))
    , you already know the key ¯\_(ツ)_/¯
     
  4. olijeffers0n

    olijeffers0n Previously jeffmcjefferson

  5. Hi, I think this is what you need :
    Code (Java):
    for(String key : getConfig().getKeys()) {

         System.out.println(key);

    }
     
  6. Try these
    Code (Java):
    getConfig().isSet("DrBlender"); // returns true if the key 'DrBlender' is present

    int value = getConfig.getInt("DrBlender"); // gets the value based on the key 'DrBlender'
     
  7. Code (Java):
    String whoChose(int chosen) {

    for(String key : config.getKeys(false)) {
       if(config.getInt(key) == chosen) {
            return key;
        }
    }
    return null;
    }
     
Thread Status:
Not open for further replies.