Solved Problem with custom config file

Discussion in 'Spigot Plugin Development' started by Menfie, May 30, 2016.

  1. Hey there I have been working on this thing for hours but can't find any solution on how to load my file properly and I came here to ask for help.

    Here is my main class:
    Code (Text):
    public class Acikarttirma extends JavaPlugin {
        public static LinkedHashMap <AItem, Player> itemshashmap = new LinkedHashMap<>();
        public File itemfile = new File(getDataFolder(), "items.yml");
        public FileConfiguration itemconfig = YamlConfiguration.loadConfiguration(itemfile);

        @Override
        public void onEnable() {
            setupEconomy();
            getCommand("aa").setExecutor(new Commands());
            getServer().getPluginManager().registerEvents(new Events(), this);

            try {
                esyaconfig.load(itemfile);
            } catch (IOException e) {
                e.printStackTrace();
            } catch (InvalidConfigurationException e) {
                e.printStackTrace();
            }

            int number = 0;
            for (String playeruuid : esyaconfig.getStringList("players.")) {
                OfflinePlayer player = Bukkit.getPlayer(UUID.fromString(playeruuid));
                Bukkit.getServer().broadcastMessage("s");
            }
        }
        public void onDisable() {
            int number = 0;
            itemfile.delete();
            for (Map.Entry<AItem, Player> e : itemshashmap.entrySet()) {
                String p = e.getValue().getUniqueId().toString().replace("-", "");
                AItem a = e.getKey();
                ItemStack i = a.item;
                int price = a.price;
                esyaconfig.set("players." + p + "." + number + ".item", i);
                esyaconfig.set("players." + p + "." + number + ".price", price);
                number = number + 1;
            }
            try {
                esyaconfig.save(itemfile);
            } catch (Exception e) {
            }
        }

        public static Economy econ = null;
        private boolean setupEconomy() {
            RegisteredServiceProvider<Economy> economyProvider = getServer().getServicesManager().getRegistration(net.milkbowl.vault.economy.Economy.class);
            if (economyProvider != null) {
                econ = economyProvider.getProvider();
            }

            return (econ != null);
        }
    }
    And this is what items.yml file looks like:
    Code (Text):
    players:
      70e7b0a2be9139e499df96094b09e8cb:
        '0':
          item:
            ==: org.bukkit.inventory.ItemStack
            type: GRASS
            meta:
              ==: ItemMeta
              meta-type: UNSPECIFIC
              lore:
              - '§cPrice: §755$ §cSeller: §7§4Menfie§r'
              - §01
          price: 55
        '1':
          item:
            ==: org.bukkit.inventory.ItemStack
            type: SAND
            meta:
              ==: ItemMeta
              meta-type: UNSPECIFIC
              lore:
              - '§cPrice: §711$ §cSeller: §7§4Menfie§r'
              - §02
          price: 11
     
    I can save the file there is zero problem with the yml file that gets generated but I can't load it. The broadcast thing is not sending any message.
     
    • Useful Useful x 1
  2. I did what you suggest but this get me the players: section how can I get the uuid and not "players:"?

    Code (Text):
    int number = 0;
            for (String playeruuid : itemconfig.getKeys(false)) {
                Player player = Bukkit.getPlayer(UUID.fromString(playeruuid));
                Bukkit.getServer().broadcastMessage(player.getDisplayName());
            }
    The error is Invalid UUID string: players
     
  3. try using
    Code (Text):
    itemconfig.getConfigurationSection("players").getKeys(false))
     
    • Useful Useful x 1
  4. Thanks for your support and help I'm really dumb with yaml and this solved my issue thanks again. -.-