From e093970992cccd565d29e661d25c76e63198726c Mon Sep 17 00:00:00 2001 From: Intelli Date: Mon, 8 Jul 2024 19:53:53 -0600 Subject: [PATCH] Fixed inventory sorting mods generating unnecessary threads --- .../player/InventoryChangeListener.java | 10 +++++++ .../java/net/coreprotect/utility/Util.java | 28 +++++++++++++++++++ 2 files changed, 38 insertions(+) diff --git a/src/main/java/net/coreprotect/listener/player/InventoryChangeListener.java b/src/main/java/net/coreprotect/listener/player/InventoryChangeListener.java index 2cbdf35..2761234 100644 --- a/src/main/java/net/coreprotect/listener/player/InventoryChangeListener.java +++ b/src/main/java/net/coreprotect/listener/player/InventoryChangeListener.java @@ -4,6 +4,7 @@ import java.util.ArrayList; import java.util.Collections; import java.util.List; import java.util.Locale; +import java.util.concurrent.ConcurrentHashMap; import java.util.concurrent.atomic.AtomicLong; import org.bukkit.Location; @@ -38,6 +39,7 @@ public final class InventoryChangeListener extends Queue implements Listener { protected static AtomicLong tasksStarted = new AtomicLong(); protected static AtomicLong tasksCompleted = new AtomicLong(); + private static ConcurrentHashMap inventoryProcessing = new ConcurrentHashMap<>(); protected static void checkTasks(long taskStarted) { try { @@ -225,11 +227,19 @@ public final class InventoryChangeListener extends Queue implements Listener { Location inventoryLocation = location; ItemStack[] containerState = Util.getContainerState(inventory.getContents()); + String loggingChestId = player.getName() + "." + location.getBlockX() + "." + location.getBlockY() + "." + location.getBlockZ(); + Boolean lastTransaction = inventoryProcessing.get(loggingChestId); + if (lastTransaction != null) { + return; + } + inventoryProcessing.put(loggingChestId, true); + final long taskStarted = InventoryChangeListener.tasksStarted.incrementAndGet(); Scheduler.runTaskAsynchronously(CoreProtect.getInstance(), () -> { try { Material containerType = (enderChest != true ? null : Material.ENDER_CHEST); InventoryChangeListener.checkTasks(taskStarted); + inventoryProcessing.remove(loggingChestId); onInventoryInteract(player.getName(), inventory, containerState, containerType, inventoryLocation, true); } catch (Exception e) { diff --git a/src/main/java/net/coreprotect/utility/Util.java b/src/main/java/net/coreprotect/utility/Util.java index fc3f5c3..7230651 100755 --- a/src/main/java/net/coreprotect/utility/Util.java +++ b/src/main/java/net/coreprotect/utility/Util.java @@ -11,6 +11,7 @@ import java.text.DecimalFormatSymbols; import java.text.SimpleDateFormat; import java.util.ArrayList; import java.util.Arrays; +import java.util.Collections; import java.util.Date; import java.util.HashMap; import java.util.List; @@ -641,6 +642,33 @@ public class Util extends Queue { return result; } + public static ItemStack[] sortContainerState(ItemStack[] array) { + if (array == null) { + return null; + } + + ItemStack[] sorted = new ItemStack[array.length]; + Map map = new HashMap<>(); + for (ItemStack itemStack : array) { + if (itemStack == null) { + continue; + } + + map.put(itemStack.toString(), itemStack); + } + + ArrayList sortedKeys = new ArrayList<>(map.keySet()); + Collections.sort(sortedKeys); + + int i = 0; + for (String key : sortedKeys) { + sorted[i] = map.get(key); + i++; + } + + return sorted; + } + /* return true if ItemStack[] contents are identical */ public static boolean compareContainers(ItemStack[] oldContainer, ItemStack[] newContainer) { if (oldContainer.length != newContainer.length) {