Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package com.ludwici.carpetvariants.block;
- import com.google.common.collect.ImmutableMap;
- import com.mojang.serialization.MapCodec;
- import net.minecraft.Util;
- import net.minecraft.core.BlockPos;
- import net.minecraft.core.Direction;
- import net.minecraft.world.item.context.BlockPlaceContext;
- import net.minecraft.world.level.BlockGetter;
- import net.minecraft.world.level.LevelAccessor;
- import net.minecraft.world.level.LevelReader;
- import net.minecraft.world.level.block.*;
- import net.minecraft.world.level.block.state.BlockState;
- import net.minecraft.world.level.block.state.StateDefinition;
- import net.minecraft.world.level.block.state.properties.BooleanProperty;
- import net.minecraft.world.phys.shapes.CollisionContext;
- import net.minecraft.world.phys.shapes.Shapes;
- import net.minecraft.world.phys.shapes.VoxelShape;
- import javax.annotation.Nullable;
- import java.util.Map;
- import java.util.function.Function;
- import java.util.stream.Collectors;
- public class TestWoolCarpetBlock extends CarpetBlock {
- public static final MapCodec<TestWoolCarpetBlock> CODEC = simpleCodec(TestWoolCarpetBlock::new);
- public static final BooleanProperty UP = PipeBlock.UP;
- public static final BooleanProperty DOWN = PipeBlock.DOWN;
- public static final BooleanProperty NORTH = PipeBlock.NORTH;
- public static final BooleanProperty EAST = PipeBlock.EAST;
- public static final BooleanProperty SOUTH = PipeBlock.SOUTH;
- public static final BooleanProperty WEST = PipeBlock.WEST;
- public static final Map<Direction, BooleanProperty> PROPERTY_BY_DIRECTION = PipeBlock.PROPERTY_BY_DIRECTION
- .entrySet()
- .stream()
- // .filter(p_57886_ -> p_57886_.getKey() != Direction.DOWN)
- .collect(Util.toMap());
- private static final VoxelShape UP_AABB = Block.box(0.0, 15.0, 0.0, 16.0, 16.0, 16.0);
- private static final VoxelShape DOWN_AABB = Block.box(0.0, 0.0, 0.0, 16.0, 1.0, 16.0);
- private static final VoxelShape WEST_AABB = Block.box(0.0, 0.0, 0.0, 1.0, 16.0, 16.0);
- private static final VoxelShape EAST_AABB = Block.box(15.0, 0.0, 0.0, 16.0, 16.0, 16.0);
- private static final VoxelShape NORTH_AABB = Block.box(0.0, 0.0, 0.0, 16.0, 16.0, 1.0);
- private static final VoxelShape SOUTH_AABB = Block.box(0.0, 0.0, 15.0, 16.0, 16.0, 16.0);
- private final Map<BlockState, VoxelShape> shapesCache;
- public TestWoolCarpetBlock(Properties p_58292_) {
- super(p_58292_);
- this.registerDefaultState(
- this.stateDefinition
- .any()
- .setValue(UP, Boolean.FALSE)
- .setValue(DOWN, Boolean.FALSE)
- .setValue(NORTH, Boolean.FALSE)
- .setValue(EAST, Boolean.FALSE)
- .setValue(SOUTH, Boolean.FALSE)
- .setValue(WEST, Boolean.FALSE)
- );
- this.shapesCache = ImmutableMap.copyOf(
- this.stateDefinition.getPossibleStates().stream().collect(Collectors.toMap(Function.identity(), TestWoolCarpetBlock::calculateShape))
- );
- }
- private static VoxelShape calculateShape(BlockState pState) {
- VoxelShape voxelshape = Shapes.empty();
- if (pState.getValue(UP)) {
- voxelshape = UP_AABB;
- }
- if (pState.getValue(DOWN)) {
- voxelshape = Shapes.or(voxelshape, DOWN_AABB);
- }
- if (pState.getValue(NORTH)) {
- voxelshape = Shapes.or(voxelshape, NORTH_AABB);
- }
- if (pState.getValue(SOUTH)) {
- voxelshape = Shapes.or(voxelshape, SOUTH_AABB);
- }
- if (pState.getValue(EAST)) {
- voxelshape = Shapes.or(voxelshape, EAST_AABB);
- }
- if (pState.getValue(WEST)) {
- voxelshape = Shapes.or(voxelshape, WEST_AABB);
- }
- return voxelshape.isEmpty() ? Shapes.block() : voxelshape;
- }
- @Override
- protected VoxelShape getShape(BlockState pState, BlockGetter pLevel, BlockPos pPos, CollisionContext pContext) {
- return this.shapesCache.get(pState);
- }
- @Override
- protected boolean canSurvive(BlockState pState, LevelReader pLevel, BlockPos pPos) {
- return this.hasFaces(this.getUpdatedState(pState, pLevel, pPos));
- }
- public static boolean isAcceptableNeighbour(BlockGetter pBlockReader, BlockPos pNeighborPos, Direction pAttachedFace) {
- return MultifaceBlock.canAttachTo(pBlockReader, pAttachedFace, pNeighborPos, pBlockReader.getBlockState(pNeighborPos));
- }
- public static BooleanProperty getPropertyForFace(Direction pFace) {
- return PROPERTY_BY_DIRECTION.get(pFace);
- }
- private boolean canSupportAtFace(BlockGetter pLevel, BlockPos pPos, Direction pDirection) {
- // if (pDirection == Direction.DOWN) {
- // return false;
- // } else {
- BlockPos blockpos = pPos.relative(pDirection);
- if (isAcceptableNeighbour(pLevel, blockpos, pDirection)) {
- return true;
- } else if (pDirection.getAxis() == Direction.Axis.Y) {
- return false;
- } else {
- BooleanProperty booleanproperty = PROPERTY_BY_DIRECTION.get(pDirection);
- BlockState blockstate = pLevel.getBlockState(pPos.above());
- return blockstate.is(this) && blockstate.getValue(booleanproperty);
- }
- // }
- }
- @Override
- protected boolean canBeReplaced(BlockState pState, BlockPlaceContext pUseContext) {
- BlockState blockstate = pUseContext.getLevel().getBlockState(pUseContext.getClickedPos());
- return blockstate.is(this) ? this.countFaces(blockstate) < PROPERTY_BY_DIRECTION.size() : super.canBeReplaced(pState, pUseContext);
- }
- private BlockState getUpdatedState(BlockState pState, BlockGetter pLevel, BlockPos pPos) {
- BlockPos blockpos = pPos.above();
- // if (pState.getValue(UP)) {
- // pState = pState.setValue(UP, Boolean.valueOf(isAcceptableNeighbour(pLevel, blockpos, Direction.DOWN)));
- // }
- //
- // if (pState.getValue(DOWN)) {
- // pState = pState.setValue(DOWN, Boolean.valueOf(isAcceptableNeighbour(pLevel, blockpos, Direction.UP)));
- // }
- BlockState blockstate = null;
- for (Direction direction : Direction.Plane.HORIZONTAL) {
- BooleanProperty booleanproperty = getPropertyForFace(direction);
- if (pState.getValue(booleanproperty)) {
- boolean flag = this.canSupportAtFace(pLevel, pPos, direction);
- if (!flag) {
- if (blockstate == null) {
- blockstate = pLevel.getBlockState(blockpos);
- }
- flag = blockstate.is(this) && blockstate.getValue(booleanproperty);
- }
- pState = pState.setValue(booleanproperty, Boolean.valueOf(flag));
- }
- }
- return pState;
- }
- private boolean hasFaces(BlockState pState) {
- return this.countFaces(pState) > 0;
- }
- private int countFaces(BlockState pState) {
- int i = 0;
- for (BooleanProperty booleanproperty : PROPERTY_BY_DIRECTION.values()) {
- if (pState.getValue(booleanproperty)) {
- i++;
- }
- }
- return i;
- }
- @Override
- protected BlockState updateShape(BlockState pState, Direction pFacing, BlockState pFacingState, LevelAccessor pLevel, BlockPos pCurrentPos, BlockPos pFacingPos) {
- // if (pFacing == Direction.DOWN) {
- // return pState;
- // } else {
- BlockState blockstate = this.getUpdatedState(pState, pLevel, pCurrentPos);
- return !this.hasFaces(blockstate) ? Blocks.AIR.defaultBlockState() : blockstate;
- // }
- }
- @Nullable
- @Override
- public BlockState getStateForPlacement(BlockPlaceContext pContext) {
- BlockState blockstate = pContext.getLevel().getBlockState(pContext.getClickedPos());
- BlockState blockstate1;
- boolean flag = false;
- if (blockstate.is(this)) {
- blockstate1 = blockstate;
- flag = true;
- } else if (blockstate.getBlock() instanceof CarpetBlock) {
- blockstate1 = this.defaultBlockState().setValue(DOWN, true);
- } else {
- blockstate1 = this.defaultBlockState();
- }
- for (Direction direction : pContext.getNearestLookingDirections()) {
- // if (direction != Direction.DOWN) {
- BooleanProperty booleanproperty = getPropertyForFace(direction);
- boolean flag1 = flag && blockstate.getValue(booleanproperty);
- if (!flag1 && this.canSupportAtFace(pContext.getLevel(), pContext.getClickedPos(), direction)) {
- return blockstate1.setValue(booleanproperty, Boolean.valueOf(true));
- }
- // }
- }
- return flag ? blockstate1 : null;
- }
- @Override
- protected void createBlockStateDefinition(StateDefinition.Builder<Block, BlockState> pBuilder) {
- pBuilder.add(UP, DOWN, NORTH, EAST, SOUTH, WEST);
- }
- @Override
- protected BlockState rotate(BlockState pState, Rotation pRotate) {
- switch (pRotate) {
- case CLOCKWISE_180:
- return pState.setValue(NORTH, pState.getValue(SOUTH))
- .setValue(EAST, pState.getValue(WEST))
- .setValue(SOUTH, pState.getValue(NORTH))
- .setValue(WEST, pState.getValue(EAST));
- case COUNTERCLOCKWISE_90:
- return pState.setValue(NORTH, pState.getValue(EAST))
- .setValue(EAST, pState.getValue(SOUTH))
- .setValue(SOUTH, pState.getValue(WEST))
- .setValue(WEST, pState.getValue(NORTH));
- case CLOCKWISE_90:
- return pState.setValue(NORTH, pState.getValue(WEST))
- .setValue(EAST, pState.getValue(NORTH))
- .setValue(SOUTH, pState.getValue(EAST))
- .setValue(WEST, pState.getValue(SOUTH));
- default:
- return pState;
- }
- }
- @Override
- protected BlockState mirror(BlockState pState, Mirror pMirror) {
- switch (pMirror) {
- case LEFT_RIGHT:
- return pState.setValue(NORTH, pState.getValue(SOUTH)).setValue(SOUTH, pState.getValue(NORTH));
- case FRONT_BACK:
- return pState.setValue(EAST, pState.getValue(WEST)).setValue(WEST, pState.getValue(EAST));
- default:
- return super.mirror(pState, pMirror);
- }
- }
- @Override
- public MapCodec<? extends CarpetBlock> codec() {
- return CODEC;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment