Before I get onto the bucket code, I almost forgot - register your BlockFluidClassic instance in your proxy like you would for any other block.
For the ItemBucket class, the only difference from a normal Item is that you pass the
super
constructor a second value: the id of your BlockFluidClassic class, which will be automatically placed when you right click.
Then, somewhere in initialisation, use the FluidContainerRegistry to register this item as a container holding one bucket's worth of that fluid:
FluidContainerRegistry.registerFluidContainer(
new FluidContainerData(
FluidRegistry.getFluidStack( ModExample.fluidExample.getName(), FluidContainerRegistry.BUCKET_VOLUME ),
new ItemStack( ModExample.bucketExample ),
new ItemStack( Item.bucketEmpty )
)
);
Finally, we need to tell minecraft to fill a bucket with this fluid whenever it is right clicked on with a bucket in hand. Fortunately, Forge provides an event listener that fires when you right click with a bucket.
@ForgeSubscribe
public void onBucketFill( FillBucketEvent event )
{
ItemStack result = attemptFill( event.world, event.target );
if ( result != null )
{
event.result = result;
event.setResult( Result.ALLOW );
}
}
where attemptFill( World, MovingObjectPosition ) is a function that checks what the picked block is, and returns an ItemStack of the relevant filled container if applicable:
private ItemStack attemptFill( World world, MovingObjectPosition p )
{
int id = world.getBlockId( p.blockX, p.blockY, p.blockZ );
if ( id == ModExample.blockFluidExample.blockID )
{
if ( world.getBlockMetadata( p.blockX, p.blockY, p.blockZ ) == 0 ) // Check that it is a source block
{
world.setBlock( p.blockX, p.blockY, p.blockZ, 0 ); // Remove the fluid block
return new ItemStack( ModExample.bucketExample );
}
}
return null;
}
Phew. I think that's all, though. If it doesn't work, I'll check to see if I've missed anything.