Solved Config list not saving ItemStacks. Saving them as nulls. Why?

Discussion in 'Plugin Development' started by MayoDwarf, Jan 11, 2014.

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

    MayoDwarf

    Code:java
    1. public class Main extends JavaPlugin implements Listener {
    2. public FileConfiguration config;
    3. public void onEnable() {
    4. config = getConfig();
    5. this.saveDefaultConfig();
    6. this.getConfig().options().copyDefaults(true);
    7. this.saveConfig();
    8. this.getServer().getPluginManager().registerEvents(this, this);
    9. }
    10. public void onDisable() {
    11. }
    12. public void openBackpack(Player p) {
    13. if(p.hasPermission("Backpack.use.54")) {
    14. Inventory inv = Bukkit.createInventory(p, 54, ChatColor.RED + "Your Backpack:");
    15. List<ItemStack> blah = (List<ItemStack>) config.getList("Players."+p.getName()+".Backpack-Items");
    16. if(config.contains("Players."+p.getName()+".Backpack-Items")) {
    17. for(ItemStack item : blah) {
    18. inv.addItem(item);
    19. }
    20. for(int i=0;i<blah.size();i++) {
    21. blah.remove(blah.get(i));
    22. }
    23. }
    24. this.saveConfig();
    25. p.openInventory(inv);
    26. } else
    27. if(p.hasPermission("Backpack.use.45")) {
    28. Inventory inv = Bukkit.createInventory(p, 45, ChatColor.RED+"Your Backpack:");
    29. List<ItemStack> blah = (List<ItemStack>) config.getList("Players."+p.getName()+".Backpack-Items");
    30. if(config.contains("Players."+p.getName()+".Backpack-Items")) {
    31. for(ItemStack item : blah) {
    32. inv.addItem(item);
    33. }
    34. for(int i=0;i<blah.size();i++) {
    35. blah.remove(blah.get(i));
    36. }
    37. }
    38. this.saveConfig();
    39. p.openInventory(inv);
    40. } else
    41. if(p.hasPermission("Backpack.use.36")) {
    42. Inventory inv = Bukkit.createInventory(p, 36, ChatColor.RED+"Your Backpack:");
    43. List<ItemStack> blah = (List<ItemStack>) config.getList("Players."+p.getName()+".Backpack-Items");
    44. if(config.contains("Players."+p.getName()+".Backpack-Items")) {
    45. for(ItemStack item : blah) {
    46. inv.addItem(item);
    47. }
    48. for(int i=0;i<blah.size();i++) {
    49. blah.remove(blah.get(i));
    50. }
    51. }
    52. this.saveConfig();
    53. p.openInventory(inv);
    54. } else
    55. if(p.hasPermission("Backpack.use.27")) {
    56. Inventory inv = Bukkit.createInventory(p, 27, ChatColor.RED+"Your Backpack:");
    57. List<ItemStack> blah = (List<ItemStack>) config.getList("Players."+p.getName()+".Backpack-Items");
    58. if(config.contains("Players."+p.getName()+".Backpack-Items")) {
    59. for(ItemStack item : blah) {
    60. inv.addItem(item);
    61. }
    62. for(int i=0;i<blah.size();i++) {
    63. blah.remove(blah.get(i));
    64. }
    65. }
    66. this.saveConfig();
    67. p.openInventory(inv);
    68. } else
    69. if(p.hasPermission("Backpack.use.18")) {
    70. Inventory inv = Bukkit.createInventory(p, 18, ChatColor.RED+"Your Backpack:");
    71. List<ItemStack> blah = (List<ItemStack>) config.getList("Players."+p.getName()+".Backpack-Items");
    72. if(config.contains("Players."+p.getName()+".Backpack-Items")) {
    73. for(ItemStack item : blah) {
    74. inv.addItem(item);
    75. }
    76. for(int i=0;i<blah.size();i++) {
    77. blah.remove(blah.get(i));
    78. }
    79. }
    80. this.saveConfig();
    81. p.openInventory(inv);
    82. } else
    83. if(p.hasPermission("Backpack.use.9")) {
    84. Inventory inv = Bukkit.createInventory(p, 9, ChatColor.RED+"Your Backpack:");
    85. List<ItemStack> blah = (List<ItemStack>) config.getList("Players."+p.getName()+".Backpack-Items");
    86. if(config.contains("Players."+p.getName()+".Backpack-Items")) {
    87. for(ItemStack item : blah) {
    88. inv.addItem(item);
    89. }
    90. for(int i=0;i<blah.size();i++) {
    91. blah.remove(blah.get(i));
    92. }
    93. }
    94. this.saveConfig();
    95. p.openInventory(inv);
    96. } else {
    97. p.sendMessage(ChatColor.RED+"You do not have any permissions for Backpacks!");
    98. }
    99. }
    100. @EventHandler
    101. public void onClose(InventoryCloseEvent evt) {
    102. if(evt.getInventory().getName().equalsIgnoreCase(ChatColor.RED+"Your Backpack:")) {
    103. if(evt.getInventory().getContents().length >= 1) {
    104. List<ItemStack> blah = new ArrayList<ItemStack>();
    105. for(int i=0;i<evt.getInventory().getContents().length;i++) {
    106. blah.add(evt.getInventory().getItem(i));
    107. }
    108. getConfig().set("Players."+evt.getPlayer().getName()+".Backpack-Items", blah);
    109. this.saveConfig();
    110. blah.clear();
    111. }
    112. }
    113. }
    114. public boolean onCommand(CommandSender sender, Command cmd, String commandLabel, String[] args) {
    115. if(cmd.getName().equalsIgnoreCase("Backpack")) {
    116. if(sender instanceof Player) {
    117. openBackpack((Player) sender);
    118. }
    119. }
    120. return true;
    121. }
    122. }

    No errors in console ever. I open my backpack and put in 1 glass. When I close it, it is supposed to save it to the list. In the list it shows up as "- null" though. When I open up the backpack again, there is no items in it still that I put in... :/ Please help me with this problem. Thank you very much. - Jared
     
  2. Online

    timtower Administrator Administrator Moderator

    MayoDwarf ItemStacks aren't designed to be stored into a list in the config
     
  3. Offline

    Sagacious_Zed Bukkit Docs

    MayoDwarf likes this.
  4. Offline

    Compressions

    MayoDwarf When you add or remove from a List, you need to push that list back to the config.
    Code:
    config.set(path, list);
     
  5. Online

    timtower Administrator Administrator Moderator

    Not in that way, you can serialize itemstacks to maps, I know that ( using it myself ) but I don't see maps here
     
  6. Offline

    MayoDwarf

    So I have to do it every blah.add(ItemStack)?
     
  7. Offline

    Compressions

    timtower getList is used to get a list flexibly and deserialize them to different types of custom objects not in native Java.
     
  8. Offline

    metalhedd


    Code:java
    1.  
    2. List<ItemStack> stacks = new ArrayList<ItemStack>();
    3. stacks.add(new ItemStack(Material.ITEM_FRAME, 1));
    4. stacks.add(new ItemStack(Material.GLASS, 3));
    5. getConfig().set("stacks", stacks);
    6. saveConfig();
    7.  

    Code:text
    1.  
    2. stacks:
    3. - ==: org.bukkit.inventory.ItemStack
    4. type: ITEM_FRAME
    5. - ==: org.bukkit.inventory.ItemStack
    6. type: GLASS
    7. amount: 3
    8.  
     
  9. Offline

    Compressions

    MayoDwarf What you said didn't make sense, but I assume you meant after? Yes.
     
  10. Online

    timtower Administrator Administrator Moderator

    Wasn't talking about the getList
    How do you load and set this then?
     
  11. Offline

    Compressions

    It's a list... The same way you would normally get a list of objects.
     
  12. Offline

    metalhedd


    Code:java
    1.  
    2. List<ItemStack> stacks = (List<ItemStack>) getConfig().getList("stacks");
    3. for (ItemStack s: stacks) {
    4. getLogger().info(s.toString());
    5. }
    6.  


    Code:text
    1.  
    2. [14:57:29 INFO]: [MetalTest] Enabling MetalTest v1.0-SNAPSHOT
    3. [14:57:29 INFO]: [MetalTest] ItemStack{ITEM_FRAME x 1}
    4. [14:57:29 INFO]: [MetalTest] ItemStack{GLASS x 3}
    5.  
     
  13. Offline

    MayoDwarf

    Okay so now I get the error in System saying null and it also can't read some things in the config. metalhedd Compressions
    The config comes up as this when I added 64 Glass:
    Code:
    Players:
      MayoDwarf:
        Backpack-Items:
        - ==: org.bukkit.inventory.ItemStack
          type: GLASS
          amount: 64
        - null
        - null
        - null
        - null
        - null
        - null
        - null
        - null
        - null
        - null
        - null
        - null
        - null
        - null
        - null
        - null
        - null
        - null
        - null
        - null
        - null
        - null
        - null
        - null
        - null
        - null
        - null
        - null
        - null
        - null
        - null
        - null
        - null
        - null
        - null
        - null
        - null
        - null
        - null
        - null
        - null
        - null
        - null
        - null
        - null
        - null
        - null
        - null
        - null
        - null
        - null
        - null
        - null
     
  14. Offline

    metalhedd

    becuse there are a ton of empty slots in the inventory, which return null. just DON'T add those to the list in the first place.
     
  15. Online

    timtower Administrator Administrator Moderator

    Wish that I knew before that it was that easy, but does it also support enchantments etc? Doubt it that it won't actually
     
  16. Offline

    MayoDwarf

    Well why is it not adding the items in the list to the inventory (BackPack) when opened tho? metalhedd
    EDIT: timtower It does
     
  17. Offline

    metalhedd

    of course it does.
     
  18. Offline

    Compressions

    MayoDwarf Add to for loop:
    Code:
    for(ItemStack item : blah) {
    if(item == null) continue;
    //do stuff
    }
     
  19. Offline

    Sagacious_Zed Bukkit Docs

    They can be used the major collections, lists, maps, sets.
     
  20. Offline

    metalhedd


    There's far too much code there to wade through to debug your issue. it could be any one of those permission checks failing, or any of a dozen other things.. do some logging to narrow it down.
     
  21. Offline

    MayoDwarf

    metalhedd FIXED! Thank you so much man!

    Well guys, I read through my code and fixed it! Thank you all for such help! Much wow :) Very happy

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

    metalhedd

    MayoDwarf glad to help. don't trust people that tell you something can't be done, unless they're me of course... ;)
     
  23. Offline

    MayoDwarf

    metalhedd No timtower is very smart and good :) We all make mistakes though and it's understandable :p
     
    timtower likes this.
  24. Online

    timtower Administrator Administrator Moderator

    Never said that is was impossible O_O
    Just thought that it wasnt that easy
     
  25. Offline

    Josh014

    MayoDwarf
    Is it maybe possible if you could send me the code what you got to work? :3 Or what I mean the part to save itemStack in a config.
     
  26. Offline

    gogobebe2

    Thanks for this post!
     
Thread Status:
Not open for further replies.

Share This Page