Files
NotEnoughItems/src/main/java/codechicken/nei/recipe/StackInfo.java
T

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

211 lines
7.0 KiB
Java
Raw Normal View History

2021-11-22 05:31:02 +02:00
package codechicken.nei.recipe;
2022-02-06 17:42:11 +02:00
import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.URL;
2021-11-22 05:31:02 +02:00
import java.util.ArrayList;
2022-02-06 17:42:11 +02:00
import java.util.HashMap;
import java.util.List;
import java.util.WeakHashMap;
import net.minecraft.item.ItemStack;
import net.minecraft.nbt.NBTBase;
import net.minecraft.nbt.NBTTagCompound;
import net.minecraft.nbt.NBTTagList;
import net.minecraftforge.fluids.FluidStack;
import org.apache.commons.io.IOUtils;
2021-11-22 05:31:02 +02:00
2022-02-06 17:42:11 +02:00
import codechicken.nei.NEIClientConfig;
2021-11-22 05:31:02 +02:00
import codechicken.nei.api.IStackStringifyHandler;
2022-01-02 23:30:54 +02:00
import codechicken.nei.recipe.stackinfo.DefaultStackStringifyHandler;
import codechicken.nei.recipe.stackinfo.GTFluidStackStringifyHandler;
public class StackInfo {
2021-11-22 05:31:02 +02:00
2022-02-06 17:42:11 +02:00
public static final ArrayList<IStackStringifyHandler> stackStringifyHandlers = new ArrayList<>();
private static final HashMap<String, HashMap<String, String[]>> guidfilters = new HashMap<>();
private static final WeakHashMap<ItemStack, String> guidcache = new WeakHashMap<>();
2022-01-02 23:30:54 +02:00
static {
stackStringifyHandlers.add(new DefaultStackStringifyHandler());
stackStringifyHandlers.add(new GTFluidStackStringifyHandler());
}
public static NBTTagCompound itemStackToNBT(ItemStack stack) {
2022-01-02 23:30:54 +02:00
return itemStackToNBT(stack, true);
}
public static NBTTagCompound itemStackToNBT(ItemStack stack, boolean saveStackSize) {
2021-11-22 05:31:02 +02:00
NBTTagCompound nbTag = null;
for (int i = stackStringifyHandlers.size() - 1; i >= 0 && nbTag == null; i--) {
2022-01-02 23:30:54 +02:00
nbTag = stackStringifyHandlers.get(i).convertItemStackToNBT(stack, saveStackSize);
2021-11-22 05:31:02 +02:00
}
return nbTag;
}
public static ItemStack loadFromNBT(NBTTagCompound nbtTag) {
2021-11-22 05:31:02 +02:00
ItemStack stack = null;
2022-02-06 17:42:11 +02:00
for (int i = stackStringifyHandlers.size() - 1; i >= 0 && nbtTag != null && stack == null; i--) {
2021-11-22 05:31:02 +02:00
stack = stackStringifyHandlers.get(i).convertNBTToItemStack(nbtTag);
}
return stack;
}
public static boolean equalItemAndNBT(ItemStack stackA, ItemStack stackB, boolean useNBT) {
2022-01-02 23:30:54 +02:00
if (!stackA.isItemEqual(stackB)) {
return false;
}
2021-11-22 05:31:02 +02:00
2022-01-02 23:30:54 +02:00
if (useNBT) {
NBTTagCompound tagCompoundA = itemStackToNBT(stackA, false);
NBTTagCompound tagCompoundB = itemStackToNBT(stackB, false);
return tagCompoundA == null && tagCompoundB == null
|| tagCompoundA != null && tagCompoundB != null && tagCompoundA.equals(tagCompoundB);
2021-11-22 05:31:02 +02:00
}
2022-01-02 23:30:54 +02:00
return true;
2021-11-22 05:31:02 +02:00
}
public static FluidStack getFluid(ItemStack stack) {
2022-01-02 23:30:54 +02:00
FluidStack fluid = null;
2021-11-22 05:31:02 +02:00
2022-01-02 23:30:54 +02:00
for (int i = stackStringifyHandlers.size() - 1; i >= 0 && fluid == null; i--) {
fluid = stackStringifyHandlers.get(i).getFluid(stack);
2021-11-22 05:31:02 +02:00
}
2022-01-02 23:30:54 +02:00
return fluid;
2021-11-22 05:31:02 +02:00
}
public static String getItemStackGUID(ItemStack stack) {
2022-02-06 17:42:11 +02:00
if (!guidcache.containsKey(stack)) {
final NBTTagCompound nbTag = itemStackToNBT(stack, false);
if (nbTag == null) {
return null;
}
nbTag.removeTag("Count");
if (nbTag.getShort("Damage") == 0) {
nbTag.removeTag("Damage");
}
if (nbTag.hasKey("tag") && nbTag.getCompoundTag("tag").hasNoTags()) {
nbTag.removeTag("tag");
}
if (nbTag.hasKey("strId") && guidfilters.containsKey(nbTag.getString("strId"))) {
final ArrayList<String> keys = new ArrayList<>();
final String strId = nbTag.getString("strId");
keys.add(strId);
guidfilters.get(strId).forEach((key, rule) -> {
Object local = nbTag;
for (int i = 0; i < rule.length; i++) {
try {
if (local instanceof NBTTagCompound) {
local = ((NBTTagCompound) local).getTag(rule[i]);
} else if (local instanceof NBTTagList) {
local = ((NBTTagList) local).tagList.get(Integer.parseInt(rule[i]));
} else {
break;
}
} catch (Throwable e) {
break;
}
}
if (local instanceof NBTBase) {
keys.add(((NBTBase) local).toString());
2022-02-06 17:42:11 +02:00
} else if (local != null) {
keys.add(String.valueOf(local));
}
});
synchronized (guidcache) {
guidcache.put(stack, keys.toString());
}
} else {
synchronized (guidcache) {
guidcache.put(stack, nbTag.toString());
}
}
}
return guidcache.get(stack);
}
public static void loadGuidFilters() {
2022-02-06 17:42:11 +02:00
guidfilters.clear();
final File guidFlitersFile = new File(NEIClientConfig.configDir, "guidfilters.cfg");
List<String> itemStrings = new ArrayList<>();
if (guidFlitersFile.exists()) {
try (FileReader reader = new FileReader(guidFlitersFile)) {
NEIClientConfig.logger.info("Loading guid filters from file {}", guidFlitersFile);
itemStrings = IOUtils.readLines(reader);
} catch (IOException e) {
NEIClientConfig.logger.error("Failed to load bookmarks from file {}", guidFlitersFile, e);
e.printStackTrace();
}
} else {
final URL filterUrl = StackInfo.class.getResource("/assets/nei/guidfilters.cfg");
2022-02-06 17:42:11 +02:00
try (BufferedReader reader = new BufferedReader(new InputStreamReader(filterUrl.openStream()))) {
itemStrings = IOUtils.readLines(reader);
} catch (IOException e) {
NEIClientConfig.logger.info("Error parsing guid filters");
e.printStackTrace();
} catch (Exception e) {
NEIClientConfig.logger.info("Error parsing guid filters");
e.printStackTrace();
}
}
for (String guidStr : itemStrings) {
2022-02-06 17:42:11 +02:00
final String[] parts = guidStr.split(",");
final HashMap<String, String[]> rules = new HashMap<>();
for (int j = 1; j < parts.length; j++) {
rules.put(parts[j], parts[j].split("\\."));
}
2022-02-06 17:42:11 +02:00
guidfilters.put(parts[0], rules);
}
}
public static ItemStack getItemStackWithMinimumDamage(ItemStack[] stacks) {
int damage = Short.MAX_VALUE;
ItemStack result = stacks[0];
if (stacks.length > 1) {
for (ItemStack stack : stacks) {
if (stack.getItem() != null && stack.getItemDamage() < damage) {
damage = stack.getItemDamage();
result = stack;
}
}
}
return result.copy();
}
2021-11-22 05:31:02 +02:00
}