Solved Dropping items from inventory until inventory is empty

Discussion in 'Plugin Development' started by Kakarot798, Oct 14, 2015.

Thread Status:
Not open for further replies.
  1. Offline

    Kakarot798

    I want to have a player drop all the items in their inventory (excluding the armour), and I wrote out this method to do so, but could I get some peoples to check it over?

    Code:
    public void dropInv(Player player){
            while(!player.getInventory().containsAtLeast(new ItemStack(Material.AIR), 36)){
                for(int slot=0; slot < 36; slot++){
                    player.getWorld().dropItemNaturally(player.getLocation().add(2.0D, 0.0D, 2.0D), player.getInventory().getItem(slot));
                }
            }
        }
     
  2. Offline

    ShadowRanger

    That should work however it might just be easier to do the following which doesn't require a while loop:

    Code:
        public void dropInv(Player player) {
            ItemStack[] contents = player.getInventory().getContents();
    
            for (ItemStack itemStack : contents) {
                if (itemStack.getType() != Material.AIR) {
                    player.getWorld().dropItemNaturally(player.getLocation().add(2.0D, 0.0D, 2.0D), itemStack);
                }
            }
        }
    
     
    Last edited: Oct 15, 2015
    Xerox262 likes this.
  3. Offline

    Scimiguy

    @ShadowRanger
    DO NOT write code for them. They will not learn from copy-pasting code.

    Explain what they need to do, so that next time they have a question, they can answer it themselves rather than posting here again

    Not only that, but your bad habits will then become their bad habits.
     
    ShadowRanger likes this.
  4. Offline

    567legodude

    @Kakarot798 Use a for-loop or an Iterator to loop through the items in their inventory, on each one drop the item, you may want to set a pickup delay for the item so it doesn't go straight back into their inventory. Once the loop is done, clear the player's inventory.

    Other (open)
    @Scimiguy I taught myself everything I know by looking at pre-written code, most without explanation. Yet when I write code today I don't use any bad practices and I make tons of money developing for servers. Code can be perfectly fine if you can understand it. If the OP is unable to understand the above code, that is their fault for not having an understanding of very basic Java, which is listed as one of the things to know before developing with Bukkit.
     
    ShadowRanger likes this.
  5. Offline

    ShadowRanger

    Yeah you're right. I didn't really think of that because it was such a small piece of code.

    Just out of curiosity, what do you mean by my bad habits will become theirs? What could honestly be a bad habit in the code I used?
     
  6. Offline

    Scimiguy

    @ShadowRanger
    Here's one example then:

    Right here:
    Code:
    ItemStack[] contents = player.getInventory().getContents();
    You just added another instance to memory, when the loop could have directly pulled from it as such:

    Code:
    for(ItemStack itemStack : player.getInventory().getContents())
     
  7. Offline

    Xerox262

    He didn't say there was anything bad specifically, which there isn't it was a good piece of code, however since you gave him code to iterate, he might not know what problem he can have if he tries to modify the contents while in the for loop, aka concurrent modification exception. However you did create a variable to hold the original contents, but he doesn't know why you done that.

    You're wrong, the way he done it he can modify the inventory within the for without throwing a concurrent modification exception.

    Wait, do arrays not throw CMEs? :I
    Regardless, he showed him to create a variable that he can iterate through that he can modify the original with
     
  8. Offline

    Scimiguy

    @Xerox262
    He's not modifying the contents of the inventory in that loop, therefore it has no chance of throwing a ConcurrentModificationException
     
  9. Offline

    Xerox262

    Read the title. He wants to empty the player's inventory. If he decided to remove the item from the inventory in the for loop rather than just empty the inventory after the for loop then he would throw it. He might not know that removing it in the for loop will throw it, that's why...

    Also @Kakarot798, don't forget your Player#updateInventory(); or the player's inventory will not actually be cleared.
     
    Last edited: Oct 15, 2015
  10. Offline

    Scimiguy

    @Xerox262
    Ah fair enough.

    Point still stands though
     
  11. Offline

    Xerox262

    My point is kinda moot since there is no method to removing an object from an array without knowing what index it is. but if he was iterating over a Collection then he would get the error. But yea, in this case since it's an array he doesn't need to create a variable for it.
     
Thread Status:
Not open for further replies.

Share This Page