2021-11-22 05:31:02 +02:00
|
|
|
package codechicken.nei.recipe;
|
|
|
|
|
|
2023-09-25 00:34:42 +02:00
|
|
|
import codechicken.nei.NEIClientConfig;
|
|
|
|
|
import codechicken.nei.api.IStackStringifyHandler;
|
|
|
|
|
import codechicken.nei.recipe.stackinfo.DefaultStackStringifyHandler;
|
|
|
|
|
import codechicken.nei.recipe.stackinfo.GTFluidStackStringifyHandler;
|
|
|
|
|
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;
|
|
|
|
|
|
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;
|
2023-01-28 17:55:51 -08:00
|
|
|
|
2022-06-24 12:32:13 +01:00
|
|
|
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-06-24 12:32:13 +01:00
|
|
|
|
2022-01-02 23:30:54 +02:00
|
|
|
static {
|
|
|
|
|
stackStringifyHandlers.add(new DefaultStackStringifyHandler());
|
|
|
|
|
stackStringifyHandlers.add(new GTFluidStackStringifyHandler());
|
|
|
|
|
}
|
|
|
|
|
|
2022-06-24 12:32:13 +01:00
|
|
|
public static NBTTagCompound itemStackToNBT(ItemStack stack) {
|
2022-01-02 23:30:54 +02:00
|
|
|
return itemStackToNBT(stack, true);
|
|
|
|
|
}
|
|
|
|
|
|
2022-06-24 12:32:13 +01:00
|
|
|
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;
|
|
|
|
|
}
|
|
|
|
|
|
2022-06-24 12:32:13 +01:00
|
|
|
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;
|
|
|
|
|
}
|
|
|
|
|
|
2022-06-24 12:32:13 +01:00
|
|
|
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);
|
|
|
|
|
|
2022-06-24 12:32:13 +01:00
|
|
|
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
|
|
|
}
|
|
|
|
|
|
2022-06-24 12:32:13 +01: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
|
|
|
}
|
|
|
|
|
|
2022-06-24 12:32:13 +01: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) {
|
2022-06-24 12:32:13 +01:00
|
|
|
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);
|
|
|
|
|
}
|
|
|
|
|
|
2022-06-24 12:32:13 +01:00
|
|
|
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 {
|
2023-01-25 19:50:48 +00:00
|
|
|
final URL filterUrl = StackInfo.class.getResource("/assets/nei/guidfilters.cfg");
|
2022-06-24 12:32:13 +01:00
|
|
|
|
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();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2022-06-24 12:32:13 +01:00
|
|
|
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-06-24 12:32:13 +01:00
|
|
|
|
2022-02-06 17:42:11 +02:00
|
|
|
guidfilters.put(parts[0], rules);
|
|
|
|
|
}
|
|
|
|
|
}
|
2022-08-19 15:03:42 +03:00
|
|
|
|
|
|
|
|
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
|
|
|
}
|