Jump to content

[1.14.4][SOLVED] set block facing direction based on player


andGarrett

Recommended Posts

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 by andGarrett
Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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);
    }

 

Link to comment
Share on other sites

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.

  • Thanks 1

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.

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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.

  • Thanks 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

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;
    }

 

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.