2022-01-02 23:30:54 +02:00
|
|
|
package codechicken.nei.recipe.stackinfo;
|
|
|
|
|
|
|
|
|
|
import codechicken.nei.api.IStackStringifyHandler;
|
|
|
|
|
import cpw.mods.fml.common.registry.GameData;
|
|
|
|
|
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;
|
|
|
|
|
|
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);
|
2022-06-24 12:32:13 +01:00
|
|
|
nbTag.setInteger("Count", Math.max(saveStackSize ? stack.stackSize : 1, 1));
|
|
|
|
|
nbTag.setShort("Damage", (short) stack.getItemDamage());
|
|
|
|
|
|
2022-01-02 23:30:54 +02:00
|
|
|
if (stack.hasTagCompound()) {
|
|
|
|
|
nbTag.setTag("tag", stack.getTagCompound());
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
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();
|
|
|
|
|
nbtTag.setInteger("id", 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;
|
|
|
|
|
}
|
|
|
|
|
}
|