Plugin Help Help with getting Item's in config (and addingItems)

Discussion in 'Plugin Help/Development/Requests' started by Ricecutter0, Feb 28, 2015.

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

    Ricecutter0

    So I'm making a plugin, and I'm having some trouble adding the items that are said in the config to the inventory. If I did anything wrong, could someone let me know? Thank you, and have a great day.


    Inventory Class:

    Code:java
    1. package me.Ricecutter0.RandomRewards;
    2.  
    3. import java.util.ArrayList;
    4. import java.util.List;
    5.  
    6. import org.bukkit.Bukkit;
    7. import org.bukkit.configuration.file.FileConfiguration;
    8. import org.bukkit.entity.Player;
    9. import org.bukkit.event.Listener;
    10. import org.bukkit.inventory.Inventory;
    11. import org.bukkit.inventory.ItemStack;
    12.  
    13. public class CommandManager implements Listener {
    14.  
    15.  
    16. Main plugin;
    17.  
    18. private CommandManager() {}
    19.  
    20. static CommandManager instance = new CommandManager();
    21.  
    22. public static CommandManager getInstance(){
    23. return instance;
    24. }
    25.  
    26. ConfigManager cm = ConfigManager.getInstance();
    27.  
    28. FileConfiguration cmg = cm.getConfig();
    29.  
    30. ArrayList<ItemStack> items = new ArrayList<ItemStack>();
    31.  
    32. @SuppressWarnings({ "deprecation" })
    33. public void openInv(Player p){
    34.  
    35. List<String> item = cm.getConfig().getStringList("items");
    36.  
    37. for(String s : item){
    38.  
    39. String[] blah = s.split(", ");
    40.  
    41. int itemId = Integer.valueOf(blah[0]);
    42.  
    43. int itemAmount = Integer.valueOf(blah[1]);
    44.  
    45. ItemStack itemtoadd = new ItemStack(itemId, itemAmount);
    46.  
    47. items.add(itemtoadd);
    48. }
    49.  
    50. for(ItemStack i : items){
    51. Inventory inv = Bukkit.createInventory(null, 9, Main.color("&4You're Welcome"));
    52. inv.addItem(i); //<--- How do I add ALL the items in the ArrayList? All it does is add the first one.
    53. p.openInventory(inv);
    54. }
    55.  
    56. }
    57. }
     
  2. Offline

    pie_flavor

    @Ricecutter0 First of all, check if i is null before adding it. General rule of thumb.
    Second of all, why do you store the data? ItemStack is serialized for a reason, you can literally just save and load the ItemStacks directly from the config. Just use getItemStack().
    Third, you have made a slightly obvious error. Create the inventory outside of the for block, then add the items inside it. You are creating a new inventory for each item.
     
  3. Offline

    Ricecutter0

  4. Offline

    pie_flavor

    @Ricecutter0 I don't understand. I literally just said it.
    If you need an actual code example, here.
    You said:
    Code:
    for(ItemStack i : items){
        Inventory inv = Bukkit.createInventory(null, 9, "§4You're Welcome");
        inv.addItem(i);
        p.openInventory(inv);
    }
    Change to:
    Code:
    Inventory inv = Bukkit.createInventory(null, 9, "§4You're Welcome");
    for (ItemStack i : items) {
        inv.addItem(i);
    }
    p.openInventory(inv);
     
  5. Offline

    Ricecutter0

    @pie_flavor

    Thank You!

    Another thing, I don't really know how serialize the items.. or how to get save and load itemstacks from the config, like you said in your post above. Do you think you could give an example please?
     
  6. Offline

    pie_flavor

    @Ricecutter0 Serializing isn't something you do, it's something they do. Just set a path in the config to an ItemStack like you would a String or an int. And to get the item stack, just use getItemStack(path) on the config.
     
  7. Offline

    Ricecutter0

    @pie_flavor

    Oh. I see.

    Do you know how to test is an itemmeta field is empty and allow it to continue the code?


    Code:java
    1. public class CommandManager{
    2.  
    3.  
    4. Main plugin;
    5.  
    6. private CommandManager() {}
    7.  
    8. static CommandManager instance = new CommandManager();
    9.  
    10. public static CommandManager getInstance(){
    11. return instance;
    12. }
    13.  
    14. ConfigManager cm = ConfigManager.getInstance();
    15.  
    16. FileConfiguration cmg = cm.getConfig();
    17.  
    18. ArrayList<ItemStack> items = new ArrayList<ItemStack>();
    19.  
    20. @SuppressWarnings({ "deprecation" })
    21. public void openInv(Player p){
    22.  
    23. List<String> item = cm.getConfig().getStringList("items");
    24.  
    25. for(String s : item){
    26.  
    27. String[] blah = s.split(", ");
    28.  
    29. int itemId = Integer.valueOf(blah[0]);
    30.  
    31. int itemAmount = Integer.valueOf(blah[1]);
    32.  
    33. String itemName = String.valueOf(blah[2]);
    34.  
    35. ItemStack itemtoadd = new ItemStack(itemId, itemAmount);
    36.  
    37. ItemMeta itemmeta = itemtoadd.getItemMeta();
    38.  
    39. itemmeta.setDisplayName(Main.color(itemName));
    40.  
    41. itemtoadd.setItemMeta((itemmeta));
    42.  
    43. items.add(itemtoadd);
    44. }
    45.  
    46. Inventory inv = Bukkit.createInventory(null, cmg.getInt("inventorySize"), Main.color("&4You're Welcome"));
    47.  
    48. for(ItemStack i : items){
    49. inv.addItem(i);
    50. p.openInventory(inv);
    51. }
    52.  
    53. }
    54. }
     
  8. Offline

    pie_flavor

    @Ricecutter0 You just answered your own question.
    Code:
    continue;
    Also, you are still opening the inventory inside the for loop. Just add the item in it! Everything you put inside that loop, will execute for every single item in there. So the player will have 9 inventories open at once.
     
    Last edited: Feb 28, 2015
  9. Offline

    Ricecutter0

    When I open the inventory another time, the items are added again. Is there a way to make a new inventory every time when executing the action?

    [Edit] And why would it open 9 inventories? I don't think it would because in the code it doesn't say to add 9 inventories.
     
    Last edited: Feb 28, 2015
  10. Offline

    pie_flavor

    @Ricecutter0 I assumed your inventory was 9 spaces. But you have said
    Code:
    for(ItemStack i : items){
        inv.addItem(i);
        p.openInventory(inv);
    }
    This means that for each item, it will open the inventory AGAIN. If you want the code to happen for every item, put it inside the for loop. If you only want it to happen once, put it outside.
    That's what you are already doing. If you wish to have only the one inventory and keep it, just take the line of code where you create the inventory and put it at the top, before the constructor (so as to be a member variable instead of a temporary one).
     
Thread Status:
Not open for further replies.

Share This Page