Drop item inventory when player quit!

Discussion in 'Spigot Plugin Development' started by matcracker, Jun 17, 2016.

  1. Hi guys, I've got a problem with my plugin. I find a bug that when a player quit the game the item remain in him inventory and don't drop. I try two ways but don't work:

    Code (Text):
    @EventHandler
        public void onPlayerQuit(PlayerQuitEvent event){
            Player p = event.getPlayer();
            for(ItemStack i : p.getInventory()){
                if(i != null){
                    p.getWorld().dropItemNaturally(p.getLocation(), i);
                    p.getInventory().remove(i);
                }
            }
        }
     
    Code (Text):
    @EventHandler
        public void onPlayerQuit(PlayerQuitEvent event){
            Player p = event.getPlayer();
            p.setHealth(0.0D);
        }
     
    Who can help me? Thanks!
     
  2. If you just want to kill them, using .damage would probably be better than .setHealth
     
  3. Choco

    Moderator

    This one is ALMOST correct. You want to loop through the CONTENTS of their inventory, and not their inventory
    for (ItemStack i : p.getInventory().getContents()){ /* Your stuff to drop it */ }

    EDIT: View @Trophonix's reply after this.

    Also, in doing that, you may get a ConcurrentModificationException if you're removing an item from the inventory while iterating through it. (I think. Not sure if remove() modifies the contents array). Regardless, I suggest instead clearing their inventory AFTER looping through all the items and dropping them
     
  4. Actually looping through the inventory works. It implements iterable or something.
     
  5. Choco

    Moderator

    Well damn! I didn't even know that!
    [​IMG]

    Make sure events are registered, and again, as I suggested before hand, clear the inventory after you're done iterating through it
     
    • Funny Funny x 1
  6. Actually there's another way to get the contents of a players inventory. I have found a neat little bug (or intended feature) in 1.8.8.
    The way to do this is actually to get the offline player and use .getplayer().get inventory().get contents();
    This method only works if the player has played before.

    Edit: maybe not what you are asking for, apologies
     
  7. That doesn't mean that looping through it and changing it won't throw a ConcurrentModificationException. If it's iterable, then you could create an iterator for the ItemStacks in the inventory and then remove them that way.
     
    • Agree Agree x 1
  8. Choco

    Moderator

    Or skip the use of an Iterator all-together and just clear the inventory when you're done with it ;P
     
    • Agree Agree x 2
  9. Alternatively, do the loop and dropping, then after the loop do inventory.clear
     
  10. The player must drop the items, because it's a Capire The Flag plugin and I've this bug, when player left, the flag remain in him inventory and the other players can't continue to play.

    Now I try your ways guys and I'll tell you if work. Thanks.