2021-11-22 05:31:02 +02:00
|
|
|
package codechicken.nei.recipe;
|
|
|
|
|
|
|
|
|
|
import java.util.ArrayList;
|
2022-02-06 17:42:11 +02:00
|
|
|
import java.util.HashMap;
|
2024-02-04 14:09:24 +02:00
|
|
|
import java.util.LinkedHashMap;
|
|
|
|
|
import java.util.Map;
|
2023-01-28 17:55:51 -08:00
|
|
|
|
2023-09-25 00:35:24 +02:00
|
|
|
import net.minecraft.item.ItemStack;
|
|
|
|
|
import net.minecraft.nbt.NBTBase;
|
|
|
|
|
import net.minecraft.nbt.NBTTagCompound;
|
|
|
|
|
import net.minecraft.nbt.NBTTagList;
|
2024-02-04 14:09:24 +02:00
|
|
|
import net.minecraftforge.fluids.FluidContainerRegistry;
|
2024-12-31 20:33:21 +02:00
|
|
|
import net.minecraftforge.fluids.FluidRegistry;
|
2023-09-25 00:35:24 +02:00
|
|
|
import net.minecraftforge.fluids.FluidStack;
|
2024-02-04 14:09:24 +02:00
|
|
|
import net.minecraftforge.fluids.IFluidContainerItem;
|
2023-09-25 00:35:24 +02:00
|
|
|
|
2024-08-29 23:14:52 +03:00
|
|
|
import codechicken.nei.ClientHandler;
|
2024-07-26 18:44:54 +03:00
|
|
|
import codechicken.nei.ItemStackMap;
|
2023-09-25 00:35:24 +02:00
|
|
|
import codechicken.nei.api.IStackStringifyHandler;
|
|
|
|
|
import codechicken.nei.recipe.stackinfo.DefaultStackStringifyHandler;
|
|
|
|
|
import codechicken.nei.recipe.stackinfo.GTFluidStackStringifyHandler;
|
2024-12-31 20:33:21 +02:00
|
|
|
import codechicken.nei.util.ItemStackKey;
|
2023-09-25 00:35:24 +02:00
|
|
|
|
2022-06-24 12:32:13 +01:00
|
|
|
public class StackInfo {
|
2021-11-22 05:31:02 +02:00
|
|
|
|
2024-12-31 20:33:21 +02:00
|
|
|
private static final FluidStack NULL_FLUID = new FluidStack(FluidRegistry.WATER, 0);
|
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<>();
|
2024-07-26 18:44:54 +03:00
|
|
|
private static final ItemStackMap<String> guidcache = new ItemStackMap<>();
|
2024-12-31 20:33:21 +02:00
|
|
|
private static final LinkedHashMap<ItemStackKey, FluidStack> fluidcache = new LinkedHashMap<>() {
|
2024-08-29 20:07:14 +02:00
|
|
|
|
|
|
|
|
private static final long serialVersionUID = 1042213947848622164L;
|
2024-02-04 14:09:24 +02:00
|
|
|
|
|
|
|
|
@Override
|
2024-12-31 20:33:21 +02:00
|
|
|
protected boolean removeEldestEntry(Map.Entry<ItemStackKey, FluidStack> eldest) {
|
|
|
|
|
return size() > 200;
|
2024-02-04 14:09:24 +02:00
|
|
|
}
|
|
|
|
|
};
|
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;
|
|
|
|
|
}
|
|
|
|
|
|
2024-02-04 14:09:24 +02:00
|
|
|
public static ItemStack loadFromNBT(NBTTagCompound nbtTag, long customCount) {
|
|
|
|
|
|
|
|
|
|
if (nbtTag != null) {
|
|
|
|
|
nbtTag = (NBTTagCompound) nbtTag.copy();
|
|
|
|
|
nbtTag.setInteger("Count", (int) Math.max(Math.min(customCount, Integer.MAX_VALUE), 0));
|
|
|
|
|
return loadFromNBT(nbtTag);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return null;
|
|
|
|
|
}
|
|
|
|
|
|
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;
|
|
|
|
|
|
2024-02-04 14:09:24 +02:00
|
|
|
if (nbtTag != null) {
|
|
|
|
|
for (int i = stackStringifyHandlers.size() - 1; i >= 0 && stack == null; i--) {
|
|
|
|
|
stack = stackStringifyHandlers.get(i).convertNBTToItemStack(nbtTag);
|
|
|
|
|
}
|
2021-11-22 05:31:02 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
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
|
|
|
}
|
|
|
|
|
|
2024-12-31 20:33:21 +02:00
|
|
|
public static synchronized FluidStack getFluid(ItemStack stack) {
|
|
|
|
|
ItemStackKey key = new ItemStackKey(stack);
|
|
|
|
|
FluidStack fluid = fluidcache.get(key);
|
2024-02-04 14:09:24 +02:00
|
|
|
|
2024-12-31 20:33:21 +02:00
|
|
|
if (fluid == null) {
|
2024-02-04 14:09:24 +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
|
|
|
|
2024-12-31 20:33:21 +02:00
|
|
|
fluidcache.put(key, fluid == null ? NULL_FLUID : fluid);
|
2021-11-22 05:31:02 +02:00
|
|
|
}
|
|
|
|
|
|
2024-12-31 20:33:21 +02:00
|
|
|
return fluid == NULL_FLUID ? null : fluid;
|
2021-11-22 05:31:02 +02:00
|
|
|
}
|
|
|
|
|
|
2024-02-04 14:09:24 +02:00
|
|
|
public static boolean isFluidContainer(ItemStack stack) {
|
|
|
|
|
return stack.getItem() instanceof IFluidContainerItem || FluidContainerRegistry.isContainer(stack);
|
|
|
|
|
}
|
|
|
|
|
|
2022-06-24 12:32:13 +01:00
|
|
|
public static String getItemStackGUID(ItemStack stack) {
|
2024-07-26 18:44:54 +03:00
|
|
|
String guid = guidcache.get(stack);
|
2022-02-06 17:42:11 +02:00
|
|
|
|
2024-07-26 18:44:54 +03:00
|
|
|
if (guid != null) {
|
|
|
|
|
return guid;
|
|
|
|
|
}
|
2022-02-06 17:42:11 +02:00
|
|
|
|
2024-07-26 18:44:54 +03:00
|
|
|
final NBTTagCompound nbTag = itemStackToNBT(stack, false);
|
2022-02-06 17:42:11 +02:00
|
|
|
|
2024-07-26 18:44:54 +03:00
|
|
|
if (nbTag == null) {
|
|
|
|
|
return null;
|
|
|
|
|
}
|
2022-02-06 17:42:11 +02:00
|
|
|
|
2024-07-26 18:44:54 +03:00
|
|
|
nbTag.removeTag("Count");
|
2022-02-06 17:42:11 +02:00
|
|
|
|
2024-07-26 18:44:54 +03:00
|
|
|
if (nbTag.getShort("Damage") == 0) {
|
|
|
|
|
nbTag.removeTag("Damage");
|
|
|
|
|
}
|
2022-02-06 17:42:11 +02:00
|
|
|
|
2024-07-26 18:44:54 +03:00
|
|
|
if (nbTag.hasKey("tag") && nbTag.getCompoundTag("tag").hasNoTags()) {
|
|
|
|
|
nbTag.removeTag("tag");
|
|
|
|
|
}
|
2022-02-06 17:42:11 +02:00
|
|
|
|
2024-07-26 18:44:54 +03:00
|
|
|
if (nbTag.hasKey("strId") && guidfilters.containsKey(nbTag.getString("strId"))) {
|
|
|
|
|
final ArrayList<String> keys = new ArrayList<>();
|
|
|
|
|
final String strId = nbTag.getString("strId");
|
2022-02-06 17:42:11 +02:00
|
|
|
|
2024-07-26 18:44:54 +03:00
|
|
|
keys.add(strId);
|
2022-02-06 17:42:11 +02:00
|
|
|
|
2024-07-26 18:44:54 +03:00
|
|
|
guidfilters.get(strId).forEach((key, rule) -> {
|
|
|
|
|
Object local = nbTag;
|
2022-02-06 17:42:11 +02:00
|
|
|
|
2024-07-26 18:44:54 +03:00
|
|
|
for (int i = 0; i < rule.length; i++) {
|
2022-02-06 17:42:11 +02:00
|
|
|
|
2024-07-26 18:44:54 +03:00
|
|
|
try {
|
2022-02-06 17:42:11 +02:00
|
|
|
|
2024-07-26 18:44:54 +03:00
|
|
|
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 {
|
2022-02-06 17:42:11 +02:00
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
|
2024-07-26 18:44:54 +03:00
|
|
|
} catch (Throwable e) {
|
|
|
|
|
break;
|
2022-02-06 17:42:11 +02:00
|
|
|
}
|
|
|
|
|
}
|
2024-07-26 18:44:54 +03:00
|
|
|
|
|
|
|
|
if (local instanceof NBTBase) {
|
|
|
|
|
keys.add(((NBTBase) local).toString());
|
|
|
|
|
} else if (local != null) {
|
|
|
|
|
keys.add(String.valueOf(local));
|
2022-02-06 17:42:11 +02:00
|
|
|
}
|
2024-07-26 18:44:54 +03:00
|
|
|
});
|
|
|
|
|
|
|
|
|
|
synchronized (guidcache) {
|
|
|
|
|
guidcache.put(stack, keys.toString());
|
|
|
|
|
}
|
|
|
|
|
} else {
|
|
|
|
|
synchronized (guidcache) {
|
|
|
|
|
guidcache.put(stack, nbTag.toString());
|
2022-02-06 17:42:11 +02:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
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();
|
|
|
|
|
|
2024-08-29 23:14:52 +03:00
|
|
|
ClientHandler.loadSettingsFile("guidfilters.cfg", lines -> lines.forEach(guidStr -> {
|
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);
|
2024-08-29 23:14:52 +03:00
|
|
|
}));
|
2022-02-06 17:42:11 +02:00
|
|
|
}
|
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
|
|
|
}
|