mirror of
https://github.com/PlayPro/CoreProtect.git
synced 2025-02-21 02:33:36 +01:00
Fixed inventory sorting mods generating unnecessary threads
This commit is contained in:
parent
63074715d3
commit
e093970992
@ -4,6 +4,7 @@ import java.util.ArrayList;
|
|||||||
import java.util.Collections;
|
import java.util.Collections;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
import java.util.Locale;
|
import java.util.Locale;
|
||||||
|
import java.util.concurrent.ConcurrentHashMap;
|
||||||
import java.util.concurrent.atomic.AtomicLong;
|
import java.util.concurrent.atomic.AtomicLong;
|
||||||
|
|
||||||
import org.bukkit.Location;
|
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 tasksStarted = new AtomicLong();
|
||||||
protected static AtomicLong tasksCompleted = new AtomicLong();
|
protected static AtomicLong tasksCompleted = new AtomicLong();
|
||||||
|
private static ConcurrentHashMap<String, Boolean> inventoryProcessing = new ConcurrentHashMap<>();
|
||||||
|
|
||||||
protected static void checkTasks(long taskStarted) {
|
protected static void checkTasks(long taskStarted) {
|
||||||
try {
|
try {
|
||||||
@ -225,11 +227,19 @@ public final class InventoryChangeListener extends Queue implements Listener {
|
|||||||
Location inventoryLocation = location;
|
Location inventoryLocation = location;
|
||||||
ItemStack[] containerState = Util.getContainerState(inventory.getContents());
|
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();
|
final long taskStarted = InventoryChangeListener.tasksStarted.incrementAndGet();
|
||||||
Scheduler.runTaskAsynchronously(CoreProtect.getInstance(), () -> {
|
Scheduler.runTaskAsynchronously(CoreProtect.getInstance(), () -> {
|
||||||
try {
|
try {
|
||||||
Material containerType = (enderChest != true ? null : Material.ENDER_CHEST);
|
Material containerType = (enderChest != true ? null : Material.ENDER_CHEST);
|
||||||
InventoryChangeListener.checkTasks(taskStarted);
|
InventoryChangeListener.checkTasks(taskStarted);
|
||||||
|
inventoryProcessing.remove(loggingChestId);
|
||||||
onInventoryInteract(player.getName(), inventory, containerState, containerType, inventoryLocation, true);
|
onInventoryInteract(player.getName(), inventory, containerState, containerType, inventoryLocation, true);
|
||||||
}
|
}
|
||||||
catch (Exception e) {
|
catch (Exception e) {
|
||||||
|
@ -11,6 +11,7 @@ import java.text.DecimalFormatSymbols;
|
|||||||
import java.text.SimpleDateFormat;
|
import java.text.SimpleDateFormat;
|
||||||
import java.util.ArrayList;
|
import java.util.ArrayList;
|
||||||
import java.util.Arrays;
|
import java.util.Arrays;
|
||||||
|
import java.util.Collections;
|
||||||
import java.util.Date;
|
import java.util.Date;
|
||||||
import java.util.HashMap;
|
import java.util.HashMap;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
@ -641,6 +642,33 @@ public class Util extends Queue {
|
|||||||
return result;
|
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 */
|
/* return true if ItemStack[] contents are identical */
|
||||||
public static boolean compareContainers(ItemStack[] oldContainer, ItemStack[] newContainer) {
|
public static boolean compareContainers(ItemStack[] oldContainer, ItemStack[] newContainer) {
|
||||||
if (oldContainer.length != newContainer.length) {
|
if (oldContainer.length != newContainer.length) {
|
||||||
|
Loading…
Reference in New Issue
Block a user