Move items from one inventory to another

Discussion in 'Spigot Plugin Development' started by Jaxophon, Jan 8, 2020.

  1. Since a week now I am working on finding a way to move items from one inventory to another one. I definitely want to avoid item duplication. Actually I just add the items from the first inventory to the second one. After that i remove them. But again and again it comes to item duplication. Now i am writing this thread to ask you how you would do it. I have two inventories, inventory one and inventory two. I want to move all items from inventory one to inventory two. Its also necessary to check if one of the inventory is full. In that case it just should add as much items as possible.

    This is definitely no spoonfeeding!

    I tried it many times, but now I deleted everything and want to start from scratch
    I also google much about this but I just found this thread. It wasnt that useful.
    https://www.spigotmc.org/threads/moving-items-to-another-inventory.90504/

    I realy hope someone can help me :)
     
  2. Something like this?

    Code (Java):
    final ItemStack stack = inventory1.getItem(5);
    assert stack != null;
    inventory2.addItem(stack.clone());
    stack.setAmount(0);
    PS: did not test, tell me if I overlooked something

    Edit: Oh you want to do this for every item. Then I'd simply loop over the contents of inventory1
     
  3. Its actually pretty simple. You can get an array representation of one inv and add it to another.
    That returns a Map<Integer, ItemStack> for all items that don't fit.
    Like this:
    Code (Java):
      public Map<Integer, ItemStack> transfer(Inventory receiver, Inventory provider) {
        Map<Integer, ItemStack> overflow = receiver.addItem(provider.getContents());
        provider.clear();
        return overflow;
      }

    So the Map<Integer, ItemStack> is a map that contains the slot and the item in that slot that did not fit in the new inventory.
    Screenshot_4.png


    So you can then either put the overflown items back in the provider inventory like this:
    Code (Java):
      public void transfer(Inventory receiver, Inventory provider) {
        Map<Integer, ItemStack> overflow = receiver.addItem(provider.getContents());
        provider.clear();
        for (Entry<Integer, ItemStack> entry : overflow.entrySet()) {
          provider.setItem(entry.getKey(), entry.getValue());
        }
      }

    Or let the method caller decide what should happen with the left over items like this:
    Code (Java):
      public void transfer(Inventory receiver, Inventory provider, Consumer<ItemStack> forEachLeft) {
        Map<Integer, ItemStack> overflow = receiver.addItem(provider.getContents());
        provider.clear();
        overflow.values().forEach(forEachLeft);
      }
    With this you can later drop the left over items on the ground or do whatever with them.
    Example:
    Code (Java):

        transfer(chestInv, playerInv, (left) ->{
          playerLoc.getWorld().dropItemNaturally(playerLoc, left);
        });
     
    • Like Like x 1
  4. Code (Java):
    Inventory i = (GET YOUR INVENTORY HERE);
    Inventory i2 = (GET YOUR SECOND INVENTORY HERE);
    ItemStack[] contents = i.getContents();
    int diff = i2.getSize() - i2.firstEmpty();
    for (int k = 0; k<diff;k++){
        i2.addItem(contents[k]);
    }
    for (int k = 0; k<diff;k++){
        i.setItem(k, new ItemStack(Material.AIR));
    }
    This should work
    Edit: I don't what you mean exactly by no duplicates please elaborate
     
  5. Many useful things. Thank you guys! :)