Use *userconfig* in other functions.

Discussion in 'Spigot Plugin Development' started by Zarlen, May 2, 2017.

  1. Hello,
    I am trying to create a config file for each player with the name of the player UUID.
    I also fixed some bugs, and it finally works. But now I'm trying to change a value in the config.
    This is my code:
    Code (Text):
    @EventHandler
    public void createUserData(PlayerJoinEvent event) throws IOException {
    File userfile = new File(this.getDataFolder() + File.separator + event.getPlayer().getUniqueId() + ".yml");
    if (!userfile.exists()) {
    getDataFolder().mkdir();
    userfile.createNewFile();
    FileConfiguration userconfig = YamlConfiguration.loadConfiguration(userfile);
    userconfig.set("balance", "250");
    userconfig.set("testboolean", "disable");
    userconfig.save(userfile);
    }
    }
    But now I want to use userconfig in the other function but how? I tried to make a public object but it faills.
     
  2. You shouldn't make a new file for every player. You should do something like this: (with real UUIDs)
    Code (Text):
    users:
      1234-5678:
        balance: 250
        testboolean: disable
      8765-4321:
        balance: 250
        testboolean: disable
    The 1234-5678 and the 8765-4321 are ConfigurationSections. Google them to learn more about them. I don't answer your question, because if you decide to use this (better) way, my answer will be obsolete.
     
  3. I won't do that because there are 5k players on the server and it will create massive lagg to read the whole config file to get one player and its variables.
     
  4. You need to ditch the whole idea of flat files and move to a database. It will be faster and easier to use.

    If you get this flatfile solution working you'll get it implemented then another 2-3k users and you'll be looking for another solution. Especially if you ever try to access data for all the players. Such as getting the player with the highest balance.

    I do hope you're not doing this just for storing a balance variable as there's tons of economy plugins that already have this sort of thing worked out for you.


    Sent from my iPhone using Tapatalk
     
  5. For 5k players I would seriously consider using a database. If you really want to use flatfiles you should use something faster than YAML (JSON/GSON). Also, storing the file in memory and not reading it in every time you need a bit of information is an option too (an option which uses up a lot of memory instead of taking time).