Improve (I hope) lamps

This commit is contained in:
Andrew-71 2023-08-24 14:25:58 +03:00
parent a166e94656
commit 25348f5e72
7 changed files with 126 additions and 55 deletions

View file

@ -1,7 +1,5 @@
=== Andrew71 agenda (14.08.23) === === Andrew71 agenda (14.08.23) ===
* Minotaur publishing * Minotaur publishing
* Set up curse page and make something like minotaur there. NB: Check cursegradle?
* Set up gitea maven
* Fix GitHub mirror * Fix GitHub mirror
* Make good README.md stuff * Make good README.md stuff
* Revise all new code for issues * Revise all new code for issues

View file

@ -2,32 +2,49 @@ package su.a71.new_soviet.blocks;
import net.minecraft.block.*; import net.minecraft.block.*;
import net.minecraft.entity.ai.pathing.NavigationType; import net.minecraft.entity.ai.pathing.NavigationType;
import net.minecraft.entity.player.PlayerEntity;
import net.minecraft.fluid.FluidState; 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.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;
import net.minecraft.state.property.Property; 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.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.world.World;
import net.minecraft.world.WorldAccess; import net.minecraft.world.WorldAccess;
import net.minecraft.world.WorldView; import net.minecraft.world.WorldView;
import org.jetbrains.annotations.Nullable; import org.jetbrains.annotations.Nullable;
import su.a71.new_soviet.Config;
public class LampBlock extends Block implements Waterloggable { 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; public static final BooleanProperty WATERLOGGED;
protected static final VoxelShape STANDING_SHAPE; protected static final VoxelShape SHAPE;
protected static final VoxelShape HANGING_SHAPE;
public LampBlock(AbstractBlock.Settings settings) { public LampBlock(AbstractBlock.Settings settings) {
super(settings.luminance((BlockState state) -> 12)); super(settings.luminance((BlockState state) -> {
this.setDefaultState(this.stateManager.getDefaultState().with(HANGING, false).with(WATERLOGGED, false)); 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 @Nullable
@ -36,7 +53,9 @@ public class LampBlock extends Block implements Waterloggable {
Direction[] directions = ctx.getPlacementDirections(); Direction[] directions = ctx.getPlacementDirections();
for (Direction direction : directions) { for (Direction direction : directions) {
if (direction.getAxis() == Direction.Axis.Y) { 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())) { if (blockState.canPlaceAt(ctx.getWorld(), ctx.getBlockPos())) {
return blockState.with(WATERLOGGED, fluidState.getFluid() == Fluids.WATER); return blockState.with(WATERLOGGED, fluidState.getFluid() == Fluids.WATER);
} }
@ -45,12 +64,19 @@ public class LampBlock extends Block implements Waterloggable {
return null; 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) { 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<Block, BlockState> builder) { protected void appendProperties(StateManager.Builder<Block, BlockState> builder) {
builder.add(new Property[]{HANGING, WATERLOGGED}); builder.add(new Property[]{ON, WATERLOGGED, INVERTED});
} }
public boolean canPlaceAt(BlockState state, WorldView world, BlockPos pos) { 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) { 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) { 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)); 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) { public FluidState getFluidState(BlockState state) {
@ -78,14 +104,23 @@ public class LampBlock extends Block implements Waterloggable {
return false; return false;
} }
public static VoxelShape getHangingShape(){ public void neighborUpdate(BlockState state, World world, BlockPos pos, Block sourceBlock, BlockPos sourcePos, boolean notify) {
VoxelShape shape = VoxelShapes.empty(); if (!world.isClient) {
shape = VoxelShapes.union(shape, VoxelShapes.cuboid(0.34375, -0.221875, 0.34375, 0.65625, 0.090625, 0.65625)); boolean bl = (Boolean)state.get(ON);
shape = VoxelShapes.union(shape, VoxelShapes.cuboid(0.125, 0.0625, 0.125, 0.875, 0.4375, 0.875)); if (bl != world.isReceivingRedstonePower(pos)) {
shape = VoxelShapes.union(shape, VoxelShapes.cuboid(0.5, 0.5, 0.125, 0.5, 0.875, 0.875)); if (bl) {
shape = VoxelShapes.union(shape, VoxelShapes.cuboid(0.125, 0.5, 0.5, 0.875, 0.875, 0.5)); world.scheduleBlockTick(pos, this, 4);
shape.simplify(); } else {
return shape; 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(){ public static VoxelShape getStandingShape(){
@ -99,9 +134,9 @@ public class LampBlock extends Block implements Waterloggable {
} }
static { static {
HANGING = Properties.HANGING; SHAPE = getStandingShape();
ON = RedstoneTorchBlock.LIT;
WATERLOGGED = Properties.WATERLOGGED; WATERLOGGED = Properties.WATERLOGGED;
STANDING_SHAPE = getStandingShape(); INVERTED = Properties.INVERTED;
HANGING_SHAPE = getHangingShape();
} }
} }

View file

@ -27,6 +27,7 @@ import net.minecraft.world.WorldAccess;
import net.minecraft.world.WorldView; 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.NewSoviet; import su.a71.new_soviet.NewSoviet;
import su.a71.new_soviet.registration.NSE_Custom; import su.a71.new_soviet.registration.NSE_Custom;
import su.a71.new_soviet.registration.NSE_Items; 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 { public class LightBulbBlock extends Block implements Waterloggable {
protected static final VoxelShape SHAPE; protected static final VoxelShape SHAPE;
public static final BooleanProperty ON; 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 static final BooleanProperty WATERLOGGED;
public LightBulbBlock(Block.Settings settings) { public LightBulbBlock(Block.Settings settings) {
super(settings.luminance((BlockState state) -> state.get(ON) && !state.get(BROKEN) ? 12 : 0)); super(settings.luminance(((BlockState state) -> {
this.setDefaultState((BlockState)this.getDefaultState().with(ON, false).with(BROKEN, false).with(WATERLOGGED, false)); 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 @Nullable
@ -48,6 +61,7 @@ public class LightBulbBlock extends Block implements Waterloggable {
return (BlockState)this.getDefaultState() return (BlockState)this.getDefaultState()
.with(ON, ctx.getWorld().isReceivingRedstonePower(ctx.getBlockPos())) .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); .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); 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) { 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 (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()) if (!player.isCreative())
player.getInventory().getMainHandStack().decrement(1); player.getInventory().getMainHandStack().decrement(1);
world.setBlockState(pos, (BlockState)state.with(BROKEN, false)); world.setBlockState(pos, (BlockState)state.with(BROKEN, false));
//.with(ON, world.isReceivingRedstonePower(pos)), 2); return ActionResult.CONSUME;
} else if (!world.isClient && state.get(BROKEN) && player.getInventory().getMainHandStack().getItem() == NSE_Items.LIGHT_BULB) { } else {
player.getItemCooldownManager().set(NSE_Items.LIGHT_BULB, 10);
player.sendMessage(Text.translatable("block.new_soviet.light_bulb_block.energized")); 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); world.playSound((PlayerEntity)null, pos.getX(), pos.getY(), pos.getZ(), NSE_Custom.ELECTRIC_HIT, SoundCategory.AMBIENT, 0.8f, 1f);
if (!player.isCreative()) { if (!player.isCreative()) {
player.heal(-1 * NewSoviet.RANDOM.nextBetween(1, 4)); player.damage(world.getDamageSources().lightningBolt(), NewSoviet.RANDOM.nextBetween(1, 4));
}
if (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);
} }
} }
return ActionResult.CONSUME;
}
return super.onUse(state, world, pos, player, hand, hit); return super.onUse(state, world, pos, player, hand, hit);
} }
@ -101,7 +119,7 @@ public class LightBulbBlock extends Block implements Waterloggable {
if (!state.get(BROKEN)) { 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.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); super.onProjectileHit(world, state, hit, projectile);
} }
@ -112,7 +130,7 @@ public class LightBulbBlock extends Block implements Waterloggable {
} }
protected void appendProperties(StateManager.Builder<Block, BlockState> builder) { protected void appendProperties(StateManager.Builder<Block, BlockState> builder) {
builder.add(new Property[]{ON, BROKEN, WATERLOGGED}); builder.add(new Property[]{ON, BROKEN, WATERLOGGED, INVERTED});
} }
public FluidState getFluidState(BlockState state) { public FluidState getFluidState(BlockState state) {
@ -137,5 +155,6 @@ public class LightBulbBlock extends Block implements Waterloggable {
ON = RedstoneTorchBlock.LIT; ON = RedstoneTorchBlock.LIT;
BROKEN = Properties.CRACKED; BROKEN = Properties.CRACKED;
WATERLOGGED = Properties.WATERLOGGED; WATERLOGGED = Properties.WATERLOGGED;
INVERTED = Properties.INVERTED;
} }
} }

View file

@ -2,7 +2,10 @@ package su.a71.new_soviet.registration;
import net.fabricmc.fabric.api.item.v1.FabricItemSettings; import net.fabricmc.fabric.api.item.v1.FabricItemSettings;
import net.fabricmc.fabric.api.itemgroup.v1.ItemGroupEvents; 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.Block;
import net.minecraft.block.entity.BlockEntity;
import net.minecraft.block.entity.BlockEntityType;
import net.minecraft.item.BlockItem; import net.minecraft.item.BlockItem;
import net.minecraft.item.Item; import net.minecraft.item.Item;
import net.minecraft.item.ItemGroup; import net.minecraft.item.ItemGroup;
@ -15,6 +18,7 @@ import net.minecraft.util.Identifier;
import java.util.Optional; import java.util.Optional;
import java.util.function.Supplier; import java.util.function.Supplier;
import su.a71.new_soviet.NewSoviet; import su.a71.new_soviet.NewSoviet;
import su.a71.new_soviet.entity.TVBlockEntity;
public class NSE_BaseRegistration { public class NSE_BaseRegistration {
@ -39,5 +43,12 @@ public class NSE_BaseRegistration {
return Registry.register(Registries.SOUND_EVENT, id, SoundEvent.of(id)); return Registry.register(Registries.SOUND_EVENT, id, SoundEvent.of(id));
} }
public static <T extends BlockEntityType> T registerBlockEntity(String name, FabricBlockEntityTypeBuilder.Factory<? extends BlockEntity> 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() {} public static void init() {}
} }

View file

@ -2,7 +2,6 @@ package su.a71.new_soviet.registration;
import net.fabricmc.fabric.api.itemgroup.v1.FabricItemGroup; 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.FabricBlockSettings;
import net.fabricmc.fabric.api.object.builder.v1.block.entity.FabricBlockEntityTypeBuilder;
import net.minecraft.block.AbstractBlock; import net.minecraft.block.AbstractBlock;
import net.minecraft.block.MapColor; import net.minecraft.block.MapColor;
import net.minecraft.block.entity.BlockEntityType; 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 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 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 TVBlock BROWN_TV = new TVBlock(FabricBlockSettings.create().mapColor(MapColor.TERRACOTTA_BROWN));
public static final BlockEntityType<TVBlockEntity> TV_BLOCK_ENTITY = Registry.register( public static final BlockEntityType<TVBlockEntity> TV_BLOCK_ENTITY = registerBlockEntity("tv_block_entity", TVBlockEntity::new, TV, RED_TV, BROWN_TV);
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 RadioBlock RADIO = new RadioBlock(); 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)); public static final SwitchBlock SWITCH = new SwitchBlock(FabricBlockSettings.create().sounds(BlockSoundGroup.METAL).notSolid().pistonBehavior(PistonBehavior.DESTROY).strength(1f, 2f).mapColor(MapColor.TERRACOTTA_WHITE));

View file

@ -1,10 +1,10 @@
{ {
"variants": { "variants": {
"hanging=false": { "inverted=true": {
"model": "new_soviet:block/table_lamp" "model": "new_soviet:block/table_lamp"
}, },
"hanging=true": { "inverted=false": {
"model": "new_soviet:block/ceiling_lamp" "model": "new_soviet:block/table_lamp"
} }
} }
} }

View file

@ -1,15 +1,27 @@
{ {
"variants": { "variants": {
"lit=true,cracked=true": { "lit=true,cracked=true,inverted=false": {
"model": "new_soviet:block/light_bulb_broken" "model": "new_soviet:block/light_bulb_broken"
}, },
"lit=true,cracked=false": { "lit=true,cracked=false,inverted=false": {
"model": "new_soviet:block/light_bulb_on" "model": "new_soviet:block/light_bulb_on"
}, },
"lit=false,cracked=false": { "lit=false,cracked=false,inverted=false": {
"model": "new_soviet:block/light_bulb_off" "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" "model": "new_soviet:block/light_bulb_broken"
} }
} }