Posted August 24, 20196 yr for some reason getFacingFromEntity always gives me the opposite facing. when looking down it gives up, when looking north it gives south etc. I'm using it when placing certain blocks. @Override public void onBlockPlacedBy(World world, BlockPos pos, BlockState state, @Nullable LivingEntity entity, ItemStack stack) { if (entity != null) { world.setBlockState(pos, state.with(BlockStateProperties.FACING, getFacingFromEntity(pos, entity)), 2); LOGGER.info(getFacingFromEntity(pos, entity).toString()); } } [EDIT] changed title from "[1.14.4] getFacingFromEntity gives opposite facing" to "[1.14.4] set block facing direction based on player" to make it more relevant to the actual topic discussed. [SOLUTION]-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- getFacingFromEntity was a method copied from this tutorial. instead of overriding onBlockPlacedBy, you should override getStateForPlacement. @Nullable @Override public BlockState getStateForPlacement(BlockItemUseContext context) { BlockState blockstate = super.getStateForPlacement(context); if (blockstate != null) { blockstate = blockstate.with(BlockStateProperties.FACING, context.getNearestLookingDirection()); } return blockstate; } Edited August 26, 20196 yr by andGarrett
August 24, 20196 yr getFacingFromEntity doesn't appear to exist in Vanilla/Forge. Is it your own method? You should be using Block#getStateForPlacement to determine the placed BlockState, this provides you with an instance of BlockItemUseContext which has the BlockItemUseContext#getNearestLookingDirection method to get the facing from the entity placing the block. 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.
August 25, 20196 yr Author 4 hours ago, Choonster said: getFacingFromEntity doesn't appear to exist in Vanilla/Forge. Is it your own method? You should be using Block#getStateForPlacement to determine the placed BlockState, this provides you with an instance of BlockItemUseContext which has the BlockItemUseContext#getNearestLookingDirection method to get the facing from the entity placing the block. I couldn't figure out how to get the BlockItemUseContext without overriding the getStateForPlacement method and storing the context in an instance field. is that the correct way to do it? something about storing the data in an instance field feels wrong, but it works. here's the relevant code: @Override public void onBlockPlacedBy(World world, BlockPos pos, BlockState state, @Nullable LivingEntity entity, ItemStack stack) { if (entity != null) { world.setBlockState(pos, state.with(BlockStateProperties.FACING, context.getNearestLookingDirection()), 2); LOGGER.info(getFacingFromEntity(pos, entity).getOpposite().toString()); } } @Nullable @Override public BlockState getStateForPlacement(BlockItemUseContext context) { this.context = context; return super.getStateForPlacement(context); }
August 25, 20196 yr 7 minutes ago, andGarrett said: is that the correct way to do it? No. Use the context in the method and return the BlockState you want placed. VANILLA MINECRAFT CLASSES ARE THE BEST RESOURCES WHEN MODDING I will be posting 1.15.2 modding tutorials on this channel. If you want to be notified of it do the normal YouTube stuff like subscribing, ect. Forge and vanilla BlockState generator.
August 25, 20196 yr Author 3 hours ago, Animefan8888 said: No. Use the context in the method and return the BlockState you want placed. I'm not sure what you mean. use the context in the "onBlockPlacedBy" method or the "getStateForPlacement" method? I don't know how to get the context any other way.
August 25, 20196 yr All you need to do is override Block#getStateForPlacement to return the state with the FACING property set based on the BlockItemUseContext. You don't need to override Block#onBlockPlacedBy or set the state in the World. 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.
August 25, 20196 yr Author 14 hours ago, Choonster said: All you need to do is override Block#getStateForPlacement to return the state with the FACING property set based on the BlockItemUseContext. You don't need to override Block#onBlockPlacedBy or set the state in the World. I think I got it now. Thanks for all the help y'all! @Nullable @Override public BlockState getStateForPlacement(BlockItemUseContext context) { BlockState blockstate = super.getStateForPlacement(context); if (blockstate != null) { blockstate = blockstate.with(BlockStateProperties.FACING, context.getNearestLookingDirection()); } return blockstate; }
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.