Solved Is there a way to get the amount addItem couldn't add

Discussion in 'Spigot Plugin Development' started by Loikas, Jan 21, 2022.

  1. When using Inventory.addItem(ItemStack[]) spigot will try to add the items to the players inventory. When the inventory and stack is full it will return a hashmap with all items it couldn't add. However when you try to add for example 5 iron and you have 62 iron in your inventory it will add 2 iron to your inventory but still return a hashmap where the ItemStack with iron has an amount of 5 instead of 3. Is there a way to get the 3 it couldn't add?
    This is the code im using to add items:
    Code (Java):
    for(int i = 0; i < drops.size(); i++) {
                        boolean remove = true;
                        for(Entry<Integer, ItemStack> entry : notFit.entrySet()) if(entry.getValue().getType().equals(event.getItems().get(i).getItemStack().getType())) {
                            remove = false;
                            event.getItems().get(i).getItemStack().setAmount(entry.getValue().getAmount());
                        }
                        if(remove) event.getItems().remove(i);
                       
    Where notFit is the HashMap with items it couldnt add and drops is the original list with items.
     
    #1 Loikas, Jan 21, 2022
    Last edited: Jan 21, 2022
  2. for(int i = 0; i < drops.size(); i++) {
    boolean remove = true;
    for(Entry<Integer, ItemStack> entry : notFit.entrySet()) if(entry.getValue().getType().equals(event.getItems().get(i).getItemStack().getType())) {
    You have to set modified ItemStack to Item entity
    ->

    Code (Java):
    for(int i = 0; i < drops.size(); i++) {
        boolean remove = true;
        for(Entry<Integer, ItemStack> entry : notFit.entrySet())
            if(entry.getValue().getType().equals(event.getItems().get(i).getItemStack().getType()) && entry.getValue().getItemMeta().equals(event.getItems().get(i).getItemStack().getItemMeta())) {
            remove = false;
            Item item  = event.getItems().get(i);
            ItemStack edit = item.getItemStack();
            edit.setAmount(entry.getValue().getAmount());
            item.setItemStack(edit);
        }
        if(remove) event.getItems().remove(i);
    }
     
  3. Ah yes but that still sets it to the wrong amount because the hashmap returns an itemstack with the amount of items you tried to add. Not the amount it couldn't add
     
  4. Inventory#addItem already returns what they couldnt add to the player's inventory as a hashmap.

     
  5. Yeah i know. But when you add for example a new ItemStack(Material.IRON_INGOT, 2); and you have space for only one ingot. You would expect the hashmap to return an itemstack with an amount of 1. Instead it returns an iron ingot with an amount of 2 although it added one to the inventory
     
  6. I did a small test plugin

    Code (Java):
    public boolean onCommand(CommandSender sender, Command command, String label, String[] args) {
            if (command.getName().equals("get"))
            {
                HashMap<Integer, ItemStack> test = ((Player) sender).getInventory().addItem(new ItemStack(Material.IRON_INGOT, 2));

                for (Map.Entry<Integer, ItemStack> entry : test.entrySet())
                    sender.sendMessage(entry.getValue().getType() + " " + entry.getValue().getAmount());
            }
    }
    And I filled my inventory
    upload_2022-1-21_9-5-45.png
    I used the command
    upload_2022-1-21_9-6-6.png
    And it returns the right amount, 1 was added cause I was at 63 and 1 is returned because we tried to add 2 (1+1). So it should returns the right amount.
     
  7. oke thats weird

    Edit: it seems like im stupid
     
    #7 Loikas, Jan 21, 2022
    Last edited: Jan 21, 2022
    • Agree Agree x 1