package su.a71.new_soviet.blocks; import net.minecraft.block.*; import net.minecraft.block.entity.BlockEntity; import net.minecraft.block.piston.PistonBehavior; import net.minecraft.entity.player.PlayerEntity; import net.minecraft.entity.projectile.ProjectileEntity; import net.minecraft.fluid.FluidState; import net.minecraft.fluid.Fluids; import net.minecraft.item.ItemPlacementContext; import net.minecraft.server.world.ServerWorld; import net.minecraft.sound.BlockSoundGroup; import net.minecraft.sound.SoundCategory; import net.minecraft.state.StateManager; import net.minecraft.state.property.BooleanProperty; import net.minecraft.state.property.Properties; import net.minecraft.util.ActionResult; import net.minecraft.util.Hand; import net.minecraft.util.hit.BlockHitResult; import net.minecraft.util.math.BlockPos; import net.minecraft.util.math.Direction; import net.minecraft.util.math.random.Random; import net.minecraft.util.shape.VoxelShape; import net.minecraft.util.shape.VoxelShapes; import net.minecraft.world.BlockView; import net.minecraft.world.World; import net.minecraft.world.WorldAccess; import su.a71.new_soviet.entities.TVBlockEntity; import su.a71.new_soviet.registration.NSE_Sounds; import su.a71.new_soviet.util.Shapes; import java.util.List; public class TVBlock extends HorizontalFacingBlock implements BlockEntityProvider, Waterloggable { protected static final Shapes.HorizontalShapeLegacy SHAPE; public static final BooleanProperty BROKEN; public static final BooleanProperty ON; public static final BooleanProperty WATERLOGGED; public static final BooleanProperty INVERTED; public TVBlock(AbstractBlock.Settings settings) { super(settings.sounds(BlockSoundGroup.METAL).pistonBehavior(PistonBehavior.BLOCK).strength(1f, 2f) .luminance((BlockState state) -> { if (state.get(BROKEN)) { return 0; } if (state.get(INVERTED)) { return state.get(ON) ? 0 : 5; } else { return state.get(ON) ? 5 : 0; } })); setDefaultState(getDefaultState() .with(Properties.HORIZONTAL_FACING, Direction.NORTH) .with(WATERLOGGED, false) .with(ON, false)); } @Override protected void appendProperties(StateManager.Builder builder) { builder.add(Properties.HORIZONTAL_FACING, WATERLOGGED, ON, BROKEN, INVERTED); } @Override public VoxelShape getOutlineShape(BlockState state, BlockView world, BlockPos pos, ShapeContext ctx) { return switch (state.get(FACING)) { case NORTH -> SHAPE.north(); case SOUTH -> SHAPE.south(); case EAST -> SHAPE.east(); case WEST -> SHAPE.west(); default -> VoxelShapes.fullCube(); }; } @Override public BlockState getPlacementState(ItemPlacementContext ctx) { FluidState fluidState = ctx.getWorld().getFluidState(ctx.getBlockPos()); return super.getPlacementState(ctx) .with(Properties.HORIZONTAL_FACING, ctx.getHorizontalPlayerFacing().getOpposite()) .with(WATERLOGGED, fluidState.getFluid() == Fluids.WATER) .with(ON, ctx.getWorld().isReceivingRedstonePower(ctx.getBlockPos())) .with(INVERTED, false) .with(BROKEN, false); } @Override public ActionResult onUse(BlockState state, World world, BlockPos pos, PlayerEntity player, Hand hand, BlockHitResult hit) { if (!world.isClient) { world.setBlockState(pos, state.cycle(INVERTED), 2); } float f = state.get(ON) ? 1f : 0.9f; world.playSound(null, pos, NSE_Sounds.SWITCH_PRESS, SoundCategory.BLOCKS, 0.6f, f); return ActionResult.SUCCESS; } @Override public void onProjectileHit(World world, BlockState state, BlockHitResult hit, ProjectileEntity projectile) { if (!state.get(BROKEN)) { world.playSound(null, hit.getBlockPos().getX(), hit.getBlockPos().getY(), hit.getBlockPos().getZ(), NSE_Sounds.LIGHT_BULB_BROKEN_SOUND, SoundCategory.BLOCKS, 0.8f, 1f); } world.setBlockState(hit.getBlockPos(), state.with(BROKEN, true), 2); super.onProjectileHit(world, state, hit, projectile); } public BlockState getStateForNeighborUpdate(BlockState state, Direction direction, BlockState neighborState, WorldAccess world, BlockPos pos, BlockPos neighborPos) { if (state.get(WATERLOGGED)) { world.scheduleFluidTick(pos, Fluids.WATER, Fluids.WATER.getTickRate(world)); } return !state.canPlaceAt(world, pos) ? Blocks.AIR.getDefaultState() : super.getStateForNeighborUpdate(state, direction, neighborState, world, pos, neighborPos); } public FluidState getFluidState(BlockState state) { return state.get(WATERLOGGED) ? Fluids.WATER.getStill(false) : super.getFluidState(state); } @Override public BlockEntity createBlockEntity(BlockPos pos, BlockState state) { return new TVBlockEntity(pos, state); } public void scheduledTick(BlockState state, ServerWorld world, BlockPos pos, Random random) { if (state.get(ON) && !world.isReceivingRedstonePower(pos)) { world.setBlockState(pos, state.cycle(ON), 2); } } public void neighborUpdate(BlockState state, World world, BlockPos pos, Block sourceBlock, BlockPos sourcePos, boolean notify) { if (!world.isClient) { boolean bl = state.get(ON); if (bl != world.isReceivingRedstonePower(pos)) { if (bl) { world.scheduleBlockTick(pos, this, 4); } else { world.setBlockState(pos, state.cycle(ON), 2); } } } } static { SHAPE = new Shapes.HorizontalShapeLegacy(List.of( List.of(0.0, 0.0, 2.0, 16.0, 13.0, 13.0), List.of(6.0, 13.0, 7.0, 10.0, 13.5, 9.0), List.of(3.0, 2.0, 13.0, 13.0, 9.0, 15.0))); ON = RedstoneTorchBlock.LIT; WATERLOGGED = Properties.WATERLOGGED; BROKEN = Properties.CRACKED; INVERTED = Properties.INVERTED; } }