Skip to content

Commit ad50810

Browse files
committed
clean up the return items for player
1 parent 811d1c1 commit ad50810

1 file changed

Lines changed: 15 additions & 9 deletions

File tree

Menu Library/src/main/java/org/broken/arrow/library/menu/CheckItemsInsideMenu.java

Lines changed: 15 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
import java.util.Collections;
1919
import java.util.HashMap;
2020
import java.util.HashSet;
21+
import java.util.Iterator;
2122
import java.util.List;
2223
import java.util.Map;
2324
import 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

Comments
 (0)