Skip to content

Commit 011368e

Browse files
committed
Fixing so the logic for check items in menu module
1 parent 4aac4d3 commit 011368e

1 file changed

Lines changed: 36 additions & 28 deletions

File tree

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

Lines changed: 36 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
package org.broken.arrow.library.menu;
22

33

4-
import org.broken.arrow.library.menu.messages.SendMsgDuplicatedItems;
54
import org.broken.arrow.library.menu.utility.FilterMatch;
65
import org.broken.arrow.library.menu.utility.ItemCreator;
76
import org.broken.arrow.library.menu.utility.MatchCheckItemStack;
@@ -28,6 +27,7 @@
2827
import java.util.Objects;
2928
import java.util.Set;
3029
import java.util.UUID;
30+
import java.util.function.Consumer;
3131
import java.util.stream.Collectors;
3232

3333

@@ -38,7 +38,7 @@
3838
*/
3939
public class CheckItemsInsideMenu {
4040

41-
private final Map<UUID, Map<ItemStack, Integer>> duplicatedItems = new HashMap<>();
41+
private final Map<UUID, ItemOverflowBatch> duplicatedItems = new HashMap<>();
4242
private final RegisterMenuAPI registerMenuAPI;
4343
private FilterMatch filterMatch = FilterMatch.TYPE;
4444
private MatchCheckItemStack matchCheck = new MatchCheckItemStack();
@@ -241,19 +241,16 @@ private Map<Integer, ItemStack> addToMuchItems(final Map<Integer, ItemStack> ite
241241
final Set<ItemStack> set = new HashSet<>();
242242
this.sendMsgPlayer = false;
243243
for (final Map.Entry<Integer, ItemStack> entity : items.entrySet()) {
244-
245244
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());
250248
}
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())))
255253
itemStacksNoDoubleEntity.put(entity.getKey(), ItemCreator.createItemStackAsOne(entity.getValue()));
256-
}
257254
}
258255
}
259256
addItemsBackToPlayer(location);
@@ -283,24 +280,26 @@ private void addItemsBackToPlayer(final Player player, final ItemStack itemStack
283280
*/
284281
private void addItemsBackToPlayer(final Location location) {
285282
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();
287284

288285
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+
}
302301
}
303-
}
302+
});
304303
iterator.remove();
305304
}
306305
}
@@ -415,6 +414,15 @@ public Map<Material, Map<ItemStack, Integer>> getItems() {
415414
));
416415
}
417416

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+
418426
/**
419427
* Adds an item stack to this batch.
420428
*
@@ -484,7 +492,7 @@ public void putItem(@Nonnull final ItemStack itemStack, final int amount) {
484492
items.compute(stackAsOne, (key, currentAmount) -> {
485493
if (currentAmount == null) {
486494
int overflow = amount - 1;
487-
return overflow > 0 ? overflow : null;
495+
return overflow > 0 ? overflow : 0;
488496
}
489497
return currentAmount + amount;
490498
});

0 commit comments

Comments
 (0)