Jump to content

[1.11.2] FluidHandler capability on buckets


Tschipp

Recommended Posts

I'm trying to check if an ItemStack has the FluidHandler capability.

For that I tried using this:

		boolean hasCap = stack.hasCapability(CapabilityFluidHandler.FLUID_HANDLER_CAPABILITY, null);

The ItemStack passed was a ItemWaterBucket. I've also tried it using forge filled buckets, but both alway return false. Am I checking for the wrong capability?

Link to comment
Share on other sites

Fluid handler items use CapabilityFluidHandler.FLUID_HANDLER_ITEM_CAPABILITY instead of CapabilityFluidHandler.FLUID_HANDLER_CAPABILITY.

 

I'm not sure exactly what you're doing with the fluid handler item, but you may want to check if the FluidUtil class already has a method that does what you want.

  • Like 1

Please don't PM me to ask for help. Asking your question in a public thread preserves it for people who are having the same problem in the future.

Link to comment
Share on other sites

Just now, Tschipp said:

Is there also a ItemHandlerUtil of some sort for ItemStackHandlers?

 

Yes, ItemHandlerHelper.

Please don't PM me to ask for help. Asking your question in a public thread preserves it for people who are having the same problem in the future.

Link to comment
Share on other sites

20 minutes ago, Tschipp said:

Thanks. One more thing:

This doesn't seem to drain the bucket:


FluidUtil.getFluidHandler(heldItem).drain(1000, true);

the FluidHandler isn't null, i do that check beforehand. What am I missing?

If I recall correctly, the second parameter of the drain method is about whether it'll be simulated(to check the validity) or the change is actually applied. So it should be false in your csse.

I. Stellarium for Minecraft: Configurable Universe for Minecraft! (WIP)

II. Stellar Sky, Better Star Rendering&Sky Utility mod, had separated from Stellarium.

Link to comment
Share on other sites

4 minutes ago, Abastro said:

If I recall correctly, the second parameter of the drain method is about whether it'll be simulated(to check the validity) or the change is actually applied. So it should be false in your csse.

I've tried both true and false, still doesn't empty the bucket.

 

Link to comment
Share on other sites

8 minutes ago, Tschipp said:

I've tried both true and false, still doesn't empty the bucket.

 

Well, I recalled it incorrectly :P

What did you get as return from the method?

I. Stellarium for Minecraft: Configurable Universe for Minecraft! (WIP)

II. Stellar Sky, Better Star Rendering&Sky Utility mod, had separated from Stellarium.

Link to comment
Share on other sites

Block#onBlockActivated, but only if !world.isRemote

 

else if(hasFluid(heldItem, BlockHandler.fluidSlop) && te.itemHandler.getStackInSlot(0).isEmpty())
		{
			te.fluidHandler.fill(new FluidStack(BlockHandler.fluidSlop, 1000), true);
			worldIn.playSound(null, pos.getX(), pos.getY(), pos.getZ(), SoundEvents.BLOCK_SLIME_PLACE, SoundCategory.PLAYERS, 0.6F, 0.8F);
			if(!playerIn.isCreative())
			{
				FluidStack fluid = FluidUtil.getFluidHandler(heldItem).drain(1000, true);
			}
			return true;

		}

 

Edited by Tschipp
Link to comment
Share on other sites

@Override
	public boolean onBlockActivated(World worldIn, BlockPos pos, IBlockState state, EntityPlayer playerIn,
			EnumHand hand, EnumFacing facing, float hitX, float hitY, float hitZ) {

		ItemStack heldItem = playerIn.getHeldItem(hand);
		
		TileEntityTrough te = (TileEntityTrough) worldIn.getTileEntity(pos);

		if (worldIn.isRemote) {
			return true;
 
		}  
		//SOLIDS
		else if(!heldItem.isEmpty() && heldItem.getItem() == Items.WHEAT)
		{
			if((!te.itemHandler.getStackInSlot(0).isEmpty() && te.itemHandler.getStackInSlot(0).getCount() < 3) || te.itemHandler.getStackInSlot(0).isEmpty() && te.fluidHandler.getFluid() == null)
			{
				te.itemHandler.insertItem(0, new ItemStack(Items.WHEAT), false);
				if(!playerIn.isCreative())
					heldItem.shrink(1);
				return true;
			}

		}
		//LIQUIDS
		else if(hasFluid(heldItem, FluidRegistry.WATER) && te.itemHandler.getStackInSlot(0).isEmpty())
		{
			te.fluidHandler.fill(new FluidStack(FluidRegistry.WATER, 1000), true);
			worldIn.playSound(null, pos.getX(), pos.getY(), pos.getZ(), SoundEvents.ITEM_BUCKET_EMPTY, SoundCategory.PLAYERS, 0.6F, 0.8F);
			if(!playerIn.isCreative())
				FluidUtil.getFluidHandler(heldItem).drain(1000, true);
			return true;

		}
		else if(hasFluid(heldItem, BlockHandler.fluidSlop) && te.itemHandler.getStackInSlot(0).isEmpty())
		{
			te.fluidHandler.fill(new FluidStack(BlockHandler.fluidSlop, 1000), true);
			worldIn.playSound(null, pos.getX(), pos.getY(), pos.getZ(), SoundEvents.BLOCK_SLIME_PLACE, SoundCategory.PLAYERS, 0.6F, 0.8F);
			if(!playerIn.isCreative())
			{
				FluidStack fluid = FluidUtil.getFluidHandler(heldItem).drain(1000, true);
			}
			return true;

		}
		return false;

	}

 

Link to comment
Share on other sites

14 minutes ago, diesieben07 said:

The IFluidHandlerItem might have to create a new ItemStack to allow for the modifications (e.g. when emptying a vanilla bucket, because they are different items). You can access the "up to date" stack with IFluidHandlerItem::getContainer. In this case you have to put it back in the player's hand.

That did the trick, thanks!

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.