Solved ItemStack from config problems :'(

Discussion in 'Spigot Plugin Development' started by FalconSeeker, Aug 16, 2018.

  1. I did it in like all my other plugins but im havng trouble in this one.

    Code (Text):
              if (main.vaults.get(p.getUniqueId() + ".Contents") == null) {
                  p.openInventory(i);
                   ArrayList<ItemStack> items = new ArrayList<ItemStack>();
                  items.add(new ItemStack(Material.AIR, 1));
                     main.vaults.set(p.getUniqueId() + ".Contents", items);
                     main.vaults.save();

                     return;
                     }
               ArrayList<ItemStack> content = (ArrayList<ItemStack>) main.vaults.get(p.getUniqueId() + ".Contents");
               for (ItemStack item : content) { //where the console says error is
                   i.addItem(item);
               }
                      p.openInventory(i);
                               
                }
    the error says string cannot be cast to itemstack.
    Code (Text):
                ArrayList<ItemStack> content = (ArrayList<ItemStack>) main.vaults.get(p.getUniqueId() + ".Contents");
     
  2. Use Arrays.asList() to convert multiple strings to a List. If you're getting a StringList from a config, it's as simple as FileConfiguration#getStringList

    Also, I'd recommend using a List instead of ArrayList to follow LSP - abstraction over implementation.
     
  3. its not working though. iM trying to get itemstack list from config, i did it before but i cant find what im doing wrong
    Code (Text):
    c3db0682-c504-3818-83b5-d1f4579e6130:
      Contents:
      - ==: org.bukkit.inventory.ItemStack
        type: AIR
    Code (Text):

            at falconseeker.extraevents.PlayerVault.restoreInventory(PlayerVault.java:69) ~[?:?]
            at falconseeker.extraevents.PlayerVault.onClick(PlayerVault.java:43) ~[?:?]
    Code (Text):
              List<ItemStack> l = (List<ItemStack>) main.vaults.getList(p.getUniqueId() + ".Contents");
                  for (ItemStack s : l) {
                       i.addItem(s);
                  }
     
  4. lol, i was silly and didn't even notice it was itemstacks, rip
    What was the solution?