use of net.minecraftforge.fluids.IFluidContainerItem in project PneumaticCraft by MineMaarten.
the class FluidUtils method tryExtractingLiquid.
public static boolean tryExtractingLiquid(TileEntity te, ItemStack liquidContainer, List<ItemStack> returnedItems) {
if (te instanceof IFluidHandler) {
IFluidHandler fluidHandler = (IFluidHandler) te;
if (liquidContainer != null) {
int containerCapacity = FluidContainerRegistry.getContainerCapacity(liquidContainer);
if (containerCapacity > 0 || liquidContainer.getItem() == Items.bucket) {
if (containerCapacity == 0)
containerCapacity = 1000;
FluidStack extractedLiquid = fluidHandler.drain(ForgeDirection.UNKNOWN, containerCapacity, false);
if (extractedLiquid != null && extractedLiquid.amount == containerCapacity) {
ItemStack filledContainer = FluidContainerRegistry.fillFluidContainer(extractedLiquid, liquidContainer);
if (filledContainer != null) {
fluidHandler.drain(ForgeDirection.UNKNOWN, containerCapacity, true);
liquidContainer.stackSize--;
returnedItems.add(filledContainer.copy());
return true;
}
}
} else if (liquidContainer.getItem() instanceof IFluidContainerItem) {
IFluidContainerItem container = (IFluidContainerItem) liquidContainer.getItem();
ItemStack singleItem = liquidContainer.copy();
singleItem.stackSize = 1;
FluidStack extractedLiquid = fluidHandler.drain(ForgeDirection.UNKNOWN, container.getCapacity(singleItem), false);
if (extractedLiquid != null) {
int filledAmount = container.fill(singleItem, extractedLiquid, true);
if (filledAmount > 0) {
liquidContainer.stackSize--;
returnedItems.add(singleItem);
FluidStack fluid = extractedLiquid.copy();
fluid.amount = filledAmount;
fluidHandler.drain(ForgeDirection.UNKNOWN, fluid, true);
return true;
}
}
}
}
}
return false;
}
use of net.minecraftforge.fluids.IFluidContainerItem in project PneumaticCraft by MineMaarten.
the class TileEntityBase method processFluidItem.
protected void processFluidItem(int inputSlot, int outputSlot) {
IInventory inv = (IInventory) this;
IFluidHandler fluidHandler = (IFluidHandler) this;
FluidTankInfo tankInfo = fluidHandler.getTankInfo(ForgeDirection.UNKNOWN)[0];
if (inv.getStackInSlot(inputSlot) != null) {
ItemStack fluidContainer = inv.getStackInSlot(inputSlot);
if (tankInfo.fluid == null || tankInfo.fluid.isFluidEqual(fluidContainer)) {
FluidStack fluid = FluidContainerRegistry.getFluidForFilledItem(fluidContainer);
int amount = FluidContainerRegistry.BUCKET_VOLUME;
if (fluid == null) {
if (fluidContainer.getItem() instanceof IFluidContainerItem) {
IFluidContainerItem containerItem = (IFluidContainerItem) fluidContainer.getItem();
fluid = containerItem.getFluid(fluidContainer);
if (fluid != null && fluidHandler.canFill(ForgeDirection.UNKNOWN, fluid.getFluid())) {
amount = fluid != null ? fluid.amount : 0;
int availableSpace = tankInfo.capacity - (tankInfo.fluid != null ? tankInfo.fluid.amount : 0);
if (availableSpace >= amount) {
ItemStack singleFuelItem = fluidContainer.copy();
singleFuelItem.stackSize = 1;
FluidStack drainedStack = containerItem.drain(singleFuelItem, availableSpace, true);
if (fluidContainer.stackSize == 1 || inv.getStackInSlot(outputSlot) == null || canStack(singleFuelItem, inv.getStackInSlot(outputSlot))) {
fluidHandler.fill(ForgeDirection.UNKNOWN, drainedStack, true);
if (fluidContainer.stackSize == 1) {
inv.setInventorySlotContents(inputSlot, singleFuelItem);
} else {
inv.getStackInSlot(inputSlot).stackSize--;
if (inv.getStackInSlot(outputSlot) == null) {
inv.setInventorySlotContents(outputSlot, singleFuelItem);
} else {
inv.getStackInSlot(outputSlot).stackSize++;
}
}
}
}
}
}
} else if (fluidHandler.canFill(ForgeDirection.UNKNOWN, fluid.getFluid())) {
if (tankInfo.capacity - (tankInfo.fluid != null ? tankInfo.fluid.amount : 0) >= amount) {
ItemStack returnedItem = null;
FluidContainerData[] allFluidData = FluidContainerRegistry.getRegisteredFluidContainerData();
for (FluidContainerData fluidData : allFluidData) {
if (fluidData.filledContainer.isItemEqual(fluidContainer)) {
returnedItem = fluidData.emptyContainer;
break;
}
}
if (returnedItem == null || inv.getStackInSlot(outputSlot) == null || canStack(returnedItem, inv.getStackInSlot(outputSlot))) {
if (returnedItem != null) {
if (inv.getStackInSlot(outputSlot) == null) {
inv.setInventorySlotContents(outputSlot, returnedItem.copy());
} else {
inv.getStackInSlot(outputSlot).stackSize += returnedItem.stackSize;
}
}
fluidHandler.fill(ForgeDirection.UNKNOWN, new FluidStack(fluid.getFluid(), amount, fluid.tag), true);
inv.getStackInSlot(inputSlot).stackSize--;
if (inv.getStackInSlot(inputSlot).stackSize <= 0)
inv.setInventorySlotContents(inputSlot, null);
}
}
}
}
if (fluidContainer.getItem() instanceof IFluidContainerItem) {
if (((IFluidContainerItem) fluidContainer.getItem()).getFluid(fluidContainer) == null && (inv.getStackInSlot(outputSlot) == null || canStack(fluidContainer, inv.getStackInSlot(outputSlot)))) {
if (inv.getStackInSlot(outputSlot) == null) {
inv.setInventorySlotContents(outputSlot, fluidContainer);
} else {
inv.getStackInSlot(outputSlot).stackSize += fluidContainer.stackSize;
}
inv.setInventorySlotContents(inputSlot, null);
}
}
}
}
use of net.minecraftforge.fluids.IFluidContainerItem in project PneumaticCraft by MineMaarten.
the class FluidUtils method tryInsertingLiquid.
public static boolean tryInsertingLiquid(TileEntity te, ItemStack liquidContainer, boolean creative, List<ItemStack> returnedItems) {
if (te instanceof IFluidHandler) {
IFluidHandler fluidHandler = (IFluidHandler) te;
if (liquidContainer != null) {
FluidStack fluid = FluidContainerRegistry.getFluidForFilledItem(liquidContainer);
if (fluid != null) {
fluid.amount = 1000;
if (fluidHandler.canFill(ForgeDirection.UNKNOWN, fluid.getFluid()) && fluidHandler.fill(ForgeDirection.UNKNOWN, fluid, false) == 1000) {
fluidHandler.fill(ForgeDirection.UNKNOWN, fluid, true);
if (!creative) {
liquidContainer.stackSize--;
ItemStack returnedItem = null;
FluidContainerData[] allFluidData = FluidContainerRegistry.getRegisteredFluidContainerData();
for (FluidContainerData fluidData : allFluidData) {
if (fluidData.filledContainer.isItemEqual(liquidContainer)) {
returnedItem = fluidData.emptyContainer;
break;
}
}
if (returnedItem != null) {
returnedItems.add(returnedItem.copy());
}
}
return true;
}
} else if (liquidContainer.getItem() instanceof IFluidContainerItem) {
IFluidContainerItem container = (IFluidContainerItem) liquidContainer.getItem();
fluid = container.getFluid(liquidContainer);
if (fluid != null) {
fluid = fluid.copy();
if (fluidHandler.canFill(ForgeDirection.UNKNOWN, fluid.getFluid()) && fluidHandler.fill(ForgeDirection.UNKNOWN, fluid, false) == fluid.amount) {
ItemStack returnedItem = liquidContainer.copy();
returnedItem.stackSize = 1;
container.drain(returnedItem, fluid.amount, true);
fluidHandler.fill(ForgeDirection.UNKNOWN, fluid, true);
if (!creative) {
liquidContainer.stackSize--;
returnedItems.add(returnedItem.copy());
}
return true;
}
}
}
}
}
return false;
}
use of net.minecraftforge.fluids.IFluidContainerItem in project NewHorizonsCoreMod by GTNewHorizons.
the class ItemInHandInfoCommand method getFluidContainerContents.
private String getFluidContainerContents(ItemStack pItemInQuestion) {
String tResult = "No fluid container";
if (pItemInQuestion.getItem() instanceof IFluidContainerItem) {
IFluidContainerItem tFluidContainer = IFluidContainerItem.class.cast(pItemInQuestion.getItem());
FluidStack tContents = tFluidContainer.getFluid(pItemInQuestion);
if (tContents != null) {
tResult = String.format("FluidID: [%d], UnlocName: [%s], Name: [%s]", tContents.getFluid().getID(), tContents.getFluid().getUnlocalizedName(), tContents.getFluid().getName());
}
}
return tResult;
}
use of net.minecraftforge.fluids.IFluidContainerItem in project NewHorizonsCoreMod by GTNewHorizons.
the class HazardousItemsHandler method CheckInventoryForItems.
private void CheckInventoryForItems(EntityPlayer pPlayer) {
if (_mRnd.nextInt(_mExecuteChance) != 0)
return;
try {
ItemStack[] tPlayerInventory = pPlayer.inventory.mainInventory;
String tCurrIS = "";
for (ItemStack is : tPlayerInventory) {
try // Safe-loop to enforce dangerous items even if something bad
// happens here
{
if (is == null)
continue;
tCurrIS = ItemDescriptor.fromStack(is).toString();
// Check if item is a fluid container
if ((Object) is.getItem() instanceof IFluidContainerItem) {
HazardousFluid hf = _mHazardItemsCollection.FindHazardousFluid(is);
if (hf != null && hf.getCheckInventory())
DoHIEffects(hf, pPlayer);
} else // Tinkers' construct smeltery tank
if (is.getItem().getClass().getName().equals("tconstruct.smeltery.itemblocks.LavaTankItemBlock")) {
// _mLogger.info("Found lavatank");
NBTTagCompound tNBT = is.getTagCompound();
if (tNBT != null && tNBT.hasKey("Fluid")) {
// _mLogger.info("...Has NBT 'Fluid'...");
NBTTagCompound tFluidCompound = tNBT.getCompoundTag("Fluid");
if (tFluidCompound != null && tFluidCompound.hasKey("FluidName")) {
// _mLogger.info("...Has NBT 'FluidName'...");
String tFluidName = tFluidCompound.getString("FluidName");
if (tFluidName != null && tFluidName.length() > 0) {
// _mLogger.info("...Finding Hazardous Fluids...");
HazardousFluid hf = _mHazardItemsCollection.FindHazardousFluidExact(tFluidName);
if (hf != null && hf.getCheckInventory()) {
// _mLogger.info("...Found Hazardous Fluids");
DoHIEffects(hf, pPlayer);
}
// else
// _mLogger.info("...Not found Hazardous Fluids");
}
}
// else
// _mLogger.info("...Has no NBT 'FluidName'");
}
// else
// _mLogger.info("...Has no NBT 'Fluid'");
} else {
HazardousItem hi = _mHazardItemsCollection.FindHazardousItem(is);
if (hi != null && hi.getCheckInventory())
DoHIEffects(hi, pPlayer);
}
} catch (Exception e) {
_mLogger.debug(String.format("Something weird happend with item %s", tCurrIS));
// Silently catching exception and continue
continue;
}
}
} catch (Exception e) {
_mLogger.error("HazardousItemsHandler.CheckInventoryForItems.error", "Something bad happend while processing the onPlayerTick event");
e.printStackTrace();
}
}
Aggregations