avraal

Untitled

Jun 7th, 2024
606
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 10.79 KB | None | 0 0
  1. package com.ludwici.carpetvariants.block;
  2.  
  3. import com.google.common.collect.ImmutableMap;
  4. import com.mojang.serialization.MapCodec;
  5. import net.minecraft.Util;
  6. import net.minecraft.core.BlockPos;
  7. import net.minecraft.core.Direction;
  8. import net.minecraft.world.item.context.BlockPlaceContext;
  9. import net.minecraft.world.level.BlockGetter;
  10. import net.minecraft.world.level.LevelAccessor;
  11. import net.minecraft.world.level.LevelReader;
  12. import net.minecraft.world.level.block.*;
  13. import net.minecraft.world.level.block.state.BlockState;
  14. import net.minecraft.world.level.block.state.StateDefinition;
  15. import net.minecraft.world.level.block.state.properties.BooleanProperty;
  16. import net.minecraft.world.phys.shapes.CollisionContext;
  17. import net.minecraft.world.phys.shapes.Shapes;
  18. import net.minecraft.world.phys.shapes.VoxelShape;
  19.  
  20. import javax.annotation.Nullable;
  21. import java.util.Map;
  22. import java.util.function.Function;
  23. import java.util.stream.Collectors;
  24.  
  25. public class TestWoolCarpetBlock extends CarpetBlock {
  26.     public static final MapCodec<TestWoolCarpetBlock> CODEC = simpleCodec(TestWoolCarpetBlock::new);
  27.  
  28.     public static final BooleanProperty UP = PipeBlock.UP;
  29.     public static final BooleanProperty DOWN = PipeBlock.DOWN;
  30.     public static final BooleanProperty NORTH = PipeBlock.NORTH;
  31.     public static final BooleanProperty EAST = PipeBlock.EAST;
  32.     public static final BooleanProperty SOUTH = PipeBlock.SOUTH;
  33.     public static final BooleanProperty WEST = PipeBlock.WEST;
  34.  
  35.     public static final Map<Direction, BooleanProperty> PROPERTY_BY_DIRECTION = PipeBlock.PROPERTY_BY_DIRECTION
  36.             .entrySet()
  37.             .stream()
  38. //            .filter(p_57886_ -> p_57886_.getKey() != Direction.DOWN)
  39.             .collect(Util.toMap());
  40.  
  41.     private static final VoxelShape UP_AABB = Block.box(0.0, 15.0, 0.0, 16.0, 16.0, 16.0);
  42.     private static final VoxelShape DOWN_AABB = Block.box(0.0, 0.0, 0.0, 16.0, 1.0, 16.0);
  43.     private static final VoxelShape WEST_AABB = Block.box(0.0, 0.0, 0.0, 1.0, 16.0, 16.0);
  44.     private static final VoxelShape EAST_AABB = Block.box(15.0, 0.0, 0.0, 16.0, 16.0, 16.0);
  45.     private static final VoxelShape NORTH_AABB = Block.box(0.0, 0.0, 0.0, 16.0, 16.0, 1.0);
  46.     private static final VoxelShape SOUTH_AABB = Block.box(0.0, 0.0, 15.0, 16.0, 16.0, 16.0);
  47.     private final Map<BlockState, VoxelShape> shapesCache;
  48.  
  49.     public TestWoolCarpetBlock(Properties p_58292_) {
  50.         super(p_58292_);
  51.         this.registerDefaultState(
  52.                 this.stateDefinition
  53.                         .any()
  54.                         .setValue(UP, Boolean.FALSE)
  55.                         .setValue(DOWN, Boolean.FALSE)
  56.                         .setValue(NORTH, Boolean.FALSE)
  57.                         .setValue(EAST, Boolean.FALSE)
  58.                         .setValue(SOUTH, Boolean.FALSE)
  59.                         .setValue(WEST, Boolean.FALSE)
  60.         );
  61.  
  62.         this.shapesCache = ImmutableMap.copyOf(
  63.                 this.stateDefinition.getPossibleStates().stream().collect(Collectors.toMap(Function.identity(), TestWoolCarpetBlock::calculateShape))
  64.         );
  65.     }
  66.  
  67.     private static VoxelShape calculateShape(BlockState pState) {
  68.         VoxelShape voxelshape = Shapes.empty();
  69.  
  70.         if (pState.getValue(UP)) {
  71.             voxelshape = UP_AABB;
  72.         }
  73.  
  74.         if (pState.getValue(DOWN)) {
  75.             voxelshape = Shapes.or(voxelshape, DOWN_AABB);
  76.         }
  77.  
  78.         if (pState.getValue(NORTH)) {
  79.             voxelshape = Shapes.or(voxelshape, NORTH_AABB);
  80.         }
  81.  
  82.         if (pState.getValue(SOUTH)) {
  83.             voxelshape = Shapes.or(voxelshape, SOUTH_AABB);
  84.         }
  85.  
  86.         if (pState.getValue(EAST)) {
  87.             voxelshape = Shapes.or(voxelshape, EAST_AABB);
  88.         }
  89.  
  90.         if (pState.getValue(WEST)) {
  91.             voxelshape = Shapes.or(voxelshape, WEST_AABB);
  92.         }
  93.  
  94.         return voxelshape.isEmpty() ? Shapes.block() : voxelshape;
  95.     }
  96.  
  97.     @Override
  98.     protected VoxelShape getShape(BlockState pState, BlockGetter pLevel, BlockPos pPos, CollisionContext pContext) {
  99.         return this.shapesCache.get(pState);
  100.     }
  101.  
  102.     @Override
  103.     protected boolean canSurvive(BlockState pState, LevelReader pLevel, BlockPos pPos) {
  104.         return this.hasFaces(this.getUpdatedState(pState, pLevel, pPos));
  105.     }
  106.  
  107.     public static boolean isAcceptableNeighbour(BlockGetter pBlockReader, BlockPos pNeighborPos, Direction pAttachedFace) {
  108.         return MultifaceBlock.canAttachTo(pBlockReader, pAttachedFace, pNeighborPos, pBlockReader.getBlockState(pNeighborPos));
  109.     }
  110.  
  111.     public static BooleanProperty getPropertyForFace(Direction pFace) {
  112.         return PROPERTY_BY_DIRECTION.get(pFace);
  113.     }
  114.  
  115.     private boolean canSupportAtFace(BlockGetter pLevel, BlockPos pPos, Direction pDirection) {
  116. //        if (pDirection == Direction.DOWN) {
  117. //            return false;
  118. //        } else {
  119.             BlockPos blockpos = pPos.relative(pDirection);
  120.             if (isAcceptableNeighbour(pLevel, blockpos, pDirection)) {
  121.                 return true;
  122.             } else if (pDirection.getAxis() == Direction.Axis.Y) {
  123.                 return false;
  124.             } else {
  125.                 BooleanProperty booleanproperty = PROPERTY_BY_DIRECTION.get(pDirection);
  126.                 BlockState blockstate = pLevel.getBlockState(pPos.above());
  127.                 return blockstate.is(this) && blockstate.getValue(booleanproperty);
  128.             }
  129. //        }
  130.     }
  131.  
  132.     @Override
  133.     protected boolean canBeReplaced(BlockState pState, BlockPlaceContext pUseContext) {
  134.         BlockState blockstate = pUseContext.getLevel().getBlockState(pUseContext.getClickedPos());
  135.         return blockstate.is(this) ? this.countFaces(blockstate) < PROPERTY_BY_DIRECTION.size() : super.canBeReplaced(pState, pUseContext);
  136.     }
  137.  
  138.     private BlockState getUpdatedState(BlockState pState, BlockGetter pLevel, BlockPos pPos) {
  139.         BlockPos blockpos = pPos.above();
  140. //        if (pState.getValue(UP)) {
  141. //            pState = pState.setValue(UP, Boolean.valueOf(isAcceptableNeighbour(pLevel, blockpos, Direction.DOWN)));
  142. //        }
  143. //
  144. //        if (pState.getValue(DOWN)) {
  145. //            pState = pState.setValue(DOWN, Boolean.valueOf(isAcceptableNeighbour(pLevel, blockpos, Direction.UP)));
  146. //        }
  147.  
  148.         BlockState blockstate = null;
  149.  
  150.         for (Direction direction : Direction.Plane.HORIZONTAL) {
  151.             BooleanProperty booleanproperty = getPropertyForFace(direction);
  152.             if (pState.getValue(booleanproperty)) {
  153.                 boolean flag = this.canSupportAtFace(pLevel, pPos, direction);
  154.                 if (!flag) {
  155.                     if (blockstate == null) {
  156.                         blockstate = pLevel.getBlockState(blockpos);
  157.                     }
  158.  
  159.                     flag = blockstate.is(this) && blockstate.getValue(booleanproperty);
  160.                 }
  161.  
  162.                 pState = pState.setValue(booleanproperty, Boolean.valueOf(flag));
  163.             }
  164.         }
  165.  
  166.         return pState;
  167.     }
  168.  
  169.     private boolean hasFaces(BlockState pState) {
  170.         return this.countFaces(pState) > 0;
  171.     }
  172.  
  173.     private int countFaces(BlockState pState) {
  174.         int i = 0;
  175.  
  176.         for (BooleanProperty booleanproperty : PROPERTY_BY_DIRECTION.values()) {
  177.             if (pState.getValue(booleanproperty)) {
  178.                 i++;
  179.             }
  180.         }
  181.  
  182.         return i;
  183.     }
  184.  
  185.     @Override
  186.     protected BlockState updateShape(BlockState pState, Direction pFacing, BlockState pFacingState, LevelAccessor pLevel, BlockPos pCurrentPos, BlockPos pFacingPos) {
  187. //        if (pFacing == Direction.DOWN) {
  188. //            return pState;
  189. //        } else {
  190.             BlockState blockstate = this.getUpdatedState(pState, pLevel, pCurrentPos);
  191.             return !this.hasFaces(blockstate) ? Blocks.AIR.defaultBlockState() : blockstate;
  192. //        }
  193.     }
  194.  
  195.     @Nullable
  196.     @Override
  197.     public BlockState getStateForPlacement(BlockPlaceContext pContext) {
  198.         BlockState blockstate = pContext.getLevel().getBlockState(pContext.getClickedPos());
  199.         BlockState blockstate1;
  200.         boolean flag = false;
  201.         if (blockstate.is(this)) {
  202.             blockstate1 = blockstate;
  203.             flag = true;
  204.         } else if (blockstate.getBlock() instanceof CarpetBlock) {
  205.             blockstate1 = this.defaultBlockState().setValue(DOWN, true);
  206.         } else {
  207.             blockstate1 = this.defaultBlockState();
  208.         }
  209.  
  210.         for (Direction direction : pContext.getNearestLookingDirections()) {
  211. //            if (direction != Direction.DOWN) {
  212.                 BooleanProperty booleanproperty = getPropertyForFace(direction);
  213.                 boolean flag1 = flag && blockstate.getValue(booleanproperty);
  214.                 if (!flag1 && this.canSupportAtFace(pContext.getLevel(), pContext.getClickedPos(), direction)) {
  215.                     return blockstate1.setValue(booleanproperty, Boolean.valueOf(true));
  216.                 }
  217. //            }
  218.         }
  219.  
  220.         return flag ? blockstate1 : null;
  221.     }
  222.  
  223.     @Override
  224.     protected void createBlockStateDefinition(StateDefinition.Builder<Block, BlockState> pBuilder) {
  225.         pBuilder.add(UP, DOWN, NORTH, EAST, SOUTH, WEST);
  226.     }
  227.  
  228.     @Override
  229.     protected BlockState rotate(BlockState pState, Rotation pRotate) {
  230.         switch (pRotate) {
  231.             case CLOCKWISE_180:
  232.                 return pState.setValue(NORTH, pState.getValue(SOUTH))
  233.                         .setValue(EAST, pState.getValue(WEST))
  234.                         .setValue(SOUTH, pState.getValue(NORTH))
  235.                         .setValue(WEST, pState.getValue(EAST));
  236.             case COUNTERCLOCKWISE_90:
  237.                 return pState.setValue(NORTH, pState.getValue(EAST))
  238.                         .setValue(EAST, pState.getValue(SOUTH))
  239.                         .setValue(SOUTH, pState.getValue(WEST))
  240.                         .setValue(WEST, pState.getValue(NORTH));
  241.             case CLOCKWISE_90:
  242.                 return pState.setValue(NORTH, pState.getValue(WEST))
  243.                         .setValue(EAST, pState.getValue(NORTH))
  244.                         .setValue(SOUTH, pState.getValue(EAST))
  245.                         .setValue(WEST, pState.getValue(SOUTH));
  246.             default:
  247.                 return pState;
  248.         }
  249.     }
  250.  
  251.     @Override
  252.     protected BlockState mirror(BlockState pState, Mirror pMirror) {
  253.         switch (pMirror) {
  254.             case LEFT_RIGHT:
  255.                 return pState.setValue(NORTH, pState.getValue(SOUTH)).setValue(SOUTH, pState.getValue(NORTH));
  256.             case FRONT_BACK:
  257.                 return pState.setValue(EAST, pState.getValue(WEST)).setValue(WEST, pState.getValue(EAST));
  258.             default:
  259.                 return super.mirror(pState, pMirror);
  260.         }
  261.     }
  262.  
  263.     @Override
  264.     public MapCodec<? extends CarpetBlock> codec() {
  265.         return CODEC;
  266.     }
  267. }
  268.  
Advertisement
Add Comment
Please, Sign In to add comment