Files
TinkersConstruct/src/main/java/tconstruct/blocks/logic/FaucetLogic.java
T

247 lines
6.4 KiB
Java
Raw Normal View History

package tconstruct.blocks.logic;
import tconstruct.TConstruct;
import mantle.blocks.iface.IActiveLogic;
import mantle.blocks.iface.IFacingLogic;
import net.minecraft.entity.EntityLivingBase;
import net.minecraft.nbt.NBTTagCompound;
2014-01-10 21:14:54 -05:00
import net.minecraft.network.NetworkManager;
2013-12-31 20:24:06 -05:00
import net.minecraft.network.Packet;
import net.minecraft.network.packet.Packet132TileEntityData;
import net.minecraft.tileentity.TileEntity;
import net.minecraft.util.MathHelper;
2013-12-31 20:24:06 -05:00
import net.minecraftforge.common.util.ForgeDirection;
import net.minecraftforge.fluids.Fluid;
import net.minecraftforge.fluids.FluidStack;
import net.minecraftforge.fluids.FluidTankInfo;
import net.minecraftforge.fluids.IFluidHandler;
public class FaucetLogic extends TileEntity implements IFacingLogic, IActiveLogic, IFluidHandler
{
byte direction;
boolean active;
public FluidStack liquid;
public boolean activateFaucet ()
{
if (liquid == null && active)
{
2014-01-08 21:59:48 -05:00
int x = field_145851_c, z = field_145849_e;
switch (getRenderDirection())
{
case 2:
z++;
break;
case 3:
z--;
break;
case 4:
x++;
break;
case 5:
x--;
break;
}
2014-01-08 23:46:45 -05:00
TileEntity drainte = field_145850_b.func_147438_o(x, field_145848_d, z);
TileEntity tankte = field_145850_b.func_147438_o(field_145851_c, field_145848_d - 1, field_145849_e);
if (drainte != null && drainte instanceof IFluidHandler && tankte != null && tankte instanceof IFluidHandler)
{
FluidStack templiquid = ((IFluidHandler) drainte).drain(getForgeDirection(), TConstruct.ingotLiquidValue, false);
if (templiquid != null)
{
int drained = ((IFluidHandler) tankte).fill(ForgeDirection.UP, templiquid, false);
if (drained > 0)
{
liquid = ((IFluidHandler) drainte).drain(getForgeDirection(), drained, true);
((IFluidHandler) tankte).fill(ForgeDirection.UP, liquid, true);
2014-01-08 21:59:48 -05:00
field_145850_b.markBlockForUpdate(field_145851_c, field_145848_d, field_145849_e);
return true;
}
else
{
return false;
}
}
}
}
return false;
}
@Override
public void updateEntity ()
{
if (liquid != null)
{
liquid.amount -= TConstruct.liquidUpdateAmount;
if (liquid.amount <= 0)
{
liquid = null;
if (!activateFaucet())
{
active = false;
2014-01-08 21:59:48 -05:00
field_145850_b.markBlockForUpdate(field_145851_c, field_145848_d, field_145849_e);
}
}
}
}
@Override
public byte getRenderDirection ()
{
return direction;
}
@Override
public ForgeDirection getForgeDirection ()
{
return ForgeDirection.VALID_DIRECTIONS[direction];
}
@Override
public void setDirection (int side)
{
//direction = (byte) side;
}
@Override
public void setDirection (float yaw, float pitch, EntityLivingBase player)
{
int facing = MathHelper.floor_double((double) (yaw / 360) + 0.5D) & 3;
switch (facing)
{
case 0:
direction = 2;
break;
case 1:
direction = 5;
break;
case 2:
direction = 3;
break;
case 3:
direction = 4;
break;
}
}
@Override
2014-01-18 02:52:49 -05:00
public void func_145839_a (NBTTagCompound tags)
{
2014-01-18 02:52:49 -05:00
super.func_145839_a(tags);
readCustomNBT(tags);
}
public void readCustomNBT (NBTTagCompound tags)
{
direction = tags.getByte("Direction");
if (tags.getBoolean("hasLiquid"))
{
this.liquid = FluidStack.loadFluidStackFromNBT(tags.getCompoundTag("Fluid"));
}
else
this.liquid = null;
}
@Override
2014-01-18 02:52:49 -05:00
public void func_145841_b (NBTTagCompound tags)
{
2014-01-18 02:52:49 -05:00
super.func_145841_b(tags);
writeCustomNBT(tags);
}
public void writeCustomNBT (NBTTagCompound tags)
{
tags.setByte("Direction", direction);
tags.setBoolean("hasLiquid", liquid != null);
if (liquid != null)
{
NBTTagCompound nbt = new NBTTagCompound();
2014-01-18 02:52:49 -05:00
liquid.func_145841_b(nbt);
2013-12-31 20:24:06 -05:00
tags.setTag("Fluid", nbt);
}
}
/* Packets */
@Override
public Packet getDescriptionPacket ()
{
NBTTagCompound tag = new NBTTagCompound();
writeCustomNBT(tag);
2014-01-08 21:59:48 -05:00
return new Packet132TileEntityData(field_145851_c, field_145848_d, field_145849_e, 1, tag);
}
@Override
2014-01-10 21:14:54 -05:00
public void onDataPacket (NetworkManager net, Packet132TileEntityData packet)
{
readCustomNBT(packet.data);
2014-01-08 21:59:48 -05:00
field_145850_b.markBlockForRenderUpdate(field_145851_c, field_145848_d, field_145849_e);
}
@Override
public boolean getActive ()
{
return active;
}
@Override
public void setActive (boolean flag)
{
if (!active)
{
active = true;
activateFaucet();
}
2013-10-07 19:58:36 +01:00
else
{
active = false;
2013-10-07 19:58:36 +01:00
}
}
@Override
public int fill (ForgeDirection from, FluidStack resource, boolean doFill)
{
// TODO Auto-generated method stub
return 0;
}
@Override
public FluidStack drain (ForgeDirection from, FluidStack resource, boolean doDrain)
{
// TODO Auto-generated method stub
return null;
}
@Override
public FluidStack drain (ForgeDirection from, int maxDrain, boolean doDrain)
{
// TODO Auto-generated method stub
return null;
}
@Override
public boolean canFill (ForgeDirection from, Fluid fluid)
{
// TODO Auto-generated method stub
return false;
}
@Override
public boolean canDrain (ForgeDirection from, Fluid fluid)
{
// TODO Auto-generated method stub
return false;
}
@Override
public FluidTankInfo[] getTankInfo (ForgeDirection from)
{
// TODO Auto-generated method stub
return null;
}
}