resonant-induction/common/dark/BasicUtilities/pipes/TileEntityPipe.java
Rseifert 28f62284a0 moved over from other repo
got around to moving this so Calc could help me with the clean up of it.
So far i havn't changed much but will soon. The main focus for the next
week on the mod will be just cleanup and bug fixing. I will also be
moving the motor from steam power to here. The mod has alos been renamed
since now it contains more than just pipes.
2012-11-14 13:16:22 -05:00

225 lines
7.4 KiB
Java

package dark.BasicUtilities.pipes;
import net.minecraft.src.EntityPlayer;
import net.minecraft.src.INetworkManager;
import net.minecraft.src.NBTTagCompound;
import net.minecraft.src.Packet;
import net.minecraft.src.Packet250CustomPayload;
import net.minecraft.src.TileEntity;
import net.minecraftforge.common.ForgeDirection;
import universalelectricity.core.Vector3;
import universalelectricity.prefab.network.IPacketReceiver;
import universalelectricity.prefab.network.PacketManager;
import com.google.common.io.ByteArrayDataInput;
import dark.BasicUtilities.BasicUtilitiesMain;
import dark.BasicUtilities.api.ILiquidConsumer;
import dark.BasicUtilities.api.ILiquidProducer;
import dark.BasicUtilities.api.Liquid;
import dark.BasicUtilities.api.MHelper;
public class TileEntityPipe extends TileEntity implements ILiquidConsumer, IPacketReceiver
{
protected Liquid type = Liquid.DEFUALT;
public int capacity = 2;
public int presure = 0;
public int connectedUnits = 0;
public int liquidStored = 0;
private int count = 0;
private int count2 = 0;
protected boolean firstUpdate = true;
public TileEntity[] connectedBlocks =
{ null, null, null, null, null, null };
public int getPressure()
{
return this.presure;
}
@Override
public void updateEntity()
{
if (++count >= 5)
{
count = 0;
this.connectedBlocks = MHelper.getSourounding(worldObj, xCoord, yCoord, zCoord);
this.presure = this.updatePressure();
if (!worldObj.isRemote)
{
if (firstUpdate || count2++ >= 5)
{
count2 = 0;
firstUpdate = false;
Packet packet = PacketManager.getPacket(BasicUtilitiesMain.CHANNEL, this, this.type.ordinal() );
PacketManager.sendPacketToClients(packet, worldObj, Vector3.get(this), 60);
}
for (int i = 0; i < 6; i++)
{
ForgeDirection dir = ForgeDirection.getOrientation(i).getOpposite();
if (connectedBlocks[i] instanceof ILiquidProducer && ((ILiquidProducer) connectedBlocks[i]).canProduceLiquid(this.type, dir))
{
int vol = ((ILiquidProducer) connectedBlocks[i]).onProduceLiquid(this.type, this.capacity - this.liquidStored, dir);
this.liquidStored = Math.min(this.liquidStored + vol,
this.capacity);
}
if (connectedBlocks[i] instanceof ILiquidConsumer && ((ILiquidConsumer) connectedBlocks[i]).canRecieveLiquid(this.type, dir) && this.liquidStored > 0 && this.presure > 0)
{
if (connectedBlocks[i] instanceof TileEntityPipe)
{
this.liquidStored--;
int vol = ((ILiquidConsumer) connectedBlocks[i]).onReceiveLiquid(this.type, Math.max(this.liquidStored, 1), dir);
this.liquidStored += vol;
}
else
{
this.liquidStored = ((ILiquidConsumer) connectedBlocks[i]).onReceiveLiquid(this.type, this.liquidStored, dir);
}
}
}
}
}
}
/**
* used to cause the pipes presure to update depending on what is connected
* to it
*
* @return
*/
public int updatePressure()
{
int highestPressure = 0;
this.connectedUnits = 0;
for (int i = 0; i < 6; i++)
{
ForgeDirection dir = ForgeDirection.getOrientation(i).getOpposite();
if (connectedBlocks[i] instanceof ILiquidConsumer && ((ILiquidConsumer) connectedBlocks[i]).canRecieveLiquid(this.type, dir))
{
this.connectedUnits++;
if (connectedBlocks[i] instanceof TileEntityPipe)
{
if (((TileEntityPipe) connectedBlocks[i]).getPressure() > highestPressure)
{
highestPressure = ((TileEntityPipe) connectedBlocks[i]).getPressure();
}
}
}
else if (connectedBlocks[i] instanceof ILiquidProducer && ((ILiquidProducer) connectedBlocks[i]).canProduceLiquid(this.type, dir))
{
this.connectedUnits++;
if (((ILiquidProducer) connectedBlocks[i]).canProducePresure(this.type, dir) && ((ILiquidProducer) connectedBlocks[i]).presureOutput(this.type, dir) > highestPressure)
{
highestPressure = ((ILiquidProducer) connectedBlocks[i]).presureOutput(this.type, ForgeDirection.getOrientation(i));
}
}
else
{
connectedBlocks[i] = null;
}
}
return highestPressure - 1;
}
// ---------------
// liquid stuff
// ---------------
@Override
public int onReceiveLiquid(Liquid type, int vol, ForgeDirection side)
{
if (type == this.type)
{
int rejectedVolume = Math.max((this.getStoredLiquid(type) + vol) - this.capacity, 0);
this.liquidStored = Math.min(Math.max((liquidStored + vol - rejectedVolume), 0), this.capacity);
return rejectedVolume;
}
return vol;
}
/**
* @return Return the stored volume in this pipe.
*/
@Override
public int getStoredLiquid(Liquid type)
{
if (type == this.type) { return this.liquidStored; }
return 0;
}
@Override
public int getLiquidCapacity(Liquid type)
{
if (type == this.type) { return this.capacity; }
return 0;
}
// find wether or not this side of X block can recieve X liquid type. Also
// use to determine connection of a pipe
@Override
public boolean canRecieveLiquid(Liquid type, ForgeDirection side)
{
if (type == this.type) { return true; }
return false;
}
// returns liquid type
public Liquid getType()
{
return this.type;
}
// used by the item to set the liquid type on spawn
public void setType(Liquid rType)
{
this.type = rType;
}
// ---------------------
// data
// --------------------
@Override
public void handlePacketData(INetworkManager network, int packetType, Packet250CustomPayload packet, EntityPlayer player, ByteArrayDataInput data)
{
try
{
this.setType(Liquid.getLiquid(data.readInt()));
}
catch (Exception e)
{
e.printStackTrace();
}
}
/**
* Reads a tile entity from NBT.
*/
@Override
public void readFromNBT(NBTTagCompound par1NBTTagCompound)
{
super.readFromNBT(par1NBTTagCompound);
this.liquidStored = par1NBTTagCompound.getInteger("liquid");
this.type = Liquid.getLiquid(par1NBTTagCompound.getInteger("type"));
}
/**
* Writes a tile entity to NBT.
*/
@Override
public void writeToNBT(NBTTagCompound par1NBTTagCompound)
{
super.writeToNBT(par1NBTTagCompound);
par1NBTTagCompound.setInteger("liquid", this.liquidStored);
par1NBTTagCompound.setInteger("type", this.type.ordinal());
}
}