Solved Get values from keys in config

Discussion in 'Plugin Development' started by pipo3090, Feb 2, 2017.

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

    pipo3090

    I'm trying to make a plugin in which you can sign up for jobs(e.g. farmer, miner etc.).
    But I have a really annoying bug. I have a forloop that goes through all the keys in the config section:

    Code:
    public void getItems(){
           
            for(String key : core.getConfig().getConfigurationSection("JobItems").getKeys(false)){
               
                //TODO fix item bug
               
                int i = Integer.parseInt(key);
               
                items.add(new ItemStack(i, 1));
               
            }
           
        }
    But now I have a NBE. I think I know what the problem is, but don't know how to fix it(problem is possibly that "getKeys()" returns the name of the key, and not the value?).

    Config:
    Code:
    JobItems:
    
        Timberman: 258
    
        Miner: 257
    
        Farmer: 292
    
    If anyone can help me, feel free to reply ;)
     
  2. Offline

    JanTuck

    @pipo3090
    core.getConfig().getConfigurationSection("JobItems")

    ^^ This should be set in a variable above and then just call .getInt(key) on the object set, settings above will make sure you dont call it twice when you have to call it once again in the loop.
     
  3. Offline

    pipo3090

    Can you give an example? I don't really know what you mean.
     
  4. Offline

    JanTuck

    Code:
    ConfigurationSection section = section
    for key in section do
          get an int from section with the key key.
    end
    
     
  5. Offline

    pipo3090

    I tried, but I get an error: Can only iterate over an array or an instance

    EDIT: here's my code that I used
    Code:
    ConfigurationSection cs = core.getConfig().getConfigurationSection("JobItems");
           
            for(String key : cs/*error is here*/){
               
               
                //items.add(new ItemStack(is, 1));
               
            }
     
    Last edited: Feb 2, 2017
  6. Offline

    timtower Administrator Administrator Moderator

  7. Offline

    pipo3090

    Made an edit with the code that I used.
     
  8. Offline

    timtower Administrator Administrator Moderator

    JanTuck likes this.
  9. Offline

    pipo3090

Thread Status:
Not open for further replies.

Share This Page