Matching plugin version with a config version?

Discussion in 'Spigot Plugin Development' started by Moderance, May 28, 2016.

  1. So I am wanting to see if the plugin version is the same as a config version so I can match them up. This is checked every time a player joins after:

    Code (Text):
    if (!file.exists()) {
    To see if they actually have their own config, that's not the problem, the problem is this:

    Code (Text):
    if (config.getString("ConfigVERSION") != plugin.getDescription().getVersion()) {
    So if the players config and the plugin version are not the same then it will check for all of the objects/'things' that should be in the config, is there a better/more efficient way of doing this:

    Code (Text):
    if (config.getString("ConfigVERSION") == null) {
                    config.set("ConfigVERSION", plugin.getDescription().getVersion());
                }
               
                if (config.getString("CurrentName") == null) {
                    config.set("CurrentName", player.getName());
                }
               
                if (config.getString("Rank") == null) {
                    config.set("Rank", "Default");
                }
               
                if (config.getString("Warnings") == null) {
                    config.set("Warnings", 0);
                }
    So it doesn't overwrite data that is already there, but adds the default settings if they don't have it in their config, and at the end I have this:

    Code (Text):
    System.out.println(player.getName() + " data updated!");
    This is always printed in the console and I don't know why so the only reason I have for it is that there is something wrong with this code:

    Code (Text):
    if (config.getString("ConfigVERSION") != plugin.getDescription().getVersion()) {
     
  2. electronicboy

    IRC Staff

    You can't compare a string with boolean operators, You'll have to compare with equals()
     
    • Like Like x 1
  3. How would it look? I'm getting NullPointerException with:

    Code (Text):
        if (!config.getString("ConfigVERSION").equals(plugin.getDescription().getVersion().toString())) {
                config.set("ConfigVERSION", plugin.getDescription().getVersion());
                //etc
     
     
  4. 2nd Question, anyway to "shorten" this?:

    Code (Text):
    if (config.getString("ConfigVERSION") == null) {
                    config.set("ConfigVERSION", plugin.getDescription().getVersion());
                }
               
                if (config.getString("CurrentName") == null) {
                    config.set("CurrentName", player.getName());
                }
               
                if (config.getString("Rank") == null) {
                    config.set("Rank", "Default");
                }
               
                if (config.getString("Warnings") == null) {
                    config.set("Warnings", 0);
                }
     
  5. Are you creating individual file for each player? If you make it all in one file, you can easily shorten it.
     
  6. Code (Text):
    if (config.getString("ConfigVERSION") == null) {
                    config.set("ConfigVERSION", plugin.getDescription().getVersion());
                }
    Remove if.
    Code (Text):
    config.set("ConfigVERSION", plugin.getDescription().getVersion());
     
  7. You can create list for each player and have it in the same config file.
     
  8. Yeah, I get that, I just need to know how.
     
  9. OHOHOOH
    Code (Text):

        @EventHandler
        public void onPlayerJoin(PlayerJoinEvent e) {
            String u = e.getPlayer().getUniqueId().toString();
            if (!(plugin.getConfig().contains(u)) {
                plugin.getConfig().set(u + ".Example, "Test");
                plugin.saveConfig();
                plugin.getLogger().info("Created info for " + u);
                return;
            }
            plugin.getLogger().info("Info already exists for " + u);
        }
     
  10. Code (Text):
        public static HashMap<Player, HashMap> players = new HashMap<Player, HashMap>();
        {     HashMap<String, String> info = new HashMap<String, String>();
            info.put("Warnings", "0");
            info.put("Name", player.getName());
            //other informations
            players.put(player, info);
        }
    Try this.
     
  11. This is good too
     
  12. Nono.. the players already have their seperated configs loaded into a seperate folder named their UUIDs. I just need to know how to add the data inside of the config if I decide to add another value rather than having like 10 if statements.
     
  13. Why are you having separate files for each player? Not only that, but why each inside a folder with their UUID?

    Anyway, Try using a for loop.
     
  14. So I can store data for each separate player? Like how many times they've been banned, their rank, etc..?
     
  15. In my server I can store the same thing, but I do it all in only 2 configs:
    punishments.yml
    data.yml
     
  16. Well that's you? I rather not have to open a big file size, easier just to search for a player's uuid
     
  17. Tru, it's up to personal preference.
    Anyway, back to topic, I'll give you this to put you on track:
    Code (Text):
    for (String s : config.getKeys(false) {
    if (config.get(s).equals(null)) {
    config.set(s, 0);
    //example value
    }
    }