Jump to content

Possible bug in TileFluidHandler


Zarathul

Recommended Posts

(forge 9.11.1.965)

Pardon me if I'm writing total nonsense (I'm kind of close to madness right now) but I've wasted hours trying to get my TileFluidHandler based TileEntitys to sync up properly when I found this in the TileFluidHandler class

 

    @Override
    public void readFromNBT(NBTTagCompound tag)
    {
        super.readFromNBT(tag);
        tank.writeToNBT(tag);
    }

    @Override
    public void writeToNBT(NBTTagCompound tag)
    {
        super.writeToNBT(tag);
        tank.readFromNBT(tag);
    }

 

Aren't the tank.writeToNBT() and tank.readFromNBT() swapped ?

Link to comment
Share on other sites

@Op: Yes, looks like a bug to me. Fix: Don't use TileFluidHandler.

 

Yeah that's what I figured. I ended up basically copying the TileFluidHandler code into my own IFluidHandler implementation, switched the NBT read/write around and added the syncing. Seems to work for now. It's neither pretty nor does it do much on it's own but here is the code if someone is interested.

 

package simplefluidtanks;

import net.minecraft.nbt.NBTTagCompound;
import net.minecraft.network.INetworkManager;
import net.minecraft.network.packet.Packet;
import net.minecraft.network.packet.Packet132TileEntityData;
import net.minecraft.tileentity.TileEntity;
import net.minecraftforge.common.ForgeDirection;
import net.minecraftforge.fluids.Fluid;
import net.minecraftforge.fluids.FluidContainerRegistry;
import net.minecraftforge.fluids.FluidStack;
import net.minecraftforge.fluids.FluidTank;
import net.minecraftforge.fluids.FluidTankInfo;
import net.minecraftforge.fluids.IFluidHandler;

public class ValveBlockEntity extends TileEntity implements IFluidHandler
{
protected FluidTank tank;

public ValveBlockEntity()
{
	super();
	this.tank = new FluidTank(SimpleFluidTanks.bucketsPerTank * FluidContainerRegistry.BUCKET_VOLUME);
}

    @Override
    public void readFromNBT(NBTTagCompound tag)
    {
        super.readFromNBT(tag);
        tank.readFromNBT(tag);
    }

    @Override
    public void writeToNBT(NBTTagCompound tag)
    {
        super.writeToNBT(tag);
        tank.writeToNBT(tag);
    }

    @Override
    public int fill(ForgeDirection from, FluidStack resource, boolean doFill)
    {
    	int fillAmount = tank.fill(resource, doFill);
this.worldObj.markBlockForUpdate(xCoord, yCoord, zCoord);
    	
        return fillAmount;
    }

    @Override
    public FluidStack drain(ForgeDirection from, FluidStack resource, boolean doDrain)
    {
        if (resource == null || !resource.isFluidEqual(tank.getFluid()))
        {
            return null;
        }
        
        FluidStack drainedFluid = tank.drain(resource.amount, doDrain);
        this.worldObj.markBlockForUpdate(xCoord, yCoord, zCoord);
        
        return drainedFluid;
    }

    @Override
    public FluidStack drain(ForgeDirection from, int maxDrain, boolean doDrain)
    {
    	this.worldObj.markBlockForUpdate(xCoord, yCoord, zCoord);
    	FluidStack drainedFluid =  tank.drain(maxDrain, doDrain);
    	
    	return drainedFluid;
    }

    @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() };
    }

@Override
public Packet getDescriptionPacket()
{
	NBTTagCompound tag = new NBTTagCompound();
	writeToNBT(tag);

	return new Packet132TileEntityData(xCoord, yCoord, zCoord, 1, tag);
}

@Override
public void onDataPacket(INetworkManager net, Packet132TileEntityData packet)
{
	readFromNBT(packet.data);
}

public int getCapacity()
{
	return tank.getCapacity();
}

public int getFluidAmount()
{
	return tank.getFluidAmount();
}

public FluidStack getFluid()
{
	return tank.getFluid();
}
}

Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Unfortunately, your content contains terms that we do not allow. Please edit your content to remove the highlighted words below.
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Announcements



×
×
  • Create New...

Important Information

By using this site, you agree to our Terms of Use.