Inventory Full

Discussion in 'Spigot Plugin Development' started by Stevie212, Apr 20, 2017.

  1. How do I check to see if a player inventory is full. firstEmpty() returns true weather or not the inventory is full. Can anyone tell me any other way I can fix this. I have tired some searched all over google didn't find any and some plugins don't support it so can I get some help please. Thanks...
     
  2. Do you need this by adding items to your inventory?
     
  3. yes I do need it by adding items to my inventory
     
  4. Then just use this:

    Code (Text):

    HashMap<Integer,ItemStack> leftItems = player.getInventory().addItem(yourItem);

    if(!(leftItems.isEmpty())){
    //Inventory is full ( leftItems has all items that weren't added to the inventory )
    }
     
     
  5. Wow thanks so much. So when the items are placed into leftitems how do I get them? p.getInventory().addItem(leftitems) ?

    Also I am adding the items from a HashMap so ItemStack[] youritems = Items.get(p.getUniqueId());
    and it gives me an error on addItem
     
  6. would be good to see that error and your code to that.
    So the hashmap contains the items that couldn't be added to the player inventory, so that means the inventory is full which also means you can't add the items anymore to the inventory until you do something like drop the items on the player location and he just grab them up by dropping items from their inventories out
     
  7. Code (Text):
        public static void Claim(Player p) {
            ItemStack[] items = PlayerStorage.get(p.getUniqueId());
            HashMap<Integer, ItemStack> leftItems = p.getInventory().addItem(items);
            PlayerStorage.remove(p.getUniqueId());
            p.sendMessage("You have recieved ur bets");
            if (!(leftItems.isEmpty())) {
                //drop items
                return;
            }
        }
    This is my code.

    Also one more question

    How do I add 2 ItemStacks with an array [] to 1 ItemStack
    So example
    HashMap<UUID, ItemStack[]> PlayerStorage = new HashMap<UUID, ItemStack[]>();
    HashMap<UUID, ItemStack[]> WinnerStorage = new HashMap<UUID, ItemStack[]>();

    I want to add these Two PlayerStorage into the WinnerStorage how do I do that?. When I try putting both, only the first player items goes through
     
  8. You mean this?

    Code (Text):

    WinnerStorage.putAll(PlayerStorage);
     
     
  9. Let me test it out and ill let you know
     
  10. Works fine for me. When the player inventory is full it the remaining items are stored in the leftItems HashMap now I want to drop the items that were remain but the thing is don't know which key is the leftItems of that player. So what I would like to know is how do i get the key of the left over items in the HashMap <Integer, ItemStack> thanks
     
  11. Code (Text):

    for(Map.Entry<Integer,ItemStack> items : yourHashMap.entrySet()){
    ItemStack item = items.getValue();
    }
     
     
  12. to check if an inventory is full,

    Code (Java):

    if (inventory.firstEmpty() == -1) {
         //inventory is full
    }
     
  13. Alright thanks so much really appreciated it
     
  14. This returns true even if the inventory is full or not
     
  15. Actually, that won't return if the inventory is full, even if a stack is not filled up it would return true
     
  16. PutAll does not work. Cause its just puts everything but I don't want to place everything. What am I trying to trying to accomplish is adding 2 array of ItemStack in 1
     
  17. ohh so you mean like you have 1 map with an itemstack of amount = 2 and another map with an itemstack of amount = 1 and you wanna put them together so there's only 1 map with itemstack amount = 3?

    By that, you actually have to compare those two maps and put check the item type / display equals the item from the other map, then just add the amount of items that is available from the other map ( note the max. stack size of items so you may have add an amount and also add a new item to the map because some items left there )
     
  18. Let me show u how I did it
    HashMap<UUID, ItemStack[]> PlayerStorage = new HashMap<UUID, ItemStack[]>();
    HashMap<UUID, ItemStack[]> WinnerStorage = new HashMap<UUID, ItemStack[]>();
    Code (Text):
        public static ItemStack[] Place(Player p, Player p2) {

            ItemStack[] items = PlayerStorage.get(p.getUniqueId());
            ItemStack[] items1 = PlayerStorage.get(p2.getUniqueId());
            ArrayList<ItemStack> items2 = new ArrayList<ItemStack>();
            for (int i = 0; i < items.length; i++) {
                ItemStack is = items[i];
                items2.add(is);
            }
            for (int i = 0; i < items1.length; i++) {
                ItemStack is = items1[i];
                items2.add(is);
            }
            return items2.toArray(new ItemStack[items2.size()]);
        }
    When this is done, ill add them to the WinnerStorage by doing
    WinnerStorage.put(p.getUniqueId(), Place(p1 , p2);