1818import java .util .Collections ;
1919import java .util .HashMap ;
2020import java .util .HashSet ;
21+ import java .util .Iterator ;
2122import java .util .List ;
2223import java .util .Map ;
2324import java .util .Map .Entry ;
@@ -276,11 +277,14 @@ private void addItemsBackToPlayer(final Player player, final ItemStack itemStack
276277 /**
277278 * Drops back all items currently stored in the duplicated items map for all players.
278279 *
279- * @param location the location to drop the items if could not add it to the player.
280+ * @param location the location to drop the items if it could not add it to the player.
280281 */
281282 private void addItemsBackToPlayer (final Location location ) {
283+ if (duplicatedItems .isEmpty ()) return ;
284+ final Iterator <Entry <UUID , Map <ItemStack , Integer >>> iterator = duplicatedItems .entrySet ().iterator ();
282285
283- for (final Entry <UUID , Map <ItemStack , Integer >> mapEntry : this .duplicatedItems .entrySet ()) {
286+ while (iterator .hasNext ()) {
287+ Entry <UUID , Map <ItemStack , Integer >> mapEntry = iterator .next ();
284288 for (final Map .Entry <ItemStack , Integer > items : mapEntry .getValue ().entrySet ()) {
285289 final ItemStack itemStack = items .getKey ();
286290 final int amount = items .getValue ();
@@ -289,17 +293,19 @@ private void addItemsBackToPlayer(final Location location) {
289293 final OfflinePlayer offlinePlayer = Bukkit .getOfflinePlayer (mapEntry .getKey ());
290294 final Player player = offlinePlayer .getPlayer ();
291295 if (player != null ) {
292- final HashMap <Integer , ItemStack > ifInventorFull = player .getInventory ().addItem (itemStack );
293- if (!ifInventorFull .isEmpty () && player .getLocation ().getWorld () != null )
294- player .getLocation ().getWorld ().dropItemNaturally (player .getLocation (), ifInventorFull .get (0 ));
295-
296+ final HashMap <Integer , ItemStack > overflow = player .getInventory ().addItem (itemStack );
297+ Location playerLocation = player .getLocation ();
298+ if (!overflow .isEmpty () && playerLocation .getWorld () != null ) {
299+ overflow .values ().forEach (item ->
300+ playerLocation .getWorld ().dropItemNaturally (playerLocation , item ));
301+ }
296302 this .registerMenuAPI .getMessages ().sendDuplicatedMessage (player , new SendMsgDuplicatedItems .DuplicatedItemWrapper (itemStack , mapEntry .getValue ().size (), amount ));
297- } else if (location != null && location .getWorld () != null )
303+ } else if (location != null && location .getWorld () != null ) {
298304 location .getWorld ().dropItemNaturally (location , itemStack );
305+ }
299306 }
300- this . duplicatedItems . remove (mapEntry . getKey () );
307+ iterator . remove ();
301308 }
302-
303309 }
304310
305311 /**
0 commit comments