mirror of
https://github.com/GTNewHorizons/TinkersConstruct.git
synced 2026-11-22 13:09:53 +00:00
169 lines
5.5 KiB
Java
169 lines
5.5 KiB
Java
package tconstruct.smeltery.logic;
|
|
|
|
import net.minecraft.nbt.NBTTagCompound;
|
|
import net.minecraft.network.NetworkManager;
|
|
import net.minecraft.network.Packet;
|
|
import net.minecraft.network.play.server.S35PacketUpdateTileEntity;
|
|
import net.minecraftforge.common.util.ForgeDirection;
|
|
import net.minecraftforge.fluids.Fluid;
|
|
import net.minecraftforge.fluids.FluidContainerRegistry;
|
|
import net.minecraftforge.fluids.FluidRegistry;
|
|
import net.minecraftforge.fluids.FluidStack;
|
|
import net.minecraftforge.fluids.FluidTank;
|
|
import net.minecraftforge.fluids.FluidTankInfo;
|
|
import net.minecraftforge.fluids.IFluidHandler;
|
|
|
|
import mantle.blocks.abstracts.MultiServantLogic;
|
|
|
|
public class LavaTankLogic extends MultiServantLogic implements IFluidHandler {
|
|
|
|
public FluidTank tank;
|
|
public int renderOffset;
|
|
public static final int tankCapacity = FluidContainerRegistry.BUCKET_VOLUME * 4;
|
|
|
|
public LavaTankLogic() {
|
|
tank = new FluidTank(tankCapacity);
|
|
}
|
|
|
|
@Override
|
|
public int fill(ForgeDirection from, FluidStack resource, boolean doFill) {
|
|
int amount = tank.fill(resource, doFill);
|
|
if (amount > 0 && doFill) {
|
|
renderOffset += amount;
|
|
worldObj.markBlockForUpdate(xCoord, yCoord, zCoord);
|
|
worldObj.notifyBlocksOfNeighborChange(xCoord, yCoord, zCoord, this.getBlockType());
|
|
}
|
|
|
|
return amount;
|
|
}
|
|
|
|
@Override
|
|
public FluidStack drain(ForgeDirection from, int maxDrain, boolean doDrain) {
|
|
FluidStack amount = tank.drain(maxDrain, doDrain);
|
|
if (amount != null && doDrain) {
|
|
renderOffset = -amount.amount;
|
|
worldObj.markBlockForUpdate(xCoord, yCoord, zCoord);
|
|
worldObj.notifyBlocksOfNeighborChange(xCoord, yCoord, zCoord, this.getBlockType());
|
|
}
|
|
return amount;
|
|
}
|
|
|
|
@Override
|
|
public FluidStack drain(ForgeDirection from, FluidStack resource, boolean doDrain) {
|
|
if (tank.getFluidAmount() == 0) return null;
|
|
if (tank.getFluid().getFluid() != resource.getFluid()) return null;
|
|
|
|
// same fluid, k
|
|
return this.drain(from, resource.amount, doDrain);
|
|
}
|
|
|
|
@Override
|
|
public boolean canFill(ForgeDirection from, Fluid fluid) {
|
|
return tank.getFluidAmount() == 0
|
|
|| (tank.getFluid().getFluid() == fluid && tank.getFluidAmount() < tank.getCapacity());
|
|
}
|
|
|
|
@Override
|
|
public boolean canDrain(ForgeDirection from, Fluid fluid) {
|
|
return tank.getFluidAmount() > 0;
|
|
}
|
|
|
|
@Override
|
|
public FluidTankInfo[] getTankInfo(ForgeDirection from) {
|
|
FluidStack fluid = null;
|
|
if (tank.getFluid() != null) fluid = tank.getFluid().copy();
|
|
return new FluidTankInfo[] { new FluidTankInfo(fluid, tank.getCapacity()) };
|
|
}
|
|
|
|
/*
|
|
* @Override public IFluidTank[] getTanks (ForgeDirection direction) { return new IFluidTank[] { tank }; }
|
|
* @Override public IFluidTank getTank (ForgeDirection direction, FluidStack type) { return tank; }
|
|
*/
|
|
|
|
public float getFluidAmountScaled() {
|
|
return (float) (tank.getFluid().amount - renderOffset) / (tank.getCapacity() * 1.01F);
|
|
}
|
|
|
|
public boolean containsFluid() {
|
|
return tank.getFluid() != null;
|
|
}
|
|
|
|
public int getBrightness() {
|
|
if (containsFluid()) {
|
|
return (tank.getFluid().getFluid().getLuminosity() * tank.getFluidAmount()) / tank.getCapacity();
|
|
}
|
|
return 0;
|
|
}
|
|
|
|
@Override
|
|
public void readFromNBT(NBTTagCompound tags) {
|
|
super.readFromNBT(tags);
|
|
readCustomNBT(tags);
|
|
}
|
|
|
|
@Override
|
|
public void writeToNBT(NBTTagCompound tags) {
|
|
super.writeToNBT(tags);
|
|
writeCustomNBT(tags);
|
|
}
|
|
|
|
@Override
|
|
public void readCustomNBT(NBTTagCompound tags) {
|
|
if (tags.getBoolean("hasFluid")) {
|
|
// TODO: Removed in future Versions, backward compat.
|
|
if (tags.getInteger("itemID") != 0) {
|
|
tank.setFluid(new FluidStack(tags.getInteger("itemID"), tags.getInteger("amount")));
|
|
} else {
|
|
tank.setFluid(FluidRegistry.getFluidStack(tags.getString("fluidName"), tags.getInteger("amount")));
|
|
}
|
|
} else tank.setFluid(null);
|
|
|
|
if (tags.hasKey("renderOffset")) renderOffset = tags.getInteger("renderOffset");
|
|
}
|
|
|
|
@Override
|
|
public void writeCustomNBT(NBTTagCompound tags) {
|
|
FluidStack liquid = tank.getFluid();
|
|
tags.setBoolean("hasFluid", liquid != null);
|
|
if (liquid != null) {
|
|
tags.setString("fluidName", liquid.getFluid().getName());
|
|
tags.setInteger("amount", liquid.amount);
|
|
}
|
|
tags.setInteger("renderOffset", renderOffset);
|
|
}
|
|
|
|
/* Packets */
|
|
@Override
|
|
public Packet getDescriptionPacket() {
|
|
NBTTagCompound tag = new NBTTagCompound();
|
|
writeCustomNBT(tag);
|
|
return new S35PacketUpdateTileEntity(xCoord, yCoord, zCoord, 1, tag);
|
|
}
|
|
|
|
@Override
|
|
public void onDataPacket(NetworkManager net, S35PacketUpdateTileEntity packet) {
|
|
readCustomNBT(packet.func_148857_g());
|
|
worldObj.func_147479_m(xCoord, yCoord, zCoord);
|
|
}
|
|
|
|
/* Updating */
|
|
@Override
|
|
public boolean canUpdate() {
|
|
return true;
|
|
}
|
|
|
|
@Override
|
|
public void updateEntity() {
|
|
if (renderOffset != 0) {
|
|
renderOffset -= renderOffset / 12 + 1; // has to be at least 1
|
|
|
|
if (renderOffset < 0) renderOffset = 0;
|
|
worldObj.func_147479_m(xCoord, yCoord, zCoord);
|
|
}
|
|
}
|
|
|
|
public int comparatorStrength() {
|
|
return 15 * tank.getFluidAmount() / tank.getCapacity();
|
|
}
|
|
}
|