Jump to content

[1.12.2] [SOLVED] getPickBlock and FluidHandler


Terrails

Recommended Posts

I have an fluid tank which has fluid handler item capability and fluid handler capability. I tried using getPickBlock so players in creative can get the filled tank in their inventory but for some reason its working only on an integrated server, when I try it on a dedicated server for some reason its only called client side, I tried using !world.isRemote but then the code doesn't run at all. This is the code I'm using:

    @Override
    public ItemStack getPickBlock(IBlockState state, RayTraceResult target, World world, BlockPos pos, EntityPlayer player) {
        ItemStack stack = new ItemStack(this);
        IFluidHandler fluidHandler = FluidUtil.getFluidHandler(world, pos, null);
        if (fluidHandler != null) {
            FluidActionResult fluidActionResult = FluidUtil.tryFillContainer(new ItemStack(this), fluidHandler, Integer.MAX_VALUE, null, false);

            if (fluidActionResult.isSuccess()) {
                stack = fluidActionResult.getResult();
            }
        }
        return stack;
    }

 

Edited by Terrails
Link to comment
Share on other sites

1 hour ago, Major Tuvok said:

I've never used getPickBlock before but you seem to fill the wrong container...

You are creating a variable called stack, but you fill a completly different instance...

I'm creating a variable so I can return an empty item if IFluidHandler is null, but when IFluidHandler isn't null I try to fill a new instance and if its filled I set the variable to that new instance.

59 minutes ago, Major Tuvok said:

Although is it possible, that this getFluidHandler Methods accessed TileEntities? Because it might be that the TileEntity you're trying to access doesn't exist and it is therefore returning null... 

Yes it needs to access the TileEntity, it checks if block at pos has tileentity and if it has it, then checks if it has fluid handler capability and returns the IFluidHandler if it has the capability, if not it returns null. FluidUtil is a class in minecraftforge itself.

 

But like I said on server it only launches with WorldClient and if I check if world isn't remote it doesn't even run

Edited by Terrails
Link to comment
Share on other sites

I now tried something simple like this, but it still doesn't work

    @Override
    public ItemStack getPickBlock(IBlockState state, RayTraceResult target, World world, BlockPos pos, EntityPlayer player) {
        ItemStack stack = new ItemStack(this);
        IFluidHandler fluidHandler = getFluidHandler(world, pos); // Gets the tile at pos and FLUID_HANDLER_CAPABILITY from it
        IFluidHandlerItem fluidHandlerItem = FluidUtil.getFluidHandler(stack);

        if (fluidHandler != null && fluidHandlerItem != null) {
            fluidHandlerItem.fill(fluidHandler.drain(Integer.MAX_VALUE, false), true);
            return fluidHandlerItem.getContainer(); // Tried without this but it makes no difference
        }

        return stack;
    }

I just fill the item fluid handler with drained fluid stack from the tile, the doDrain in drain method is false because I don't want it to drain the tile entity.

 

also this is how I display the fluid amount in the ItemBlock class

    @Override
    public void addInformation(ItemStack stack, @Nullable World worldIn, List<String> tooltip, ITooltipFlag flagIn) {
        IFluidHandler fluidHandler = FluidUtil.getFluidHandler(stack);
        if (fluidHandler == null)
            return;

        for (IFluidTankProperties properties : fluidHandler.getTankProperties()) {
            FluidStack fluidStack = properties.getContents();

            if (fluidStack == null)
                return;

            tooltip.add(fluidStack.amount + "/" + properties.getCapacity());
        }
    }

I know it didn't fill it because fluidStack is null

 

EDIT: Looks like I fixed it, in initCapabilities of my itemblock I used my own handler, now tried FluidHandlerItemStack and it works

Edited by Terrails
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.