Solved Creating and setting items in a config list

Discussion in 'Plugin Development' started by plisov, Mar 21, 2017.

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

    plisov

    I'm trying to create a rewards system so that when a player completes an achievement they receive the items in the config list. I don't necesarilly know how to set ItemStacks into a list however I do know how to set a specific item on one line and retrieve it like so
    Code:
                player.getInventory().addItem(new ItemStack(Material.getMaterial(config.getString("Config.Rewards.Item").toUpperCase()), config.getInt("Config.Rewards.Quantity")));
    
    However I would like to create a list of items and give the players all the items in that list?

    Code:
            config = YamlConfiguration.loadConfiguration(file);
         
            config.getList("Config.Rewards.Tier1.Task1");
         
            player.getInventory().addItem(new ItemStack(Material.REDSTONE, 8));
            player.getInventory().addItem(new ItemStack(Material.IRON_INGOT, 3));
    Example:
    Code:
    Rewards:
         - diamond
              - 5
         - iron_ingot
              - 8
         - iron_hoe
              - 1
     
    Last edited: Mar 21, 2017
  2. Offline

    mehboss

    • Make a for-loop and getKeys(false)
    • Make another for-loop to get the amounts
    Code:
    .. for(item : getKeys(false).. {
       .. for(String amount : getStringList("Rewards." + item + ".amount").. {
                  addItem(new Item(Material.matchMaterial(item), amount)
    Your config would look like this:
    Code:
    Rewards:
      Diamond:
        - 1
      Iron_Ingot:
        - 4
    
       etc
    By the way, this code isn't exact and it won't work by just copy and pasting. It is just to give you an idea. Hope I helped!

    I don't know why you would want the config to have a list for the amounts if you only have one 'amount' per item stack, but meh..
     
  3. Offline

    plisov

    Thanks. The reason as to why I want quantities in the config is because I want the quantities of the item to be changed. So lets say you want to give 4 diamonds instead of 1. You would be able to change the quantity in the config.
     
  4. Offline

    mehboss

    If your problem has been solved, mark this thread as solved.
     
  5. Offline

    plisov

    Haven't tried it yet

    What does getKeys do?
    @mehboss

    I currently have this however matchMaterial is red
    Code:
            List<?> Items = config.getList("Config.Rewards");
            for (int i = 0; i < Items.size(); i++) {
                ItemStack Item = (ItemStack) Items.get(i);
                if (Item == null) {
                    continue;
                }
                player.getInventory().addItem(Material.matchMaterial(Items), 1);
            }
     
    Last edited: Mar 22, 2017
  6. Offline

    mehboss

    You didn't even make the for-loops. getKeys will get every first string in a config section that you specify, if you do specify one.


    Sent from my iPhone using Tapatalk
     
  7. Offline

    plisov

    I've gotten it to work using
    Code:
                ItemStack[] t2t1 = {(new ItemStack(Material.IRON_AXE, 1)), (new ItemStack(Material.IRON_INGOT, 3))};
    
                config.set("Config.Tier2.Task1.Rewards", t2t1);
    
    However in the config it shows up as
    Code:
        Task1:
          Need: 20
          Rewards:
          - ==: org.bukkit.inventory.ItemStack
            type: IRON_AXE
          - ==: org.bukkit.inventory.ItemStack
            type: IRON_INGOT
            amount: 3
    How do I get rid of the - ==: org.bukkit.inventory.ItemStack so that I don't have to copy that plus the type to add a new item to the list?
     
  8. Offline

    raunak114

    If you're gonna be storing custom items (w/ custom names/lores), instead of storing the whole item all together, store it's display name , lore, enchantments seperately, and then get the objects and compile them into 1 itemMeta seperately,


    Instead of storing he itemstack, store the material name, and generate a new itemstack
     
  9. Offline

    mehboss

    You said you wanted to get a string from config, not set.. this code was close:
    It should be this:
    Code:
    for (String items : getConfig.getString("Items")) {
         for (String amount : getConfig.getString(Items + ".Amount")) {
              player.getInventory().addItem(Material.matchMaterial(items), amount);
        }
    }
    
    Code:
    Items:
      Diamond:
        Amount: 1
      Iron_Ingot:
        Amount: 1
    
     
  10. Offline

    plisov

    @raunak114
    I'm not trying to save any item meta of the items. I tried making a material array list however it didn't want to work
     
  11. Offline

    mehboss

    What would you need this for?
     
  12. Offline

    plisov

    Sorry, replied to the wrong person
     
  13. Offline

    mehboss

    @plisov
    Were you directing that message towards me? Here's a post big post to hopefully get things cleared up. It has been way too long and this post should already be solved. :)

    You want to specify items in the config... As asked in this thread. Here is an example config below to have better understanding.

    Example Config:
    Code:
    Items: //this is a normal configuration section
      IRON_INGOT: //this is an item material
        Amount: 1 //string says it all
      REDSTONE:
        Amount: 1
      DIAMOND:
        Amount: 1
    • So basically to go about getting all the materials in this configuration section. First, you want to make a for-loop to loop through all the keys, in this case, there are 3 keys: IRON_INGOT, REDSTONE, & DIAMOND.
    • Once we make the for-loop, we want to get the amount. To do this, we make another for-loop and get the string "Amount" for every key in the configuration section.
    • After that, we make each one of those an itemstack.
    • Next, we can do whatever we want with those, add it to an array, re-save it, add the itemstack to the player's item, etc..
    • In this case, we will add it to the player's inventory, just because this is an example.

    Code:
    1.
            for (String item : getConfig().getConfigurationSection("Items").getKeys(false)) {
    
        * ^ ^ So basically, what this line does, it it returns all the keys in that section, in this case: (IRON_INGOT, REDSTONE, & DIAMOND).
    
    2.
        if (getConfig.getInt(item + ".Amount")) {
    
       * ^ ^ So basically, this gets all the "Amount"'s in each key of that configuration section.
    
    3.
                ItemStack item = new ItemStack(<MATERIAL>, <AMOUNT> //showing the parameters
    
           DO:
               ItemStack item = new ItemStack(Material.matchMaterial(items), amount);
    
         * ^ ^ What this does is sets the itemstack variable to the material + the amount of the stack. [NOTE: Materials should be fully UPPER-CASED
    
    4.
        p.getInventory().addItem(item);
    
         * ^ ^ Here, since this is just a tutorial, I add each item to a player's inventory to finish this tutorial off.
    
    I suggest learning about configuration sections if you STILL do not know how to do this. Personally, I believe I have explained this to you good enough & you should be able to get an idea of how to deal with your question/problem.

    SAVING/GETTING ITEMSTACK TO/FROM CONFIG:
    You can save the itemstack separately (ex. displayname, material, amount) or you can save it all together & make config ugly. Hence this is a really easy part of java, I'm not going to explain it to you, but you can find about it here:

    https://bukkit.org/threads/tut-bukkits-new-fileconfiguration-api-create-a-yaml-configuration.42775/
    https://bukkit.org/threads/tut-bukkits-new-fileconfiguration-api-create-a-yaml-configuration.42775/
    http://bukkit.gamepedia.com/Configuration_API_Reference

    To store a material from an itemstack to config you would do:
    Code:
    
                getConfig.set(STRING, item.getType());
    
        ^^ Item would be the itemstack variable from above when you got the itemstack data from the config.
    NOTICE: getType() > returns the material type of something, without this it will return the whole item stack.
    
    
    Hope I helped! If your problem has been solved please change the thread prefix to solved. This saves us developers time when scanning the plugin development section.
     
    Last edited: Mar 25, 2017
Thread Status:
Not open for further replies.

Share This Page