How to make different block hitbox for each blockstate?

Started by Cdztw on

Topic category: Advanced modding

Joined Feb 2018
Points:

User statistics:

  • Modifications:
  • Forum topics:
  • Wiki pages:
  • MCreator plugins:
  • Comments:
How to make different block hitbox for each blockstate?
Sun, 06/18/2023 - 02:25 (edited)

Hello there, I'm trying to make a custom fence. It was almost done except for block hitbox.
This fence have some custom boolean properties I created: "NORTH", "EAST", "SOUTH", "WEST" and "UP".

Screenshot of custom fence and F3 menu.

I want to make this block's hitbox work like this:

  • If north is true, add a box into hitbox (box: 7, 7, 0, 9, 9, 8)
  • If east is true, add a box into hitbox (box: 8, 7, 7, 16, 9, 9)
  • If south is true, add a box into hitbox (box: 7, 7, 8, 9, 9, 16)
  • If west is true, add a box into hitbox (box: 0, 7, 7, 8, 9, 9)
  • If up is true, add a box into hitbox (box: 6, 0, 0, 10, 16, 10)
  • If none of above is true, add a box into hitbox (box: 6, 0, 0, 10, 16, 10)

But I don't know how to do that. I searched up some tutorials, but them seem to be not suitable for the version I use.
I only knows how to make a static hitbox that won't adapt the block's custom state.
I messed around the codes and of course, nothing works. 

Can someone help me? That will be a huge appreciated.
The version of MCreator I currently running is 2023.2. 
Minecraft version is 1.19.2.
Here is parts of my code:

@Override
public VoxelShape getShape(BlockState state, BlockGetter world, BlockPos pos, CollisionContext context) {
	/*
	Shapes BLOCKSHAPE = new Shapes();
	if (state.getValue(NORTH))
	BLOCKSHAPE.or(box(7, 7, 0, 9, 9, 8));
	if (state.getValue(EAST))
	BLOCKSHAPE.or(BLOCKSHAPE.block(), box(8, 7, 7, 16, 9, 9));
	if (state.getValue(SOUTH))
	BLOCKSHAPE.or(BLOCKSHAPE.block(), box(7, 7, 8, 9, 9, 16));
	if (state.getValue(WEST))
	BLOCKSHAPE.or(BLOCKSHAPE.block(), box(0, 7, 7, 8, 9, 9));
	if (state.getValue(UP))
	BLOCKSHAPE.or(BLOCKSHAPE.block(), box(6, 0, 6, 10, 16, 10));
	*/
	return box(6, 0, 6, 10, 16, 10);
}

 

 

 

 

 

Edited by Cdztw on Sun, 06/18/2023 - 02:25
Joined Feb 2018
Points:

User statistics:

  • Modifications:
  • Forum topics:
  • Wiki pages:
  • MCreator plugins:
  • Comments:
nvmI got the solution, works…
Mon, 07/10/2023 - 11:06

nvm
I got the solution, works as expected but it needs lot of if-else statements.

