Fixed inventory sorting mods generating unnecessary threads

This commit is contained in:
Intelli 2024-07-08 19:53:53 -06:00
parent 63074715d3
commit e093970992
2 changed files with 38 additions and 0 deletions

View File

@ -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<String, Boolean> 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) {

View File

@ -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<String, ItemStack> map = new HashMap<>();
for (ItemStack itemStack : array) {
if (itemStack == null) {
continue;
}
map.put(itemStack.toString(), itemStack);
}
ArrayList<String> 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) {