Add siren functionality

This commit is contained in:
Andrew-71 2023-08-05 22:54:03 +03:00
parent 33249927b1
commit 48423a2325
5 changed files with 59 additions and 9 deletions

View file

@ -3,28 +3,52 @@ package su.a71.new_soviet.blocks;
import net.fabricmc.fabric.api.object.builder.v1.block.FabricBlockSettings; import net.fabricmc.fabric.api.object.builder.v1.block.FabricBlockSettings;
import net.minecraft.block.*; import net.minecraft.block.*;
import net.minecraft.block.piston.PistonBehavior; import net.minecraft.block.piston.PistonBehavior;
import net.minecraft.entity.player.PlayerEntity;
import net.minecraft.item.ItemPlacementContext; import net.minecraft.item.ItemPlacementContext;
import net.minecraft.server.world.ServerWorld;
import net.minecraft.sound.BlockSoundGroup; import net.minecraft.sound.BlockSoundGroup;
import net.minecraft.sound.SoundCategory;
import net.minecraft.state.StateManager; import net.minecraft.state.StateManager;
import net.minecraft.state.property.BooleanProperty;
import net.minecraft.state.property.Properties; import net.minecraft.state.property.Properties;
import net.minecraft.util.math.BlockPos; import net.minecraft.util.math.BlockPos;
import net.minecraft.util.math.Direction; import net.minecraft.util.math.Direction;
import net.minecraft.util.math.random.Random;
import net.minecraft.util.shape.VoxelShape; import net.minecraft.util.shape.VoxelShape;
import net.minecraft.util.shape.VoxelShapes; import net.minecraft.util.shape.VoxelShapes;
import net.minecraft.world.BlockView; import net.minecraft.world.BlockView;
import net.minecraft.block.TripwireHookBlock; import net.minecraft.world.World;
import net.minecraft.world.WorldView; import net.minecraft.world.WorldView;
import su.a71.new_soviet.NewSoviet;
import su.a71.new_soviet.registration.NSE_Custom;
public class AirRaidBlock extends HorizontalFacingBlock { public class SirenBlock extends HorizontalFacingBlock {
public AirRaidBlock() { public static final BooleanProperty ON;
public SirenBlock() {
super(FabricBlockSettings.create().sounds(BlockSoundGroup.METAL).notSolid().pistonBehavior(PistonBehavior.DESTROY).strength(1f, 2f)); super(FabricBlockSettings.create().sounds(BlockSoundGroup.METAL).notSolid().pistonBehavior(PistonBehavior.DESTROY).strength(1f, 2f));
setDefaultState(getDefaultState().with(Properties.HORIZONTAL_FACING, Direction.NORTH)); setDefaultState(getDefaultState().with(Properties.HORIZONTAL_FACING, Direction.NORTH).with(ON, false));
} }
@Override @Override
protected void appendProperties(StateManager.Builder<Block, BlockState> builder) { protected void appendProperties(StateManager.Builder<Block, BlockState> builder) {
builder.add(Properties.HORIZONTAL_FACING); 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);
}
}
}
} }
@Override @Override
@ -46,7 +70,7 @@ public class AirRaidBlock extends HorizontalFacingBlock {
@Override @Override
public BlockState getPlacementState(ItemPlacementContext ctx) { public BlockState getPlacementState(ItemPlacementContext ctx) {
return super.getPlacementState(ctx).with(Properties.HORIZONTAL_FACING, ctx.getHorizontalPlayerFacing().getOpposite()); return super.getPlacementState(ctx).with(Properties.HORIZONTAL_FACING, ctx.getHorizontalPlayerFacing().getOpposite()).with(ON, ctx.getWorld().isReceivingRedstonePower(ctx.getBlockPos()));
} }
public boolean canPlaceAt(BlockState state, WorldView world, BlockPos pos) { public boolean canPlaceAt(BlockState state, WorldView world, BlockPos pos) {
@ -55,4 +79,21 @@ public class AirRaidBlock extends HorizontalFacingBlock {
BlockState blockState = world.getBlockState(blockPos); BlockState blockState = world.getBlockState(blockPos);
return direction.getAxis().isHorizontal() && blockState.isSideSolidFullSquare(world, blockPos, direction); return direction.getAxis().isHorizontal() && blockState.isSideSolidFullSquare(world, blockPos, direction);
} }
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;
}
} }

