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?
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"); }
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.
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?
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.
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.
??? Set a value? "Set" is an object similar to a list, it's just a group of objects of the same type. Here, strings.
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)); }
If you're in your main class, replace: Code: FileConfiguration config; with: Code: FileConfiguration config = getConfig();