Custom Config files help

Discussion in 'Plugin Development' started by TekxWolf, Jul 15, 2015.

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

    TekxWolf

    First and foremost I am not talking about the default config.yml

    Now that thats out of the way, onto my point.
    For my plugin I am looking to use custom YAML files to store the data specific to a certain part.
    I know the normal config.yml methods like getConfig(), soveConfig(), etc. As well I did read the the bukkit wiki documentations. but it didnt help me in what I am trying to do

    So I know using this:
    Code:
    String name = arg[0];
    File customConfigFile = new File(this.getDataFolder(), "users/" + name + ".yml");
    
    I can create the actual file which I will use. Which is good but its only half the battle...
    I know that in order to set values and retrieve values from it you need to declare it using
    Code:
    FileConfiguration <name>;
    and to use it you need to define it
    Code:
    <name> = new YamlConfiguration();
    and then using the <name> above you load the contents of the File to its FileConfiguration
    Code:
    <name>.load(<name>File);
    and then you can use it like you could do for default config.yml

    What I am trying to do is make the .yml custom named so using my plugin they would do
    " /egui create gui <name> " and it would generate a new configuration yml file special to just that yml file

    and then using the org.bukkit.inventory.Inventory generate a new Inventory partial to just that yml file
    like so
    Code:
    Inventory Inv = Bukkit.createInventory(p, 9, "EGui");
    Where Inv would be a custom name which would be the same name registered to the .yml file declared in the command " /egui create gui <name> "

    I know this seems like a lot and maybe it is. I honestly don't even know if what I am trying to do is possible but any informative reply would be greatly appreciated
     
  2. @TekxWolf I needed the same (almost) in my Administration Panel plugin. Maybe you could use HashMaps to put the actual inventory as a key and it's name as a value. Then create a second HashMap, having the inventory name as key and it's generated ConfigurationFile as the value. Using the inventories name you could then retrieve the config you need.
     
  3. Offline

    TekxWolf

    I understand the concept of what you mean but I've never been overly skilled with hashmaps, mainly because when I was learning java HashMaps weren't part of my tutorials.

    Could you show me an example? Or direct me to a place where I can find out how to do this?
    Thnx
     
  4. @TekxWolf Alright I'm home now. Of course!
    First of all, I think that you only need 1 hashmap, the one with Inventory and Inventory is not needed. And also instead of Inventory Name and FileConfiguration, I would use Inventory and FileConfiguration, as with the Inventory you can retrieve it's title + much more things, but it's up to you how you set it up.

    So, to create HashMaps, you simply do
    Code:
    HashMap<Key, Value> variable = new HashMap<Key, Value>();
    You replace Key and Value by the Variable Type or Object or whatever. So in your case you need an Inventory as the key and a FileConfiguration as the value, so you would do
    Code:
    HashMap<Inventory, FileConfiguration> invConfigs = new HashMap<Inventory, FileConfiguration>();
    Then, whenever the user types in your command
    Code:
    /egui create gui <name>
    you would create an Inventory with the given name, create a configuration file, no matter which variable name you use and put them into the HashMap, using
    Code:
    invConfigs.put(Inventory, FileConfiguration);
    To keep the configs during reloads and so on, also save it to a database or another config, or however you wish to do that. You could also have all the custom generated configs in a separate folder in the plugin's data folder, so you could then on server start / reload just iterate through the whole folder and for each found file and create a new inventory with the title set to the file's name without the .yml extension. In the .yml file you would then just store some basic information of how the inventory looked like, how many slots it had and it's content.

    But for simplicity, let's just save the hashmap to the default config, but you could consider the above second solution.
    Code:
    for(Entry<Inventory, FileConfiguration> invConfigsEntry : invConfigs.EntrySet()) {
    plugin.getConfig().set("hashmap." + invConfigsEntry .getKey(), invConfigsEntry .getValue());
    }
    EDIT: Actually, I don't know how it would work if you save Inventory and FileConfigurations, so just try it, otherwise 2 Strings saying the title of the inventory and another one for the config file name would do it too. Or only 1 string if those 2 things are the same.

    Hopefully I could help, if you still need some help or it didn't work how I was expecting, feel free to contact me either per PM or here on the thread.
     
    Last edited: Jul 15, 2015
  5. Offline

    TekxWolf

    Do you know how essentials does it?

    Like how they store a players data in their own YAML file. Thats basically what I am trying to. I understand the process but i don't understand how I am supposed to do it...
     
  6. @TekxWolf Yeah, the data they need they just retrieve and save it, nothing hard. But they have only one file, you need multiple files, as you told.
    If you want to stotre someones uuid, you just use player.getUniqueId().toString() and save that to the config. If you want to store his health you retrieve the players health and save it, etc. Or what were you asking for? The above examples do the same, just in multiple files. Say you want to access the file of TestGui Inventory, you would use invConfigs().get(testgui).set(path, toSave);

    EDIT: and please tag me next time so I know about you earlier.
     
    Last edited: Jul 15, 2015
Thread Status:
Not open for further replies.

Share This Page