mirror of
https://github.com/GTNewHorizons/TinkersConstruct.git
synced 2026-11-21 04:32:26 +00:00
Implement IFluidContainerItem for LavaTankItemBlock (#46)
* Update build.gradle * Implement IFluidContainerItem for LavaTankItemBlock * Drop the tag compound from ItemStack when no any other tag
This commit is contained in:
1 parent
e77d237649
commit
e8ee8b10f7
4 files changed
+237
-109
No files matched your search
@@ -1,26 +1,33 @@
|
||||
package tconstruct.smeltery.blocks;
|
||||
|
||||
import cpw.mods.fml.relauncher.*;
|
||||
import java.util.List;
|
||||
import cpw.mods.fml.relauncher.Side;
|
||||
import cpw.mods.fml.relauncher.SideOnly;
|
||||
import mantle.blocks.iface.IServantLogic;
|
||||
import net.minecraft.block.*;
|
||||
import net.minecraft.block.Block;
|
||||
import net.minecraft.block.BlockContainer;
|
||||
import net.minecraft.block.material.Material;
|
||||
import net.minecraft.client.renderer.texture.IIconRegister;
|
||||
import net.minecraft.creativetab.CreativeTabs;
|
||||
import net.minecraft.entity.EntityLivingBase;
|
||||
import net.minecraft.entity.item.EntityItem;
|
||||
import net.minecraft.entity.player.EntityPlayer;
|
||||
import net.minecraft.item.*;
|
||||
import net.minecraft.nbt.NBTTagCompound;
|
||||
import net.minecraft.item.Item;
|
||||
import net.minecraft.item.ItemStack;
|
||||
import net.minecraft.tileentity.TileEntity;
|
||||
import net.minecraft.util.IIcon;
|
||||
import net.minecraft.world.*;
|
||||
import net.minecraft.world.IBlockAccess;
|
||||
import net.minecraft.world.World;
|
||||
import net.minecraftforge.common.util.ForgeDirection;
|
||||
import net.minecraftforge.fluids.*;
|
||||
import net.minecraftforge.fluids.FluidContainerRegistry;
|
||||
import net.minecraftforge.fluids.FluidStack;
|
||||
import net.minecraftforge.fluids.FluidTankInfo;
|
||||
import tconstruct.library.TConstructRegistry;
|
||||
import tconstruct.smeltery.itemblocks.LavaTankItemBlock;
|
||||
import tconstruct.smeltery.logic.LavaTankLogic;
|
||||
import tconstruct.smeltery.model.TankRender;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
public class LavaTankBlock extends BlockContainer
|
||||
{
|
||||
public IIcon[] icons;
|
||||
@@ -274,11 +281,8 @@ public class LavaTankBlock extends BlockContainer
|
||||
FluidStack liquid = logic.tank.getFluid();
|
||||
if (liquid != null)
|
||||
{
|
||||
NBTTagCompound tag = new NBTTagCompound();
|
||||
NBTTagCompound liquidTag = new NBTTagCompound();
|
||||
liquid.writeToNBT(liquidTag);
|
||||
tag.setTag("Fluid", liquidTag);
|
||||
stack.setTagCompound(tag);
|
||||
LavaTankItemBlock lavaTankItemBlock = (LavaTankItemBlock) stack.getItem();
|
||||
lavaTankItemBlock.setFluid(stack, liquid);
|
||||
}
|
||||
if (!player.capabilities.isCreativeMode || player.isSneaking())
|
||||
dropTankBlock(world, x, y, z, stack);
|
||||
@@ -309,15 +313,11 @@ public class LavaTankBlock extends BlockContainer
|
||||
@Override
|
||||
public void onBlockPlacedBy (World world, int x, int y, int z, EntityLivingBase living, ItemStack stack)
|
||||
{
|
||||
if (stack.hasTagCompound())
|
||||
{
|
||||
NBTTagCompound liquidTag = stack.getTagCompound().getCompoundTag("Fluid");
|
||||
if (liquidTag != null)
|
||||
{
|
||||
FluidStack liquid = FluidStack.loadFluidStackFromNBT(liquidTag);
|
||||
LavaTankLogic logic = (LavaTankLogic) world.getTileEntity(x, y, z);
|
||||
logic.tank.setFluid(liquid);
|
||||
}
|
||||
LavaTankItemBlock lavaTankItemBlock = (LavaTankItemBlock) stack.getItem();
|
||||
FluidStack liquid = lavaTankItemBlock.getFluid(stack);
|
||||
if (liquid != null) {
|
||||
LavaTankLogic logic = (LavaTankLogic) world.getTileEntity(x, y, z);
|
||||
logic.tank.setFluid(liquid);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -339,4 +339,4 @@ public class LavaTankBlock extends BlockContainer
|
||||
{
|
||||
return (LavaTankLogic) blockAccess.getTileEntity(par1, par2, par3);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -1,14 +1,18 @@
|
||||
package tconstruct.smeltery.itemblocks;
|
||||
|
||||
import java.util.List;
|
||||
import mantle.blocks.abstracts.MultiItemBlock;
|
||||
import net.minecraft.block.Block;
|
||||
import net.minecraft.entity.player.EntityPlayer;
|
||||
import net.minecraft.item.ItemStack;
|
||||
import net.minecraft.nbt.NBTTagCompound;
|
||||
import net.minecraft.util.StatCollector;
|
||||
import net.minecraftforge.fluids.FluidStack;
|
||||
import net.minecraftforge.fluids.IFluidContainerItem;
|
||||
import tconstruct.smeltery.logic.LavaTankLogic;
|
||||
|
||||
public class LavaTankItemBlock extends MultiItemBlock
|
||||
import java.util.List;
|
||||
|
||||
public class LavaTankItemBlock extends MultiItemBlock implements IFluidContainerItem
|
||||
{
|
||||
public static final String blockTypes[] = { "Tank", "Gague", "Window" };
|
||||
|
||||
@@ -37,4 +41,101 @@ public class LavaTankItemBlock extends MultiItemBlock
|
||||
list.add(StatCollector.translateToLocal("searedtank2.tooltip"));
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* @param container
|
||||
* ItemStack which is the fluid container.
|
||||
* @return FluidStack representing the fluid in the container, null if the container is empty.
|
||||
*/
|
||||
@Override
|
||||
public FluidStack getFluid(ItemStack container) {
|
||||
if (container.hasTagCompound()) {
|
||||
NBTTagCompound nbt = container.getTagCompound();
|
||||
if (nbt.hasKey("Fluid")) {
|
||||
return FluidStack.loadFluidStackFromNBT(nbt.getCompoundTag("Fluid"));
|
||||
}
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param container
|
||||
* ItemStack which is the fluid container.
|
||||
* @param resource
|
||||
* FluidStack attempting to set to the container.
|
||||
*/
|
||||
public void setFluid(ItemStack container, FluidStack resource) {
|
||||
if (container == null) return;
|
||||
if (resource != null && 0 < resource.amount) {
|
||||
NBTTagCompound nbt = container.getTagCompound() != null ? container.getTagCompound() : new NBTTagCompound();
|
||||
nbt.setTag("Fluid", resource.writeToNBT(new NBTTagCompound()));
|
||||
container.setTagCompound(nbt);
|
||||
return;
|
||||
}
|
||||
NBTTagCompound nbt = container.getTagCompound();
|
||||
if (nbt == null) return;
|
||||
nbt.removeTag("Fluid");
|
||||
if (nbt.hasNoTags()) nbt = null;
|
||||
container.setTagCompound(nbt);
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* @param container
|
||||
* ItemStack which is the fluid container.
|
||||
* @return Capacity of this fluid container.
|
||||
*/
|
||||
@Override
|
||||
public int getCapacity(ItemStack container) {
|
||||
return LavaTankLogic.tankCapacity;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param container
|
||||
* ItemStack which is the fluid container.
|
||||
* @param resource
|
||||
* FluidStack attempting to fill the container.
|
||||
* @param doFill
|
||||
* If false, the fill will only be simulated.
|
||||
* @return Amount of fluid that was (or would have been, if simulated) filled into the
|
||||
* container.
|
||||
*/
|
||||
@Override
|
||||
public int fill(ItemStack container, FluidStack resource, boolean doFill) {
|
||||
if (container.stackSize != 1) return 0;
|
||||
if (resource == null || resource.amount <= 0) return 0;
|
||||
FluidStack fluidStack = this.getFluid(container);
|
||||
if (fluidStack == null) fluidStack = new FluidStack(resource, 0);
|
||||
if (!fluidStack.isFluidEqual(resource)) return 0;
|
||||
int amount = Math.min(this.getCapacity(container) - fluidStack.amount, resource.amount);
|
||||
if (doFill && amount > 0) {
|
||||
fluidStack.amount += amount;
|
||||
this.setFluid(container, fluidStack);
|
||||
}
|
||||
return amount;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param container
|
||||
* ItemStack which is the fluid container.
|
||||
* @param maxDrain
|
||||
* Maximum amount of fluid to be removed from the container.
|
||||
* @param doDrain
|
||||
* If false, the drain will only be simulated.
|
||||
* @return Amount of fluid that was (or would have been, if simulated) drained from the
|
||||
* container.
|
||||
*/
|
||||
@Override
|
||||
public FluidStack drain(ItemStack container, int maxDrain, boolean doDrain) {
|
||||
if (container.stackSize != 1) return null;
|
||||
FluidStack fluidStack = this.getFluid(container);
|
||||
if (fluidStack == null || fluidStack.amount <= 0) return null;
|
||||
int drain = Math.min(fluidStack.amount, maxDrain);
|
||||
if (drain <= 0) return null;
|
||||
if (doDrain) {
|
||||
fluidStack.amount -= drain;
|
||||
this.setFluid(container, fluidStack);
|
||||
}
|
||||
return new FluidStack(fluidStack, drain);
|
||||
}
|
||||
}
|
||||
@@ -11,10 +11,11 @@ public class LavaTankLogic extends MultiServantLogic implements IFluidHandler
|
||||
{
|
||||
public FluidTank tank;
|
||||
public int renderOffset;
|
||||
public final static int tankCapacity = FluidContainerRegistry.BUCKET_VOLUME * 4;
|
||||
|
||||
public LavaTankLogic()
|
||||
{
|
||||
tank = new FluidTank(FluidContainerRegistry.BUCKET_VOLUME * 4);
|
||||
tank = new FluidTank(tankCapacity);
|
||||
}
|
||||
|
||||
@Override
|
||||
@@ -80,7 +81,7 @@ public class LavaTankLogic extends MultiServantLogic implements IFluidHandler
|
||||
/*
|
||||
* @Override public IFluidTank[] getTanks (ForgeDirection direction) {
|
||||
* return new IFluidTank[] { tank }; }
|
||||
*
|
||||
*
|
||||
* @Override public IFluidTank getTank (ForgeDirection direction, FluidStack
|
||||
* type) { return tank; }
|
||||
*/
|
||||
@@ -193,4 +194,4 @@ public class LavaTankLogic extends MultiServantLogic implements IFluidHandler
|
||||
{
|
||||
return 15 * tank.getFluidAmount() / tank.getCapacity();
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user