|
1 | 1 | package org.broken.arrow.library.menu; |
2 | 2 |
|
3 | 3 |
|
4 | | -import org.broken.arrow.library.menu.messages.SendMsgDuplicatedItems; |
5 | 4 | import org.broken.arrow.library.menu.utility.FilterMatch; |
6 | 5 | import org.broken.arrow.library.menu.utility.ItemCreator; |
7 | 6 | import org.broken.arrow.library.menu.utility.MatchCheckItemStack; |
|
28 | 27 | import java.util.Objects; |
29 | 28 | import java.util.Set; |
30 | 29 | import java.util.UUID; |
| 30 | +import java.util.function.Consumer; |
31 | 31 | import java.util.stream.Collectors; |
32 | 32 |
|
33 | 33 |
|
|
38 | 38 | */ |
39 | 39 | public class CheckItemsInsideMenu { |
40 | 40 |
|
41 | | - private final Map<UUID, Map<ItemStack, Integer>> duplicatedItems = new HashMap<>(); |
| 41 | + private final Map<UUID, ItemOverflowBatch> duplicatedItems = new HashMap<>(); |
42 | 42 | private final RegisterMenuAPI registerMenuAPI; |
43 | 43 | private FilterMatch filterMatch = FilterMatch.TYPE; |
44 | 44 | private MatchCheckItemStack matchCheck = new MatchCheckItemStack(); |
@@ -241,19 +241,16 @@ private Map<Integer, ItemStack> addToMuchItems(final Map<Integer, ItemStack> ite |
241 | 241 | final Set<ItemStack> set = new HashSet<>(); |
242 | 242 | this.sendMsgPlayer = false; |
243 | 243 | for (final Map.Entry<Integer, ItemStack> entity : items.entrySet()) { |
244 | | - |
245 | 244 | if (entity.getValue() != null) { |
246 | | - |
247 | | - if (entity.getValue().getAmount() > 1) { |
248 | | - cachedDuplicatedItems.put(ItemCreator.createItemStackAsOne(entity.getValue()), (ItemCreator.countItemStacks(entity.getValue(), itemStacks)) - 1); |
249 | | - duplicatedItems.put(player.getUniqueId(), cachedDuplicatedItems); |
| 245 | + /* if (entity.getValue().getAmount() > 1) { |
| 246 | + //cachedDuplicatedItems.put(ItemCreator.createItemStackAsOne(entity.getValue()), (ItemCreator.countItemStacks(entity.getValue(), itemStacks)) - 1); |
| 247 | + duplicatedItems.computeIfAbsent(player.getUniqueId(), uuid -> new ItemOverflowBatch()).putItem(entity.getValue()); |
250 | 248 | } |
251 | | - if (!set.add(ItemCreator.createItemStackAsOne(entity.getValue()))) { |
252 | | - cachedDuplicatedItems.put(ItemCreator.createItemStackAsOne(entity.getValue()), (ItemCreator.countItemStacks(entity.getValue(), itemStacks)) - 1); |
253 | | - duplicatedItems.put(player.getUniqueId(), cachedDuplicatedItems); |
254 | | - } else { |
| 249 | +*/ |
| 250 | + //cachedDuplicatedItems.put(ItemCreator.createItemStackAsOne(entity.getValue()), (ItemCreator.countItemStacks(entity.getValue(), itemStacks)) - 1); |
| 251 | + duplicatedItems.computeIfAbsent(player.getUniqueId(), uuid -> new ItemOverflowBatch()).putItem(entity.getValue()); |
| 252 | + if (set.add(ItemCreator.createItemStackAsOne(entity.getValue()))) |
255 | 253 | itemStacksNoDoubleEntity.put(entity.getKey(), ItemCreator.createItemStackAsOne(entity.getValue())); |
256 | | - } |
257 | 254 | } |
258 | 255 | } |
259 | 256 | addItemsBackToPlayer(location); |
@@ -283,24 +280,26 @@ private void addItemsBackToPlayer(final Player player, final ItemStack itemStack |
283 | 280 | */ |
284 | 281 | private void addItemsBackToPlayer(final Location location) { |
285 | 282 | if (duplicatedItems.isEmpty()) return; |
286 | | - final Iterator<Entry<UUID, Map<ItemStack, Integer>>> iterator = duplicatedItems.entrySet().iterator(); |
| 283 | + final Iterator<Entry<UUID, ItemOverflowBatch>> iterator = duplicatedItems.entrySet().iterator(); |
287 | 284 |
|
288 | 285 | while (iterator.hasNext()) { |
289 | | - Entry<UUID, Map<ItemStack, Integer>> mapEntry = iterator.next(); |
290 | | - for (final Map.Entry<ItemStack, Integer> items : mapEntry.getValue().entrySet()) { |
291 | | - final ItemStack itemStack = items.getKey(); |
292 | | - final int amount = items.getValue(); |
293 | | - |
294 | | - itemStack.setAmount(amount); |
295 | | - final OfflinePlayer offlinePlayer = Bukkit.getOfflinePlayer(mapEntry.getKey()); |
296 | | - final Player player = offlinePlayer.getPlayer(); |
297 | | - if (player != null) { |
298 | | - this.returnsBackItems(player, itemStack); |
299 | | - this.registerMenuAPI.getMessages().sendDuplicatedMessage(player, new DuplicatedItemWrapper(itemStack, mapEntry.getValue().size(), amount)); |
300 | | - } else if (location != null && location.getWorld() != null) { |
301 | | - location.getWorld().dropItemNaturally(location, itemStack); |
| 286 | + Entry<UUID, ItemOverflowBatch> mapEntry = iterator.next(); |
| 287 | + mapEntry.getValue().getItem(duplicateStacks -> { |
| 288 | + for (final Entry<ItemStack, Integer> items : duplicateStacks.entrySet()) { |
| 289 | + final ItemStack itemStack = items.getKey(); |
| 290 | + final int amount = items.getValue(); |
| 291 | + |
| 292 | + itemStack.setAmount(amount); |
| 293 | + final OfflinePlayer offlinePlayer = Bukkit.getOfflinePlayer(mapEntry.getKey()); |
| 294 | + final Player player = offlinePlayer.getPlayer(); |
| 295 | + if (player != null) { |
| 296 | + this.returnsBackItems(player, itemStack); |
| 297 | + this.registerMenuAPI.getMessages().sendDuplicatedMessage(player, new DuplicatedItemWrapper(itemStack, duplicateStacks.size(), amount)); |
| 298 | + } else if (location != null && location.getWorld() != null) { |
| 299 | + location.getWorld().dropItemNaturally(location, itemStack); |
| 300 | + } |
302 | 301 | } |
303 | | - } |
| 302 | + }); |
304 | 303 | iterator.remove(); |
305 | 304 | } |
306 | 305 | } |
@@ -415,6 +414,15 @@ public Map<Material, Map<ItemStack, Integer>> getItems() { |
415 | 414 | )); |
416 | 415 | } |
417 | 416 |
|
| 417 | + /** |
| 418 | + * Returns a callback to access the of the batched items. |
| 419 | + * |
| 420 | + * @param callback the callback for get the items that is duplicated. |
| 421 | + */ |
| 422 | + public void getItem(final Consumer<Map<ItemStack, Integer>> callback) { |
| 423 | + items.forEach((key, value) -> callback.accept(value.getItems())); |
| 424 | + } |
| 425 | + |
418 | 426 | /** |
419 | 427 | * Adds an item stack to this batch. |
420 | 428 | * |
@@ -484,7 +492,7 @@ public void putItem(@Nonnull final ItemStack itemStack, final int amount) { |
484 | 492 | items.compute(stackAsOne, (key, currentAmount) -> { |
485 | 493 | if (currentAmount == null) { |
486 | 494 | int overflow = amount - 1; |
487 | | - return overflow > 0 ? overflow : null; |
| 495 | + return overflow > 0 ? overflow : 0; |
488 | 496 | } |
489 | 497 | return currentAmount + amount; |
490 | 498 | }); |
|
0 commit comments