Retrieve data from config.yml to hashmap

Discussion in 'Plugin Development' started by zoren3105, Jan 4, 2022.

  1. Offline

    zoren3105

    I am currently making a land claim plugin that stores the chunk coordinate and the player's UUID and have got it to work when it comes to storing the data in the yml file but when it comes to retrieving the data it does not work correctly

    this is the code:
    Code:
    public void restoreClaimedChunks() {
            for (String s : getConfig().getConfigurationSection("data").getKeys(false)) {
                UUID content = UUID.fromString("data");
                chunks.put(s, content);
            }
        }
    and this is the code in the on enable function

    Code:
    public void onEnable() {
            getCommand("claim").setExecutor(new ClaimCommand(this));
            getCommand("unclaim").setExecutor(new UnclaimCommand(this));
            getServer().getPluginManager().registerEvents(new PreventionListener(this),this);
            this.saveDefaultConfig();
    
            if (this.getConfig().contains("data")) {
                this.restoreClaimedChunks();
            }
        }
     
  2. Offline

    The_Spaceman

    UUID.fromString("data") is wrong, you want UUID.fromString(s). This will get the UUID object of s, otherwise UUID will try to create the UUID object of the string "data"
     
  3. Offline

    byteful

    I'd recommend using a flatfile data storage solution like SQLite or H2. If you need the ability to easily edit data, I guess YML or JSON works, but you could still have a GUI-based or command-based editor for data.
     
  4. Offline

    Strahan

    I'd also suggest you assume any data you read from a config is suspect. getConfigurationSection() returns null if the section isn't there, and that would crash the plugin.

    Never hang methods off of other methods that could return null; null check it first. Also I assume your config is setup with chunk coord as the key and the UUID of the owner as the value, right? If so, you need to getString the value to be able to get the UUID. Example:

    Code:
    public void restoreClaimedChunks() {
      ConfigurationSection data = getConfig().getConfigurationSection("data");
      if (data == null) return;
    
      for (String s : data.getKeys(false)) {
        try {
          UUID content = UUID.fromString(data.getString(s, ""));
          chunks.put(s, content);
        } catch (IllegalArgumentException ex) {
          getLogger().info("** Invalid UUID for chunk " + s + "!");
          continue;
        }
      }
    }
    I also wrapped the UUID conversion in a try/catch, again, suspecting someone may have fat-fingered an edit of the config. I am very distrustful of data people can touch, lol
     

Share This Page