Support getHitVec for right-click actions

Started by NorthWestTrees on

Topic category: Feature requests and ideas for MCreator

Support getHitVec for right-click actions

We need a way to test for the player's right-click block face location. From what I can read from slabs this is a number value of 0.5B (byte)

I think I asked Klemen to add this a long time ago before I could read the code and knew about the game files, we did get some support for block faces but not exactly what I was trying to ask for. What I meant to say was the position of the block right-click action slabs use > 0.5D as seen below under SlabBlock.java file under net/minecraft/block I have taken a clip of the code from the file to show as an example what I am describing.

EXAMPLE 1 CODE (SlabBlock.java)

   @Nullable
   public BlockState getStateForPlacement(BlockItemUseContext context) {
      BlockPos blockpos = context.getPos();
      BlockState blockstate = context.getWorld().getBlockState(blockpos);
      if (blockstate.isIn(this)) {
         return blockstate.with(TYPE, SlabType.DOUBLE).with(WATERLOGGED, Boolean.valueOf(false));
      } else {
         FluidState fluidstate = context.getWorld().getFluidState(blockpos);
         BlockState blockstate1 = this.getDefaultState().with(TYPE, SlabType.BOTTOM).with(WATERLOGGED, Boolean.valueOf(fluidstate.getFluid() == Fluids.WATER));
         Direction direction = context.getFace();
         return direction != Direction.DOWN && (direction == Direction.UP || !(context.getHitVec().y - (double)blockpos.getY() > 0.5D)) ? blockstate1 : blockstate1.with(TYPE, SlabType.TOP);
      }
   }

   public boolean isReplaceable(BlockState state, BlockItemUseContext useContext) {
      ItemStack itemstack = useContext.getItem();
      SlabType slabtype = state.get(TYPE);
      if (slabtype != SlabType.DOUBLE && itemstack.getItem() == this.asItem()) {
         if (useContext.replacingClickedOnBlock()) {
            boolean flag = useContext.getHitVec().y - (double)useContext.getPos().getY() > 0.5D;
            Direction direction = useContext.getFace();
            if (slabtype == SlabType.BOTTOM) {
               return direction == Direction.UP || flag && direction.getAxis().isHorizontal();
            } else {
               return direction == Direction.DOWN || !flag && direction.getAxis().isHorizontal();
            }
         } else {
            return true;
         }
      } else {
         return false;
      }
   }

As seen below

EXAMPLE 2 CODE PART (SlabBlock.java)

!(context.getHitVec().y - (double)blockpos.getY() > 0.5D))

This "getHitVec().y" may control the position of the right click the > would be testing if NOT above 0.5 double which I assume means not the upper half of the block being right-click on. the lower half being =< 0.5 Y.

If we could have support for X, Y and Z HitVec for getting the value for both right-click on block for global and block procedures this would be really helpful for making more things like micro blocks or custom slabs/stair blocks. Its not possible currently to make custom slabs with overlays like grass or leaves due to not being able to test for the HitVec of the right-click event, stairs also use this code to flip the block upside down from what I can tell from testing in-game.

I posted on the MCToolkit…
Wed, 08/11/2021 - 17:25

I posted on the MCToolkit discord as well just now, and your welcome for the code :D

This part of the slab wiki explains it a bit more in detail as well.
https://minecraft.fandom.com/wiki/Slab#Placement