Jump to content

Recommended Posts

Posted

My custom block has its hitbox facing the opposite direction from where it is placed, while the model itself faces towards the player, the hitbox faces away from the player.

 


package sword.red.main.init;


import net.minecraft.block.Block;
import net.minecraft.block.BlockState;
import net.minecraft.block.HorizontalBlock;
import net.minecraft.item.BlockItemUseContext;
import net.minecraft.state.StateContainer;
import net.minecraft.util.math.BlockPos;
import net.minecraft.util.math.shapes.IBooleanFunction;
import net.minecraft.util.math.shapes.ISelectionContext;
import net.minecraft.util.math.shapes.VoxelShape;
import net.minecraft.util.math.shapes.VoxelShapes;
import net.minecraft.world.IBlockReader;

import javax.annotation.Nullable;
import java.util.stream.Stream;

public class ModSimpBlock extends HorizontalBlock {
    public ModSimpBlock(Properties builder) {
        super(builder);
    }

    private static final VoxelShape SHAPE_S = Stream.of(
    		Block.box(5, 17, 6, 11, 18, 11),
    		Block.box(7, 16, 7, 9, 17, 9),
    		Block.box(5, 8, 7, 11, 16, 10),
    		Block.box(5, 9, 6, 11, 12, 7),
    		Block.box(2, 8, 7, 4, 16, 10),
    		Block.box(4, 14, 8, 5, 15, 9),
    		Block.box(11, 14, 8, 12, 15, 9),
    		Block.box(12, 8, 7, 14, 16, 10),
    		Block.box(5, 0, 7, 7, 8, 9),
    		Block.box(9, 0, 7, 11, 8, 9),
    		Block.box(9, 0, 9, 11, 1, 11),
    		Block.box(5, 0, 9, 7, 1, 11),
    		Block.box(5, 9, 10, 11, 15, 11),
    		Block.box(5, 10, 11, 11, 15, 12),
    		Block.box(5, 19, 6, 11, 24, 12),
    		Block.box(5, 18, 6, 11, 19, 12),
    		Block.box(5, 24, 9, 8, 25, 10),
    		Block.box(8, 24, 12, 11, 25, 13),
    		Block.box(5, 24, 11, 8, 25, 12),
    		Block.box(8, 24, 10, 11, 25, 11),
    		Block.box(8, 24, 8, 11, 25, 9),
    		Block.box(5, 24, 7, 8, 25, 8),
    		Block.box(8, 24, 6, 11, 25, 7),
    		Block.box(5, 24, 5, 8, 25, 6),
    		Block.box(9, 21, 12, 10, 22, 13),
    		Block.box(6, 22, 12, 7, 23, 13),
    		Block.box(7, 19, 12, 9, 20, 13)
    		).reduce((v1, v2) -> VoxelShapes.join(v1, v2, IBooleanFunction.OR)).get();

    private static final VoxelShape SHAPE_E = Stream.of(
    		Block.box(5, 17, 6, 10, 18, 12),
    		Block.box(6, 16, 8, 8, 17, 10),
    		Block.box(6, 8, 6, 9, 16, 12),
    		Block.box(5, 9, 6, 6, 12, 12),
    		Block.box(6, 8, 13, 9, 16, 15),
    		Block.box(7, 14, 12, 8, 15, 13),
    		Block.box(7, 14, 5, 8, 15, 6),
    		Block.box(6, 8, 3, 9, 16, 5),
    		Block.box(6, 0, 10, 8, 8, 12),
    		Block.box(6, 0, 6, 8, 8, 8),
    		Block.box(8, 0, 6, 10, 1, 8),
    		Block.box(8, 0, 10, 10, 1, 12),
    		Block.box(9, 9, 6, 10, 15, 12),
    		Block.box(10, 10, 6, 11, 15, 12),
    		Block.box(5, 19, 6, 11, 24, 12),
    		Block.box(5, 18, 6, 11, 19, 12),
    		Block.box(8, 24, 9, 9, 25, 12),
    		Block.box(11, 24, 6, 12, 25, 9),
    		Block.box(10, 24, 9, 11, 25, 12),
    		Block.box(9, 24, 6, 10, 25, 9),
    		Block.box(7, 24, 6, 8, 25, 9),
    		Block.box(6, 24, 9, 7, 25, 12),
    		Block.box(5, 24, 6, 6, 25, 9),
    		Block.box(4, 24, 9, 5, 25, 12),
    		Block.box(11, 21, 7, 12, 22, 8),
    		Block.box(11, 22, 10, 12, 23, 11),
    		Block.box(11, 19, 8, 12, 20, 10)
    		).reduce((v1, v2) -> VoxelShapes.join(v1, v2, IBooleanFunction.OR)).get();

