Dropping items on ground if inventory is completely full

Discussion in 'Plugin Development' started by Blackwing_Forged, Sep 10, 2016.

Thread Status:
Not open for further replies.
  1. So my problem is i have it check if the inventory is full and if it is drop the item on the ground, but i filled my whole inventory with 1 cobblestone each and mined a block of stone and the cobble went on the ground instead of a slot it could've gone into, how can i fix this, thanks in advance.
     
  2. Offline

    Zombie_Striker

    @Blackwing_Forged
    1. For loop through all the player's inventory.
    2. If the item's material is equal to the mined item's material and if the amount is less than 64 (or the max the slot can hold)
    3. If the amount the player has + the amount you minded is less than or equal to the max amount, add it to the player's inventory.
    4. If not, add the amount needed to make a stack and drop the rest.
     
  3. What if its with multiple blocks being mined
     
  4. Offline

    Zombie_Striker

  5. Offline

    kameronn

    what are you trying to do because im confussed, are you trying to add a item to the players inventory if it has an available slot of that item? Or drop items on the ground if a players inventory is full because what your saying in your thread and the thread are saying completely different things

    edit: nevermind i understand now.
    In my opinion I would say do what Zombie_Strike said, but add a switch statement to check for multiple blocks (looks cleaner imo)
     
  6. Alright, i got the for loop and checking if its the same and is less then the max size, but how do i add the remainder
     
  7. Offline

    Zombie_Striker

    @Blackwing_Forged
    To get the amount needed for a full stack, use "64 - the amount in the player's slot". After you get this number, set the slot's amount to 64 and subtract that number from the amount you want to give the player. What this means is the you have now added all the items you can to that one slot, and the rest are the items that cannot fit in the player's inventory.
     
  8. @Zombie_Striker
    i guess im still confused, here's what i have so far

    Code:
    Inventory inv = p.getInventory();
                                for(ItemStack itemstack : inv)
                                {
                                    if(itemstack.equals(block.getDrops()) && itemstack.getAmount() > 64)
                                    {
                                      
                                    }
                                    else
                                    {
                                        p.getWorld().dropItemNaturally(event.getBlock().getLocation(), is);
                                        p.updateInventory();  
                                    }
                                  
                                }
     
    Last edited: Sep 11, 2016
  9. Offline

    Zombie_Striker

    @Blackwing_Forged
    1. You can't get an itemstack out of an inventory. You need to get the inventory's contents. Use Inventory#getContents() to get all the items an inventory has. Remember: Empty slots will return null itemstacks, which means this code will throw NPES. Check if the item is null before using it.
    2. An itemstack will never be equal to an array of itemstacks. For loop through the drops and check if the items are similar before adding them.
    3. The itemstack's amount will never be greater than 64. Change this.
    4. That if statement does not even do anything, so either add code into it or simply invert the if statement.
    5. You never define "is". Replace it with the item you want to drop.
    6. p.updateInventory() should not be used because A) you have no need for it in that statement, and B) should only be used to update all items in an inventory.
    I really think you are confused. Let me post again what you need to do:
    1. For loop through all the drops.
    2. For loop through all the contents in a player's inventory.
    3. If the drop is not null, and if the drop is the same type as the itemstack from the inventory.
    4. -If the itemstack's amount plus the drop's amount is less than or equal to 64, set the itemstack's amount to that new amount and remove the drop.
    5. -if the itemstack's amount plus drop's amount is greater than 64, set the itemstack's amount to 64 and subtract the added amount from the drops. (What you are doing is taking items from the Drop and giving it to the Itemstack) After this, continue looping.
    6. If the drop has looped through all slots and there are still items remaining, drop those items.
     
Thread Status:
Not open for further replies.

Share This Page