From 9758a0ad31ba977fceeff29e44c467e3c6be4dc9 Mon Sep 17 00:00:00 2001 From: Andrew-71 Date: Sun, 3 Sep 2023 15:09:48 +0300 Subject: [PATCH] Fix lamps --- src/main/java/su/a71/new_soviet/Config.java | 4 - .../su/a71/new_soviet/blocks/LampBlock.java | 61 ++++------ .../a71/new_soviet/blocks/LightBulbBlock.java | 105 +++++------------- .../su/a71/new_soviet/blocks/SwitchBlock.java | 6 +- .../a71/new_soviet/blocks/TableLampBlock.java | 42 +++++++ .../new_soviet/registration/NSE_Custom.java | 2 +- .../assets/new_soviet/lang/en_us.json | 3 +- 7 files changed, 97 insertions(+), 126 deletions(-) create mode 100644 src/main/java/su/a71/new_soviet/blocks/TableLampBlock.java diff --git a/src/main/java/su/a71/new_soviet/Config.java b/src/main/java/su/a71/new_soviet/Config.java index 2ece446..0d70fea 100644 --- a/src/main/java/su/a71/new_soviet/Config.java +++ b/src/main/java/su/a71/new_soviet/Config.java @@ -3,7 +3,6 @@ package su.a71.new_soviet; import java.io.*; public class Config { - private boolean invert_lamps = false; private boolean announce_dice = false; public static Config INSTANCE; @@ -12,9 +11,6 @@ public class Config { INSTANCE = this; } - public boolean shouldInvertLamps() { - return invert_lamps; - } public boolean shouldAnnounceDice() { return announce_dice; } diff --git a/src/main/java/su/a71/new_soviet/blocks/LampBlock.java b/src/main/java/su/a71/new_soviet/blocks/LampBlock.java index 13380bf..c55e664 100644 --- a/src/main/java/su/a71/new_soviet/blocks/LampBlock.java +++ b/src/main/java/su/a71/new_soviet/blocks/LampBlock.java @@ -7,6 +7,7 @@ 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.SoundCategory; import net.minecraft.state.StateManager; import net.minecraft.state.property.BooleanProperty; import net.minecraft.state.property.Properties; @@ -17,32 +18,29 @@ 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 net.minecraft.world.WorldView; - import org.jetbrains.annotations.Nullable; -import su.a71.new_soviet.Config; +import su.a71.new_soviet.registration.NSE_Sounds; -public class LampBlock extends Block implements Waterloggable { +public abstract class LampBlock extends Block implements Waterloggable { public static final BooleanProperty ON; public static final BooleanProperty INVERTED; public static final BooleanProperty WATERLOGGED; - protected static final VoxelShape SHAPE; + public boolean defaultLightState; - public LampBlock(AbstractBlock.Settings settings) { - super(settings.luminance((BlockState state) -> { + public LampBlock(AbstractBlock.Settings settings, boolean defaultLightState, java.util.function.ToIntFunction luminance) { + super(settings.luminance(luminance == null ? (BlockState state) -> { if (state.get(INVERTED)) { - return state.get(ON) ? 12 : 0; - } else { return state.get(ON) ? 0 : 12; + } else { + return state.get(ON) ? 12 : 0; } - })); + } : luminance)); + this.defaultLightState = defaultLightState; this.setDefaultState(this.stateManager.getDefaultState() - .with(INVERTED, false) + .with(INVERTED, defaultLightState) .with(WATERLOGGED, false) .with(ON, false)); } @@ -54,7 +52,7 @@ public class LampBlock extends Block implements Waterloggable { for (Direction direction : directions) { if (direction.getAxis() == Direction.Axis.Y) { BlockState blockState = this.getDefaultState() - .with(INVERTED, Config.INSTANCE.shouldInvertLamps()) + .with(INVERTED, defaultLightState) .with(ON, ctx.getWorld().isReceivingRedstonePower(ctx.getBlockPos())); if (blockState.canPlaceAt(ctx.getWorld(), ctx.getBlockPos())) { return blockState.with(WATERLOGGED, fluidState.getFluid() == Fluids.WATER); @@ -66,34 +64,24 @@ public class LampBlock extends Block implements Waterloggable { @Override public ActionResult onUse(BlockState state, World world, BlockPos pos, PlayerEntity player, Hand hand, BlockHitResult hit) { - if (world.isClient) return super.onUse(state, world, pos, player, hand, hit); -// world.setBlockState(pos, state.cycle(INVERTED)); - return super.onUse(state, world, pos, player, hand, hit); - } - - public VoxelShape getOutlineShape(BlockState state, BlockView world, BlockPos pos, ShapeContext context) { - return SHAPE; + if (!world.isClient) { + world.setBlockState(pos, (BlockState)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; } protected void appendProperties(StateManager.Builder builder) { builder.add(new Property[]{ON, WATERLOGGED, INVERTED}); } - public boolean canPlaceAt(BlockState state, WorldView world, BlockPos pos) { - Direction direction = attachedDirection(state).getOpposite(); - return Block.sideCoversSmallSquare(world, pos.offset(direction), direction.getOpposite()); - } - - protected static Direction attachedDirection(BlockState state) { - return Direction.UP; - } - 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 Direction.DOWN == direction && !state.canPlaceAt(world, pos) ? Blocks.AIR.getDefaultState() : super.getStateForNeighborUpdate(state, direction, neighborState, world, pos, neighborPos); + return !state.canPlaceAt(world, pos) ? Blocks.AIR.getDefaultState() : super.getStateForNeighborUpdate(state, direction, neighborState, world, pos, neighborPos); } public FluidState getFluidState(BlockState state) { @@ -123,18 +111,7 @@ public class LampBlock extends Block implements Waterloggable { } } - public static VoxelShape getStandingShape(){ - VoxelShape shape = VoxelShapes.empty(); - shape = VoxelShapes.union(shape, VoxelShapes.cuboid(0.4375, 0.25, 0.4375, 0.5625, 0.875, 0.5625)); - shape = VoxelShapes.union(shape, VoxelShapes.cuboid(0.375, 0, 0.375, 0.625, 0.25, 0.625)); - shape = VoxelShapes.union(shape, VoxelShapes.cuboid(0.25, 0.6875, 0.25, 0.75, 0.9375, 0.75)); - shape = VoxelShapes.union(shape, VoxelShapes.cuboid(0.125, 0.3125, 0.125, 0.875, 0.6875, 0.875)); - shape.simplify(); - return shape; - } - static { - SHAPE = getStandingShape(); ON = RedstoneTorchBlock.LIT; WATERLOGGED = Properties.WATERLOGGED; INVERTED = Properties.INVERTED; diff --git a/src/main/java/su/a71/new_soviet/blocks/LightBulbBlock.java b/src/main/java/su/a71/new_soviet/blocks/LightBulbBlock.java index ff74e32..171989d 100644 --- a/src/main/java/su/a71/new_soviet/blocks/LightBulbBlock.java +++ b/src/main/java/su/a71/new_soviet/blocks/LightBulbBlock.java @@ -33,84 +33,52 @@ import su.a71.new_soviet.registration.NSE_Custom; import su.a71.new_soviet.registration.NSE_Items; import su.a71.new_soviet.registration.NSE_Sounds; -public class LightBulbBlock extends Block implements Waterloggable { +public class LightBulbBlock extends LampBlock { protected static final VoxelShape SHAPE; - public static final BooleanProperty ON; - public static final BooleanProperty INVERTED; public static final BooleanProperty BROKEN; - public static final BooleanProperty WATERLOGGED; public LightBulbBlock(Block.Settings settings) { - super(settings.luminance(((BlockState state) -> { - if (state.get(BROKEN)) return 0; - if (!state.get(INVERTED)) { - return state.get(ON) ? 12 : 0; - } else { - return state.get(ON) ? 0 : 12; + super(settings, false, (BlockState state) -> { + if (state.get(BROKEN)) { + return 0; } - }))); - this.setDefaultState((BlockState)this.getDefaultState() - .with(ON, false) - .with(BROKEN, false) - .with(WATERLOGGED, false) - .with(INVERTED, false)); + if (state.get(INVERTED)) { + return state.get(ON) ? 0 : 12; + } else { + return state.get(ON) ? 12 : 0; + } + }); } @Nullable public BlockState getPlacementState(ItemPlacementContext ctx) { return (BlockState)this.getDefaultState() - .with(ON, ctx.getWorld().isReceivingRedstonePower(ctx.getBlockPos())) - .with(BROKEN, false) - .with(INVERTED, Config.INSTANCE.shouldInvertLamps()) - .with(WATERLOGGED, ctx.getWorld().getFluidState(ctx.getBlockPos()).getFluid() == Fluids.WATER); - } - - 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.setBlockState(pos, (BlockState)state.cycle(ON), 2); - } - } - } - } - - @Override - 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 direction == Direction.UP && !state.canPlaceAt(world, pos) ? Blocks.AIR.getDefaultState() : super.getStateForNeighborUpdate(state, direction, neighborState, world, pos, neighborPos); + .with(BROKEN, false); } public ActionResult onUse(BlockState state, World world, BlockPos pos, PlayerEntity player, Hand hand, BlockHitResult hit) { - if (world.isClient) return super.onUse(state, world, pos, player, hand, hit); -// if (!state.get(BROKEN)) { -// world.setBlockState(pos, state.cycle(INVERTED)); -// return ActionResult.CONSUME; -// } - - if (state.get(BROKEN) && player.getInventory().getMainHandStack().getItem() == NSE_Items.LIGHT_BULB && !player.getItemCooldownManager().isCoolingDown(NSE_Items.LIGHT_BULB)) { - if (world.isReceivingRedstonePower(pos) == state.get(INVERTED) || (NewSoviet.RANDOM.nextBetween(1, 32) == 1)) { - if (!player.isCreative()) - player.getInventory().getMainHandStack().decrement(1); - world.setBlockState(pos, (BlockState)state.with(BROKEN, false)); - return ActionResult.CONSUME; - } else { - player.getItemCooldownManager().set(NSE_Items.LIGHT_BULB, 10); - player.sendMessage(Text.translatable("block.new_soviet.light_bulb_block.energized")); - world.playSound((PlayerEntity)null, pos.getX(), pos.getY(), pos.getZ(), NSE_Custom.ELECTRIC_HIT, SoundCategory.AMBIENT, 0.8f, 1f); - if (!player.isCreative()) { - player.damage(world.getDamageSources().lightningBolt(), NewSoviet.RANDOM.nextBetween(1, 4)); + if (state.get(BROKEN)) { + if (player.getInventory().getMainHandStack().getItem() == NSE_Items.LIGHT_BULB && !player.getItemCooldownManager().isCoolingDown(NSE_Items.LIGHT_BULB)) { + if (world.isReceivingRedstonePower(pos) == state.get(INVERTED) || (NewSoviet.RANDOM.nextBetween(1, 32) == 1)) { + if (!player.isCreative()) + player.getInventory().getMainHandStack().decrement(1); + world.setBlockState(pos, (BlockState)state.with(BROKEN, false)); + return ActionResult.SUCCESS; + } else { + player.getItemCooldownManager().set(NSE_Items.LIGHT_BULB, 10); + player.sendMessage(Text.translatable("block.new_soviet.light_bulb_block.energized")); + world.playSound((PlayerEntity)null, pos.getX(), pos.getY(), pos.getZ(), NSE_Custom.ELECTRIC_HIT, SoundCategory.AMBIENT, 0.8f, 1f); + if (!player.isCreative()) { + player.damage(world.getDamageSources().lightningBolt(), NewSoviet.RANDOM.nextBetween(1, 4)); + } } + return ActionResult.SUCCESS; } - return ActionResult.CONSUME; - } + return ActionResult.PASS; + } else { + return super.onUse(state, world, pos, player, hand, hit); - return super.onUse(state, world, pos, player, hand, hit); + } } @@ -123,20 +91,10 @@ public class LightBulbBlock extends Block implements Waterloggable { super.onProjectileHit(world, state, hit, projectile); } - public void scheduledTick(BlockState state, ServerWorld world, BlockPos pos, Random random) { - if ((Boolean)state.get(ON) && !world.isReceivingRedstonePower(pos)) { - world.setBlockState(pos, (BlockState)state.cycle(ON), 2); - } - } - protected void appendProperties(StateManager.Builder builder) { builder.add(new Property[]{ON, BROKEN, WATERLOGGED, INVERTED}); } - public FluidState getFluidState(BlockState state) { - return state.get(WATERLOGGED) ? Fluids.WATER.getStill(false) : super.getFluidState(state); - } - public VoxelShape getOutlineShape(BlockState state, BlockView world, BlockPos pos, ShapeContext context) { if (state.get(BROKEN)) { return SHAPE; @@ -152,9 +110,6 @@ public class LightBulbBlock extends Block implements Waterloggable { static { SHAPE = VoxelShapes.union(Block.createCuboidShape(7, 15, 7, 9, 16, 9), Block.createCuboidShape(7, 6, 7, 9, 8, 9)); //VoxelShapes.union(Block.createCuboidShape(4.0, 2.0, 4.0, 12.0, 7.0, 12.0), Block.createCuboidShape(7.0, 7.0, 7.0, 9.0, 13.0, 9.0), Block.createCuboidShape(6.0, 13.0, 6.0, 10.0, 16.0, 10.0)); - ON = RedstoneTorchBlock.LIT; BROKEN = Properties.CRACKED; - WATERLOGGED = Properties.WATERLOGGED; - INVERTED = Properties.INVERTED; } } \ No newline at end of file diff --git a/src/main/java/su/a71/new_soviet/blocks/SwitchBlock.java b/src/main/java/su/a71/new_soviet/blocks/SwitchBlock.java index 0a26a95..159db2f 100644 --- a/src/main/java/su/a71/new_soviet/blocks/SwitchBlock.java +++ b/src/main/java/su/a71/new_soviet/blocks/SwitchBlock.java @@ -83,9 +83,9 @@ public class SwitchBlock extends LeverBlock { return ActionResult.SUCCESS; } BlockState blockState = this.togglePower(state, world, pos); - float f = blockState.get(POWERED) != false ? 1f : 0.9f; + float f = blockState.get(POWERED) ? 1f : 0.9f; world.playSound(null, pos, NSE_Sounds.SWITCH_PRESS, SoundCategory.BLOCKS, 0.6f, f); - world.emitGameEvent((Entity)player, blockState.get(POWERED) != false ? GameEvent.BLOCK_ACTIVATE : GameEvent.BLOCK_DEACTIVATE, pos); + world.emitGameEvent((Entity)player, blockState.get(POWERED) ? GameEvent.BLOCK_ACTIVATE : GameEvent.BLOCK_DEACTIVATE, pos); return ActionResult.CONSUME; } @@ -100,7 +100,7 @@ public class SwitchBlock extends LeverBlock { @Override public void randomDisplayTick(BlockState state, World world, BlockPos pos, Random random) { - if (state.get(POWERED).booleanValue() && random.nextFloat() < 0.25f) { + if (state.get(POWERED) && random.nextFloat() < 0.25f) { //SwitchBlock.spawnParticles(state, world, pos, 0.5f); } } diff --git a/src/main/java/su/a71/new_soviet/blocks/TableLampBlock.java b/src/main/java/su/a71/new_soviet/blocks/TableLampBlock.java new file mode 100644 index 0000000..7f59e3f --- /dev/null +++ b/src/main/java/su/a71/new_soviet/blocks/TableLampBlock.java @@ -0,0 +1,42 @@ +package su.a71.new_soviet.blocks; + +import net.minecraft.block.*; +import net.minecraft.util.math.BlockPos; +import net.minecraft.util.math.Direction; +import net.minecraft.util.shape.VoxelShape; +import net.minecraft.util.shape.VoxelShapes; +import net.minecraft.world.BlockView; +import net.minecraft.world.WorldView; + +public class TableLampBlock extends LampBlock { + protected static final VoxelShape SHAPE; + + public TableLampBlock(AbstractBlock.Settings settings) { + super(settings, true, null); + } + + public VoxelShape getOutlineShape(BlockState state, BlockView world, BlockPos pos, ShapeContext context) { + return SHAPE; + } + + public boolean canPlaceAt(BlockState state, WorldView world, BlockPos pos) { + Direction direction = Direction.DOWN; + return Block.sideCoversSmallSquare(world, pos.offset(direction), direction.getOpposite()); + } + + + + public static VoxelShape getStandingShape(){ + VoxelShape shape = VoxelShapes.empty(); + shape = VoxelShapes.union(shape, VoxelShapes.cuboid(0.4375, 0.25, 0.4375, 0.5625, 0.875, 0.5625)); + shape = VoxelShapes.union(shape, VoxelShapes.cuboid(0.375, 0, 0.375, 0.625, 0.25, 0.625)); + shape = VoxelShapes.union(shape, VoxelShapes.cuboid(0.25, 0.6875, 0.25, 0.75, 0.9375, 0.75)); + shape = VoxelShapes.union(shape, VoxelShapes.cuboid(0.125, 0.3125, 0.125, 0.875, 0.6875, 0.875)); + shape.simplify(); + return shape; + } + + static { + SHAPE = getStandingShape(); + } +} \ No newline at end of file diff --git a/src/main/java/su/a71/new_soviet/registration/NSE_Custom.java b/src/main/java/su/a71/new_soviet/registration/NSE_Custom.java index ef8326a..1e5b590 100644 --- a/src/main/java/su/a71/new_soviet/registration/NSE_Custom.java +++ b/src/main/java/su/a71/new_soviet/registration/NSE_Custom.java @@ -31,7 +31,7 @@ public class NSE_Custom extends NSE_BaseRegistration { public static final RadioReceiverBlock RADIO_RECEIVER = new RadioReceiverBlock(); public static final SwitchBlock SWITCH = new SwitchBlock(FabricBlockSettings.create().sounds(BlockSoundGroup.METAL).notSolid().pistonBehavior(PistonBehavior.DESTROY).strength(1f, 2f).mapColor(MapColor.TERRACOTTA_WHITE)); - public static final LampBlock LAMP = new LampBlock(FabricBlockSettings.create().sounds(BlockSoundGroup.LANTERN).strength(1f, 1.5f).mapColor(MapColor.WHITE)); + public static final TableLampBlock LAMP = new TableLampBlock(FabricBlockSettings.create().sounds(BlockSoundGroup.LANTERN).strength(1f, 1.5f).mapColor(MapColor.WHITE)); public static final LightBulbBlock LIGHT_BULB = new LightBulbBlock(FabricBlockSettings.create().sounds(BlockSoundGroup.GLASS).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)); diff --git a/src/main/resources/assets/new_soviet/lang/en_us.json b/src/main/resources/assets/new_soviet/lang/en_us.json index 571a728..10ab373 100644 --- a/src/main/resources/assets/new_soviet/lang/en_us.json +++ b/src/main/resources/assets/new_soviet/lang/en_us.json @@ -431,5 +431,6 @@ "block.new_soviet.cyan_linoleum_slab": "Cyan Linoleum Slab", "block.new_soviet.cyan_linoleum_stairs": "Cyan Linoleum Stairs", "block.new_soviet.metal_plating_slab": "Metal Plating Slab", - "block.new_soviet.metal_plating_stairs": "Metal Plating Stairs" + "block.new_soviet.metal_plating_stairs": "Metal Plating Stairs", + "subtitles.new_soviet.electric_hit": "Electric shock" } \ No newline at end of file