Solved ItemStack is times 0

Discussion in 'Spigot Plugin Development' started by Kompye, Apr 4, 2020.

  1. Hi, I know the thread's title is pretty vague but I don't know how to describe this better.
    Code (Java):
        private void exportItemStack(ItemStack itemStack, Player p){

            System.out.println(itemStack);

            List<ItemStack> currentItems = (List<ItemStack>) backpackItemsFile.getBackpackConfig().getList(p.getUniqueId().toString());

            if(currentItems == null) currentItems = new ArrayList<ItemStack>();

            currentItems.add(itemStack);

            System.out.println(currentItems);

            backpackItemsFile.getBackpackConfig().set(p.getUniqueId().toString(), currentItems);

            backpackItemsFile.saveConfig();

        }
    What this method is supposed to do is take an ItemStack and a Player as parameters and add the itemstack to the current itemstack list from the file, and replace the old one in the file. As you can see I have two debugs. The problem is that in the second debug I saw that the first ItemStack's amount is times zero and I don't understand why. Debug when adding a stick:
    Code (Text):
    ItemStack{STICK x 1, UNSPECIFIC_META:{meta-type=UNSPECIFIC, lore=[º7Monu I], enchants={Monu=1}}}
    [10:11:34 INFO]: [ItemStack{SAND x 0}, ItemStack{STICK x 1, UNSPECIFIC_META:{meta-type=UNSPECIFIC, lore=[º7Monu I], enchants={Monu=1}}}]
     
  2. Can you post code where you call this function?
     
  3. sure
    Code (Java):
        @EventHandler
        public void onInventoryClick(InventoryClickEvent e) {

            if (e.getClickedInventory() == null) return;

            if (e.getClickedInventory().getHolder() == this) {

                //CANCEL LOWBAR
                for (int i = 45; i <= 53; i++) {

                    if (e.getSlot() == i) {

                        e.setCancelled(true);
                        break;

                    }

                }

                //EXPORT ITEM
                if(e.getAction() == InventoryAction.PLACE_ALL || e.getAction().equals(InventoryAction.PLACE_ONE)){

                    exportItemStack(e.getCursor(), (Player) e.getWhoClicked());

                }

            }
     
  4. Don't you mean
    Code (Text):
    e.getCurrentItem();
    instead of
    Code (Text):
    e.getCursor();
    ?
     
  5. Do a debug message of e.getCursor(); before you call exportitemstack. Is it still sandx0?

    Maybe could
    e.getClickedInventory().getItem(e.getSlot);
    Work?

    Btw, please don't use a for loop to check if a number is in range. Use
    if(maxValue>e.getSlot()&&minValue<e.getSlot) e.setCancelled(true);

    A lot less code and 10 times more efficient.
     
  6. I doubt that method already existed in the 1.8 API. Does it? Otherwise that's the way to go.
     
  7. No. I'm working on a backpack plugin so I want items to be added to a file for each player when placed in the inventory. As the event is called before the actual action, the item I would add would be air (the slot in which the player places the item), so I'm using getCursor() which returns the item in the cursor which I want to add to the file.
    It's not 0 if I debug getCursor()
    It does, they do different things
     
  8. Was the sand previously added ? Delete the config file and try again.
     
  9. It was, and I tried deleting the file multiple times :/
     
  10. Maybe try
    currentItemstack.add(itemstack.clone());
    ? Somehow the stack gets 'wiped' when you add it to the arraylist.
     
  11. why would it get wiped? also only the amount
     
  12. Have you fixed it yet ? Print the list before you add the item, if the config is empty it should show and empty message.
     
  13. Yeah I fixed it. I just rewrote the code so the list gets replaced by the inventory every time it gets closed and it's working now. Thanks though