Solved Plugin is not loading values in config.yml correctly

Discussion in 'Spigot Plugin Development' started by SandboxHD, Feb 8, 2018.

  1. When plugin sets new data in config.yml (saveConfig();) and I try to read the config with new values, it shows me the data that was before saving.

    This is the code that is saving data in config.yml
    Code (Text):

                           
        public boolean onCommand(CommandSender sender, Command cmd, String label, String[] args) {  
            FileConfiguration config = plugin.getConfig();
            if(cmd.getName().equalsIgnoreCase("ciudades")) {
                Player p = (Player)sender;
                p.openInventory(CityListMenu.ListaCiudades);
            }
           
            if(cmd.getName().equalsIgnoreCase("ciudadesadmin")) {
                if(!sender.hasPermission("ciudades.admin")) {
                    sender.sendMessage("§4No tienes permiso para usar esto");
                    return true;
                } else if(args.length == 0) {
                    sender.sendMessage("§cComandos de Ciudades:");
                } else if(args[0].equalsIgnoreCase("create")) {
                    String ciudad = args[1];
                    String owner = args[2];
                    if(ciudad.equalsIgnoreCase(null) || owner.equalsIgnoreCase(null)) {
                        sender.sendMessage("§cModo de uso: /ciudadesadmin create <ciudad> <dueño>");
                    } else {
                        if(ciudad.equalsIgnoreCase(config.getString("Ciudades." + ciudad))) {
                            sender.sendMessage("§cEsta ciudad ya existe");
                        } else {
                            config.set("Ciudades." + ciudad, ciudad);
                            config.set("Ciudades." + ciudad + ".owner", owner);                      
                            config.set("Ciudades." + ciudad + ".home.x", 0);                  
                            config.set("Ciudades." + ciudad + ".home.y", 0);                  
                            config.set("Ciudades." + ciudad + ".home.z", 0);                  
                            config.set("Ciudades." + ciudad + ".home.yaw", 0);                  
                            config.set("Ciudades." + ciudad + ".home.pitch", 0);                
                            config.set("Ciudades." + ciudad + ".home.world", "not-set");
                            plugin.saveConfig();
                            sender.sendMessage("§a¡Ciudad creada con exito! §fColoca el home de la ciudad con §a/ciudadesadmin sethome " + ciudad);
     
    And this is the code that is trying to get the data:

    Code (Text):
           FileConfiguration config = plugin.getConfig();
            Set<String> CityList = config.getConfigurationSection("Ciudades").getKeys(false);
            int i = -1;
            for (String Ciudad : CityList) {
                ItemStack item = new ItemStack(Material.EMPTY_MAP);
                ItemMeta meta = item.getItemMeta();
                meta.setDisplayName("§fCiudad: §a§l" + Ciudad);
                List<String> lore = new ArrayList<String>();
                lore.add("§6");
                lore.add("§fDueño: " + config.getString("Ciudades." + Ciudad + ".owner"));  
                lore.add("§6");          
                String world = config.getString("Ciudades." + Ciudad + ".home.world");
    PD: Sorry if my English is not very good :/
     
    #1 SandboxHD, Feb 8, 2018
    Last edited: Feb 8, 2018
    • Creative Creative x 1
  2. You need to use try catch

    Code (Text):
    try {

    // set config and save config here

    }catch(Exception ex) {
    }
     
    • Creative Creative x 1
  3. I don't think that's entirely necessary.
    My guess is that it's because you're storing the config in a variable. Try just plugin.getConfig().set for all of them.
     
    • Like Like x 1
  4. The part of the plugin where I save the config is working perfectly. The problem appear when I try to get the value from config. Here the problem with images:

    Only one city is in config:
    [​IMG]
    [​IMG]

    Creating city:
    [​IMG]

    City is saved in config...:
    [​IMG]

    ...but it does not appear in menu:
    [​IMG]

    I tried with reloadConfig(); but it still does not work...
     
    • Like Like x 1
  5. Plz I need some help :(. Can anybody help me with this issue?
     
    • Like Like x 1
  6. can you post the rest of the code? Youre missing the code where you add the item to an inventory ?
     
    • Agree Agree x 1
  7. No. I'm not missing that part of the code.

    Here is the full code:
    Code (Text):

        public static Inventory ListaCiudades = Bukkit.createInventory(null, 27, "§8Lista de Ciudades");
            static {
                FileConfiguration config = plugin.getConfig();
                if(config.getConfigurationSection("Ciudades").getKeys(false).isEmpty()) {
                    ItemStack item = new ItemStack(Material.BARRIER);
                    ItemMeta meta = item.getItemMeta();
                    meta.setDisplayName("§cNo hay ninguna ciudad creada");
                    item.setItemMeta(meta);            
                    ListaCiudades.setItem(13, item);  
                } else {
                    genMenu();              
                   
                }
               
        }





        private static void genMenu() {
            FileConfiguration config = plugin.getConfig();
            try {
                File f = new File("config.yml");
                plugin.getConfig().load(f);
            } catch (IOException | InvalidConfigurationException ex) {
                Logger.getLogger(CityCommands.class.getName()).log(Level.SEVERE, null, ex);
            }
            Set<String> CityList = config.getConfigurationSection("Ciudades").getKeys(false);
            int i = -1;
            for (String Ciudad : CityList) {
                Bukkit.broadcastMessage(Ciudad);
                ItemStack item = new ItemStack(Material.EMPTY_MAP);
                ItemMeta meta = item.getItemMeta();
                meta.setDisplayName("§fCiudad: §a§l" + Ciudad);
                List<String> lore = new ArrayList<String>();
                lore.add("§6");
                lore.add("§fDueño: " + config.getString("Ciudades." + Ciudad + ".owner"));    
                lore.add("§6");            
                String world = config.getString("Ciudades." + Ciudad + ".home.world");
                if (world.equalsIgnoreCase("not-set")) {                      
                    lore.add("§cNo puedes teletransportarte a esta ciudad");
                } else {                                      
                    lore.add("§aHaz click para teletransportarte");
                }
                meta.setLore(lore);
                item.setItemMeta(meta);            
                ListaCiudades.setItem(++i, item);
            }
        }    
     
    • Like Like x 1
  8. It seems like it is not loading ConfigurationSection in config.yml. I've tried with all possible methods but it still not works,
     
    • Like Like x 1
  9. Remove this part and it should work. You dont have to load the file again. That's not how the Configurations work.

    Code (Text):
    try {
        File f = new File("config.yml");
        plugin.getConfig().load(f);
    } catch (IOException | InvalidConfigurationException ex) {
        Logger.getLogger(CityCommands.class.getName()).log(Level.SEVERE, null, ex);
    }
     
    • Agree Agree x 1
  10. It does not work removing that part :/
     
  11. Can you print all strings in the list ?
     
    • Agree Agree x 1
  12. Yes, I can. For example: If I create new ConfigurationSection via commands and I try to send all sections with a message in other command, the new section appears but if I create new ConfigurationSection via commands and I try to show the new section in a menu with an new item, this section does not appear. The only way that makes this new item appear in the menu is reloading the plugin...
     
    • Like Like x 1
  13. Doesn't seem you call genMenu whenever you set values to the config.
     
    • Agree Agree x 1
  14. I would never recommend catching a global exception in any situation, if you want players to understand why your plugin has gone wrong I would catch the correct error, for example NullPointer or NunberFormat, otherwise your players will review your plugins as “not working random error” and you can’t find which code and what part is causing the error.
     
  15. You need to call the generation of thet Inventory all the times it opens, because if you generate it only when the server starts it will not add the items created after the start of the server. Call it every time a player wants to open it or every time when a new item should be added
     
  16. How can I do that?? Can you make an example plz? :) I tried to do that and the inventory is not opening after p.openInventory().
     
  17. OH IT WORKS!!!! I didn't put correctly p.openInventory()

    Thanks a lot @dominikremes
     
    #17 SandboxHD, Feb 11, 2018
    Last edited: Feb 11, 2018
    • Like Like x 1
    • Funny Funny x 1
  18. You are welcome!
     
  19. I-I helped too :<
     

Share This Page