Fix lamps

This commit is contained in:
Andrew-71 2023-09-03 15:09:48 +03:00
parent 12c6cc3821
commit 9758a0ad31
7 changed files with 97 additions and 126 deletions

View file

@ -3,7 +3,6 @@ package su.a71.new_soviet;
import java.io.*; import java.io.*;
public class Config { public class Config {
private boolean invert_lamps = false;
private boolean announce_dice = false; private boolean announce_dice = false;
public static Config INSTANCE; public static Config INSTANCE;
@ -12,9 +11,6 @@ public class Config {
INSTANCE = this; INSTANCE = this;
} }
public boolean shouldInvertLamps() {
return invert_lamps;
}
public boolean shouldAnnounceDice() { public boolean shouldAnnounceDice() {
return announce_dice; return announce_dice;
} }

View file

@ -7,6 +7,7 @@ import net.minecraft.fluid.FluidState;
import net.minecraft.fluid.Fluids; import net.minecraft.fluid.Fluids;
import net.minecraft.item.ItemPlacementContext; import net.minecraft.item.ItemPlacementContext;
import net.minecraft.server.world.ServerWorld; import net.minecraft.server.world.ServerWorld;
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.BooleanProperty;
import net.minecraft.state.property.Properties; 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.BlockPos;
import net.minecraft.util.math.Direction; import net.minecraft.util.math.Direction;
import net.minecraft.util.math.random.Random; 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.BlockView;
import net.minecraft.world.World; import net.minecraft.world.World;
import net.minecraft.world.WorldAccess; import net.minecraft.world.WorldAccess;
import net.minecraft.world.WorldView;
import org.jetbrains.annotations.Nullable; 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 ON;
public static final BooleanProperty INVERTED; public static final BooleanProperty INVERTED;
public static final BooleanProperty WATERLOGGED; public static final BooleanProperty WATERLOGGED;
protected static final VoxelShape SHAPE; public boolean defaultLightState;
public LampBlock(AbstractBlock.Settings settings) { public LampBlock(AbstractBlock.Settings settings, boolean defaultLightState, java.util.function.ToIntFunction<net.minecraft.block.BlockState> luminance) {
super(settings.luminance((BlockState state) -> { super(settings.luminance(luminance == null ? (BlockState state) -> {
if (state.get(INVERTED)) { if (state.get(INVERTED)) {
return state.get(ON) ? 12 : 0;
} else {
return state.get(ON) ? 0 : 12; return state.get(ON) ? 0 : 12;
} else {
return state.get(ON) ? 12 : 0;
} }
})); } : luminance));
this.defaultLightState = defaultLightState;
this.setDefaultState(this.stateManager.getDefaultState() this.setDefaultState(this.stateManager.getDefaultState()
.with(INVERTED, false) .with(INVERTED, defaultLightState)
.with(WATERLOGGED, false) .with(WATERLOGGED, false)
.with(ON, false)); .with(ON, false));
} }
@ -54,7 +52,7 @@ public class LampBlock extends Block implements Waterloggable {
for (Direction direction : directions) { for (Direction direction : directions) {
if (direction.getAxis() == Direction.Axis.Y) { if (direction.getAxis() == Direction.Axis.Y) {
BlockState blockState = this.getDefaultState() BlockState blockState = this.getDefaultState()
.with(INVERTED, Config.INSTANCE.shouldInvertLamps()) .with(INVERTED, defaultLightState)
.with(ON, ctx.getWorld().isReceivingRedstonePower(ctx.getBlockPos())); .with(ON, ctx.getWorld().isReceivingRedstonePower(ctx.getBlockPos()));
if (blockState.canPlaceAt(ctx.getWorld(), ctx.getBlockPos())) { if (blockState.canPlaceAt(ctx.getWorld(), ctx.getBlockPos())) {
return blockState.with(WATERLOGGED, fluidState.getFluid() == Fluids.WATER); return blockState.with(WATERLOGGED, fluidState.getFluid() == Fluids.WATER);
@ -66,34 +64,24 @@ public class LampBlock extends Block implements Waterloggable {
@Override @Override
public ActionResult onUse(BlockState state, World world, BlockPos pos, PlayerEntity player, Hand hand, BlockHitResult hit) { 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 (!world.isClient) {
// world.setBlockState(pos, state.cycle(INVERTED)); world.setBlockState(pos, (BlockState)state.cycle(INVERTED), 2);
return super.onUse(state, world, pos, player, hand, hit); }
} float f = state.get(ON) ? 1f : 0.9f;
world.playSound(null, pos, NSE_Sounds.SWITCH_PRESS, SoundCategory.BLOCKS, 0.6f, f);
public VoxelShape getOutlineShape(BlockState state, BlockView world, BlockPos pos, ShapeContext context) { return ActionResult.SUCCESS;
return SHAPE;
} }
protected void appendProperties(StateManager.Builder<Block, BlockState> builder) { protected void appendProperties(StateManager.Builder<Block, BlockState> builder) {
builder.add(new Property[]{ON, WATERLOGGED, INVERTED}); 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) { public BlockState getStateForNeighborUpdate(BlockState state, Direction direction, BlockState neighborState, WorldAccess world, BlockPos pos, BlockPos neighborPos) {
if (state.get(WATERLOGGED)) { if (state.get(WATERLOGGED)) {
world.scheduleFluidTick(pos, Fluids.WATER, Fluids.WATER.getTickRate(world)); 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) { 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 { static {
SHAPE = getStandingShape();
ON = RedstoneTorchBlock.LIT; ON = RedstoneTorchBlock.LIT;
WATERLOGGED = Properties.WATERLOGGED; WATERLOGGED = Properties.WATERLOGGED;
INVERTED = Properties.INVERTED; INVERTED = Properties.INVERTED;

View file

@ -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_Items;
import su.a71.new_soviet.registration.NSE_Sounds; 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; protected static final VoxelShape SHAPE;
public static final BooleanProperty ON;
public static final BooleanProperty INVERTED;
public static final BooleanProperty BROKEN; public static final BooleanProperty BROKEN;
public static final BooleanProperty WATERLOGGED;
public LightBulbBlock(Block.Settings settings) { public LightBulbBlock(Block.Settings settings) {
super(settings.luminance(((BlockState state) -> { super(settings, false, (BlockState state) -> {
if (state.get(BROKEN)) return 0; if (state.get(BROKEN)) {
if (!state.get(INVERTED)) { return 0;
return state.get(ON) ? 12 : 0;
} else {
return state.get(ON) ? 0 : 12;
} }
}))); if (state.get(INVERTED)) {
this.setDefaultState((BlockState)this.getDefaultState() return state.get(ON) ? 0 : 12;
.with(ON, false) } else {
.with(BROKEN, false) return state.get(ON) ? 12 : 0;
.with(WATERLOGGED, false) }
.with(INVERTED, false)); });
} }
@Nullable @Nullable
public BlockState getPlacementState(ItemPlacementContext ctx) { public BlockState getPlacementState(ItemPlacementContext ctx) {
return (BlockState)this.getDefaultState() return (BlockState)this.getDefaultState()
.with(ON, ctx.getWorld().isReceivingRedstonePower(ctx.getBlockPos())) .with(BROKEN, false);
.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);
} }
public ActionResult onUse(BlockState state, World world, BlockPos pos, PlayerEntity player, Hand hand, BlockHitResult hit) { 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)) {
// if (!state.get(BROKEN)) { if (player.getInventory().getMainHandStack().getItem() == NSE_Items.LIGHT_BULB && !player.getItemCooldownManager().isCoolingDown(NSE_Items.LIGHT_BULB)) {
// world.setBlockState(pos, state.cycle(INVERTED)); if (world.isReceivingRedstonePower(pos) == state.get(INVERTED) || (NewSoviet.RANDOM.nextBetween(1, 32) == 1)) {
// return ActionResult.CONSUME; if (!player.isCreative())
// } player.getInventory().getMainHandStack().decrement(1);
world.setBlockState(pos, (BlockState)state.with(BROKEN, false));
if (state.get(BROKEN) && player.getInventory().getMainHandStack().getItem() == NSE_Items.LIGHT_BULB && !player.getItemCooldownManager().isCoolingDown(NSE_Items.LIGHT_BULB)) { return ActionResult.SUCCESS;
if (world.isReceivingRedstonePower(pos) == state.get(INVERTED) || (NewSoviet.RANDOM.nextBetween(1, 32) == 1)) { } else {
if (!player.isCreative()) player.getItemCooldownManager().set(NSE_Items.LIGHT_BULB, 10);
player.getInventory().getMainHandStack().decrement(1); player.sendMessage(Text.translatable("block.new_soviet.light_bulb_block.energized"));
world.setBlockState(pos, (BlockState)state.with(BROKEN, false)); world.playSound((PlayerEntity)null, pos.getX(), pos.getY(), pos.getZ(), NSE_Custom.ELECTRIC_HIT, SoundCategory.AMBIENT, 0.8f, 1f);
return ActionResult.CONSUME; if (!player.isCreative()) {
} else { player.damage(world.getDamageSources().lightningBolt(), NewSoviet.RANDOM.nextBetween(1, 4));
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); 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<Block, BlockState> builder) { protected void appendProperties(StateManager.Builder<Block, BlockState> builder) {
builder.add(new Property[]{ON, BROKEN, WATERLOGGED, INVERTED}); 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) { public VoxelShape getOutlineShape(BlockState state, BlockView world, BlockPos pos, ShapeContext context) {
if (state.get(BROKEN)) { if (state.get(BROKEN)) {
return SHAPE; return SHAPE;
@ -152,9 +110,6 @@ public class LightBulbBlock extends Block implements Waterloggable {
static { 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)); 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; BROKEN = Properties.CRACKED;
WATERLOGGED = Properties.WATERLOGGED;
INVERTED = Properties.INVERTED;
} }
} }

View file

@ -83,9 +83,9 @@ public class SwitchBlock extends LeverBlock {
return ActionResult.SUCCESS; return ActionResult.SUCCESS;
} }
BlockState blockState = this.togglePower(state, world, pos); 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.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; return ActionResult.CONSUME;
} }
@ -100,7 +100,7 @@ public class SwitchBlock extends LeverBlock {
@Override @Override
public void randomDisplayTick(BlockState state, World world, BlockPos pos, Random random) { 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); //SwitchBlock.spawnParticles(state, world, pos, 0.5f);
} }
} }

View file

@ -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();
}
}

View file

@ -31,7 +31,7 @@ public class NSE_Custom extends NSE_BaseRegistration {
public static final RadioReceiverBlock RADIO_RECEIVER = new RadioReceiverBlock(); 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 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 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)); public static final CeilingFanBlock CEILING_FAN = new CeilingFanBlock(FabricBlockSettings.create().sounds(BlockSoundGroup.METAL).strength(1f, 1.5f).mapColor(MapColor.WHITE));

View file

@ -431,5 +431,6 @@
"block.new_soviet.cyan_linoleum_slab": "Cyan Linoleum Slab", "block.new_soviet.cyan_linoleum_slab": "Cyan Linoleum Slab",
"block.new_soviet.cyan_linoleum_stairs": "Cyan Linoleum Stairs", "block.new_soviet.cyan_linoleum_stairs": "Cyan Linoleum Stairs",
"block.new_soviet.metal_plating_slab": "Metal Plating Slab", "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"
} }