Search code examples
minecraftminecraft-forge

1.20 Minecraft Forge Bed Model not lining with Boundry box


Another issue. My Bed model is not lining up properly with the boundary box. IT works perfectly when facing south or east, but facing north or west puts the block one block towards east or south. So the model and the block boundary do not line up.

Image of the boundary block and model not aligning properly.

My code is:

package io.github.magikpups.testing.block;

import net.minecraft.core.BlockPos;
import net.minecraft.core.Direction;
import net.minecraft.core.Holder;
import net.minecraft.world.item.context.BlockPlaceContext;
import net.minecraft.world.level.BlockGetter;
import net.minecraft.world.level.block.BedBlock;
import net.minecraft.world.level.block.Block;
import net.minecraft.world.level.block.EntityBlock;
import net.minecraft.world.level.block.HorizontalDirectionalBlock;
import net.minecraft.world.level.block.state.BlockState;
import net.minecraft.world.level.block.state.StateDefinition;
import net.minecraft.world.level.block.state.properties.BlockStateProperties;
import net.minecraft.world.level.block.state.properties.DirectionProperty;
import net.minecraft.world.phys.shapes.CollisionContext;
import net.minecraft.world.phys.shapes.Shapes;
import net.minecraft.world.phys.shapes.VoxelShape;
import org.jetbrains.annotations.NotNull;

import java.awt.*;

public class advhopebed extends HorizontalDirectionalBlock {

    public static final VoxelShape SHAPE_N = Shapes.box(0, 0, 0,
            1, 0.55, 2);
    public static final VoxelShape SHAPE_S = Shapes.box(0, 0, 0,
            1, 0.55, 2);
    public static final VoxelShape SHAPE_E = Shapes.box(0, 0, 0,
            2, 0.55, 1);
    public static final VoxelShape SHAPE_W = Shapes.box(0, 0, 0,
            2, 0.55, 1);

    public static final DirectionProperty FACING = BlockStateProperties.HORIZONTAL_FACING;

    public advhopebed(Properties properties) {
        super(properties);
        this.registerDefaultState(this.stateDefinition.any().setValue(FACING, Direction.NORTH));
    }

    @Override

    public VoxelShape getShape(BlockState state, BlockGetter getter, BlockPos pos, CollisionContext context) {
        Direction direction = state.getValue(FACING);
        switch(state.getValue(FACING)) {
            case EAST:
                return SHAPE_E;
            case SOUTH:
                return SHAPE_S;
            case WEST:
                return SHAPE_W;
            default:
                return SHAPE_N;
        }
    }

    public BlockState getStateForPlacement(BlockPlaceContext context) {
        return this.defaultBlockState().setValue(FACING, context.getHorizontalDirection().getOpposite());
    }

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

I've tried messing more with the VoxelShapes to see if that was the issue, trying to mess with the blockshapes as well. As far as I can tell it might have something to do with orientation or FACING but I've yet to figure out exactly how it works.

I've looked through other's codes that use beds but I can't find why my boundary box is so different from theirs.


Solution

  • Figured this one out with help from someone on discord.

    Had to change my Voxelshapes which I knew but didn't understand how. Here's the updated Voxel Shape code.

    public static final VoxelShape SHAPE_N = Shapes.box(0, 0, 0,
                1, 0.55, 2);
        public static final VoxelShape SHAPE_S = Shapes.box(0, 0, -1,
                1, 0.55, 1);
        public static final VoxelShape SHAPE_E = Shapes.box(-1, 0, 0,
                1, 0.55, 1);
        public static final VoxelShape SHAPE_W = Shapes.box(0, 0, 0,
                2, 0.55, 1);
    

    Now all the models fit with the model boxes