View file

@ -13,6 +13,7 @@ import net.minecraft.registry.Registries;
import net.minecraft.registry.Registry; import net.minecraft.registry.Registry;
import net.minecraft.registry.RegistryKey; import net.minecraft.registry.RegistryKey;
import net.minecraft.sound.BlockSoundGroup; import net.minecraft.sound.BlockSoundGroup;
import net.minecraft.sound.SoundEvent;
import net.minecraft.text.Text; import net.minecraft.text.Text;
import net.minecraft.util.Identifier; import net.minecraft.util.Identifier;
import su.a71.new_soviet.NewSoviet; import su.a71.new_soviet.NewSoviet;
@ -30,8 +31,8 @@ public class NSE_Custom {
public static final LampBlock LAMP = new LampBlock(FabricBlockSettings.create().sounds(BlockSoundGroup.LANTERN).strength(1f, 1.5f).mapColor(MapColor.WHITE)); public static final LampBlock LAMP = new LampBlock(FabricBlockSettings.create().sounds(BlockSoundGroup.LANTERN).strength(1f, 1.5f).mapColor(MapColor.WHITE));
public static final CeilingFanBlock CEILING_FAN = new CeilingFanBlock(FabricBlockSettings.create().sounds(BlockSoundGroup.METAL).strength(1f, 1.5f).mapColor(MapColor.WHITE)); public static final CeilingFanBlock CEILING_FAN = new CeilingFanBlock(FabricBlockSettings.create().sounds(BlockSoundGroup.METAL).strength(1f, 1.5f).mapColor(MapColor.WHITE));
public static final AirRaidBlock SIREN = new AirRaidBlock(); public static final SirenBlock SIREN = new SirenBlock();
public static final SoundEvent SIREN_SOUND = SoundEvent.of(new Identifier("new_soviet", "siren_sound"));
private static final ItemGroup NSE_CUSTOM_TAB = FabricItemGroup.builder() private static final ItemGroup NSE_CUSTOM_TAB = FabricItemGroup.builder()
.icon(() -> new ItemStack(TV)) .icon(() -> new ItemStack(TV))
@ -60,5 +61,6 @@ public class NSE_Custom {
register("ceiling_fan", () -> CEILING_FAN, NSE_CUSTOM_TAB); register("ceiling_fan", () -> CEILING_FAN, NSE_CUSTOM_TAB);
register("siren", () -> SIREN, NSE_CUSTOM_TAB); register("siren", () -> SIREN, NSE_CUSTOM_TAB);
Registry.register(Registries.SOUND_EVENT, new Identifier("new_soviet", "siren_sound"), SIREN_SOUND);
} }
} }

View file

@ -140,5 +140,6 @@
"block.new_soviet.landmine": "AP Landmine", "block.new_soviet.landmine": "AP Landmine",
"block.new_soviet.chiseled_mangrove_door": "Chiseled Mangrove Door", "block.new_soviet.chiseled_mangrove_door": "Chiseled Mangrove Door",
"block.new_soviet.chiseled_oak_door": "Chiseled Oak Door", "block.new_soviet.chiseled_oak_door": "Chiseled Oak Door",
"block.new_soviet.chiseled_spruce_door": "Chiseled Spruce Door" "block.new_soviet.chiseled_spruce_door": "Chiseled Spruce Door",
"subtitles.new_soviet.siren": "Siren goes off"
} }

View file

@ -4,6 +4,12 @@
"sounds": [ "sounds": [
"new_soviet:dice_roll" "new_soviet:dice_roll"
] ]
},
"siren_sound": {
"subtitle": "subtitles.new_soviet.siren",
"sounds": [
"new_soviet:siren"
]
} }
} }

Binary file not shown.