Tank Not Filling Past 2 Buckets

Discussion in 'Mod Development' started by RavynousHunter, Jun 11, 2015.

  1. RavynousHunter

    RavynousHunter New Member

    So, I'm trying my hand at a deep storage tank, basically a DSU for liquids. I've got...most of the stuff working. Its textures are showing up and right-clicking is working, except for one irritating little detail: once I put two buckets worth of a fluid (I'm using water in my tests) into the tank, it refuses to accept any more, the bucket simply pours out into the world. Below is the code for both the block and its associated tile entity. Can anyone spot the issue, because I'm, quite frankly, at a loss.

    Code:
    package RavynousHunter.DeepLiquidStorage.Blocks;
    
    import cpw.mods.fml.common.registry.GameRegistry;
    import cpw.mods.fml.relauncher.Side;
    import cpw.mods.fml.relauncher.SideOnly;
    import RavynousHunter.DeepLiquidStorage.DeepLiquidStorage;
    import RavynousHunter.DeepLiquidStorage.Entities.TileDLS;
    import net.minecraft.block.Block;
    import net.minecraft.block.BlockContainer;
    import net.minecraft.block.material.Material;
    import net.minecraft.client.renderer.texture.IIconRegister;
    import net.minecraft.creativetab.CreativeTabs;
    import net.minecraft.entity.player.EntityPlayer;
    import net.minecraft.item.ItemStack;
    import net.minecraft.tileentity.TileEntity;
    import net.minecraft.util.IIcon;
    import net.minecraft.world.World;
    import net.minecraftforge.fluids.FluidContainerRegistry;
    
    public class BlockDLS
      extends BlockContainer
    {
      public String Name = "Deep Liquid Storage Tank";
    
      private IIcon topTexture;
      private IIcon sideTexture;
      private IIcon bottomTexture;
    
      public BlockDLS( Material material )
      {
        super( material );
      
        this.setBlockName( this.Name );
        this.setCreativeTab( CreativeTabs.tabBlock );
        this.setStepSound( Block.soundTypeMetal );
        this.setHardness( 0.5F );
      }
    
      @Override
      public TileEntity createNewTileEntity( World world, int intX )
      {
        return new TileDLS();
      }
    
      @Override
      public void registerBlockIcons( IIconRegister register )
      {
        this.topTexture  = register.registerIcon( "DeepLiquidStorage:dls_top"  );
        this.sideTexture  = register.registerIcon( "DeepLiquidStorage:dls_side"  );
        this.bottomTexture = register.registerIcon( "DeepLiquidStorage:dls_bottom" );
      }
    
      @Override
      public IIcon getIcon( int side, int meta )
      {
        switch ( side )
        {
          case 1:
          {
            return this.topTexture;
          }
          case 0:
          {
            return this.bottomTexture;
          }
          default:
          {
            return this.sideTexture;
          }
        }
      }
    
      @Override
      public boolean onBlockActivated( World world, int x, int y, int z, EntityPlayer player, int par6, float par7, float par8, float par9 )
      {
        ItemStack stack  = player.inventory.getCurrentItem();
        TileDLS  tank  = ( TileDLS ) world.getTileEntity( x, y, z );
        int  remaining = tank.getCapacity() - tank.getFluidAmount();
      
        System.out.println( "remaining = " + Integer.toString( remaining  ) );
        System.out.println( "capacity  = " + Integer.toString( tank.getCapacity()  ) );
        System.out.println( "amount  = " + Integer.toString( tank.getFluidAmount() ) );
      
        if ( tank.getFluid() != null )
        {
          if ( FluidContainerRegistry.isFilledContainer( stack ) == true && FluidContainerRegistry.containsFluid( stack, tank.getFluid() ) == true )
          {
            if ( remaining >= FluidContainerRegistry.getContainerCapacity( stack ) )
            {
              tank.fill( FluidContainerRegistry.getFluidForFilledItem( stack ), false );
      
              if ( player.capabilities.isCreativeMode == false )
              {
                stack = FluidContainerRegistry.drainFluidContainer( stack );
              }
      
              return true;
            }
            else
            {
              return false;
            }
          }
          else
          {
            return false;
          }
        }
        else
        {
          tank.fill( FluidContainerRegistry.getFluidForFilledItem( stack ), false );
      
          if ( player.capabilities.isCreativeMode == false )
          {
            stack = FluidContainerRegistry.drainFluidContainer( stack );
          }
      
          return true;
        }
      }
    }
    
    

    Code:
    package RavynousHunter.DeepLiquidStorage.Entities;
    
    import net.minecraft.nbt.NBTTagCompound;
    import net.minecraft.tileentity.TileEntity;
    import net.minecraftforge.fluids.FluidStack;
    import net.minecraftforge.fluids.FluidTankInfo;
    import net.minecraftforge.fluids.IFluidTank;
    
    public class TileDLS
      extends TileEntity
      implements IFluidTank
    {
      private FluidStack Fluid;
      private int  RemainingFluid = Integer.MAX_VALUE - 1;
    
      public TileDLS()
      {
      }
    
      public boolean canFill()
      {
        return true;
      }
    
      public boolean canDrain()
      {
        return true;
      }
    
      @Override
      public FluidStack getFluid()
      {
        return this.Fluid;
      }
    
      @Override
      public int getFluidAmount()
      {
        if ( this.Fluid != null )
        {
          return this.Fluid.amount;
        }
        else
        {
          return 0;
        }
      }
    
      @Override
      public int getCapacity()
      {
        return Integer.MAX_VALUE;
      }
    
      @Override
      public FluidTankInfo getInfo()
      {
        return new FluidTankInfo( this.Fluid, this.getCapacity() );
      }
    
      @Override
      public int fill( FluidStack resource, boolean doFill )
      {
        if ( this.Fluid == null )
        {
          this.Fluid  = new FluidStack( resource, resource.amount );
          this.RemainingFluid -= resource.amount;
      
          return resource.amount;
        }
        else
        {
          if ( this.Fluid.isFluidEqual( resource ) == true )
          {
            if ( this.Fluid.amount < this.getCapacity() )
            {
              if ( this.RemainingFluid > resource.amount )
              {
                this.Fluid.amount  += resource.amount;
                this.RemainingFluid -= resource.amount;
      
                return resource.amount;
              }
              else // resource.amount > this.RemainingFluid
              {
                int FillAmount = resource.amount = this.RemainingFluid;
      
                this.Fluid.amount  += resource.amount - this.RemainingFluid;
                this.RemainingFluid  = 0;
      
                return FillAmount;
              }
            }
            else
            {
              return 0;
            }
          }
          else
          {
            return 0;
          }
        }
      }
    
      @Override
      public FluidStack drain( int maxDrain, boolean doDrain )
      {
        if ( this.Fluid.amount == 0 )
        {
          return new FluidStack( this.Fluid.fluidID, 0 );
        }
        if ( maxDrain >= this.Fluid.amount )
        {
          int DrainAmount = maxDrain - this.Fluid.amount;
      
          this.Fluid.amount  = 0;
          this.RemainingFluid  = this.getCapacity();
      
          return new FluidStack( this.Fluid.fluidID, DrainAmount );
        }
        else
        {
          this.Fluid.amount  -= maxDrain;
          this.RemainingFluid += maxDrain;
      
          return new FluidStack( this.Fluid.fluidID, maxDrain );
        }
      }
    
      @Override
      public void writeToNBT( NBTTagCompound nbt )
      {
        super.writeToNBT( nbt );
      }
    
      @Override
      public void readFromNBT( NBTTagCompound nbt )
      {
        super.readFromNBT( nbt );
      }
    }
    

    (Formatting is a little janked. Copying from Eclipse to here is always a headache.)
     
    Last edited: Jun 11, 2015
  2. ljfa

    ljfa New Member

    I think the problem is at "FluidContainerRegistry.containsFluid( stack, tank.getFluid() )". This only returns true if "stack" contains equally or more fluid than "tank.getFluid()" (see FluidStack.containsFluid). So it doesn't work when the tank has more than one bucket.
     
    RavynousHunter likes this.
  3. RavynousHunter

    RavynousHunter New Member

    Aaah, so a comparison by ID would be more appropriate, eh? Aight, then, I'll give that a try in a little bit, thankee.
     
  4. ljfa

    ljfa New Member

    Yeah, or FluidStack.isFluidEqual().
    Debugging with breakpoints helps a lot in cases like this.
     
  5. RavynousHunter

    RavynousHunter New Member

    Aah, I didn't think of that! Its surprising how often I use breakpoints...and how often I completely forget that they exist. :p
     
  6. RavynousHunter

    RavynousHunter New Member

    Got that problem fixed, but now its not saving the requisite data...hmm...
     
  7. ljfa

    ljfa New Member

    Did you implement writeToNBT and readFromNBT ?
     
  8. RavynousHunter

    RavynousHunter New Member

    Aye, managed to get it. Made a tag for fluid ID and amount stored. Now, to figure out everything else!
     

Share This Page