2013-01-02 19:55:29 +01:00
|
|
|
package liquidmechanics.common.tileentity;
|
2012-12-22 10:44:17 +01:00
|
|
|
|
2013-01-02 19:55:29 +01:00
|
|
|
import liquidmechanics.api.IReadOut;
|
2013-01-05 01:03:20 +01:00
|
|
|
import liquidmechanics.api.IPressure;
|
2013-01-03 15:58:19 +01:00
|
|
|
import liquidmechanics.api.helpers.TankHelper;
|
2013-01-02 19:55:29 +01:00
|
|
|
import liquidmechanics.common.LiquidMechanics;
|
2013-01-03 18:18:47 +01:00
|
|
|
import liquidmechanics.common.handlers.LiquidData;
|
|
|
|
import liquidmechanics.common.handlers.LiquidHandler;
|
2012-12-22 10:44:17 +01:00
|
|
|
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
|
|
|
|
{
|
2013-01-03 18:18:47 +01:00
|
|
|
public LiquidData type = LiquidHandler.air;
|
2013-01-05 01:03:20 +01:00
|
|
|
|
2012-12-22 10:44:17 +01:00
|
|
|
private int count = 20;
|
|
|
|
private int count2, presure = 0;
|
2013-01-05 01:03:20 +01:00
|
|
|
|
2013-01-03 18:18:47 +01:00
|
|
|
public boolean converted = false;
|
2012-12-22 10:44:17 +01:00
|
|
|
protected boolean firstUpdate = true;
|
2013-01-05 01:03:20 +01:00
|
|
|
public boolean isUniversal = false;
|
2012-12-22 10:44:17 +01:00
|
|
|
|
|
|
|
public TileEntity[] connectedBlocks = { null, null, null, null, null, null };
|
2013-01-05 01:03:20 +01:00
|
|
|
|
2012-12-22 10:44:17 +01:00
|
|
|
public LiquidTank stored = new LiquidTank(LiquidContainerRegistry.BUCKET_VOLUME * 3);
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public void updateEntity()
|
|
|
|
{
|
|
|
|
if (++count >= 40)
|
|
|
|
{
|
|
|
|
count = 0;
|
2013-01-05 01:03:20 +01:00
|
|
|
this.connectedBlocks = TankHelper.getSurroundings(worldObj, xCoord, yCoord, zCoord);
|
2012-12-22 10:44:17 +01:00
|
|
|
for (int e = 0; e < 6; e++)
|
|
|
|
{
|
2013-01-05 01:03:20 +01:00
|
|
|
if (connectedBlocks[e] instanceof ITankContainer || connectedBlocks[e] instanceof IPressure)
|
2012-12-22 10:44:17 +01:00
|
|
|
{
|
|
|
|
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;
|
2013-01-03 18:18:47 +01:00
|
|
|
Packet packet = PacketManager.getPacket(LiquidMechanics.CHANNEL, this, LiquidData.getName(type));
|
2012-12-22 10:44:17 +01:00
|
|
|
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
|
2013-01-03 18:18:47 +01:00
|
|
|
public LiquidData getType()
|
2012-12-22 10:44:17 +01:00
|
|
|
{
|
|
|
|
return this.type;
|
|
|
|
}
|
|
|
|
|
|
|
|
// used by the item to set the liquid type on spawn
|
2013-01-03 18:18:47 +01:00
|
|
|
public void setType(LiquidData rType)
|
2012-12-22 10:44:17 +01:00
|
|
|
{
|
|
|
|
this.type = rType;
|
|
|
|
}
|
|
|
|
|
|
|
|
// ---------------------
|
|
|
|
// data
|
|
|
|
// --------------------
|
|
|
|
@Override
|
|
|
|
public void handlePacketData(INetworkManager network, int packetType, Packet250CustomPayload packet, EntityPlayer player, ByteArrayDataInput data)
|
|
|
|
{
|
|
|
|
try
|
|
|
|
{
|
2013-01-03 18:18:47 +01:00
|
|
|
this.setType(LiquidHandler.get(data.readUTF()));
|
2012-12-22 10:44:17 +01:00
|
|
|
}
|
|
|
|
catch (Exception e)
|
|
|
|
{
|
|
|
|
e.printStackTrace();
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Reads a tile entity from NBT.
|
|
|
|
*/
|
|
|
|
@Override
|
2013-01-03 18:18:47 +01:00
|
|
|
public void readFromNBT(NBTTagCompound nbt)
|
2012-12-22 10:44:17 +01:00
|
|
|
{
|
2013-01-03 18:18:47 +01:00
|
|
|
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));
|
2012-12-22 10:44:17 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Writes a tile entity to NBT.
|
|
|
|
*/
|
|
|
|
@Override
|
2013-01-03 18:18:47 +01:00
|
|
|
public void writeToNBT(NBTTagCompound nbt)
|
2012-12-22 10:44:17 +01:00
|
|
|
{
|
2013-01-03 18:18:47 +01:00
|
|
|
super.writeToNBT(nbt);
|
|
|
|
nbt.setBoolean("converted", this.converted);
|
2012-12-22 10:44:17 +01:00
|
|
|
int s = 0;
|
2013-01-03 18:18:47 +01:00
|
|
|
if (stored.getLiquid() != null) s = stored.getLiquid().amount;
|
|
|
|
nbt.setInteger("liquid", s);
|
|
|
|
|
|
|
|
nbt.setString("name", LiquidData.getName(type));
|
2012-12-22 10:44:17 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public String getMeterReading(EntityPlayer user, ForgeDirection side)
|
|
|
|
{
|
2013-01-03 18:18:47 +01:00
|
|
|
if (type == null) return "Error: No Type";
|
2012-12-22 10:44:17 +01:00
|
|
|
String output = "";
|
|
|
|
LiquidStack stack = stored.getLiquid();
|
2013-01-02 19:55:29 +01:00
|
|
|
if (stack != null)
|
2013-01-03 18:18:47 +01:00
|
|
|
output += (stack.amount / LiquidContainerRegistry.BUCKET_VOLUME) + " " + LiquidData.getName(type);
|
2012-12-22 10:44:17 +01:00
|
|
|
output += " @" + this.presure + "psi";
|
2013-01-03 18:18:47 +01:00
|
|
|
|
2013-01-02 19:55:29 +01:00
|
|
|
if (stack != null)
|
|
|
|
return output;
|
2012-12-22 10:44:17 +01:00
|
|
|
|
|
|
|
return "Error";
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public int fill(ForgeDirection from, LiquidStack resource, boolean doFill)
|
|
|
|
{
|
|
|
|
LiquidStack stack = stored.getLiquid();
|
2013-01-03 18:18:47 +01:00
|
|
|
if (stack == null) stored.setLiquid(LiquidHandler.getStack(this.type, 1));
|
|
|
|
if (stack != null && LiquidHandler.isEqual(resource, this.type)) return fill(0, resource, doFill);
|
2013-01-02 19:55:29 +01:00
|
|
|
|
2012-12-22 10:44:17 +01:00
|
|
|
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)
|
|
|
|
{
|
2013-01-02 19:55:29 +01:00
|
|
|
return new ILiquidTank[] { this.stored };
|
2012-12-22 10:44:17 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public ILiquidTank getTank(ForgeDirection direction, LiquidStack type)
|
|
|
|
{
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Used to determan pipe connection rules
|
|
|
|
*/
|
2013-01-05 01:03:20 +01:00
|
|
|
public boolean canConnect(TileEntity entity)
|
2012-12-22 10:44:17 +01:00
|
|
|
{
|
|
|
|
if (entity instanceof TileEntityPipe)
|
|
|
|
{
|
2013-01-03 18:18:47 +01:00
|
|
|
if (((TileEntityPipe) entity).type == this.type) { return true; }
|
2012-12-22 10:44:17 +01:00
|
|
|
}
|
|
|
|
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);
|
|
|
|
|
2013-01-05 01:03:20 +01:00
|
|
|
if (connectedBlocks[i] instanceof TileEntityPipe && ((TileEntityPipe) connectedBlocks[i]).canConnect(this))
|
2012-12-22 10:44:17 +01:00
|
|
|
{
|
|
|
|
if (((TileEntityPipe) connectedBlocks[i]).getPressure() > highestPressure)
|
|
|
|
{
|
|
|
|
highestPressure = ((TileEntityPipe) connectedBlocks[i]).getPressure();
|
|
|
|
}
|
|
|
|
}
|
2013-01-05 01:03:20 +01:00
|
|
|
if (connectedBlocks[i] instanceof IPressure && ((IPressure) connectedBlocks[i]).canPressureToo(this.type, dir))
|
2012-12-22 10:44:17 +01:00
|
|
|
{
|
|
|
|
|
2013-01-05 01:03:20 +01:00
|
|
|
int p = ((IPressure) connectedBlocks[i]).presureOutput(this.type, dir);
|
2013-01-02 19:55:29 +01:00
|
|
|
if (p > highestPressure)
|
|
|
|
highestPressure = p;
|
2012-12-22 10:44:17 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
this.presure = highestPressure - 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
public int getPressure()
|
|
|
|
{
|
|
|
|
return this.presure;
|
|
|
|
}
|
|
|
|
}
|