Prepare new siren system

This commit is contained in:
Andrew-71 2023-09-27 17:14:36 +03:00
parent 0291788285
commit 5e60100eca
6 changed files with 297 additions and 7 deletions

View file

@ -3,15 +3,22 @@ package su.a71.new_soviet.blocks;
import net.fabricmc.fabric.api.object.builder.v1.block.FabricBlockSettings;
import net.minecraft.block.*;
import net.minecraft.block.piston.PistonBehavior;
import net.minecraft.entity.player.PlayerEntity;
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.sound.SoundEvent;
import net.minecraft.state.StateManager;
import net.minecraft.state.property.BooleanProperty;
import net.minecraft.state.property.IntProperty;
import net.minecraft.state.property.Properties;
import net.minecraft.text.Text;
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;
@ -24,18 +31,26 @@ import net.minecraft.world.WorldView;
import su.a71.new_soviet.registration.NSE_Sounds;
import java.util.ArrayList;
import java.util.List;
public class SirenBlock extends HorizontalFacingBlock implements Waterloggable {
public static final BooleanProperty ON;
public static final BooleanProperty WATERLOGGED;
public static List<SirenSound> SIREN_SOUNDS = new ArrayList<>();
public static final IntProperty SOUND_INDEX = IntProperty.of("siren_sound", 0, 32);
public SirenBlock() {
super(FabricBlockSettings.create().sounds(BlockSoundGroup.METAL).notSolid().pistonBehavior(PistonBehavior.DESTROY).strength(1f, 2f));
setDefaultState(getDefaultState().with(Properties.HORIZONTAL_FACING, Direction.NORTH).with(ON, false).with(WATERLOGGED, false));
setDefaultState(getDefaultState().with(Properties.HORIZONTAL_FACING, Direction.NORTH).with(ON, false).with(WATERLOGGED, false).with(SOUND_INDEX, 0));
SIREN_SOUNDS.add(new SirenSound("Air raid", NSE_Sounds.SIREN_SOUND, 140));
}
@Override
protected void appendProperties(StateManager.Builder<Block, BlockState> builder) {
builder.add(Properties.HORIZONTAL_FACING, ON, WATERLOGGED);
builder.add(Properties.HORIZONTAL_FACING, ON, WATERLOGGED, SOUND_INDEX);
}
public void neighborUpdate(BlockState state, World world, BlockPos pos, Block sourceBlock, BlockPos sourcePos, boolean notify) {
@ -45,14 +60,26 @@ public class SirenBlock extends HorizontalFacingBlock implements Waterloggable {
if (bl) {
world.scheduleBlockTick(pos, this, 4);
} else {
world.playSound(null, pos.getX(), pos.getY(), pos.getZ(), NSE_Sounds.SIREN_SOUND, SoundCategory.NEUTRAL, getSirenVolume(world, pos), 1f);
world.playSound(null, pos.getX(), pos.getY(), pos.getZ(), SIREN_SOUNDS.get(world.getBlockState(pos).get(SOUND_INDEX)).getSound(), SoundCategory.NEUTRAL, getSirenVolume(world, pos), 1f);
world.setBlockState(pos, state.cycle(ON), 2);
world.scheduleBlockTick(pos, this, 140);
world.scheduleBlockTick(pos, this, SIREN_SOUNDS.get(world.getBlockState(pos).get(SOUND_INDEX)).getDuration());
}
}
}
}
@Override
public ActionResult onUse(BlockState state, World world, BlockPos pos, PlayerEntity player, Hand hand, BlockHitResult hit) {
if (player.isSneaking()) {
if (!world.isClient) {
incrementSoundIndex(world, pos);
}
player.sendMessage(Text.translatable("block.new_soviet.siren.set", SIREN_SOUNDS.get(world.getBlockState(pos).get(SOUND_INDEX)).getName()), true);
return ActionResult.SUCCESS;
}
return super.onUse(state, world, pos, player, hand, hit);
}
@Override
public VoxelShape getOutlineShape(BlockState state, BlockView world, BlockPos pos, ShapeContext ctx) {
Direction dir = state.get(FACING);
@ -70,7 +97,8 @@ public class SirenBlock extends HorizontalFacingBlock implements Waterloggable {
return super.getPlacementState(ctx)
.with(Properties.HORIZONTAL_FACING, ctx.getHorizontalPlayerFacing().getOpposite())
.with(ON, ctx.getWorld().isReceivingRedstonePower(ctx.getBlockPos()))
.with(WATERLOGGED, ctx.getWorld().getFluidState(ctx.getBlockPos()).getFluid() == Fluids.WATER);
.with(WATERLOGGED, ctx.getWorld().getFluidState(ctx.getBlockPos()).getFluid() == Fluids.WATER)
.with(SOUND_INDEX, 0);
}
public boolean canPlaceAt(BlockState state, WorldView world, BlockPos pos) {
@ -84,8 +112,8 @@ public class SirenBlock extends HorizontalFacingBlock implements Waterloggable {
if (state.get(ON) && !world.isReceivingRedstonePower(pos)) {
world.setBlockState(pos, state.cycle(ON), 2);
} else {
world.playSound(null, pos.getX(), pos.getY(), pos.getZ(), NSE_Sounds.SIREN_SOUND, SoundCategory.NEUTRAL, getSirenVolume(world, pos), 1f);
world.scheduleBlockTick(pos, this, 140);
world.playSound(null, pos.getX(), pos.getY(), pos.getZ(), SIREN_SOUNDS.get(world.getBlockState(pos).get(SOUND_INDEX)).getSound(), SoundCategory.NEUTRAL, getSirenVolume(world, pos), 1f);
world.scheduleBlockTick(pos, this, SIREN_SOUNDS.get(world.getBlockState(pos).get(SOUND_INDEX)).getDuration());
}
}
@ -99,6 +127,15 @@ public class SirenBlock extends HorizontalFacingBlock implements Waterloggable {
};
}
public void incrementSoundIndex(World world, BlockPos pos) {
BlockState state = world.getBlockState(pos);
int newIndex = state.get(SOUND_INDEX) + 1;
if (newIndex >= SIREN_SOUNDS.size()) {
newIndex = 0;
}
world.setBlockState(pos, world.getBlockState(pos).with(SOUND_INDEX, newIndex));
}
@Override
public BlockState getStateForNeighborUpdate(BlockState state, Direction direction, BlockState neighborState, WorldAccess world, BlockPos pos, BlockPos neighborPos) {
if (state.get(WATERLOGGED)) {
@ -115,4 +152,28 @@ public class SirenBlock extends HorizontalFacingBlock implements Waterloggable {
ON = RedstoneTorchBlock.LIT;
WATERLOGGED = Properties.WATERLOGGED;
}
public class SirenSound {
private final SoundEvent sound;
private final int duration;
private final String name;
public SirenSound( String name, SoundEvent sound, int duration) {
this.sound = sound;
this.duration = duration;
this.name = name;
}
public SoundEvent getSound() {
return sound;
}
public int getDuration() {
return duration;
}
public String getName() {
return name;
}
}
}

View file

@ -0,0 +1,13 @@
{
"variants": {
"type=bottom": {
"model": "new_soviet:block/cracked_light_blue_bricks_slab"
},
"type=double": {
"model": "new_soviet:block/cracked_light_blue_bricks_1"
},
"type=top": {
"model": "new_soviet:block/cracked_light_blue_bricks_slab_top"
}
}
}

View file

@ -0,0 +1,209 @@
{
"variants": {
"facing=east,half=bottom,shape=inner_left": {
"model": "new_soviet:block/cracked_light_blue_bricks_stairs_inner",
"uvlock": true,
"y": 270
},
"facing=east,half=bottom,shape=inner_right": {
"model": "new_soviet:block/cracked_light_blue_bricks_stairs_inner"
},
"facing=east,half=bottom,shape=outer_left": {
"model": "new_soviet:block/cracked_light_blue_bricks_stairs_outer",
"uvlock": true,
"y": 270
},
"facing=east,half=bottom,shape=outer_right": {
"model": "new_soviet:block/cracked_light_blue_bricks_stairs_outer"
},
"facing=east,half=bottom,shape=straight": {
"model": "new_soviet:block/cracked_light_blue_bricks_stairs_stairs"
},
"facing=east,half=top,shape=inner_left": {
"model": "new_soviet:block/cracked_light_blue_bricks_stairs_inner",
"uvlock": true,
"x": 180
},
"facing=east,half=top,shape=inner_right": {
"model": "new_soviet:block/cracked_light_blue_bricks_stairs_inner",
"uvlock": true,
"x": 180,
"y": 90
},
"facing=east,half=top,shape=outer_left": {
"model": "new_soviet:block/cracked_light_blue_bricks_stairs_outer",
"uvlock": true,
"x": 180
},
"facing=east,half=top,shape=outer_right": {
"model": "new_soviet:block/cracked_light_blue_bricks_stairs_outer",
"uvlock": true,
"x": 180,
"y": 90
},
"facing=east,half=top,shape=straight": {
"model": "new_soviet:block/cracked_light_blue_bricks_stairs_stairs",
"uvlock": true,
"x": 180
},
"facing=north,half=bottom,shape=inner_left": {
"model": "new_soviet:block/cracked_light_blue_bricks_stairs_inner",
"uvlock": true,
"y": 180
},
"facing=north,half=bottom,shape=inner_right": {
"model": "new_soviet:block/cracked_light_blue_bricks_stairs_inner",
"uvlock": true,
"y": 270
},
"facing=north,half=bottom,shape=outer_left": {
"model": "new_soviet:block/cracked_light_blue_bricks_stairs_outer",
"uvlock": true,
"y": 180
},
"facing=north,half=bottom,shape=outer_right": {
"model": "new_soviet:block/cracked_light_blue_bricks_stairs_outer",
"uvlock": true,
"y": 270
},
"facing=north,half=bottom,shape=straight": {
"model": "new_soviet:block/cracked_light_blue_bricks_stairs",
"uvlock": true,
"y": 270
},
"facing=north,half=top,shape=inner_left": {
"model": "new_soviet:block/cracked_light_blue_bricks_stairs_inner",
"uvlock": true,
"x": 180,
"y": 270
},
"facing=north,half=top,shape=inner_right": {
"model": "new_soviet:block/cracked_light_blue_bricks_stairs_inner",
"uvlock": true,
"x": 180
},
"facing=north,half=top,shape=outer_left": {
"model": "new_soviet:block/cracked_light_blue_bricks_stairs_outer",
"uvlock": true,
"x": 180,
"y": 270
},
"facing=north,half=top,shape=outer_right": {
"model": "new_soviet:block/cracked_light_blue_bricks_stairs_outer",
"uvlock": true,
"x": 180
},
"facing=north,half=top,shape=straight": {
"model": "new_soviet:block/cracked_light_blue_bricks_stairs",
"uvlock": true,
"x": 180,
"y": 270
},
"facing=south,half=bottom,shape=inner_left": {
"model": "new_soviet:block/cracked_light_blue_bricks_stairs_inner"
},
"facing=south,half=bottom,shape=inner_right": {
"model": "new_soviet:block/cracked_light_blue_bricks_stairs_inner",
"uvlock": true,
"y": 90
},
"facing=south,half=bottom,shape=outer_left": {
"model": "new_soviet:block/cracked_light_blue_bricks_stairs_outer"
},
"facing=south,half=bottom,shape=outer_right": {
"model": "new_soviet:block/cracked_light_blue_bricks_stairs_outer",
"uvlock": true,
"y": 90
},
"facing=south,half=bottom,shape=straight": {
"model": "new_soviet:block/cracked_light_blue_bricks_stairs",
"uvlock": true,
"y": 90
},
"facing=south,half=top,shape=inner_left": {
"model": "new_soviet:block/cracked_light_blue_bricks_stairs_inner",
"uvlock": true,
"x": 180,
"y": 90
},
"facing=south,half=top,shape=inner_right": {
"model": "new_soviet:block/cracked_light_blue_bricks_stairs_inner",
"uvlock": true,
"x": 180,
"y": 180
},
"facing=south,half=top,shape=outer_left": {
"model": "new_soviet:block/cracked_light_blue_bricks_stairs_outer",
"uvlock": true,
"x": 180,
"y": 90
},
"facing=south,half=top,shape=outer_right": {
"model": "new_soviet:block/cracked_light_blue_bricks_stairs_outer",
"uvlock": true,
"x": 180,
"y": 180
},
"facing=south,half=top,shape=straight": {
"model": "new_soviet:block/cracked_light_blue_bricks_stairs",
"uvlock": true,
"x": 180,
"y": 90
},
"facing=west,half=bottom,shape=inner_left": {
"model": "new_soviet:block/cracked_light_blue_bricks_stairs_inner",
"uvlock": true,
"y": 90
},
"facing=west,half=bottom,shape=inner_right": {
"model": "new_soviet:block/cracked_light_blue_bricks_stairs_inner",
"uvlock": true,
"y": 180
},
"facing=west,half=bottom,shape=outer_left": {
"model": "new_soviet:block/cracked_light_blue_bricks_stairs_outer",
"uvlock": true,
"y": 90
},
"facing=west,half=bottom,shape=outer_right": {
"model": "new_soviet:block/cracked_light_blue_bricks_stairs_outer",
"uvlock": true,
"y": 180
},
"facing=west,half=bottom,shape=straight": {
"model": "new_soviet:block/cracked_light_blue_bricks_stairs",
"uvlock": true,
"y": 180
},
"facing=west,half=top,shape=inner_left": {
"model": "new_soviet:block/cracked_light_blue_bricks_stairs_inner",
"uvlock": true,
"x": 180,
"y": 180
},
"facing=west,half=top,shape=inner_right": {
"model": "new_soviet:block/cracked_light_blue_bricks_stairs_inner",
"uvlock": true,
"x": 180,
"y": 270
},
"facing=west,half=top,shape=outer_left": {
"model": "new_soviet:block/cracked_light_blue_bricks_stairs_outer",
"uvlock": true,
"x": 180,
"y": 180
},
"facing=west,half=top,shape=outer_right": {
"model": "new_soviet:block/cracked_light_blue_bricks_stairs_outer",
"uvlock": true,
"x": 180,
"y": 270
},
"facing=west,half=top,shape=straight": {
"model": "new_soviet:block/cracked_light_blue_bricks_stairs",
"uvlock": true,
"x": 180,
"y": 180
}
}
}

View file

@ -150,6 +150,7 @@
"block.new_soviet.table_lamp": "Table Lamp",
"block.new_soviet.ceiling_fan": "Ceiling Fan",
"block.new_soviet.siren": "Siren",
"block.new_soviet.siren.set": "Siren sounds set to: %s",
"item.new_soviet.dice_d6": "Dice",
"item.new_soviet.dice_d4": "Dice",
"item.new_soviet.dice_d20": "Dice",

View file

@ -0,0 +1,3 @@
{
"parent": "new_soviet:block/big_green_tiles_cracked_1"
}

View file

@ -0,0 +1,3 @@
{
"parent": "new_soviet:block/cracked_light_blue_bricks_1"
}