|
| 1 | +package fr.geomtech.universegate; |
| 2 | + |
| 3 | +import net.minecraft.core.BlockPos; |
| 4 | +import net.minecraft.core.Direction; |
| 5 | +import net.minecraft.world.item.context.BlockPlaceContext; |
| 6 | +import net.minecraft.world.level.BlockGetter; |
| 7 | +import net.minecraft.world.level.LevelAccessor; |
| 8 | +import net.minecraft.world.level.block.Block; |
| 9 | +import net.minecraft.world.level.block.state.BlockState; |
| 10 | +import net.minecraft.world.level.block.state.StateDefinition; |
| 11 | +import net.minecraft.world.level.block.state.properties.BlockStateProperties; |
| 12 | +import net.minecraft.world.level.block.state.properties.BooleanProperty; |
| 13 | +import net.minecraft.world.phys.shapes.CollisionContext; |
| 14 | +import net.minecraft.world.phys.shapes.Shapes; |
| 15 | +import net.minecraft.world.phys.shapes.VoxelShape; |
| 16 | + |
| 17 | +import java.util.Map; |
| 18 | + |
| 19 | +import net.minecraft.core.registries.BuiltInRegistries; |
| 20 | + |
| 21 | +public class DarkEnergyConduitBlock extends Block { |
| 22 | + |
| 23 | + public static final BooleanProperty NORTH = BlockStateProperties.NORTH; |
| 24 | + public static final BooleanProperty EAST = BlockStateProperties.EAST; |
| 25 | + public static final BooleanProperty SOUTH = BlockStateProperties.SOUTH; |
| 26 | + public static final BooleanProperty WEST = BlockStateProperties.WEST; |
| 27 | + public static final BooleanProperty UP = BlockStateProperties.UP; |
| 28 | + public static final BooleanProperty DOWN = BlockStateProperties.DOWN; |
| 29 | + |
| 30 | + private static final Map<Direction, BooleanProperty> PROPERTY_BY_DIRECTION = Map.of( |
| 31 | + Direction.NORTH, NORTH, |
| 32 | + Direction.EAST, EAST, |
| 33 | + Direction.SOUTH, SOUTH, |
| 34 | + Direction.WEST, WEST, |
| 35 | + Direction.UP, UP, |
| 36 | + Direction.DOWN, DOWN |
| 37 | + ); |
| 38 | + |
| 39 | + private static final VoxelShape CENTER_SHAPE = Block.box(6.0D, 6.0D, 6.0D, 10.0D, 10.0D, 10.0D); |
| 40 | + private static final VoxelShape NORTH_SHAPE = Block.box(6.0D, 6.0D, 0.0D, 10.0D, 10.0D, 6.0D); |
| 41 | + private static final VoxelShape SOUTH_SHAPE = Block.box(6.0D, 6.0D, 10.0D, 10.0D, 10.0D, 16.0D); |
| 42 | + private static final VoxelShape WEST_SHAPE = Block.box(0.0D, 6.0D, 6.0D, 6.0D, 10.0D, 10.0D); |
| 43 | + private static final VoxelShape EAST_SHAPE = Block.box(10.0D, 6.0D, 6.0D, 16.0D, 10.0D, 10.0D); |
| 44 | + private static final VoxelShape UP_SHAPE = Block.box(6.0D, 10.0D, 6.0D, 10.0D, 16.0D, 10.0D); |
| 45 | + private static final VoxelShape DOWN_SHAPE = Block.box(6.0D, 0.0D, 6.0D, 10.0D, 6.0D, 10.0D); |
| 46 | + private static final VoxelShape[] SHAPE_CACHE = buildShapeCache(); |
| 47 | + |
| 48 | + public DarkEnergyConduitBlock(Properties properties) { |
| 49 | + super(properties); |
| 50 | + this.registerDefaultState(this.stateDefinition.any() |
| 51 | + .setValue(NORTH, false) |
| 52 | + .setValue(EAST, false) |
| 53 | + .setValue(SOUTH, false) |
| 54 | + .setValue(WEST, false) |
| 55 | + .setValue(UP, false) |
| 56 | + .setValue(DOWN, false)); |
| 57 | + } |
| 58 | + |
| 59 | + @Override |
| 60 | + public BlockState getStateForPlacement(BlockPlaceContext context) { |
| 61 | + return updateConnections(context.getLevel(), context.getClickedPos(), this.defaultBlockState()); |
| 62 | + } |
| 63 | + |
| 64 | + @Override |
| 65 | + protected BlockState updateShape(BlockState state, Direction direction, BlockState neighborState, LevelAccessor level, BlockPos currentPos, BlockPos neighborPos) { |
| 66 | + BooleanProperty property = PROPERTY_BY_DIRECTION.get(direction); |
| 67 | + if (property == null) return state; |
| 68 | + return state.setValue(property, canConnectTo(level, neighborPos, direction)); |
| 69 | + } |
| 70 | + |
| 71 | + @Override |
| 72 | + protected VoxelShape getShape(BlockState state, BlockGetter level, BlockPos pos, CollisionContext context) { |
| 73 | + return SHAPE_CACHE[shapeIndex(state)]; |
| 74 | + } |
| 75 | + |
| 76 | + @Override |
| 77 | + protected void createBlockStateDefinition(StateDefinition.Builder<Block, BlockState> builder) { |
| 78 | + builder.add(NORTH, EAST, SOUTH, WEST, UP, DOWN); |
| 79 | + } |
| 80 | + |
| 81 | + private BlockState updateConnections(BlockGetter level, BlockPos pos, BlockState state) { |
| 82 | + return state |
| 83 | + .setValue(NORTH, canConnectTo(level, pos.north(), Direction.NORTH)) |
| 84 | + .setValue(EAST, canConnectTo(level, pos.east(), Direction.EAST)) |
| 85 | + .setValue(SOUTH, canConnectTo(level, pos.south(), Direction.SOUTH)) |
| 86 | + .setValue(WEST, canConnectTo(level, pos.west(), Direction.WEST)) |
| 87 | + .setValue(UP, canConnectTo(level, pos.above(), Direction.UP)) |
| 88 | + .setValue(DOWN, canConnectTo(level, pos.below(), Direction.DOWN)); |
| 89 | + } |
| 90 | + |
| 91 | + private boolean canConnectTo(BlockGetter level, BlockPos pos, Direction direction) { |
| 92 | + BlockState state = level.getBlockState(pos); |
| 93 | + Block block = state.getBlock(); |
| 94 | + if (block instanceof DarkEnergyConduitBlock || block instanceof DarkEnergyGeneratorBlock) return true; |
| 95 | + |
| 96 | + net.minecraft.resources.ResourceLocation key = BuiltInRegistries.BLOCK.getKey(state.getBlock()); |
| 97 | + return key.getPath().startsWith("portal_"); |
| 98 | + } |
| 99 | + |
| 100 | + private static VoxelShape[] buildShapeCache() { |
| 101 | + VoxelShape[] shapes = new VoxelShape[64]; |
| 102 | + for (int i = 0; i < shapes.length; i++) { |
| 103 | + VoxelShape shape = CENTER_SHAPE; |
| 104 | + if ((i & 1) != 0) shape = Shapes.or(shape, NORTH_SHAPE); |
| 105 | + if ((i & 2) != 0) shape = Shapes.or(shape, EAST_SHAPE); |
| 106 | + if ((i & 4) != 0) shape = Shapes.or(shape, SOUTH_SHAPE); |
| 107 | + if ((i & 8) != 0) shape = Shapes.or(shape, WEST_SHAPE); |
| 108 | + if ((i & 16) != 0) shape = Shapes.or(shape, UP_SHAPE); |
| 109 | + if ((i & 32) != 0) shape = Shapes.or(shape, DOWN_SHAPE); |
| 110 | + shapes[i] = shape; |
| 111 | + } |
| 112 | + return shapes; |
| 113 | + } |
| 114 | + |
| 115 | + private static int shapeIndex(BlockState state) { |
| 116 | + int index = 0; |
| 117 | + if (state.getValue(NORTH)) index |= 1; |
| 118 | + if (state.getValue(EAST)) index |= 2; |
| 119 | + if (state.getValue(SOUTH)) index |= 4; |
| 120 | + if (state.getValue(WEST)) index |= 8; |
| 121 | + if (state.getValue(UP)) index |= 16; |
| 122 | + if (state.getValue(DOWN)) index |= 32; |
| 123 | + return index; |
| 124 | + } |
| 125 | +} |
0 commit comments