    private static final VoxelShape SHAPE_N = Stream.of(
    		Block.box(5, 17, 7, 11, 18, 12),
    		Block.box(7, 16, 9, 9, 17, 11),
    		Block.box(5, 8, 8, 11, 16, 11),
    		Block.box(5, 9, 11, 11, 12, 12),
    		Block.box(12, 8, 8, 14, 16, 11),
    		Block.box(11, 14, 9, 12, 15, 10),
    		Block.box(4, 14, 9, 5, 15, 10),
    		Block.box(2, 8, 8, 4, 16, 11),
    		Block.box(9, 0, 9, 11, 8, 11),
    		Block.box(5, 0, 9, 7, 8, 11),
    		Block.box(5, 0, 7, 7, 1, 9),
    		Block.box(9, 0, 7, 11, 1, 9),
    		Block.box(5, 9, 7, 11, 15, 8),
    		Block.box(5, 10, 6, 11, 15, 7),
    		Block.box(5, 19, 6, 11, 24, 12),
    		Block.box(5, 18, 6, 11, 19, 12),
    		Block.box(8, 24, 8, 11, 25, 9),
    		Block.box(5, 24, 5, 8, 25, 6),
    		Block.box(8, 24, 6, 11, 25, 7),
    		Block.box(5, 24, 7, 8, 25, 8),
    		Block.box(5, 24, 9, 8, 25, 10),
    		Block.box(8, 24, 10, 11, 25, 11),
    		Block.box(5, 24, 11, 8, 25, 12),
    		Block.box(8, 24, 12, 11, 25, 13),
    		Block.box(6, 21, 5, 7, 22, 6),
    		Block.box(9, 22, 5, 10, 23, 6),
    		Block.box(7, 19, 5, 9, 20, 6)
    		).reduce((v1, v2) -> VoxelShapes.join(v1, v2, IBooleanFunction.OR)).get();

    private static final VoxelShape SHAPE_W = Stream.of(
    		Block.box(6, 17, 6, 11, 18, 12),
    		Block.box(8, 16, 8, 10, 17, 10),
    		Block.box(7, 8, 6, 10, 16, 12),
    		Block.box(10, 9, 6, 11, 12, 12),
    		Block.box(7, 8, 3, 10, 16, 5),
    		Block.box(8, 14, 5, 9, 15, 6),
    		Block.box(8, 14, 12, 9, 15, 13),
    		Block.box(7, 8, 13, 10, 16, 15),
    		Block.box(8, 0, 6, 10, 8, 8),
    		Block.box(8, 0, 10, 10, 8, 12),
    		Block.box(6, 0, 10, 8, 1, 12),
    		Block.box(6, 0, 6, 8, 1, 8),
    		Block.box(6, 9, 6, 7, 15, 12),
    		Block.box(5, 10, 6, 6, 15, 12),
    		Block.box(5, 19, 6, 11, 24, 12),
    		Block.box(5, 18, 6, 11, 19, 12),
    		Block.box(7, 24, 6, 8, 25, 9),
    		Block.box(4, 24, 9, 5, 25, 12),
    		Block.box(5, 24, 6, 6, 25, 9),
    		Block.box(6, 24, 9, 7, 25, 12),
    		Block.box(8, 24, 9, 9, 25, 12),
    		Block.box(9, 24, 6, 10, 25, 9),
    		Block.box(10, 24, 9, 11, 25, 12),
    		Block.box(11, 24, 6, 12, 25, 9),
    		Block.box(4, 21, 10, 5, 22, 11),
    		Block.box(4, 22, 7, 5, 23, 8),
    		Block.box(4, 19, 8, 5, 20, 10)
    		).reduce((v1, v2) -> VoxelShapes.join(v1, v2, IBooleanFunction.OR)).get();

    @Override
    public VoxelShape getShape(BlockState state, IBlockReader worldIn, BlockPos pos, ISelectionContext context) {
        switch (state.getValue(FACING)) {
            case NORTH:
                return SHAPE_N;
            case SOUTH:
                return SHAPE_S;
            case WEST:
                return SHAPE_W;
            case EAST:
                return SHAPE_E;
            default:
                return SHAPE_N;
        }
    }

    @Override
    protected void createBlockStateDefinition(StateContainer.Builder<Block, BlockState> builder) {
        builder.add(FACING);
    }

    @Nullable
    @Override
    public BlockState getStateForPlacement(BlockItemUseContext context) {
        return this.defaultBlockState().setValue(FACING, context.getHorizontalDirection());
    }
}

 

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.