Solved Get parent of value where value equals something ?

Discussion in 'Spigot Plugin Development' started by Reiika, Jul 5, 2018.

  1. Hi people,

    I want to know how can I got parent config section of value where value equals something like that:
    [​IMG]
    I want to got the id ( in red ) using the value "name" (in blue)

    Thanks in advance for your answers
     
  2. ConfigurationSection#getKeys(false) and loop through the entries until you get a matching name. Then you'll know the key (the id in red)
     
  3. for now i do that :
    Code (Java):
    for(String id : ConfigManager.getConfig().getConfigurationSection("crystals").getKeys(false)){
                                ConfigManager.getConfig().set("crystals."+id, null);
                            }
    but now how can i match with my value ?
     
  4. Strahan

    Benefactor

    That will not do anything as you aren't saving, but if you were saving that would wipe out all the crystal info. If you have a name value and you are trying to find the section, you would do

    Code (Text):
    if (ConfigManager.getConfig().getString("crystals." + id + ".name").equalsIgnoreCase(variableWithName)) {
      // do shit
    }
     
  5. I've some time and am bored so I thought sharing this with you.
    Code (Java):

    String result = getConfig().getConfigurationSection("yourSection").getKeys(false).stream()
               .filter(key -> {

               String childname = getConfig().getString("yourSection." + key + ".someValue");
               if (childname.equalsIgnoreCase("something")) {
                   return true;
               }
               return false;

           }).findFirst().orElse(null);
     
    Pretty simple and handy.
     
    #5 darklazerog, Jul 5, 2018
    Last edited: Jul 5, 2018
    • Winner Winner x 1
  6. Inb4 "i'm on bukkit 1.8 and use Jaba 7, tho"

    ^_^
     
    • Funny Funny x 3
  7. It's not entirely true, many people aren't aware of the java 8 changes though they use it.
     
  8. That work ! Thanks


    You mean something like that ?

    Code (Java):
    String result = ConfigManager.getConfig().getConfigurationSection("crystals").getKeys(false).stream()
                                    .filter(key -> {

                                        String childname = ConfigManager.getConfig().getString("crystals." + key + ".name");
                                        if (childname.equalsIgnoreCase(e.getRightClicked().getName())) {
                                            ConfigManager.getConfig().set("crystals."+key, null);
                                            return true;
                                        }
                                        return false;

                                    }).findFirst().orElse(null);
    EDIT : That solved, thanks people <3
     
    #8 Reiika, Jul 5, 2018
    Last edited: Jul 5, 2018
  9. If you're attempting to set something to null this is not the code to do so. Instead you should do something like this:
    Code (Java):
    getConfig().getConfigurationSection("crystals").getKeys(false).stream().filter(key -> {
               String childname = ConfigManager.getConfig().getString("crystals." + key + ".name");
               return childname.equalsIgnoreCase(e.getRightClicked().getName());
           }).findFirst().ifPresent(str -> getConfig().set("crystals." + str, null));
    // save config