ItemStack[] to Inventory

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

  1. How do I add an item to a player inventory which is in an ItemStack[] 1 by 1. Instead of placing all and checking if player inventory is full. So basically what I have is when a player does /items claim. It claims the items from the ItemStack[] if the player inventory is full it tells the player their inventory is full and stop giving the player the items. Can someone assist me in doing this. My ItemStack[] is in a HashMap and the key is the player name.
     
  2. You can check the size of player.getInventory() to see if it's full to start with.

    Other than that you would need to iterate over each item in the inventory, see if it is of the same type as you're trying to give, if it is then see if there's room for the amount, like if is.getAmount < is.getType().getMaxStackSize()?
    If so add the ones there's room for and update the current item in your map. Continue on until there are no more items.


    Then run a second loop to try and place the remaining items in empty slots remembering to remove the items if they were able to be placed.

    After the loop has finished if there are still items left over then notify the player more items remain


    Sent from my iPhone using Tapatalk
     
  3. Alright thanks but I don't know how to remove an item from a ItemStack[] I only know how to remove the item from an ItemStack. Can you tell me or show me?
     
  4. Set it to null or change its type to air then ignore air types or null types


    Sent from my iPhone using Tapatalk
     
  5. yes I understand setting it to null but how do I actually get the item from the ItemStack[] array
     
  6. Well an array has a finite size.
    Code (Text):

    for(i = 0;i<array.length;i++)
        ItemStack is = array[i];
     
    Would get each item from the array in order.


    Sent from my iPhone using Tapatalk[/i]
     
  7. ok thank you so much
     
  8. HashMap<UUID, ItemStack[]> = WinnerStorage;

    Items are store in this HashMap. After they are store I want to retrieve them

    ItemStack[] items = WinnerStorage.get(p.getUniqueId());

    If(!p.getInventory.firstEmpty() == -1){
    for(int x = 0; x< items.length; x++){
    ItemStack is = items[x]
    p.getInventory().addItem(is);
    }
    }else{
    p.sendMessage("Please clear your inventory");
    }

    I typed up this code on my phone so there might be some errors
     
  9. FrostedSnowman

    Resource Staff



    you would need to check if all slots are empty, otherwise, if they have multiple items that u store, and they only clear out one slot in their inventory, items will be lost.
     
  10. If(!p.getInventory.firstEmpty() == -1){

    This checks if player inventory is not empty
     
  11. Do you want to keep the remainder in the Map? Or just check if it even fits, otherwise just not give them a thing and wait for them to create enough space?
     
  12. I want to check if it fits if it can fit it gives the item, if it cannot fit it sends them a message saying that you need to clear your inventory. So they actually need to create space. When do, they we will have to run the /items claim command again to get their stuff from the map. Every item that is set into the player inventory it gets removed from the HashMap

    Edit: Alright guys everything works fine the only thing am missing to do is remove the item in the hashmap when it is given to the player

    Code (Text):
        public static void Claim(Player p) {
            ItemStack[] items = WinnerStorage.get(p.getUniqueId());

            if (!(p.getInventory().firstEmpty() == -1)) {
                for (int x = 0; x < items.length; x++) {
                    ItemStack is = items[x];
                    p.getInventory().addItem(is);
                 
                    Winner.remove(p.getUniqueId(), is);
                 
                }
            } else {
                p.sendMessage("Please clear your inventory");
            }
         

        }
    }
    my only problem is that it doesn't remove the item from the hashmap and it keeps repeating itself I used Winner.remove(p.getUniqueId(), is); to remove the item but it still does not work. Can someone tell me when it doesn't work? thanks

    Edit: Another problem that I am having is putting two Different ItemStacks[] into one for example

    Code (Text):
        public static void Combine(Player p1, Player p2) {

            UUID uuidp = p.getUniqueId();
            ItemStack[] items = .PlayerStorage.get(p1);
            ItemStack[] items1 = PlayerStorage.get(p2);
            if (!(p.getInventory().firstEmpty() == -1)) {
                for (int x = 0; x < items.length; x++) {
                    WinnerStorage.put(uuidp, items);
                }
                for (int x = 0; x < items1.length; x++) {
                    WinnerStorage.put(uuidp, items1);
                }
            }
            return;
        }
       
    I am trying to put the two player ItemStacks[] which is in a HashMap into the WinnerStorage HashMap, But It just wont work. Can I get a little help please.. thanks
     
    #12 Stevie212, Apr 20, 2017
    Last edited: Apr 20, 2017
  13. @Stevie212 You know, addItem(ItemStack...) returns a Map containinig items it couldn't fit. You could simply store items that couldn't fit in a big list.
     

  14. Oh wow. So ur saying that if the items don't fit is it is stored in a Map containing the items? So how do I return those items from the map?
     
  15. Map<ItemStack> didntfit = p.getInventory().addAll(someItemArray);

    The leftovers should be in the didntfit map afterwards.

    Also you can't do anArray.remove(ItemStack)

    Remove works on maps, sets and lists.

    Winner.remove(uuid,is) would work if your map had a single item not an array of items.
    You'll have to do. ItemArray[index] = null to clear the item out of the array.

    If you used a list you could add/remove items using .add or .remove


    Sent from my iPhone using Tapatalk
     
  16. Thank you so very much I will test it out right now

    Edit: Map<ItemStack> didntfit = p.getInventory().addAll(someItemArray); gives me an error on Map


    Edit: How do I check to see if someones inventory is full. I have tried firstEmpty and it only returns true even if inventory is full or not
     
    #16 Stevie212, Apr 20, 2017
    Last edited: Apr 20, 2017
  17. If inventory.size() == 0 would mean completely empty

    And what error does it give you when trying to add all?

    Sent from my iPhone using Tapatalk