How to send Configuration Keys to a player

Discussion in 'Spigot Plugin Help' started by morningstarIWI, Feb 29, 2020.

  1. Screenshot_33.png

    How can I send these "COAL_ORE, IRON_ORE, LAPIS_ORE etc." to a player as a message?

    I've tried:
    Code (Java):
    Set<String> bloks = plugin.c3.getConfigurationSection(orr+".requirements.block-break").getKeys(false);
        for (String blck : bloks) {
        player.sendMessage(blck);
    }
    I've tried this but It only returns the first one, which is "COAL_ORE"
     
  2. No one that can help? Not even a clue?
     
  3. i can't help you without more code. what is c3? the config? what is orr?
     
  4. c3 is the config, "orr" is only a standart string
     
  5. Material.toString(Material.materialid)
     
  6. so
    p.sendMessage(Material.toString(Material.COAL));
     
  7. No, you didn't understand. I'm trying to send keys of a configuration section. Which is;
    Code (Java):
    Set<String> bloks = plugin.c3.getConfigurationSection(orr+".requirements.block-break").getKeys(false);
    I've tried to send it with this;
    Code (Text):
    for (String blck : bloks) {
        player.sendMessage(blck);
    }
    But it only sent the first key
     
  8. drives_a_ford

    Moderator

    You could simply add a debug message where you print out the ConfigurationSection.
    Chances are, this section only has that one key.
    Perhaps you're accessing a different part of the config than you think you are?
     
  9. I'm using this exact same code to control these keys. And I can control all the keys with just "if (blck.contains("stuff"))...." I tested it and it does get all of the keys and checks them all, no problem here. The problem is, I'm trying to send these keys to a player as a message but It only sends the first one. I don't know what mistake I made and apparently no one else as well.
     
  10. Strahan

    Benefactor

    Well, we're not omniscient. Your code sends all of them to the player for me. If you change it to:
    Code (Text):
    plugin.getLogger().info("Subkey count: " + bloks.size());
    for (String blck : bloks) {
        plugin.getLogger().info("Reading: " + blck);
        player.sendMessage(blck);
    }
    ...does the console say count 4 and show all keys or just first as well?
     
  11. @morningstarIWI

    You could use this method

    Code (Java):
    plugin.configuration.getConfigurationSection("section").getKeys(false).forEach(a -> {
                 player.sendMessage(a);
                });
    "section", would be in this case, the 'block-break'.
     
    • Winner Winner x 1
  12. sub.png
    Thank you! It worked!
     
    • Like Like x 1
  13. Sorry, one last question. I can send the keys thanks to your method. It's perfect! But how can I send the keys with their values?
     
  14. drives_a_ford

    Moderator

    If you insist on using that specific for, you'd add player.sendMessage(plugin.configuration.getConfigurationSection("section").getString(a)) right below the last send.
    But you should really cache your ConfigurationSection and use the methods of the cached instance. Not to mention the fact that this is prone to produce NPEs when the section does not exist.
     
    • Winner Winner x 1
  15. Thank you very much it worked! Can't I just send them as they are, like: "COAL_ORE: 15", because your code sends a message like this:
    "COAL _ORE
    15"
     
  16. helping us, we can do it!