Improve (I hope) lamps
This commit is contained in:
parent
a166e94656
commit
25348f5e72
7 changed files with 126 additions and 55 deletions
|
@ -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<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) {
|
||||
|
@ -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;
|
||||
}
|
||||
}
|
|
@ -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<Block, BlockState> 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;
|
||||
}
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue