SimpleMultipart/src/test/java/net/shadowfacts/simplemultipart/test/SlabMultipart.java

58 lines
2.1 KiB
Java
Raw Normal View History

2018-12-24 23:25:40 +00:00
package net.shadowfacts.simplemultipart.test;
import net.minecraft.block.enums.BlockHalf;
import net.minecraft.state.StateFactory;
import net.minecraft.state.property.EnumProperty;
import net.minecraft.util.math.Direction;
import net.minecraft.util.shape.VoxelShape;
import net.minecraft.util.shape.VoxelShapes;
import net.shadowfacts.simplemultipart.multipart.Multipart;
import net.shadowfacts.simplemultipart.multipart.MultipartState;
import net.shadowfacts.simplemultipart.util.MultipartPlacementContext;
2018-12-28 02:55:51 +00:00
import net.shadowfacts.simplemultipart.multipart.MultipartView;
2018-12-24 23:25:40 +00:00
/**
* @author shadowfacts
*/
public class SlabMultipart extends Multipart {
public static final EnumProperty<BlockHalf> HALF = EnumProperty.create("half", BlockHalf.class);
private static final VoxelShape LOWER_BOX = VoxelShapes.cube(0, 0, 0, 1, 0.5f, 1);
private static final VoxelShape UPPER_BOX = VoxelShapes.cube(0, 0.5f, 0, 1, 1, 1);
public SlabMultipart() {
2018-12-29 18:36:29 +00:00
setDefaultState(getDefaultState().with(HALF, BlockHalf.BOTTOM));
2018-12-24 23:25:40 +00:00
}
@Override
protected void appendProperties(StateFactory.Builder<Multipart, MultipartState> builder) {
super.appendProperties(builder);
builder.with(HALF);
}
@Override
public MultipartState getPlacementState(MultipartPlacementContext context) {
Direction hitSide = context.getFacing();
BlockHalf half;
2019-01-19 16:59:58 +00:00
double absoluteHitY = context.method_17698().y; // method_17698 returns an absolutely position vector (i.e. in the world's coordinate space)
double relativeHitY = absoluteHitY - Math.floor(absoluteHitY); // this converts it to the block's coordinate space (e.g. 4.5 - Math.floor(4.5) = 0.5)
2018-12-24 23:25:40 +00:00
if (hitSide == Direction.DOWN) {
2019-01-19 16:59:58 +00:00
half = relativeHitY >= 0.5f ? BlockHalf.BOTTOM : BlockHalf.TOP;
2018-12-24 23:25:40 +00:00
} else if (hitSide == Direction.UP) {
2019-01-19 16:59:58 +00:00
half = 0.5f <= relativeHitY && relativeHitY < 1 ? BlockHalf.TOP : BlockHalf.BOTTOM;
2018-12-24 23:25:40 +00:00
} else {
2019-01-19 16:59:58 +00:00
half = relativeHitY >= 0.5f ? BlockHalf.TOP : BlockHalf.BOTTOM;
2018-12-24 23:25:40 +00:00
}
return getDefaultState().with(HALF, half);
}
@Override
2018-12-28 18:14:50 +00:00
@Deprecated
2018-12-25 15:20:44 +00:00
public VoxelShape getBoundingShape(MultipartState state, MultipartView view) {
2018-12-29 18:36:29 +00:00
return state.get(HALF) == BlockHalf.TOP ? UPPER_BOX : LOWER_BOX;
2018-12-24 23:25:40 +00:00
}
}