@Override
public VoxelShape getShape(BlockState state, BlockGetter world, BlockPos pos, CollisionContext context) {
    boolean stateUp = state.getValue(UP);
    boolean stateNorth = state.getValue(NORTH);
    boolean stateEast = state.getValue(EAST);
    boolean stateSouth = state.getValue(SOUTH);
    boolean stateWest = state.getValue(WEST);
    if (stateUp && stateNorth && stateEast && stateSouth && stateWest)
        return Shapes.or(box(7, 7, 0, 9, 9, 16), box(0, 7, 7, 16, 9, 9), box(6, 0, 6, 10, 16, 10));
    else if (stateUp && stateNorth && stateEast && stateSouth)
        return Shapes.or(box(7, 7, 0, 9, 9, 16), box(8, 7, 7, 16, 9, 9), box(6, 0, 6, 10, 16, 10));
    else if (stateUp && stateNorth && stateEast && stateWest)
        return Shapes.or(box(7, 7, 0, 9, 9, 8), box(0, 7, 7, 16, 9, 9), box(6, 0, 6, 10, 16, 10));
    else if (stateUp && stateNorth && stateSouth && stateWest)
        return Shapes.or(box(7, 7, 0, 9, 9, 16), box(0, 7, 7, 8, 9, 9), box(6, 0, 6, 10, 16, 10));
    else if (stateUp && stateEast && stateSouth && stateWest)
        return Shapes.or(box(7, 7, 8, 9, 9, 16), box(0, 7, 7, 16, 9, 9), box(6, 0, 6, 10, 16, 10));
    else if (stateUp && stateNorth && stateEast)
        return Shapes.or(box(7, 7, 0, 9, 9, 8), box(8, 7, 7, 16, 9, 9), box(6, 0, 6, 10, 16, 10));
    else if (stateUp && stateNorth && stateSouth)
        return Shapes.or(box(7, 7, 0, 9, 9, 16), box(6, 0, 6, 10, 16, 10));
    else if (stateUp && stateNorth && stateWest)
        return Shapes.or(box(7, 7, 0, 9, 9, 8), box(0, 7, 7, 8, 9, 9), box(6, 0, 6, 10, 16, 10));
    else if (stateUp && stateEast && stateSouth)
        return Shapes.or(box(8, 7, 7, 16, 9, 9), box(7, 7, 8, 9, 9, 16), box(6, 0, 6, 10, 16, 10));
    else if (stateUp && stateEast && stateWest)
        return Shapes.or(box(0, 7, 7, 16, 9, 9), box(6, 0, 6, 10, 16, 10));
    else if (stateUp && stateSouth && stateWest)
        return Shapes.or(box(7, 7, 8, 9, 9, 16), box(0, 7, 7, 8, 9, 9), box(6, 0, 6, 10, 16, 10));
    else if (stateUp && stateNorth)
        return Shapes.or(box(7, 7, 0, 9, 9, 8), box(6, 0, 6, 10, 16, 10));
    else if (stateUp && stateEast)
        return Shapes.or(box(8, 7, 7, 16, 9, 9), box(6, 0, 6, 10, 16, 10));
    else if (stateUp && stateSouth)
        return Shapes.or(box(7, 7, 8, 9, 9, 16), box(6, 0, 6, 10, 16, 10));
    else if (stateUp && stateWest)
        return Shapes.or(box(0, 7, 7, 8, 9, 9), box(6, 0, 6, 10, 16, 10));
    else if (stateNorth && stateEast && stateSouth && stateWest)
        return Shapes.or(box(7, 7, 0, 9, 9, 16), box(0, 7, 7, 16, 9, 9));
    else if (stateNorth && stateEast && stateSouth)
        return Shapes.or(box(7, 7, 0, 9, 9, 16), box(8, 7, 7, 16, 9, 9));
    else if (stateNorth && stateEast && stateWest)
        return Shapes.or(box(7, 7, 0, 9, 9, 8), box(0, 7, 7, 16, 9, 9));
    else if (stateNorth && stateSouth && stateWest)
        return Shapes.or(box(7, 7, 0, 9, 9, 16), box(0, 7, 7, 8, 9, 9));
    else if (stateEast && stateSouth && stateWest)
        return Shapes.or(box(7, 7, 8, 9, 9, 16), box(0, 7, 7, 16, 9, 9));
    else if (stateNorth && stateEast)
        return Shapes.or(box(7, 7, 0, 9, 9, 8), box(8, 7, 7, 16, 9, 9));
    else if (stateNorth && stateSouth)
        return Shapes.or(box(7, 7, 0, 9, 9, 16));
    else if (stateNorth && stateWest)
        return Shapes.or(box(7, 7, 0, 9, 9, 8), box(0, 7, 7, 8, 9, 9));
    else if (stateEast && stateSouth)
        return Shapes.or(box(8, 7, 7, 16, 9, 9), box(7, 7, 8, 9, 9, 16));
    else if (stateEast && stateWest)
        return Shapes.or(box(0, 7, 7, 16, 9, 9));
    else if (stateSouth && stateWest)
        return Shapes.or(box(7, 7, 8, 9, 9, 16), box(0, 7, 7, 8, 9, 9));
    else if (stateNorth)
        return Shapes.or(box(7, 7, 0, 9, 9, 8));
    else if (stateEast)
        return Shapes.or(box(8, 7, 7, 16, 9, 9));
    else if (stateSouth)
        return Shapes.or(box(7, 7, 8, 9, 9, 16));
    else if (stateWest)
        return Shapes.or(box(0, 7, 7, 8, 9, 9));
    else
        return box(6, 0, 6, 10, 16, 10);
}
Joined Feb 2018
Points:

User statistics:

  • Modifications:
  • Forum topics:
  • Wiki pages:
  • MCreator plugins:
  • Comments:
I got a better solution!It…
Thu, 12/28/2023 - 16:23

I got a better solution!
It doesn't need that ridiculous amount of if-else statement to get the job done.

@Override
public VoxelShape getShape(BlockState state, BlockGetter world, BlockPos pos, CollisionContext context) {
boolean stateUP = state.getValue(UP);
boolean stateNORTH = state.getValue(NORTH);
boolean stateEAST = state.getValue(EAST);
boolean stateSOUTH = state.getValue(SOUTH);
boolean stateWEST = state.getValue(WEST);
VoxelShape result = box(0, 0, 0, 0, 0, 0);
if (stateUP || (!stateUP && !stateNORTH && !stateEAST && !stateSOUTH && !stateWEST))
result = Shapes.or(result, box(6, 0, 6, 10, 16, 10));
if (stateNORTH)
result = Shapes.or(result, box(7, 7, 0, 9, 9, 8));
if (stateEAST)
result = Shapes.or(result, box(8, 7, 7, 16, 9, 9));
if (stateSOUTH)
result = Shapes.or(result, box(7, 7, 8, 9, 9, 16));
if (stateWEST)
result = Shapes.or(result, box(0, 7, 7, 8, 9, 9));
return result;
}