Can't get a String with getConfig().getString()

Discussion in 'Plugin Development' started by Wayz3ks, Jul 6, 2021.

Thread Status:
Not open for further replies.
  1. Offline

    Wayz3ks

    I tried to put a name in main.getConfig().getString() in an ItemMeta
    sorry for my english I'm french
    upload_2021-7-6_18-33-23.png
    Code:
    
        @EventHandler
        public void onClick(InventoryClickEvent event) {
           
            Inventory inv = event.getInventory();
            Player player = (Player) event.getWhoClicked();
            ItemStack current = event.getCurrentItem();
            ItemStack rank = new ItemStack(Material.PAPER);
            ItemStack next = new ItemStack(Material.STICK);
           
            ItemMeta ranks = rank.getItemMeta();
            ranks.setDisplayName(main.getConfig().getString("ranks.name1"));
            rank.setItemMeta(ranks);
           
           
            if(current == null) return;
           
            if(inv.contains(current)) {
               
                if(current.getType() == Material.PAPER && current.hasItemMeta()) {
                   
                    inv.clear();
                    event.setCancelled(true);
                                           
                    int all = main.getConfig().getInt("ranks.nbr"); 
                            for(int i = 0; i < all; i++){
                                List<String> lore = main.getConfig().getStringList("ranks.name" + i + "." + name);
                                String name = main.getConfig().getString("ranks.name" + i);
                                System.out.println(name);
                                ItemStack skull = new ItemStack(Material.BLAZE_ROD, 1, (byte) 3);
                                ItemMeta meta = skull.getItemMeta();
                                meta.setDisplayName(name);
                                //meta.setLore(lore);
                                skull.setItemMeta(meta);
                                inv.setItem(i, skull);
                        }
                   
                }
           
        }
    }
    
    upload_2021-7-6_18-37-8.png
     

    Attached Files:

    Last edited by a moderator: Jul 6, 2021
  2. Offline

    Tim_M

    Code:
    //This is what your config would have to be for your code to work
    ranks:
      nbr: 1
      name0: ply
    
    In your config "name0" holds ranktest1, which is not a string, it's a list.

    So, to get "ply" from ranktest1 in name0 here's what you should do:
    Code:
    List<String> list = main.getConfig().getStringList("ranks.name0.ranktest1");
    String name = list.get(0);
    
    I would give you the code you need, but then you wouldn't learn anything. Try to read my entire post and at the least TRY to solve it yourself.
     
    Last edited: Jul 29, 2021
  3. Offline

    Strahan

    Agreed. But per SOLID design, you should be instantiating that list as List<String> list which would give you the added benefit of not having to cast your get.
     
    KarimAKL likes this.
  4. Offline

    Tim_M

    Fair enough, it's just a bad habit of mine.
     
Thread Status:
Not open for further replies.

Share This Page