From 25348f5e72ca4b0abb32440851ee6a626e13869b Mon Sep 17 00:00:00 2001 From: Andrew-71 Date: Thu, 24 Aug 2023 14:25:58 +0300 Subject: [PATCH] Improve (I hope) lamps --- TODO.md | 2 - .../su/a71/new_soviet/blocks/LampBlock.java | 77 ++++++++++++++----- .../a71/new_soviet/blocks/LightBulbBlock.java | 57 +++++++++----- .../registration/NSE_BaseRegistration.java | 11 +++ .../new_soviet/registration/NSE_Custom.java | 8 +- .../assets/new_soviet/blockstates/lamp.json | 6 +- .../new_soviet/blockstates/light_bulb.json | 20 ++++- 7 files changed, 126 insertions(+), 55 deletions(-) diff --git a/TODO.md b/TODO.md index 3941465..3ef5f90 100644 --- a/TODO.md +++ b/TODO.md @@ -1,7 +1,5 @@ === Andrew71 agenda (14.08.23) === * Minotaur publishing -* Set up curse page and make something like minotaur there. NB: Check cursegradle? -* Set up gitea maven * Fix GitHub mirror * Make good README.md stuff * Revise all new code for issues 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 4865a50..65b4787 100644 --- a/src/main/java/su/a71/new_soviet/blocks/LampBlock.java +++ b/src/main/java/su/a71/new_soviet/blocks/LampBlock.java @@ -2,32 +2,49 @@ package su.a71.new_soviet.blocks; import net.minecraft.block.*; import net.minecraft.entity.ai.pathing.NavigationType; +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.state.StateManager; import net.minecraft.state.property.BooleanProperty; import net.minecraft.state.property.Properties; import net.minecraft.state.property.Property; +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; 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; public class LampBlock extends Block implements Waterloggable { - public static final BooleanProperty HANGING; + public static final BooleanProperty ON; + public static final BooleanProperty INVERTED; public static final BooleanProperty WATERLOGGED; - protected static final VoxelShape STANDING_SHAPE; - protected static final VoxelShape HANGING_SHAPE; + protected static final VoxelShape SHAPE; public LampBlock(AbstractBlock.Settings settings) { - super(settings.luminance((BlockState state) -> 12)); - this.setDefaultState(this.stateManager.getDefaultState().with(HANGING, false).with(WATERLOGGED, false)); + super(settings.luminance((BlockState state) -> { + if (!state.get(INVERTED)) { + return state.get(ON) ? 12 : 0; + } else { + return state.get(ON) ? 0 : 12; + } + })); + this.setDefaultState(this.stateManager.getDefaultState() + .with(INVERTED, false) + .with(WATERLOGGED, false) + .with(ON, false)); } @Nullable @@ -36,7 +53,9 @@ public class LampBlock extends Block implements Waterloggable { Direction[] directions = ctx.getPlacementDirections(); for (Direction direction : directions) { if (direction.getAxis() == Direction.Axis.Y) { - BlockState blockState = this.getDefaultState().with(HANGING, direction == Direction.UP); + BlockState blockState = this.getDefaultState() + .with(INVERTED, Config.INSTANCE.shouldInvertLamps()) + .with(ON, ctx.getWorld().isReceivingRedstonePower(ctx.getBlockPos())); if (blockState.canPlaceAt(ctx.getWorld(), ctx.getBlockPos())) { return blockState.with(WATERLOGGED, fluidState.getFluid() == Fluids.WATER); } @@ -45,12 +64,19 @@ public class LampBlock extends Block implements Waterloggable { return null; } + @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 state.get(HANGING) ? HANGING_SHAPE : STANDING_SHAPE; + return SHAPE; } protected void appendProperties(StateManager.Builder builder) { - builder.add(new Property[]{HANGING, WATERLOGGED}); + builder.add(new Property[]{ON, WATERLOGGED, INVERTED}); } public boolean canPlaceAt(BlockState state, WorldView world, BlockPos pos) { @@ -59,7 +85,7 @@ public class LampBlock extends Block implements Waterloggable { } protected static Direction attachedDirection(BlockState state) { - return state.get(HANGING) ? Direction.DOWN : Direction.UP; + return Direction.UP; } public BlockState getStateForNeighborUpdate(BlockState state, Direction direction, BlockState neighborState, WorldAccess world, BlockPos pos, BlockPos neighborPos) { @@ -67,7 +93,7 @@ public class LampBlock extends Block implements Waterloggable { world.scheduleFluidTick(pos, Fluids.WATER, Fluids.WATER.getTickRate(world)); } - return attachedDirection(state).getOpposite() == direction && !state.canPlaceAt(world, pos) ? Blocks.AIR.getDefaultState() : super.getStateForNeighborUpdate(state, direction, neighborState, world, pos, neighborPos); + return Direction.DOWN == direction && !state.canPlaceAt(world, pos) ? Blocks.AIR.getDefaultState() : super.getStateForNeighborUpdate(state, direction, neighborState, world, pos, neighborPos); } public FluidState getFluidState(BlockState state) { @@ -78,14 +104,23 @@ public class LampBlock extends Block implements Waterloggable { return false; } - public static VoxelShape getHangingShape(){ - VoxelShape shape = VoxelShapes.empty(); - shape = VoxelShapes.union(shape, VoxelShapes.cuboid(0.34375, -0.221875, 0.34375, 0.65625, 0.090625, 0.65625)); - shape = VoxelShapes.union(shape, VoxelShapes.cuboid(0.125, 0.0625, 0.125, 0.875, 0.4375, 0.875)); - shape = VoxelShapes.union(shape, VoxelShapes.cuboid(0.5, 0.5, 0.125, 0.5, 0.875, 0.875)); - shape = VoxelShapes.union(shape, VoxelShapes.cuboid(0.125, 0.5, 0.5, 0.875, 0.875, 0.5)); - shape.simplify(); - return shape; + 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); + } + } + } + } + + 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); + } } public static VoxelShape getStandingShape(){ @@ -99,9 +134,9 @@ public class LampBlock extends Block implements Waterloggable { } static { - HANGING = Properties.HANGING; + SHAPE = getStandingShape(); + ON = RedstoneTorchBlock.LIT; WATERLOGGED = Properties.WATERLOGGED; - STANDING_SHAPE = getStandingShape(); - HANGING_SHAPE = getHangingShape(); + INVERTED = Properties.INVERTED; } } \ No newline at end of file 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 4562b7b..ff74e32 100644 --- a/src/main/java/su/a71/new_soviet/blocks/LightBulbBlock.java +++ b/src/main/java/su/a71/new_soviet/blocks/LightBulbBlock.java @@ -27,6 +27,7 @@ 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.NewSoviet; import su.a71.new_soviet.registration.NSE_Custom; import su.a71.new_soviet.registration.NSE_Items; @@ -35,12 +36,24 @@ import su.a71.new_soviet.registration.NSE_Sounds; public class LightBulbBlock extends Block implements Waterloggable { 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) -> state.get(ON) && !state.get(BROKEN) ? 12 : 0)); - this.setDefaultState((BlockState)this.getDefaultState().with(ON, false).with(BROKEN, false).with(WATERLOGGED, false)); + 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; + } + }))); + this.setDefaultState((BlockState)this.getDefaultState() + .with(ON, false) + .with(BROKEN, false) + .with(WATERLOGGED, false) + .with(INVERTED, false)); } @Nullable @@ -48,6 +61,7 @@ public class LightBulbBlock extends Block implements Waterloggable { 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); } @@ -72,26 +86,30 @@ public class LightBulbBlock extends Block implements Waterloggable { return direction == Direction.UP && !state.canPlaceAt(world, pos) ? Blocks.AIR.getDefaultState() : super.getStateForNeighborUpdate(state, direction, neighborState, world, pos, neighborPos); } - @Override public ActionResult onUse(BlockState state, World world, BlockPos pos, PlayerEntity player, Hand hand, BlockHitResult hit) { - if (!world.isClient && state.get(BROKEN) && player.getInventory().getMainHandStack().getItem() == NSE_Items.LIGHT_BULB && !world.isReceivingRedstonePower(pos)) { - if (!player.isCreative()) - player.getInventory().getMainHandStack().decrement(1); - world.setBlockState(pos, (BlockState)state.with(BROKEN, false)); - //.with(ON, world.isReceivingRedstonePower(pos)), 2); - } else if (!world.isClient && state.get(BROKEN) && player.getInventory().getMainHandStack().getItem() == NSE_Items.LIGHT_BULB) { - 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.heal(-1 * NewSoviet.RANDOM.nextBetween(1, 4)); - } - if (NewSoviet.RANDOM.nextBetween(1, 32) == 1){ + 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) - .with(ON, world.isReceivingRedstonePower(pos)), 2); + 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)); + } } + return ActionResult.CONSUME; } + return super.onUse(state, world, pos, player, hand, hit); } @@ -101,7 +119,7 @@ public class LightBulbBlock extends Block implements Waterloggable { if (!state.get(BROKEN)) { world.playSound((PlayerEntity)null, hit.getBlockPos().getX(), hit.getBlockPos().getY(), hit.getBlockPos().getZ(), NSE_Sounds.LIGHT_BULB_BROKEN_SOUND, SoundCategory.NEUTRAL, 0.8f, 1f); } - world.setBlockState(hit.getBlockPos(), (BlockState)state.with(BROKEN, true).with(ON, false), 2); + world.setBlockState(hit.getBlockPos(), (BlockState)state.with(BROKEN, true), 2); super.onProjectileHit(world, state, hit, projectile); } @@ -112,7 +130,7 @@ public class LightBulbBlock extends Block implements Waterloggable { } protected void appendProperties(StateManager.Builder builder) { - builder.add(new Property[]{ON, BROKEN, WATERLOGGED}); + builder.add(new Property[]{ON, BROKEN, WATERLOGGED, INVERTED}); } public FluidState getFluidState(BlockState state) { @@ -137,5 +155,6 @@ public class LightBulbBlock extends Block implements Waterloggable { 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/registration/NSE_BaseRegistration.java b/src/main/java/su/a71/new_soviet/registration/NSE_BaseRegistration.java index 94f62ce..cb3b029 100644 --- a/src/main/java/su/a71/new_soviet/registration/NSE_BaseRegistration.java +++ b/src/main/java/su/a71/new_soviet/registration/NSE_BaseRegistration.java @@ -2,7 +2,10 @@ package su.a71.new_soviet.registration; import net.fabricmc.fabric.api.item.v1.FabricItemSettings; import net.fabricmc.fabric.api.itemgroup.v1.ItemGroupEvents; +import net.fabricmc.fabric.api.object.builder.v1.block.entity.FabricBlockEntityTypeBuilder; import net.minecraft.block.Block; +import net.minecraft.block.entity.BlockEntity; +import net.minecraft.block.entity.BlockEntityType; import net.minecraft.item.BlockItem; import net.minecraft.item.Item; import net.minecraft.item.ItemGroup; @@ -15,6 +18,7 @@ import net.minecraft.util.Identifier; import java.util.Optional; import java.util.function.Supplier; import su.a71.new_soviet.NewSoviet; +import su.a71.new_soviet.entity.TVBlockEntity; public class NSE_BaseRegistration { @@ -39,5 +43,12 @@ public class NSE_BaseRegistration { return Registry.register(Registries.SOUND_EVENT, id, SoundEvent.of(id)); } + public static T registerBlockEntity(String name, FabricBlockEntityTypeBuilder.Factory factory, net.minecraft.block.Block... blocks) { + return (T) Registry.register( + Registries.BLOCK_ENTITY_TYPE, + new Identifier(NewSoviet.MOD_ID, name), + FabricBlockEntityTypeBuilder.create(factory, blocks).build()); + } + public static void init() {} } 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 4b5caf5..bd29137 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 @@ -2,7 +2,6 @@ package su.a71.new_soviet.registration; import net.fabricmc.fabric.api.itemgroup.v1.FabricItemGroup; import net.fabricmc.fabric.api.object.builder.v1.block.FabricBlockSettings; -import net.fabricmc.fabric.api.object.builder.v1.block.entity.FabricBlockEntityTypeBuilder; import net.minecraft.block.AbstractBlock; import net.minecraft.block.MapColor; import net.minecraft.block.entity.BlockEntityType; @@ -24,11 +23,8 @@ public class NSE_Custom extends NSE_BaseRegistration { public static final TVBlock TV = new TVBlock(FabricBlockSettings.create().mapColor(MapColor.TERRACOTTA_YELLOW)); public static final TVBlock RED_TV = new TVBlock(FabricBlockSettings.create().mapColor(MapColor.TERRACOTTA_RED)); public static final TVBlock BROWN_TV = new TVBlock(FabricBlockSettings.create().mapColor(MapColor.TERRACOTTA_BROWN)); - public static final BlockEntityType TV_BLOCK_ENTITY = Registry.register( - Registries.BLOCK_ENTITY_TYPE, - new Identifier(NewSoviet.MOD_ID, "tv_block_entity"), - FabricBlockEntityTypeBuilder.create(TVBlockEntity::new, TV, RED_TV, BROWN_TV).build() - ); + public static final BlockEntityType TV_BLOCK_ENTITY = registerBlockEntity("tv_block_entity", TVBlockEntity::new, TV, RED_TV, BROWN_TV); + public static final RadioBlock RADIO = new RadioBlock(); public static final SwitchBlock SWITCH = new SwitchBlock(FabricBlockSettings.create().sounds(BlockSoundGroup.METAL).notSolid().pistonBehavior(PistonBehavior.DESTROY).strength(1f, 2f).mapColor(MapColor.TERRACOTTA_WHITE)); diff --git a/src/main/resources/assets/new_soviet/blockstates/lamp.json b/src/main/resources/assets/new_soviet/blockstates/lamp.json index db8e5dd..985f00c 100644 --- a/src/main/resources/assets/new_soviet/blockstates/lamp.json +++ b/src/main/resources/assets/new_soviet/blockstates/lamp.json @@ -1,10 +1,10 @@ { "variants": { - "hanging=false": { + "inverted=true": { "model": "new_soviet:block/table_lamp" }, - "hanging=true": { - "model": "new_soviet:block/ceiling_lamp" + "inverted=false": { + "model": "new_soviet:block/table_lamp" } } } \ No newline at end of file diff --git a/src/main/resources/assets/new_soviet/blockstates/light_bulb.json b/src/main/resources/assets/new_soviet/blockstates/light_bulb.json index e04cfea..5400d8b 100644 --- a/src/main/resources/assets/new_soviet/blockstates/light_bulb.json +++ b/src/main/resources/assets/new_soviet/blockstates/light_bulb.json @@ -1,15 +1,27 @@ { "variants": { - "lit=true,cracked=true": { + "lit=true,cracked=true,inverted=false": { "model": "new_soviet:block/light_bulb_broken" }, - "lit=true,cracked=false": { + "lit=true,cracked=false,inverted=false": { "model": "new_soviet:block/light_bulb_on" }, - "lit=false,cracked=false": { + "lit=false,cracked=false,inverted=false": { "model": "new_soviet:block/light_bulb_off" }, - "lit=false,cracked=true": { + "lit=false,cracked=true,inverted=false": { + "model": "new_soviet:block/light_bulb_broken" + }, + "lit=true,cracked=true,inverted=true": { + "model": "new_soviet:block/light_bulb_broken" + }, + "lit=true,cracked=false,inverted=true": { + "model": "new_soviet:block/light_bulb_off" + }, + "lit=false,cracked=false,inverted=true": { + "model": "new_soviet:block/light_bulb_on" + }, + "lit=false,cracked=true,inverted=true": { "model": "new_soviet:block/light_bulb_broken" } }