2022-01-02 23:30:54 +02:00
|
|
|
package codechicken.nei.recipe.stackinfo;
|
|
|
|
|
|
|
|
|
|
import net.minecraft.item.Item;
|
|
|
|
|
import net.minecraft.item.ItemStack;
|
|
|
|
|
import net.minecraft.nbt.NBTTagCompound;
|
|
|
|
|
import net.minecraftforge.fluids.FluidContainerRegistry;
|
|
|
|
|
import net.minecraftforge.fluids.FluidStack;
|
|
|
|
|
import net.minecraftforge.fluids.IFluidContainerItem;
|
|
|
|
|
|
2023-09-25 00:35:24 +02:00
|
|
|
import codechicken.nei.api.IStackStringifyHandler;
|
|
|
|
|
import cpw.mods.fml.common.registry.GameData;
|
|
|
|
|
|
2022-06-24 12:32:13 +01:00
|
|
|
public class DefaultStackStringifyHandler implements IStackStringifyHandler {
|
2022-01-02 23:30:54 +02:00
|
|
|
|
2022-06-24 12:32:13 +01:00
|
|
|
public NBTTagCompound convertItemStackToNBT(ItemStack stack, boolean saveStackSize) {
|
2022-01-07 19:06:05 +02:00
|
|
|
final String strId = Item.itemRegistry.getNameForObject(stack.getItem());
|
|
|
|
|
|
|
|
|
|
if (strId == null) {
|
|
|
|
|
return null;
|
|
|
|
|
}
|
|
|
|
|
|
2022-01-02 23:30:54 +02:00
|
|
|
final NBTTagCompound nbTag = new NBTTagCompound();
|
2022-01-07 19:06:05 +02:00
|
|
|
nbTag.setString("strId", strId);
|
2024-02-04 14:09:24 +02:00
|
|
|
nbTag.setInteger("Count", saveStackSize ? stack.stackSize : 1);
|
2022-06-24 12:32:13 +01:00
|
|
|
nbTag.setShort("Damage", (short) stack.getItemDamage());
|
|
|
|
|
|
2025-03-24 16:48:11 +02:00
|
|
|
if (stack.hasTagCompound() && !stack.getTagCompound().hasNoTags()) {
|
2025-09-23 06:28:05 +03:00
|
|
|
nbTag.setTag("tag", stack.getTagCompound().copy());
|
2022-01-02 23:30:54 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return nbTag;
|
|
|
|
|
}
|
|
|
|
|
|
2022-06-24 12:32:13 +01:00
|
|
|
public ItemStack convertNBTToItemStack(NBTTagCompound nbtTag) {
|
2022-01-02 23:30:54 +02:00
|
|
|
final String strId = nbtTag.getString("strId");
|
|
|
|
|
|
2022-06-24 12:32:13 +01:00
|
|
|
nbtTag = (NBTTagCompound) nbtTag.copy();
|
2023-10-15 16:12:39 +09:00
|
|
|
nbtTag.setShort("id", (short) GameData.getItemRegistry().getId(strId)); // getObject
|
2022-01-02 23:30:54 +02:00
|
|
|
|
|
|
|
|
final ItemStack stack = ItemStack.loadItemStackFromNBT(nbtTag);
|
|
|
|
|
|
|
|
|
|
if (stack != null) {
|
|
|
|
|
stack.stackSize = nbtTag.getInteger("Count");
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return stack;
|
|
|
|
|
}
|
|
|
|
|
|
2022-06-24 12:32:13 +01:00
|
|
|
public FluidStack getFluid(ItemStack stack) {
|
2022-01-02 23:30:54 +02:00
|
|
|
FluidStack fluidStack = FluidContainerRegistry.getFluidForFilledItem(stack);
|
|
|
|
|
|
|
|
|
|
if (fluidStack == null && stack.getItem() instanceof IFluidContainerItem) {
|
|
|
|
|
fluidStack = ((IFluidContainerItem) stack.getItem()).getFluid(stack);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return fluidStack;
|
|
|
|
|
}
|
2025-06-22 18:49:20 +03:00
|
|
|
|
2022-01-02 23:30:54 +02:00
|
|
|
}
|