resonant-induction/minecraft/liquidmechanics/common/tileentity/TileEntityPipe.java
Rseifert 77de6e6353 Making Changes for the better
Starting down the long road of making my mod even more compatable with
other mod's liquids. This will take some time, patience, and pain
killers.

Plan of action
*Release Valve will not store any liquids but rather direction output to
pipes from other TileEntities
*Release Valve will have a gui to restrict it to outputing one or more
types of Liquids that are predefined
*Pipes will go from being fully liquid restricted to color based(0-15)
and have a universal uncolor pipe that can accept all liquids
*Once a pipe is place a tool can be used to change its color just like
in other mods.
*Some colors will be restricted to select liquids for example Blue is
water, Red is Lava, Black is oil, Yellow Fuel, White Milk,
*Steam will have its own pipe made out of bronze to fit the machines it
goes too.
*Tanks will go in the same direction
*Pumps will still be liquid restricted but come with unique textures,
models, and animation per liquid type

Current issues to resolve that are broken with push
*Release valve doesn't work at all due to changes in progress
*back compatable must be added for pipes and old release valves
2013-01-04 19:03:34 -05:00

289 lines
8.8 KiB
Java

package liquidmechanics.common.tileentity;
import liquidmechanics.api.IReadOut;
import liquidmechanics.api.IPressure;
import liquidmechanics.api.helpers.TankHelper;
import liquidmechanics.common.LiquidMechanics;
import liquidmechanics.common.handlers.LiquidData;
import liquidmechanics.common.handlers.LiquidHandler;
import net.minecraft.entity.player.EntityPlayer;
import net.minecraft.nbt.NBTTagCompound;
import net.minecraft.network.INetworkManager;
import net.minecraft.network.packet.Packet;
import net.minecraft.network.packet.Packet250CustomPayload;
import net.minecraft.tileentity.TileEntity;
import net.minecraftforge.common.ForgeDirection;
import net.minecraftforge.liquids.ILiquidTank;
import net.minecraftforge.liquids.ITankContainer;
import net.minecraftforge.liquids.LiquidContainerRegistry;
import net.minecraftforge.liquids.LiquidStack;
import net.minecraftforge.liquids.LiquidTank;
import universalelectricity.core.vector.Vector3;
import universalelectricity.prefab.network.IPacketReceiver;
import universalelectricity.prefab.network.PacketManager;
import com.google.common.io.ByteArrayDataInput;
public class TileEntityPipe extends TileEntity implements ITankContainer, IPacketReceiver, IReadOut
{
public LiquidData type = LiquidHandler.air;
private int count = 20;
private int count2, presure = 0;
public boolean converted = false;
protected boolean firstUpdate = true;
public boolean isUniversal = false;
public TileEntity[] connectedBlocks = { null, null, null, null, null, null };
public LiquidTank stored = new LiquidTank(LiquidContainerRegistry.BUCKET_VOLUME * 3);
@Override
public void updateEntity()
{
if (++count >= 40)
{
count = 0;
this.connectedBlocks = TankHelper.getSurroundings(worldObj, xCoord, yCoord, zCoord);
for (int e = 0; e < 6; e++)
{
if (connectedBlocks[e] instanceof ITankContainer || connectedBlocks[e] instanceof IPressure)
{
if (connectedBlocks[e] instanceof TileEntityPipe && ((TileEntityPipe) connectedBlocks[e]).type != this.type)
{
connectedBlocks[e] = null;
}
}
else
{
connectedBlocks[e] = null;
}
}
if (!worldObj.isRemote)
{
this.updatePressure();
if (count2-- <= 0)
{
count2 = 5;
firstUpdate = false;
Packet packet = PacketManager.getPacket(LiquidMechanics.CHANNEL, this, LiquidData.getName(type));
PacketManager.sendPacketToClients(packet, worldObj, new Vector3(this), 60);
}
LiquidStack stack = stored.getLiquid();
if (stack != null && stack.amount >= 0)
{
for (int i = 0; i < 6; i++)
{
ForgeDirection dir = ForgeDirection.getOrientation(i);
int moved = 0;
if (connectedBlocks[i] instanceof ITankContainer)
{
if (connectedBlocks[i] instanceof TileEntityPipe)
{
if (((TileEntityPipe) connectedBlocks[i]).presure < this.presure)
{
moved = ((TileEntityPipe) connectedBlocks[i]).stored.fill(stack, true);
}
}
else
{
moved = ((ITankContainer) connectedBlocks[i]).fill(dir.getOpposite(), stack, true);
}
}
stored.drain(moved, true);
// FMLLog.warning("Moved "+moved+ " "+ i);
if (stack.amount <= 0)
{
break;
}
}
}
}
}
}
// returns liquid type
public LiquidData getType()
{
return this.type;
}
// used by the item to set the liquid type on spawn
public void setType(LiquidData rType)
{
this.type = rType;
}
// ---------------------
// data
// --------------------
@Override
public void handlePacketData(INetworkManager network, int packetType, Packet250CustomPayload packet, EntityPlayer player, ByteArrayDataInput data)
{
try
{
this.setType(LiquidHandler.get(data.readUTF()));
}
catch (Exception e)
{
e.printStackTrace();
}
}
/**
* Reads a tile entity from NBT.
*/
@Override
public void readFromNBT(NBTTagCompound nbt)
{
super.readFromNBT(nbt);
this.converted = nbt.getBoolean("converted");
if (!converted)
{
int t = nbt.getInteger("type");
this.type = LiquidHandler.getFromMeta(t);
this.converted = true;
}
else
{
this.type = LiquidHandler.get(nbt.getString("name"));
}
if (this.type == null) type = LiquidHandler.air;
int vol = nbt.getInteger("liquid");
this.stored.setLiquid(LiquidHandler.getStack(type, vol));
}
/**
* Writes a tile entity to NBT.
*/
@Override
public void writeToNBT(NBTTagCompound nbt)
{
super.writeToNBT(nbt);
nbt.setBoolean("converted", this.converted);
int s = 0;
if (stored.getLiquid() != null) s = stored.getLiquid().amount;
nbt.setInteger("liquid", s);
nbt.setString("name", LiquidData.getName(type));
}
@Override
public String getMeterReading(EntityPlayer user, ForgeDirection side)
{
if (type == null) return "Error: No Type";
String output = "";
LiquidStack stack = stored.getLiquid();
if (stack != null)
output += (stack.amount / LiquidContainerRegistry.BUCKET_VOLUME) + " " + LiquidData.getName(type);
output += " @" + this.presure + "psi";
if (stack != null)
return output;
return "Error";
}
@Override
public int fill(ForgeDirection from, LiquidStack resource, boolean doFill)
{
LiquidStack stack = stored.getLiquid();
if (stack == null) stored.setLiquid(LiquidHandler.getStack(this.type, 1));
if (stack != null && LiquidHandler.isEqual(resource, this.type)) return fill(0, resource, doFill);
return 0;
}
@Override
public int fill(int tankIndex, LiquidStack resource, boolean doFill)
{
if (tankIndex != 0 || resource == null)
return 0;
return stored.fill(resource, doFill);
}
@Override
public LiquidStack drain(ForgeDirection from, int maxDrain, boolean doDrain)
{
return drain(0, maxDrain, doDrain);
}
@Override
public LiquidStack drain(int tankIndex, int maxDrain, boolean doDrain)
{
return stored.drain(maxDrain, doDrain);
}
@Override
public ILiquidTank[] getTanks(ForgeDirection direction)
{
return new ILiquidTank[] { this.stored };
}
@Override
public ILiquidTank getTank(ForgeDirection direction, LiquidStack type)
{
return null;
}
/**
* Used to determan pipe connection rules
*/
public boolean canConnect(TileEntity entity)
{
if (entity instanceof TileEntityPipe)
{
if (((TileEntityPipe) entity).type == this.type) { return true; }
}
return false;
}
/**
* used to cause the pipes pressure to update depending on what is connected
* to it
*
* @return
*/
public void updatePressure()
{
int highestPressure = 0;
this.presure = 0;
for (int i = 0; i < 6; i++)
{
ForgeDirection dir = ForgeDirection.getOrientation(i);
if (connectedBlocks[i] instanceof TileEntityPipe && ((TileEntityPipe) connectedBlocks[i]).canConnect(this))
{
if (((TileEntityPipe) connectedBlocks[i]).getPressure() > highestPressure)
{
highestPressure = ((TileEntityPipe) connectedBlocks[i]).getPressure();
}
}
if (connectedBlocks[i] instanceof IPressure && ((IPressure) connectedBlocks[i]).canPressureToo(this.type, dir))
{
int p = ((IPressure) connectedBlocks[i]).presureOutput(this.type, dir);
if (p > highestPressure)
highestPressure = p;
}
}
this.presure = highestPressure - 1;
}
public int getPressure()
{
return this.presure;
}
}