Reworked the tank a bit

*changed how it sends packets to only update if something changes or at
random points in time.
*changed the getColor() to use the blocks metadata and not a var
*added code to the setColor to change the blocks metadata
*Fixed load function to not set content if it read as unkown
This commit is contained in:
Rseifert 2013-03-29 13:24:54 -04:00
parent 3dd19fc09d
commit b604dafa50

View file

@ -1,5 +1,7 @@
package fluidmech.common.machines; package fluidmech.common.machines;
import java.util.Random;
import fluidmech.common.FluidMech; import fluidmech.common.FluidMech;
import fluidmech.common.machines.pipes.TileEntityPipe; import fluidmech.common.machines.pipes.TileEntityPipe;
import hydraulic.api.ColorCode; import hydraulic.api.ColorCode;
@ -24,45 +26,66 @@ import net.minecraftforge.liquids.LiquidTank;
import universalelectricity.core.vector.Vector3; import universalelectricity.core.vector.Vector3;
import universalelectricity.prefab.network.IPacketReceiver; import universalelectricity.prefab.network.IPacketReceiver;
import universalelectricity.prefab.network.PacketManager; import universalelectricity.prefab.network.PacketManager;
import universalelectricity.prefab.tile.TileEntityAdvanced;
import com.google.common.io.ByteArrayDataInput; import com.google.common.io.ByteArrayDataInput;
public class TileEntityTank extends TileEntity implements IPacketReceiver, IReadOut, IPsiCreator, ITankContainer, IColorCoded public class TileEntityTank extends TileEntityAdvanced implements IPacketReceiver, IReadOut, IPsiCreator, ITankContainer, IColorCoded
{ {
public TileEntity[] cc = { null, null, null, null, null, null }; public TileEntity[] cc = { null, null, null, null, null, null };
private ColorCode color = ColorCode.NONE;
public static final int LMax = 4; public static final int LMax = 4;
private int count, count2 = 0;
public int pVolume = 0; private Random random = new Random();
private boolean sendPacket = true;
private LiquidTank tank = new LiquidTank(LiquidContainerRegistry.BUCKET_VOLUME * LMax); private LiquidTank tank = new LiquidTank(LiquidContainerRegistry.BUCKET_VOLUME * LMax);
@Override
public void initiate()
{
}
public void updateEntity() public void updateEntity()
{ {
this.color = ColorCode.get(worldObj.getBlockMetadata(xCoord, yCoord, zCoord)); this.cc = connectionHelper.getSurroundingTileEntities(worldObj, xCoord, yCoord, zCoord);
if (++count >= 40) if (!worldObj.isRemote)
{ {
count = 0; int originalVolume = 0;
this.cc = connectionHelper.getSurroundingTileEntities(worldObj, xCoord, yCoord, zCoord); LiquidStack sendStack = new LiquidStack(0, 0, 0);
if (!worldObj.isRemote)
if (this.tank.getLiquid() != null)
{ {
this.tradeDown(); sendStack = this.tank.getLiquid();
this.tradeArround(); originalVolume = this.tank.getLiquid().amount;
this.fillPipe();
LiquidStack stack = new LiquidStack(0, 0, 0); if (ticks % 20 >= 0)
if (this.tank.getLiquid() != null)
{ {
stack = this.tank.getLiquid(); this.tradeDown();
this.tradeArround();
this.fillPipe();
} }
Packet packet = PacketManager.getPacket(FluidMech.CHANNEL, this, new Object[] { stack.itemID, stack.amount, stack.itemMeta });
PacketManager.sendPacketToClients(packet, worldObj, new Vector3(this), 20);
if (this.tank.getLiquid() == null && originalVolume != 0)
{
this.sendPacket = true;
}
else if (this.tank.getLiquid() != null && this.tank.getLiquid().amount != originalVolume)
{
sendStack = this.tank.getLiquid();
}
} }
if (sendPacket || ticks % (random.nextInt(5) * 10 + 20) == 0)
{
Packet packet = PacketManager.getPacket(FluidMech.CHANNEL, this, sendStack.itemID, sendStack.amount, sendStack.itemMeta);
PacketManager.sendPacketToClients(packet, worldObj, new Vector3(this), 20);
sendPacket = false;
}
} }
} }
@ -88,7 +111,10 @@ public class TileEntityTank extends TileEntity implements IPacketReceiver, IRead
LiquidStack liquid = new LiquidStack(0, 0, 0); LiquidStack liquid = new LiquidStack(0, 0, 0);
liquid.readFromNBT(nbt.getCompoundTag("stored")); liquid.readFromNBT(nbt.getCompoundTag("stored"));
tank.setLiquid(liquid); if (!liquid.isLiquidEqual(LiquidHandler.unkown.getStack()))
{
tank.setLiquid(liquid);
}
} }
@Override @Override
@ -119,7 +145,7 @@ public class TileEntityTank extends TileEntity implements IPacketReceiver, IRead
@Override @Override
public int fill(ForgeDirection from, LiquidStack resource, boolean doFill) public int fill(ForgeDirection from, LiquidStack resource, boolean doFill)
{ {
if (resource == null || (!color.getLiquidData().getStack().isLiquidEqual(resource) && this.color != ColorCode.NONE)) if (resource == null || (!getColor().getLiquidData().getStack().isLiquidEqual(resource) && this.getColor() != ColorCode.NONE))
{ {
return 0; return 0;
} }
@ -208,7 +234,7 @@ public class TileEntityTank extends TileEntity implements IPacketReceiver, IRead
@Override @Override
public int getPressureOut(LiquidStack type, ForgeDirection dir) public int getPressureOut(LiquidStack type, ForgeDirection dir)
{ {
if (color.isValidLiquid(type) || type.isLiquidEqual(LiquidHandler.unkown.getStack())) if (getColor().isValidLiquid(type) || type.isLiquidEqual(LiquidHandler.unkown.getStack()))
{ {
LiquidData data = LiquidHandler.get(type); LiquidData data = LiquidHandler.get(type);
if (data.getCanFloat() && dir == ForgeDirection.DOWN) if (data.getCanFloat() && dir == ForgeDirection.DOWN)
@ -225,7 +251,7 @@ public class TileEntityTank extends TileEntity implements IPacketReceiver, IRead
for (int i = 0; i < stacks.length; i++) for (int i = 0; i < stacks.length; i++)
{ {
LiquidData data = LiquidHandler.get(stacks[i]); LiquidData data = LiquidHandler.get(stacks[i]);
if (color.isValidLiquid(stacks[i]) && ((data.getCanFloat() && dir == ForgeDirection.DOWN) || (!data.getCanFloat() && dir == ForgeDirection.UP))) if (getColor().isValidLiquid(stacks[i]) && ((data.getCanFloat() && dir == ForgeDirection.DOWN) || (!data.getCanFloat() && dir == ForgeDirection.UP)))
{ {
return true; return true;
} }
@ -241,7 +267,7 @@ public class TileEntityTank extends TileEntity implements IPacketReceiver, IRead
if (this.tank.getLiquid() == null || this.tank.getLiquid().amount <= 0) if (this.tank.getLiquid() == null || this.tank.getLiquid().amount <= 0)
return; return;
TileEntity ent = worldObj.getBlockTileEntity(xCoord, yCoord - 1, zCoord); TileEntity ent = worldObj.getBlockTileEntity(xCoord, yCoord - 1, zCoord);
if (ent instanceof TileEntityTank && ((TileEntityTank) ent).getColor() == this.color && !((TileEntityTank) ent).isFull()) if (ent instanceof TileEntityTank && ((TileEntityTank) ent).getColor() == this.getColor() && !((TileEntityTank) ent).isFull())
{ {
int f = ((TileEntityTank) ent).tank.fill(this.tank.getLiquid(), true); int f = ((TileEntityTank) ent).tank.fill(this.tank.getLiquid(), true);
this.tank.drain(f, true); this.tank.drain(f, true);
@ -263,7 +289,7 @@ public class TileEntityTank extends TileEntity implements IPacketReceiver, IRead
for (int i = 2; i < 6; i++) for (int i = 2; i < 6; i++)
{ {
if (ents[i] instanceof TileEntityTank && ((TileEntityTank) ents[i]).color == this.color) if (ents[i] instanceof TileEntityTank && ((TileEntityTank) ents[i]).getColor() == this.getColor())
{ {
tanks++; tanks++;
if (((TileEntityTank) ents[i]).tank.getLiquid() != null) if (((TileEntityTank) ents[i]).tank.getLiquid() != null)
@ -282,7 +308,7 @@ public class TileEntityTank extends TileEntity implements IPacketReceiver, IRead
break; break;
} }
if (ents[i] instanceof TileEntityTank && ((TileEntityTank) ents[i]).color == this.color && !((TileEntityTank) ents[i]).isFull()) if (ents[i] instanceof TileEntityTank && ((TileEntityTank) ents[i]).getColor() == this.getColor() && !((TileEntityTank) ents[i]).isFull())
{ {
LiquidStack target = ((TileEntityTank) ents[i]).tank.getLiquid(); LiquidStack target = ((TileEntityTank) ents[i]).tank.getLiquid();
LiquidStack filling = this.tank.getLiquid(); LiquidStack filling = this.tank.getLiquid();
@ -326,7 +352,7 @@ public class TileEntityTank extends TileEntity implements IPacketReceiver, IRead
if (ent instanceof TileEntityPipe) if (ent instanceof TileEntityPipe)
{ {
ColorCode c = ((TileEntityPipe) ent).getColor(); ColorCode c = ((TileEntityPipe) ent).getColor();
if (c == ColorCode.NONE || c == this.color) if (c == ColorCode.NONE || c == this.getColor())
{ {
int vol = LiquidContainerRegistry.BUCKET_VOLUME; int vol = LiquidContainerRegistry.BUCKET_VOLUME;
if (this.tank.getLiquid().amount < vol) if (this.tank.getLiquid().amount < vol)
@ -343,13 +369,16 @@ public class TileEntityTank extends TileEntity implements IPacketReceiver, IRead
@Override @Override
public void setColor(Object obj) public void setColor(Object obj)
{ {
this.color = ColorCode.get(cc); ColorCode code = ColorCode.get(obj);
if (worldObj.isRemote && code != this.getColor() && (this.tank != null || code.isValidLiquid(this.tank.getLiquid())))
{
this.worldObj.setBlockMetadataWithNotify(xCoord, yCoord, zCoord, code.ordinal() & 15, 3);
}
} }
@Override @Override
public ColorCode getColor() public ColorCode getColor()
{ {
return color; return ColorCode.get(worldObj.getBlockMetadata(xCoord, yCoord, zCoord));
} }
} }