Hello,
I m doing my first mod and i wanna to make a block which use water. In gui i want see how many water are in this block. So, this is my tile entity of my block
public class StrikingMachineTileEntity extends TileEntity implements ISidedInventory, IFluidHandler {
public class Tank extends GenericFluidTank{
public Tank(int capacity)
{
super(capacity);
}
@Override
public boolean canAccept(FluidStack fluid){
switch(fluid.fluidID)
{
case 1:
return true;
default:
return false;
}
}
}
public Tank tank;
...
@Override
public int fill(ForgeDirection from, FluidStack resource, boolean doFill) {
return tank.fill(resource, doFill);
}
@Override
public FluidStack drain(ForgeDirection from, FluidStack resource, boolean doDrain) {
return tank.drain(resource, doDrain);
}
@Override
public FluidStack drain(ForgeDirection from, int maxDrain, boolean doDrain) {
return tank.drain(maxDrain, doDrain);
}
@Override
public boolean canFill(ForgeDirection from, Fluid fluid) {
return true;
}
@Override
public boolean canDrain(ForgeDirection from, Fluid fluid) {
return true;
}
@Override
public FluidTankInfo[] getTankInfo(ForgeDirection from) {
return new FluidTankInfo[]{tank.getInfo()};
}
public int getFluidAmount(){
return tank.getFluidAmount();
}
public FluidStack getFluid()
{
return tank.getFluid();
}
public int getCapacity(){
return tank.getCapacity();
}
}
Now this is my class for my tnak
public class GenericFluidTank implements IFluidTank{
private FluidStack fluid;
private boolean changeType;
private int capacity;
public GenericFluidTank(FluidStack type, int capacity)
{
if(type == null)
{
fluid = new FluidStack(0, 0);
this.changeType = true;
}
else
fluid = FluidUtils.copy(type, 0);
this.capacity = capacity;
}
public GenericFluidTank(int capacity){
this(null, capacity);
}
@Override
public int getCapacity(){
return capacity;
}
@Override
public FluidStack getFluid() {
return fluid.copy();
}
@Override
public FluidTankInfo getInfo() {
return new FluidTankInfo(this);
}
@Override
public int fill(FluidStack resource, boolean doFill) {
if(resource == null || resource.fluidID < 0){
return 0;
}
if(!canAccept(resource)){
return 0;
}
Console.println("Fluid amout before : " +this.getFluidAmount());
int tofill = Math.min(getCapacity()-this.fluid.amount, resource.amount);
if(doFill && tofill > 0)
{
if(!this.fluid.isFluidEqual(resource)){
this.fluid = copy(resource, fluid.amount+tofill);
}else{
this.fluid.amount+=tofill;
}
onLiquidChanged();
}
Console.println("Fluid amout after : " +this.getFluidAmount());
return tofill;
}
@Override
public FluidStack drain(int maxDrain, boolean doDrain) {
if(fluid.amount == 0 || maxDrain <= 0)
return null;
int todrain = Math.min(maxDrain, fluid.amount);
if(doDrain && todrain > 0)
{
fluid.amount-=todrain;
onLiquidChanged();
}
return FluidUtils.copy(fluid, todrain);
}
public FluidStack drain(FluidStack resource, boolean doDrain)
{
if (resource == null || !resource.isFluidEqual(fluid))
return null;
return drain(resource.amount, doDrain);
}
public void onLiquidChanged()
{
}
public boolean canAccept(FluidStack type){
return type == null || type.fluidID <= 0 || (fluid.amount == 0 && changeType) || fluid.isFluidEqual(type);
}
public static FluidStack copy(FluidStack liquid, int quantity)
{
liquid = liquid.copy();
liquid.amount = quantity;
return liquid;
}
public NBTTagCompound toTag()
{
return FluidUtils.write(fluid, new NBTTagCompound());
}
public void fromTag(NBTTagCompound tag)
{
fluid = FluidUtils.read(tag);
}
@Override
public int getFluidAmount() {
return this.fluid.amount;
}
}
And now my class for my gui
@Override
protected void drawGuiContainerBackgroundLayer(float f, int i, int j) {
// TODO Auto-generated method stub
GL11.glColor4f(1.0F, 1.0F, 1.0F, 1.0F);
this.mc.getTextureManager().bindTexture(background);
int var5 = (this.width - this.xSize) / 2;
int var6 = (this.height - this.ySize) / 2;
this.drawTexturedModalRect(var5, var6, 0, 0, this.xSize, this.ySize);
// Progress Bar
int x = (this.width - this.xSize) / 2;
int y = (this.height - this.ySize) / 2;
double ratioPixelBar = (double) 112/tileEntity.getCapacity();
int stored = tileEntity.tank.getFluidAmount();
Console.println("Fluid amount for gui :" +stored);
//int stored = (int)(tileEntity.( ) *ratioPixelBar);
//int stored = 0;
this.drawTexturedModalRect(x + 32, y + 24, 0, 166, stored, 7);
}
Well, now i ll show you the output
So when i don't open the machine block, we can see the fluid amount up
2014-06-14 14:44:11 [iNFOS] [sTDOUT] Fluid amout before : 0
2014-06-14 14:44:11 [iNFOS] [sTDOUT] Fluid amout after : 0
2014-06-14 14:44:11 [iNFOS] [sTDOUT] Fluid amout before : 0
2014-06-14 14:44:11 [iNFOS] [sTDOUT] Fluid amout after : 90
2014-06-14 14:44:11 [iNFOS] [sTDOUT] Fluid amout before : 90
2014-06-14 14:44:11 [iNFOS] [sTDOUT] Fluid amout after : 90
2014-06-14 14:44:11 [iNFOS] [sTDOUT] Fluid amout before : 90
2014-06-14 14:44:11 [iNFOS] [sTDOUT] Fluid amout after : 180
2014-06-14 14:44:11 [iNFOS] [sTDOUT] Fluid amout before : 180
2014-06-14 14:44:11 [iNFOS] [sTDOUT] Fluid amout after : 180
2014-06-14 14:44:11 [iNFOS] [sTDOUT] Fluid amout before : 180
2014-06-14 14:44:11 [iNFOS] [sTDOUT] Fluid amout after : 268
2014-06-14 14:44:11 [iNFOS] [sTDOUT] Fluid amout before : 268
...
2014-06-14 14:44:34 [iNFOS] [sTDOUT] Fluid amout after : 12000
2014-06-14 14:44:34 [iNFOS] [sTDOUT] Fluid amout before : 12000
I get the fluidamount by the function in my GenericTankFluid (getFluidAmount);
But the probleme is when i open the gui
2014-06-14 14:44:42 [iNFOS] [sTDOUT] Fluid amount for gui :0
Somebody can tell me where my fluid is gone? i don't realy understand why i don't get the good value. While i open the gui, i can already see this line
2014-06-14 14:44:42 [iNFOS] [sTDOUT] Fluid amount for gui :0
2014-06-14 14:44:42 [iNFOS] [sTDOUT] Fluid amout before : 12000
2014-06-14 14:44:42 [iNFOS] [sTDOUT] Fluid amout after : 12000
Tanks if people read all my post ^^