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.

235 lines
7.8 KiB
Java
Raw Normal View History

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;
2025-01-25 21:16:28 +02:00
import java.util.List;
import java.util.Map;
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;
import net.minecraftforge.fluids.FluidContainerRegistry;
2025-01-21 18:50:16 +02:00
import net.minecraftforge.fluids.FluidRegistry;
2023-09-25 00:35:24 +02:00
import net.minecraftforge.fluids.FluidStack;
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;
2025-01-25 21:16:28 +02:00
import codechicken.nei.LRUCache;
2023-09-25 00:35:24 +02:00
import codechicken.nei.api.IStackStringifyHandler;
2025-01-25 21:16:28 +02:00
import codechicken.nei.api.ItemInfo;
2023-09-25 00:35:24 +02:00
import codechicken.nei.recipe.stackinfo.DefaultStackStringifyHandler;
import codechicken.nei.recipe.stackinfo.GTFluidStackStringifyHandler;
2025-01-21 18:50:16 +02:00
import codechicken.nei.util.ItemStackKey;
2023-09-25 00:35:24 +02:00
public class StackInfo {
2021-11-22 05:31:02 +02:00
2025-01-21 18:50:16 +02:00
private static final FluidStack NULL_FLUID = new FluidStack(FluidRegistry.WATER, 0);
2025-01-25 21:16:28 +02:00
public static final List<IStackStringifyHandler> stackStringifyHandlers = new ArrayList<>();
private static final Map<String, HashMap<String, String[]>> guidfilters = new HashMap<>();
2024-07-26 18:44:54 +03:00
private static final ItemStackMap<String> guidcache = new ItemStackMap<>();
2025-01-25 21:16:28 +02:00
private static final LRUCache<ItemStackKey, FluidStack> fluidcache = new LRUCache<>(200);
2025-07-11 17:33:14 +03:00
private static boolean isPausedItemDamageSound = false;
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, 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;
}
public static ItemStack loadFromNBT(NBTTagCompound nbtTag) {
2021-11-22 05:31:02 +02:00
ItemStack stack = null;
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;
}
2025-07-11 17:33:14 +03:00
public static void pauseItemDamageSound(boolean pause) {
if (isPausedItemDamageSound != (isPausedItemDamageSound = pause)) {
for (IStackStringifyHandler handler : stackStringifyHandlers) {
handler.pauseItemDamageSound(pause);
}
2025-06-22 18:49:20 +03:00
}
}
2025-07-11 17:33:14 +03:00
public static boolean isPausedItemDamageSound() {
return isPausedItemDamageSound;
}
2025-01-25 21:16:28 +02:00
public static ItemStack withAmount(ItemStack stack, long customCount) {
if (stack == null) return null;
final NBTTagCompound nbTag = StackInfo.itemStackToNBT(stack);
return nbTag != null ? loadFromNBT(nbTag, customCount) : null;
}
public static int getAmount(ItemStack stack) {
if (stack == null) return 0;
final NBTTagCompound nbTag = StackInfo.itemStackToNBT(stack);
return nbTag != null ? nbTag.getInteger("Count") : 0;
}
public static boolean equalItemAndNBT(ItemStack stackA, ItemStack stackB, boolean useNBT) {
2025-03-24 16:48:11 +02:00
if (stackA == null || stackB == null || stackA.getItem() != stackB.getItem()) {
2022-01-02 23:30:54 +02:00
return false;
}
2021-11-22 05:31:02 +02:00
if (useNBT && stackA != stackB) {
2022-01-02 23:30:54 +02:00
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
}
2025-01-21 18:50:16 +02:00
public static synchronized FluidStack getFluid(ItemStack stack) {
ItemStackKey key = new ItemStackKey(stack);
FluidStack fluid = fluidcache.get(key);
2025-01-21 18:50:16 +02:00
if (fluid == null) {
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
2025-01-21 18:50:16 +02:00
fluidcache.put(key, fluid == null ? NULL_FLUID : fluid);
2021-11-22 05:31:02 +02:00
}
2025-01-21 18:50:16 +02:00
return fluid == NULL_FLUID ? null : fluid;
2021-11-22 05:31:02 +02:00
}
public static boolean isFluidContainer(ItemStack stack) {
return stack.getItem() instanceof IFluidContainerItem || FluidContainerRegistry.isContainer(stack);
}
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
2025-01-25 21:16:28 +02:00
if (local instanceof NBTTagCompound item) {
local = item.getTag(rule[i]);
} else if (local instanceof NBTTagList item) {
local = item.tagList.get(Integer.parseInt(rule[i]));
2024-07-26 18:44:54 +03:00
} 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
2025-01-25 21:16:28 +02:00
if (local instanceof NBTBase item) {
keys.add(item.toString());
2024-07-26 18:44:54 +03:00
} 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);
}
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-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
}
public static ItemStack getItemStackWithMinimumDamage(ItemStack[] stacks) {
int damage = Short.MAX_VALUE;
ItemStack result = stacks[0];
if (stacks.length > 1) {
for (ItemStack stack : stacks) {
2025-01-25 21:16:28 +02:00
if (stack.getItem() != null && !ItemInfo.isHidden(stack) && stack.getItemDamage() < damage) {
damage = stack.getItemDamage();
result = stack;
}
}
}
return result.copy();
}
2021-11-22 05:31:02 +02:00
}