Files
NotEnoughItems/src/main/java/codechicken/nei/CollapsibleItems.java
T

Ignoring revisions in .git-blame-ignore-revs. Click here to bypass and see the normal blame view.

248 lines
7.6 KiB
Java
Raw Normal View History

2024-07-26 18:44:54 +03:00
package codechicken.nei;
import java.io.File;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
2024-08-17 23:24:27 +03:00
import java.util.UUID;
2024-07-26 18:44:54 +03:00
import java.util.concurrent.ConcurrentHashMap;
import java.util.stream.Collectors;
import net.minecraft.item.ItemStack;
2024-08-17 23:24:27 +03:00
import net.minecraft.nbt.NBTBase.NBTPrimitive;
import net.minecraft.nbt.NBTTagCompound;
import net.minecraft.util.StatCollector;
2024-07-26 18:44:54 +03:00
2024-08-17 23:24:27 +03:00
import com.google.gson.JsonObject;
import com.google.gson.JsonParser;
import com.google.gson.JsonSyntaxException;
2024-07-26 18:44:54 +03:00
import codechicken.nei.ItemList.AnyMultiItemFilter;
import codechicken.nei.ItemList.EverythingItemFilter;
import codechicken.nei.ItemList.NothingItemFilter;
import codechicken.nei.PresetsList.Preset;
import codechicken.nei.PresetsList.PresetMode;
import codechicken.nei.api.ItemFilter;
2024-08-29 23:14:52 +03:00
import codechicken.nei.util.ItemStackFilterParser;
2024-07-26 18:44:54 +03:00
public class CollapsibleItems {
protected static class GroupItem {
2024-08-17 23:24:27 +03:00
public String guid;
public ItemFilter filter;
2024-07-26 18:44:54 +03:00
public boolean expanded = false;
2024-08-17 23:24:27 +03:00
public String displayName = "";
2024-07-26 18:44:54 +03:00
2024-08-17 23:24:27 +03:00
public void setFilter(String filter) {
2024-08-29 23:14:52 +03:00
this.filter = ItemStackFilterParser.parse(filter.trim());
this.guid = this.filter != null ? UUID.nameUUIDFromBytes(filter.getBytes()).toString() : "";
2024-08-17 23:24:27 +03:00
}
public void setFilter(ItemFilter filter, String guid) {
2024-07-26 18:44:54 +03:00
this.filter = filter;
2024-08-17 23:24:27 +03:00
this.guid = guid;
2024-07-26 18:44:54 +03:00
}
public boolean matches(ItemStack stack) {
return this.filter.matches(stack);
}
}
2024-08-29 23:14:52 +03:00
private static final String STATE_KEY = "collapsibleitems";
2024-08-17 23:24:27 +03:00
protected File statesFile;
protected final List<GroupItem> groups = new ArrayList<>();
2024-07-26 18:44:54 +03:00
protected final Map<ItemStack, Integer> cache = new ConcurrentHashMap<>();
2024-08-29 23:14:52 +03:00
public void load() {
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 : 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();
2024-07-26 18:44:54 +03:00
}
2024-08-29 23:14:52 +03:00
public void reloadGroups() {
2024-07-26 18:44:54 +03:00
this.groups.clear();
this.cache.clear();
for (int i = PresetsList.presets.size() - 1; i >= 0; i--) {
Preset preset = PresetsList.presets.get(i);
if (preset.enabled && preset.mode == PresetMode.GROUP) {
2024-08-17 23:24:27 +03:00
GroupItem group = new GroupItem();
group.setFilter(preset, UUID.nameUUIDFromBytes(preset.items.toString().getBytes()).toString());
group.displayName = preset.name;
addGroup(group);
2024-07-26 18:44:54 +03:00
}
}
2024-08-17 23:24:27 +03:00
if (NEIClientConfig.enableCollapsibleItems()) {
2024-08-29 23:14:52 +03:00
ClientHandler.loadSettingsFile(
"collapsibleitems.cfg",
lines -> parseFile(lines.collect(Collectors.toCollection(ArrayList::new))));
2024-07-26 18:44:54 +03:00
}
}
2024-08-29 23:14:52 +03:00
private void parseFile(List<String> itemStrings) {
2024-08-17 23:24:27 +03:00
final JsonParser parser = new JsonParser();
GroupItem group = new GroupItem();
for (String itemStr : itemStrings) {
try {
if (itemStr.startsWith("; ")) {
JsonObject settings = parser.parse(itemStr.substring(2)).getAsJsonObject();
if (settings.get("displayName") != null) {
group.displayName = settings.get("displayName").getAsString();
}
if (settings.get("unlocalizedName") != null) {
2024-08-29 23:14:52 +03:00
String unlocalizedName = settings.get("unlocalizedName").getAsString();
String displayName = StatCollector.translateToLocal(unlocalizedName);
2024-08-17 23:24:27 +03:00
2024-08-29 23:14:52 +03:00
if (!displayName.equals(unlocalizedName)) {
2024-08-17 23:24:27 +03:00
group.displayName = displayName;
}
}
if (settings.get("expanded") != null) {
group.expanded = settings.get("expanded").getAsBoolean();
}
} else {
group.setFilter(itemStr);
}
if (group != null && group.filter != null) {
addGroup(group);
group = new GroupItem();
}
} catch (IllegalArgumentException | JsonSyntaxException | IllegalStateException e) {
NEIClientConfig.logger.error("Failed to load collapsible items from json string:\n{}", itemStr);
}
}
}
protected void addGroup(GroupItem group) {
if (group == null || group.filter == null
|| group.filter instanceof EverythingItemFilter
|| group.filter instanceof NothingItemFilter)
return;
this.groups.add(group);
2024-07-26 18:44:54 +03:00
}
2024-08-17 23:24:27 +03:00
public boolean isEmpty() {
return this.groups.isEmpty();
2024-07-26 18:44:54 +03:00
}
public ItemFilter getItemFilter() {
AnyMultiItemFilter filter = new AnyMultiItemFilter();
2024-08-17 23:24:27 +03:00
for (GroupItem group : this.groups) {
2024-07-26 18:44:54 +03:00
filter.filters.add(group.filter);
}
return filter;
}
public void updateCache(final List<ItemStack> items) {
this.cache.clear();
try {
ItemList.forkJoinPool.submit(() -> items.parallelStream().forEach(stack -> {
2024-08-17 23:24:27 +03:00
GroupItem group = this.groups.stream().filter(g -> g.matches(stack)).findFirst().orElse(null);
2024-07-26 18:44:54 +03:00
if (group != null) {
2024-08-17 23:24:27 +03:00
this.cache.put(stack, this.groups.indexOf(group));
2024-07-26 18:44:54 +03:00
}
})).get();
} catch (Exception e) {
NEIClientConfig.logger.error("Error create collapsible items groups", e);
}
}
public int getGroupIndex(ItemStack stack) {
if (stack == null) {
return -1;
}
return this.cache.getOrDefault(stack, -1);
}
2024-08-17 23:24:27 +03:00
public String getDisplayName(int groupIndex) {
if (groupIndex < this.groups.size()) {
return this.groups.get(groupIndex).displayName;
}
return null;
}
2024-07-26 18:44:54 +03:00
public boolean isExpanded(int groupIndex) {
2024-08-17 23:24:27 +03:00
if (groupIndex < this.groups.size()) {
2024-07-26 18:44:54 +03:00
return this.groups.get(groupIndex).expanded;
}
return true;
}
public void setExpanded(int groupIndex, boolean expanded) {
2024-08-17 23:24:27 +03:00
if (groupIndex < this.groups.size()) {
2024-07-26 18:44:54 +03:00
this.groups.get(groupIndex).expanded = expanded;
2024-08-17 23:24:27 +03:00
saveStates();
2024-07-26 18:44:54 +03:00
}
}
public void toggleGroups(Boolean expanded) {
if (expanded == null) {
2024-08-29 23:14:52 +03:00
expanded = this.groups.stream().noneMatch(g -> g.expanded);
2024-07-26 18:44:54 +03:00
}
2024-08-17 23:24:27 +03:00
for (GroupItem group : this.groups) {
2024-07-26 18:44:54 +03:00
group.expanded = expanded;
}
2024-08-17 23:24:27 +03:00
saveStates();
}
private void saveStates() {
NBTTagCompound list = new NBTTagCompound();
for (GroupItem group : this.groups) {
list.setBoolean(group.guid, group.expanded);
}
2024-08-29 23:14:52 +03:00
NEIClientConfig.world.nbt.setTag(STATE_KEY, list);
2024-07-26 18:44:54 +03:00
}
}