Iterate over strings?

Discussion in 'Plugin Development' started by se1by, Oct 28, 2011.

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

    se1by

    I need to get three string values and an int value from my config, even if I don't know how often I got to load them.
    Here is an example:
    Code:
    reward1:
      commandPre:  /give
      commandSuf:  diamond 1
      description: get 1 diamond
      prize:  5
    reward2:
      commandPre:  /give
      commandSuf:  iron 6
      description: get 6 iron
      prize:  3
    And I don't know if reward3, reward4 or reward5 exists.
    I want /rewards to list all rewards ingame like that:
    Code:
    type in reward1 to get 1 diamond!
    type in reward2 to get 6 iron!
    Is it possible to make the /rewards command that flexible?
     
  2. Offline

    scranner

    so what do you want to actully send the player?
     
  3. Offline

    se1by

    The /rewards should give them a list of every possible reward.
     
  4. Offline

    DDoS

    Get the keys for your config (getKeys method, set deep to false), and give the returned set to a new variable. Now iterate over the values, and get the stuff you want. Example:

    Code:
    FileConfiguration config = getConfig();
    
    Set<String> rewards = config.getKeys(false);
    
    for (String reward : rewards) {
    
    ConfigurationSection configSec = config.getConfigurationSection(reward);
    
    String cmdPre = configSec.getString("commandPre");
    String cmdSuf = configSec.getString("commandSuf");
    String descr = configSec.getString("description");
    int prize = configSec.getInt("prize");
    
    }
     
  5. Offline

    se1by

    And then inside the for loop:
    Code:
    public void listRewards(CommandSender sender)
        {
            FileConfiguration config;
            Set<String> rewards = config.getKeys(false);
            for (String reward: rewards) {
    
                ConfigurationSection configSec = config.getConfigurationSection(reward);
    
                String cmdPre = configSec.getString("commandPre");
                String cmdSuf = configSec.getString("commandSuf");
                String descr = configSec.getString("description");
                int prize = configSec.getInt("prize");
    
                sender.sendMessage("Type in /reward1 to get " + descr + ".");
                sender.sendMessage("Costs: " + prize + " .");
    
                }
        }
    right?

    Could someone help me with this part, I have totally no experience with that.

    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: May 20, 2016
  6. Offline

    DDoS

    @se1by Can you explain in more detail what you're looking for? What do you want to achieve?
     
  7. Offline

    se1by

    The admin should be able to create as many rewards as he want in the config.yml.
    The plugin should be able to see how much rewards are set and list them if you type in "/rewards".
    I think of adding a array where the admins write in which rewards are set, and that will e used to iterate to lsit all rewards.
    Do you understand?
     
  8. Offline

    DDoS

    Make a new class for a reward, with the command prefix and suffix, the description, and the prize as class variables.
    Give it a constructor to set those values, and two methods: getDescription and giveReward(Player player). When you load the config stuff, in the for loop, create a new instance of the class with the variables you got from the config as the constructor values, then store the object in a hash map for your main class (use the reward string as the key). When someone types in "/rewards" (onCommand) get the values from the hash map, iterate over them in the same way as shown above, and for each reward you'll iterate over, get the description using the method getDescription() and use that to send the message.

    Edit: For your config, create a loader class with a load method that takes a FileConfiguration as an argument, and that returns a hash map with all the rewards in it, and in onEnable() make a new loader instance, call the load method and store the returned hash map with all the rewards in your main class.
     
  9. Offline

    se1by

    I'll try that, thx

    I got a problem with this part:

    Code:
    FileConfiguration config;
    Set<String> rewards = config.getKeys(false);
    If I import Set, I would have to set a value to config.
    That would work imo.
    Can you please help me?

    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: May 20, 2016
  10. Offline

    DDoS

    ???

    Set a value? "Set" is an object similar to a list, it's just a group of objects of the same type. Here, strings.
     
  11. Offline

    se1by

    seems as I mixed up a few things, sry.
    It says "Set" cannot be resolved to a type.
     
  12. Offline

    DDoS

    Fix your imports.

    Code:
    import java.util.Set;
    Works fine for me:

    Code:
    Set<String> subZones = configSec2.getKeys(false);
    
    for (String subZone : subZones) {
    
        ConfigurationSection configSec3 = configSec2.getConfigurationSection(subZone);
    
        int numOfMobs = configSec3.getInt("number_of_mobs");
        List<String> mobTypes = configSec3.getList("mob_types");
    
        subZoneData.put(subZone, new QSubZoneData(numOfMobs, mobTypes));
    
    }
     
  13. Offline

    se1by

    Next error:
    "The local variable config may not have been initialized"
    Thanks for your patience :)
     
  14. Offline

    DDoS

    If you're in your main class, replace:

    Code:
    FileConfiguration config;
    with:

    Code:
    FileConfiguration config = getConfig();
     
  15. Offline

    se1by

    Seems to work now, thank you so much[diamond] :)[diamond]
     
  16. Offline

    DDoS

    No problem!
     
Thread Status:
Not open for further replies.

Share This Page