Skip to content

Commit

Permalink
Item List Sort: Ensure access to shared private field is atomic (GTNe…
Browse files Browse the repository at this point in the history
…wHorizons#584)

Co-authored-by: lailani-f <>
  • Loading branch information
lailani-f authored Jan 4, 2025
1 parent 6374c16 commit e0dd684
Showing 1 changed file with 15 additions and 11 deletions.
26 changes: 15 additions & 11 deletions src/main/java/codechicken/nei/ItemList.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Comparator;
import java.util.HashMap;
import java.util.HashSet;
import java.util.LinkedList;
Expand Down Expand Up @@ -44,7 +45,7 @@ public class ItemList {

private static final HashSet<Item> erroredItems = new HashSet<>();
private static final HashSet<String> stackTraces = new HashSet<>();
private static final HashMap<ItemStack, Integer> ordering = new HashMap<>();
private static HashMap<ItemStack, Integer> ordering = new HashMap<>();
/**
* Unlike {@link LayoutManager#itemsLoaded}, this indicates whether item loading is actually finished or not.
*/
Expand Down Expand Up @@ -234,10 +235,10 @@ private String getTooltip(ItemStack stack) {
}

private void updateOrdering(List<ItemStack> items) {
ItemList.ordering.clear();

ItemSorter.sort(items);

HashMap<ItemStack, Integer> newOrdering = new HashMap<>();

if (!CollapsibleItems.isEmpty()) {
HashMap<Integer, Integer> groups = new HashMap<>();
int orderIndex = 0;
Expand All @@ -246,23 +247,25 @@ private void updateOrdering(List<ItemStack> items) {
final int groupIndex = CollapsibleItems.getGroupIndex(stack);

if (groupIndex == -1) {
ItemList.ordering.put(stack, orderIndex++);
newOrdering.put(stack, orderIndex++);
} else {

if (!groups.containsKey(groupIndex)) {
groups.put(groupIndex, orderIndex++);
}

ItemList.ordering.put(stack, groups.get(groupIndex));
newOrdering.put(stack, groups.get(groupIndex));
}
}
} else {
int orderIndex = 0;

for (ItemStack stack : items) {
ItemList.ordering.put(stack, orderIndex++);
newOrdering.put(stack, orderIndex++);
}
}

ItemList.ordering = newOrdering;
}

@Override
Expand Down Expand Up @@ -346,12 +349,9 @@ public ForkJoinWorkerThread newThread(ForkJoinPool pool) {

public static final RestartableTask updateFilter = new RestartableTask("NEI Item Filtering") {

private int compare(ItemStack o1, ItemStack o2) {
return Integer.compare(ItemList.ordering.get(o1), ItemList.ordering.get(o2));
}

@Override
public void execute() {

if (!loadFinished) return;
ItemFilter filter = getItemListFilter();
ArrayList<ItemStack> filtered;
Expand All @@ -368,8 +368,12 @@ public void execute() {
}

if (interrupted()) return;
filtered.sort(this::compare);

Comparator<ItemStack> comparator = Comparator.comparingInt(ItemList.ordering::get);
filtered.sort(comparator::compare);

if (interrupted()) return;

ItemPanel.updateItemList(filtered);
}
};
Expand Down

0 comments on commit e0dd684

Please sign in to comment.