|
| 1 | +package me.alexdevs.classicPeripherals.peripherals.crypto.slim; |
| 2 | + |
| 3 | +import com.mojang.serialization.MapCodec; |
| 4 | +import dan200.computercraft.shared.peripheral.modem.ModemShapes; |
| 5 | +import me.alexdevs.classicPeripherals.peripherals.crypto.full.CryptographicAcceleratorBlockEntity; |
| 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.entity.BlockEntity; |
| 14 | +import net.minecraft.world.level.block.state.BlockState; |
| 15 | +import net.minecraft.world.level.block.state.StateDefinition; |
| 16 | +import net.minecraft.world.level.block.state.properties.BlockStateProperties; |
| 17 | +import net.minecraft.world.level.block.state.properties.BooleanProperty; |
| 18 | +import net.minecraft.world.level.material.FluidState; |
| 19 | +import net.minecraft.world.level.material.Fluids; |
| 20 | +import net.minecraft.world.phys.shapes.CollisionContext; |
| 21 | +import net.minecraft.world.phys.shapes.VoxelShape; |
| 22 | +import org.jetbrains.annotations.Nullable; |
| 23 | +import org.jspecify.annotations.NonNull; |
| 24 | + |
| 25 | +public class CryptographicAcceleratorSlimBlock extends DirectionalBlock implements EntityBlock, SimpleWaterloggedBlock { |
| 26 | + public static final BooleanProperty WATERLOGGED = BlockStateProperties.WATERLOGGED; |
| 27 | + |
| 28 | + public CryptographicAcceleratorSlimBlock(Properties properties) { |
| 29 | + super(properties); |
| 30 | + |
| 31 | + registerDefaultState(defaultBlockState() |
| 32 | + .setValue(DirectionalBlock.FACING, Direction.NORTH) |
| 33 | + .setValue(WATERLOGGED, false) |
| 34 | + ); |
| 35 | + } |
| 36 | + |
| 37 | + @Override |
| 38 | + protected @NonNull MapCodec<? extends DirectionalBlock> codec() { |
| 39 | + return simpleCodec(CryptographicAcceleratorSlimBlock::new); |
| 40 | + } |
| 41 | + |
| 42 | + @Override |
| 43 | + protected void createBlockStateDefinition(StateDefinition.Builder<Block, BlockState> builder) { |
| 44 | + builder.add(DirectionalBlock.FACING, WATERLOGGED); |
| 45 | + } |
| 46 | + |
| 47 | + @Override |
| 48 | + public @NonNull VoxelShape getShape(BlockState state, @NonNull BlockGetter level, @NonNull BlockPos pos, @NonNull CollisionContext context) { |
| 49 | + return ModemShapes.getBounds(state.getValue(FACING)); |
| 50 | + } |
| 51 | + |
| 52 | + @Override |
| 53 | + public @NonNull BlockState updateShape(BlockState state, @NonNull Direction direction, @NonNull BlockState neighborState, @NonNull LevelAccessor level, @NonNull BlockPos pos, @NonNull BlockPos neighborPos) { |
| 54 | + if (state.getValue(WATERLOGGED)) { |
| 55 | + level.scheduleTick(pos, Fluids.WATER, Fluids.WATER.getTickDelay(level)); |
| 56 | + } |
| 57 | + |
| 58 | + return super.updateShape(state, direction, neighborState, level, pos, neighborPos); |
| 59 | + } |
| 60 | + |
| 61 | + public @NonNull FluidState getFluidState(BlockState state) { |
| 62 | + return state.getValue(WATERLOGGED) ? Fluids.WATER.getSource(false) : Fluids.EMPTY.defaultFluidState(); |
| 63 | + } |
| 64 | + |
| 65 | + @Override |
| 66 | + public boolean canSurvive(BlockState state, @NonNull LevelReader level, BlockPos pos) { |
| 67 | + var facing = state.getValue(FACING); |
| 68 | + return ModemShapes.canSupport(level, pos.relative(facing), facing.getOpposite()); |
| 69 | + } |
| 70 | + |
| 71 | + @Override |
| 72 | + public @Nullable BlockState getStateForPlacement(BlockPlaceContext context) { |
| 73 | + var waterlogged = context.getLevel().getFluidState(context.getClickedPos()).getType() == Fluids.WATER; |
| 74 | + return defaultBlockState() |
| 75 | + .setValue(FACING, context.getClickedFace().getOpposite()) |
| 76 | + .setValue(WATERLOGGED, waterlogged); |
| 77 | + } |
| 78 | + |
| 79 | + @Override |
| 80 | + @Deprecated |
| 81 | + public @NonNull BlockState mirror(BlockState state, Mirror mirrorIn) { |
| 82 | + return state.rotate(mirrorIn.getRotation(state.getValue(FACING))); |
| 83 | + } |
| 84 | + |
| 85 | + @Override |
| 86 | + @Deprecated |
| 87 | + public @NonNull BlockState rotate(BlockState state, Rotation rot) { |
| 88 | + return state.setValue(FACING, rot.rotate(state.getValue(FACING))); |
| 89 | + } |
| 90 | + |
| 91 | + @Override |
| 92 | + public @Nullable BlockEntity newBlockEntity(@NonNull BlockPos pos, @NonNull BlockState state) { |
| 93 | + return new CryptographicAcceleratorBlockEntity(pos, state); |
| 94 | + } |
| 95 | +} |
0 commit comments