Skip to content

Commit

Permalink
Fix restoring collapsible items state (#564)
Browse files Browse the repository at this point in the history
Co-authored-by: slprime <history-21@yandex.ru>
  • Loading branch information
slprime and slprime authored Dec 5, 2024
1 parent 9288fc4 commit 6049d0e
Show file tree
Hide file tree
Showing 8 changed files with 136 additions and 126 deletions.
129 changes: 67 additions & 62 deletions src/main/java/codechicken/nei/CollapsibleItems.java
Original file line number Diff line number Diff line change
Expand Up @@ -52,40 +52,15 @@ public boolean matches(ItemStack stack) {

private static final String STATE_KEY = "collapsibleitems";

protected File statesFile;
protected final List<GroupItem> groups = new ArrayList<>();
protected final Map<ItemStack, Integer> cache = new ConcurrentHashMap<>();
protected static File statesFile;
protected static final List<GroupItem> groups = new ArrayList<>();
protected static final Map<ItemStack, Integer> cache = new ConcurrentHashMap<>();

public void load() {
try {
private CollapsibleItems() {}

if (NEIClientConfig.world.nbt.hasKey(STATE_KEY)) {
NBTTagCompound states = NEIClientConfig.world.nbt.getCompoundTag(STATE_KEY);
@SuppressWarnings("unchecked")
final Map<String, NBTPrimitive> list = (Map<String, NBTPrimitive>) states.tagMap;
final Map<String, GroupItem> mapping = new HashMap<>();

for (GroupItem group : this.groups) {
mapping.put(group.guid, group);
}

for (Map.Entry<String, NBTPrimitive> nbtEntry : list.entrySet()) {
if (mapping.containsKey(nbtEntry.getKey())) {
mapping.get(nbtEntry.getKey()).expanded = nbtEntry.getValue().func_150290_f() == 1;
}
}
}

} catch (Exception e) {
NEIClientConfig.logger.error("Error loading collapsible items states", e);
}

reloadGroups();
}

public void reloadGroups() {
this.groups.clear();
this.cache.clear();
public static void load() {
CollapsibleItems.groups.clear();
CollapsibleItems.cache.clear();

for (int i = PresetsList.presets.size() - 1; i >= 0; i--) {
Preset preset = PresetsList.presets.get(i);
Expand All @@ -102,9 +77,11 @@ public void reloadGroups() {
"collapsibleitems.cfg",
lines -> parseFile(lines.collect(Collectors.toCollection(ArrayList::new))));
}

loadStates();
}

private void parseFile(List<String> itemStrings) {
private static void parseFile(List<String> itemStrings) {
final JsonParser parser = new JsonParser();
GroupItem group = new GroupItem();

Expand Down Expand Up @@ -146,38 +123,39 @@ private void parseFile(List<String> itemStrings) {
}
}

protected void addGroup(GroupItem group) {
private static void addGroup(GroupItem group) {
if (group == null || group.filter == null
|| group.filter instanceof EverythingItemFilter
|| group.filter instanceof NothingItemFilter)
return;
this.groups.add(group);
CollapsibleItems.groups.add(group);
}

public boolean isEmpty() {
return this.groups.isEmpty();
public static boolean isEmpty() {
return CollapsibleItems.groups.isEmpty();
}

public ItemFilter getItemFilter() {
public static ItemFilter getItemFilter() {
AnyMultiItemFilter filter = new AnyMultiItemFilter();

for (GroupItem group : this.groups) {
for (GroupItem group : CollapsibleItems.groups) {
filter.filters.add(group.filter);
}

return filter;
}

public void updateCache(final List<ItemStack> items) {
this.cache.clear();
public static void updateCache(final List<ItemStack> items) {
CollapsibleItems.cache.clear();

try {

ItemList.forkJoinPool.submit(() -> items.parallelStream().forEach(stack -> {
GroupItem group = this.groups.stream().filter(g -> g.matches(stack)).findFirst().orElse(null);
GroupItem group = CollapsibleItems.groups.stream().filter(g -> g.matches(stack)).findFirst()
.orElse(null);

if (group != null) {
this.cache.put(stack, this.groups.indexOf(group));
CollapsibleItems.cache.put(stack, CollapsibleItems.groups.indexOf(group));
}
})).get();

Expand All @@ -187,62 +165,89 @@ public void updateCache(final List<ItemStack> items) {

}

public int getGroupIndex(ItemStack stack) {
public static int getGroupIndex(ItemStack stack) {

if (stack == null) {
return -1;
}

return this.cache.getOrDefault(stack, -1);
return CollapsibleItems.cache.getOrDefault(stack, -1);
}

public String getDisplayName(int groupIndex) {
public static String getDisplayName(int groupIndex) {

if (groupIndex < this.groups.size()) {
return this.groups.get(groupIndex).displayName;
if (groupIndex < CollapsibleItems.groups.size()) {
return CollapsibleItems.groups.get(groupIndex).displayName;
}

return null;
}

public boolean isExpanded(int groupIndex) {
public static boolean isExpanded(int groupIndex) {

if (groupIndex < this.groups.size()) {
return this.groups.get(groupIndex).expanded;
if (groupIndex < CollapsibleItems.groups.size()) {
return CollapsibleItems.groups.get(groupIndex).expanded;
}

return true;
}

public void setExpanded(int groupIndex, boolean expanded) {
public static void setExpanded(int groupIndex, boolean expanded) {

if (groupIndex < this.groups.size()) {
this.groups.get(groupIndex).expanded = expanded;
saveStates();
if (groupIndex < CollapsibleItems.groups.size()) {
CollapsibleItems.groups.get(groupIndex).expanded = expanded;
}
}

public void toggleGroups(Boolean expanded) {
public static void toggleGroups(Boolean expanded) {

if (expanded == null) {
expanded = this.groups.stream().noneMatch(g -> g.expanded);
expanded = CollapsibleItems.groups.stream().noneMatch(g -> g.expanded);
}

for (GroupItem group : this.groups) {
for (GroupItem group : CollapsibleItems.groups) {
group.expanded = expanded;
}

saveStates();
}

private void saveStates() {
public static void saveStates() {
NBTTagCompound list = new NBTTagCompound();

for (GroupItem group : this.groups) {
for (GroupItem group : CollapsibleItems.groups) {
list.setBoolean(group.guid, group.expanded);
}

NEIClientConfig.world.nbt.setTag(STATE_KEY, list);
if (NEIClientConfig.world != null) {
NEIClientConfig.world.nbt.setTag(STATE_KEY, list);
}
}

private static void loadStates() {

try {

if (NEIClientConfig.world.nbt.hasKey(STATE_KEY)) {
NBTTagCompound states = NEIClientConfig.world.nbt.getCompoundTag(STATE_KEY);
@SuppressWarnings("unchecked")
final Map<String, NBTPrimitive> list = (Map<String, NBTPrimitive>) states.tagMap;
final Map<String, GroupItem> mapping = new HashMap<>();

for (GroupItem group : CollapsibleItems.groups) {
mapping.put(group.guid, group);
}

for (Map.Entry<String, NBTPrimitive> nbtEntry : list.entrySet()) {
if (mapping.containsKey(nbtEntry.getKey())) {
mapping.get(nbtEntry.getKey()).expanded = nbtEntry.getValue().func_150290_f() == 1;
}
}
}

} catch (Exception e) {
NEIClientConfig.logger.error("Error loading collapsible items states", e);
}

}

}
7 changes: 3 additions & 4 deletions src/main/java/codechicken/nei/ItemList.java
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,6 @@ public class ItemList {
*/
public static final List<ItemFilterProvider> itemFilterers = new LinkedList<>();
public static final List<ItemsLoadedCallback> loadCallbacks = new LinkedList<>();
public static final CollapsibleItems collapsibleItems = new CollapsibleItems();

private static final HashSet<Item> erroredItems = new HashSet<>();
private static final HashSet<String> stackTraces = new HashSet<>();
Expand Down Expand Up @@ -239,12 +238,12 @@ private void updateOrdering(List<ItemStack> items) {

ItemSorter.sort(items);

if (!ItemList.collapsibleItems.isEmpty()) {
if (!CollapsibleItems.isEmpty()) {
HashMap<Integer, Integer> groups = new HashMap<>();
int orderIndex = 0;

for (ItemStack stack : items) {
final int groupIndex = ItemList.collapsibleItems.getGroupIndex(stack);
final int groupIndex = CollapsibleItems.getGroupIndex(stack);

if (groupIndex == -1) {
ItemList.ordering.put(stack, orderIndex++);
Expand Down Expand Up @@ -318,7 +317,7 @@ public void execute() {
for (ItemsLoadedCallback callback : loadCallbacks) callback.itemsLoaded();

if (interrupted()) return;
ItemList.collapsibleItems.updateCache(items);
CollapsibleItems.updateCache(items);
updateOrdering(items);

loadFinished = true;
Expand Down
18 changes: 9 additions & 9 deletions src/main/java/codechicken/nei/ItemPanel.java
Original file line number Diff line number Diff line change
Expand Up @@ -98,22 +98,22 @@ public void refresh(GuiContainer gui) {
this.groupItems.clear();
this.forceExpand = false;

if (!ItemList.collapsibleItems.isEmpty() && !this.newItems.isEmpty()) {
if (!CollapsibleItems.isEmpty() && !this.newItems.isEmpty()) {
final Set<Integer> groups = new HashSet<>();
boolean outsideGroup = false;

this.realItems = new ArrayList<>();
this.rawItems = new ArrayList<>(this.newItems);

for (ItemStack stack : this.newItems) {
final int groupIndex = ItemList.collapsibleItems.getGroupIndex(stack);
final int groupIndex = CollapsibleItems.getGroupIndex(stack);

if (groupIndex == -1) {
this.realItems.add(stack);
outsideGroup = true;
} else {

if (!groups.contains(groupIndex) || ItemList.collapsibleItems.isExpanded(groupIndex)) {
if (!groups.contains(groupIndex) || CollapsibleItems.isExpanded(groupIndex)) {
this.realItems.add(stack);
groups.add(groupIndex);
}
Expand Down Expand Up @@ -175,7 +175,7 @@ protected void calculateGroupBorders(List<Integer> mask) {
}

int idx = mask.get(slotIndex);
maskGroup.put(idx, ItemList.collapsibleItems.getGroupIndex(getItem(idx)));
maskGroup.put(idx, CollapsibleItems.getGroupIndex(getItem(idx)));
}

for (int slotIndex = 0; slotIndex < mask.size(); slotIndex++) {
Expand All @@ -197,8 +197,8 @@ protected void calculateGroupBorders(List<Integer> mask) {
MaskMetadata metadata = new MaskMetadata();

metadata.groupIndex = groupIndex;
metadata.displayName = ItemList.collapsibleItems.getDisplayName(groupIndex);
metadata.extended = this.forceExpand || ItemList.collapsibleItems.isExpanded(groupIndex);
metadata.displayName = CollapsibleItems.getDisplayName(groupIndex);
metadata.extended = this.forceExpand || CollapsibleItems.isExpanded(groupIndex);
metadata.bgColor = metadata.extended ? expandedColor : collapsedColor;
metadata.color = darkerColor(metadata.bgColor);
metadata.left = column == 0 || idx == 0
Expand Down Expand Up @@ -339,7 +339,7 @@ public String getButtonTip() {

@Override
public boolean onButtonPress(boolean rightclick) {
ItemList.collapsibleItems.toggleGroups(rightclick ? false : null);
CollapsibleItems.toggleGroups(rightclick ? false : null);
ItemList.updateFilter.restart();
return true;
}
Expand Down Expand Up @@ -482,7 +482,7 @@ public void setVisible() {
LayoutManager.addWidget(quantity);
}

if (!ItemList.collapsibleItems.isEmpty()) {
if (!CollapsibleItems.isEmpty()) {
LayoutManager.addWidget(toggleGroups);
}

Expand Down Expand Up @@ -560,7 +560,7 @@ public boolean handleClick(int mousex, int mousey, int button) {
final MaskMetadata metadata = panelGrid.maskMetadata.get(hoverSlot.slotIndex);

if (metadata != null) {
ItemList.collapsibleItems.setExpanded(metadata.groupIndex, !metadata.extended);
CollapsibleItems.setExpanded(metadata.groupIndex, !metadata.extended);
panelGrid.setItems(panelGrid.rawItems);
return true;
}
Expand Down
Loading

0 comments on commit 6049d0e

Please sign in to comment.