New-Soviet-Era/src/main/java/su/a71/new_soviet/blocks/SirenBlock.java

99 lines
4.1 KiB
Java
Raw Normal View History

2023-07-13 23:24:18 +03:00
package su.a71.new_soviet.blocks;
2023-07-14 19:47:44 +03:00
import net.fabricmc.fabric.api.object.builder.v1.block.FabricBlockSettings;
import net.minecraft.block.*;
import net.minecraft.block.piston.PistonBehavior;
2023-08-05 22:54:03 +03:00
import net.minecraft.entity.player.PlayerEntity;
2023-07-14 19:47:44 +03:00
import net.minecraft.item.ItemPlacementContext;
2023-08-05 22:54:03 +03:00
import net.minecraft.server.world.ServerWorld;
2023-07-14 19:47:44 +03:00
import net.minecraft.sound.BlockSoundGroup;
2023-08-05 22:54:03 +03:00
import net.minecraft.sound.SoundCategory;
2023-07-14 19:47:44 +03:00
import net.minecraft.state.StateManager;
2023-08-05 22:54:03 +03:00
import net.minecraft.state.property.BooleanProperty;
2023-07-14 19:47:44 +03:00
import net.minecraft.state.property.Properties;
import net.minecraft.util.math.BlockPos;
import net.minecraft.util.math.Direction;
2023-08-05 22:54:03 +03:00
import net.minecraft.util.math.random.Random;
2023-07-14 19:47:44 +03:00
import net.minecraft.util.shape.VoxelShape;
import net.minecraft.util.shape.VoxelShapes;
import net.minecraft.world.BlockView;
2023-07-13 23:24:18 +03:00
2023-08-05 22:54:03 +03:00
import net.minecraft.world.World;
2023-07-14 19:47:44 +03:00
import net.minecraft.world.WorldView;
2023-08-05 22:54:03 +03:00
import su.a71.new_soviet.NewSoviet;
import su.a71.new_soviet.registration.NSE_Custom;
2023-07-13 23:24:18 +03:00
2023-08-05 22:54:03 +03:00
public class SirenBlock extends HorizontalFacingBlock {
public static final BooleanProperty ON;
public SirenBlock() {
2023-08-02 10:46:46 +03:00
super(FabricBlockSettings.create().sounds(BlockSoundGroup.METAL).notSolid().pistonBehavior(PistonBehavior.DESTROY).strength(1f, 2f));
2023-08-05 22:54:03 +03:00
setDefaultState(getDefaultState().with(Properties.HORIZONTAL_FACING, Direction.NORTH).with(ON, false));
2023-07-13 23:24:18 +03:00
}
2023-07-14 19:47:44 +03:00
@Override
protected void appendProperties(StateManager.Builder<Block, BlockState> builder) {
2023-08-05 22:54:03 +03:00
builder.add(Properties.HORIZONTAL_FACING, ON);
}
public void neighborUpdate(BlockState state, World world, BlockPos pos, Block sourceBlock, BlockPos sourcePos, boolean notify) {
if (!world.isClient) {
boolean bl = (Boolean)state.get(ON);
if (bl != world.isReceivingRedstonePower(pos)) {
if (bl) {
world.scheduleBlockTick(pos, this, 4);
} else {
world.playSound((PlayerEntity)null, pos.getX(), pos.getY(), pos.getZ(), NSE_Custom.SIREN_SOUND, SoundCategory.NEUTRAL, 1F, 1f);
world.setBlockState(pos, (BlockState)state.cycle(ON), 2);
world.scheduleBlockTick(pos, this, 140);
}
}
}
2023-07-14 19:47:44 +03:00
}
@Override
public VoxelShape getOutlineShape(BlockState state, BlockView world, BlockPos pos, ShapeContext ctx) {
Direction dir = state.get(FACING);
switch(dir) {
case NORTH:
return Block.createCuboidShape(3.5, 2, 11, 12.5, 15, 16);
case SOUTH:
return Block.createCuboidShape(3.5, 2, 0, 12.5, 15, 5);
case EAST:
return Block.createCuboidShape(0, 2, 3.5, 5, 15, 12.5);
case WEST:
return Block.createCuboidShape(11, 2, 3.5, 16, 15, 12.5);
default:
return VoxelShapes.fullCube();
}
}
@Override
public BlockState getPlacementState(ItemPlacementContext ctx) {
2023-08-05 22:54:03 +03:00
return super.getPlacementState(ctx).with(Properties.HORIZONTAL_FACING, ctx.getHorizontalPlayerFacing().getOpposite()).with(ON, ctx.getWorld().isReceivingRedstonePower(ctx.getBlockPos()));
2023-07-14 19:47:44 +03:00
}
public boolean canPlaceAt(BlockState state, WorldView world, BlockPos pos) {
Direction direction = (Direction)state.get(FACING);
BlockPos blockPos = pos.offset(direction.getOpposite());
BlockState blockState = world.getBlockState(blockPos);
return direction.getAxis().isHorizontal() && blockState.isSideSolidFullSquare(world, blockPos, direction);
}
2023-08-05 22:54:03 +03:00
public void scheduledTick(BlockState state, ServerWorld world, BlockPos pos, Random random) {
NewSoviet.LOG.info("Scheduled tick");
if ((Boolean)state.get(ON) && !world.isReceivingRedstonePower(pos)) {
world.setBlockState(pos, (BlockState)state.cycle(ON), 2);
NewSoviet.LOG.info("Stopping!");
} else {
NewSoviet.LOG.info("Playing!");
world.playSound((PlayerEntity)null, pos.getX(), pos.getY(), pos.getZ(), NSE_Custom.SIREN_SOUND, SoundCategory.NEUTRAL, 1F, 1f);
world.scheduleBlockTick(pos, this, 140);
}
}
static {
ON = RedstoneTorchBlock.LIT;
}
2023-07-14 19:47:44 +03:00
}