Skip to content

Commit

Permalink
Fixed inventory sorting mods generating unnecessary threads
Browse files Browse the repository at this point in the history
  • Loading branch information
Intelli committed Jul 9, 2024
1 parent 6307471 commit e093970
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
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;
Expand Down Expand Up @@ -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 {
Expand Down Expand Up @@ -225,11 +227,19 @@ static void onInventoryInteractAsync(Player player, Inventory inventory, boolean
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) {
Expand Down
28 changes: 28 additions & 0 deletions src/main/java/net/coreprotect/utility/Util.java
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
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;
Expand Down Expand Up @@ -641,6 +642,33 @@ public static ItemStack[] getContainerState(ItemStack[] array) {
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) {
Expand Down

0 comments on commit e093970

Please sign in to comment.