How to access a config from another plugin?

Discussion in 'Spigot Plugin Development' started by Fluddershy, Apr 30, 2017.

  1. Hello, I googled this question about 100 times already, and I've found some helpful things, but I can't get the concept. I want to access the essentails/worth.yml and gather all the strings inside. I know what I got to do for that, but getting the file is what is most confusing. My first bit of code that I found slightly helpful was:
    Code (Text):
    FileConfiguration EssentialsConfig;
    File f = new File("plugins/Essentials/worth.yml");
    if(!file.exists()) return;
    EssentialsConfig = YamlConfiguration.loadConfiguration(f);
    I used that, but I would keep having to make an instance and the strings and values we're empty so I don't think I was getting it the right file. I've also found:
    Code (Text):
    But, I wanted to get a specific file and Essentials has multiple configs. How would I go about getting this.
    Where would I put the code and how would I instantiate it? (Doesn't matter what method) I've never done anything like this before, so I'm fairly new to it. Thanks in advance!
  2. Well if you're trying to read the worth file it might be a good idea to look at essentials source code on github and see how they went about it.

    They will have the file reading / parsing done for you.

    The config files however you've already done!

    If you want to read it in once do that in your on enable and Save it to a config or file variable then use a getter to get it in other classes. If it's something you're accessing rarely it's okay to read it in when you need it if it's often use a variable to store it or part of the information.

    Sent from my iPhone using Tapatalk
  3. You should do something like
    Code (Text):
    File worthCfgFile = new File(Bukkit.getPluginManager().getPlugin("Essentials").getDataFolder(), "worth.yml");
  4. I can use any of the things like save() and getString() with it tho.
  5. That is a File ( not a FileConfiguration, you have to check if it exists and if it does, load it with final FileConfiguration config = YamlConfiguration.loadConfiguration(file);
  6. You probably shouldn't be updating another plugins config directly, use their built in methods for modifying things and let the plugin save the data as designed.

    For example if you want to get a balance using vault get the balance through the API and update it add/subtract it has all those methods for you. Then the plugin will write that file as needed.

    If you manually update that file and the plugin does not know about it because you bypassed it it could overwrite your changes later and cause problems you'll have a hard time tracking down.

    Sent from my iPhone using Tapatalk
    • Agree Agree x 2
  7. Yep I forgot that. And check if it exists or else return out of the method.
  8. Okay, so I got the config, but now, I want to get something else, if you know, I'd be very grateful, since I've been multitasking at googling and reading this thread.
    This is what I want to do with the file
    Code (Text):
      wheat: 15.0
      cooked_pork : 6.0
    That's not the actual thing, just an example. And, by googling, I found out a bit, and I can loop through the items and get all thje amounts, but not the exact type tho. Here is what I'm using:
    Code (Text):
    for (String key : config.getConfigurationSection("worth").getKeys(false)) {
                String string = config.getString("worth." + key);
    That give's me this:

    When I kinda wanna check if the player has any of the strings in their inventory.
  9. Material blah = Material.getMaterial(string);

    Will get you the material type from a name.

    Then you could loop through the inventory and see if the ItemStack.getType() == blah

    Sent from my iPhone using Tapatalk
  10. I think saving a configurationsection instance would be better

    Code (Text):
    ConfigurationSection section = config.getConfigurationSection("worth");
    for (String key : section.getKeys(false)) {
                String string = key + ':' + section.getDouble(key);
    I just made this of the top of my mind
  11. Okay, there's a problem in your code:
    ยท You're not getting a String, those values are Doubles, so do something like for (final Double current : config.getDoubleList("worth"))

    EDIT: Okay, my bad, you're trying to get each key... Then the code of @ramidzk is right, but that spoon-feed :p
  12. Gianluca

    Gianluca Retired Resource Staff
    Retired Patron

    If you want to access Essentials' worth.yml file, no need for all this fancy stuff, they made it very easy (here).
    • Informative Informative x 2
    • Winner Winner x 1
  13. I never used Essential's API, but how would you get the instance?
  14. Just google how to hook into essentials or use vault it's even easier

    Sent from my iPhone using Tapatalk
  15. A) Add it as library (not maven)
    B) Import the class (com.earth2me.essentials.Worth)
    C) Worth w = new Worth();

    Note: the arg of the Worth class is a File, so you just have to define a File that has the path to the Essentials config folder

    EDIT: This works for every single .jar that has a public method, that's why I recommend using getters and those things ALWAYS (some exceptions are included xdd) ^.^
    • Like Like x 1