Minor datagen fix and lamp package

This commit is contained in:
Andrew-71 2023-09-04 09:49:47 +03:00
parent 224584875d
commit be4d1b5af8
8 changed files with 20 additions and 9 deletions

View file

@ -0,0 +1,119 @@
package su.a71.new_soviet.blocks.lamps;
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.sound.SoundCategory;
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.world.BlockView;
import net.minecraft.world.World;
import net.minecraft.world.WorldAccess;
import org.jetbrains.annotations.Nullable;
import su.a71.new_soviet.registration.NSE_Sounds;
public abstract class LampBlock extends Block implements Waterloggable {
public static final BooleanProperty ON;
public static final BooleanProperty INVERTED;
public static final BooleanProperty WATERLOGGED;
public boolean defaultLightState;
public LampBlock(AbstractBlock.Settings settings, boolean defaultLightState, java.util.function.ToIntFunction<net.minecraft.block.BlockState> luminance) {
super(settings.luminance(luminance == null ? (BlockState state) -> {
if (state.get(INVERTED)) {
return state.get(ON) ? 0 : 14;
} else {
return state.get(ON) ? 14 : 0;
}
} : luminance));
this.defaultLightState = defaultLightState;
this.setDefaultState(this.stateManager.getDefaultState()
.with(INVERTED, defaultLightState)
.with(WATERLOGGED, false)
.with(ON, false));
}
@Nullable
public BlockState getPlacementState(ItemPlacementContext ctx) {
FluidState fluidState = ctx.getWorld().getFluidState(ctx.getBlockPos());
Direction[] directions = ctx.getPlacementDirections();
for (Direction direction : directions) {
if (direction.getAxis() == Direction.Axis.Y) {
BlockState blockState = this.getDefaultState()
.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);
}
}
}
return null;
}
@Override
public ActionResult onUse(BlockState state, World world, BlockPos pos, PlayerEntity player, Hand hand, BlockHitResult hit) {
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<Block, BlockState> builder) {
builder.add(new Property[]{ON, WATERLOGGED, INVERTED});
}
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 !state.canPlaceAt(world, pos) ? Blocks.AIR.getDefaultState() : super.getStateForNeighborUpdate(state, direction, neighborState, world, pos, neighborPos);
}
public FluidState getFluidState(BlockState state) {
return state.get(WATERLOGGED) ? Fluids.WATER.getStill(false) : super.getFluidState(state);
}
public boolean canPathfindThrough(BlockState state, BlockView world, BlockPos pos, NavigationType type) {
return false;
}
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);
}
}
static {
ON = RedstoneTorchBlock.LIT;
WATERLOGGED = Properties.WATERLOGGED;
INVERTED = Properties.INVERTED;
}
}

View file

@ -0,0 +1,109 @@
package su.a71.new_soviet.blocks.lamps;
import net.minecraft.block.*;
import net.minecraft.text.Text;
import net.minecraft.entity.player.PlayerEntity;
import net.minecraft.entity.projectile.ProjectileEntity;
import net.minecraft.item.ItemPlacementContext;
import net.minecraft.sound.SoundCategory;
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.shape.VoxelShape;
import net.minecraft.util.shape.VoxelShapes;
import net.minecraft.world.BlockView;
import net.minecraft.world.World;
import net.minecraft.world.WorldView;
import org.jetbrains.annotations.Nullable;
import su.a71.new_soviet.NewSoviet;
import su.a71.new_soviet.blocks.lamps.LampBlock;
import su.a71.new_soviet.registration.NSE_Items;
import su.a71.new_soviet.registration.NSE_Sounds;
public class LightBulbLampBlock extends LampBlock {
protected static final VoxelShape SHAPE;
public static final BooleanProperty BROKEN;
public LightBulbLampBlock(Block.Settings settings) {
super(settings, false, (BlockState state) -> {
if (state.get(BROKEN)) {
return 0;
}
if (state.get(INVERTED)) {
return state.get(ON) ? 0 : 14;
} else {
return state.get(ON) ? 14 : 0;
}
});
}
@Nullable
public BlockState getPlacementState(ItemPlacementContext ctx) {
return (BlockState)this.getDefaultState()
.with(BROKEN, false);
}
public ActionResult onUse(BlockState state, World world, BlockPos pos, PlayerEntity player, Hand hand, BlockHitResult hit) {
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_Sounds.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.PASS;
} else {
return super.onUse(state, world, pos, player, hand, hit);
}
}
@Override
public void onProjectileHit(World world, BlockState state, BlockHitResult hit, ProjectileEntity projectile) {
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), 2);
super.onProjectileHit(world, state, hit, projectile);
}
protected void appendProperties(StateManager.Builder<Block, BlockState> builder) {
builder.add(new Property[]{ON, BROKEN, WATERLOGGED, INVERTED});
}
public VoxelShape getOutlineShape(BlockState state, BlockView world, BlockPos pos, ShapeContext context) {
if (state.get(BROKEN)) {
return SHAPE;
}
return VoxelShapes.union(SHAPE, Block.createCuboidShape(7, 3, 7, 10, 6, 10).offset(-0.03125, 0, -0.03125));
}
public boolean canPlaceAt(BlockState state, WorldView world, BlockPos pos) {
Direction direction = Direction.UP;
return Block.sideCoversSmallSquare(world, pos.offset(direction), direction.getOpposite());
}
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));
BROKEN = Properties.CRACKED;
}
}

View file

@ -0,0 +1,37 @@
package su.a71.new_soviet.blocks.lamps;
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;
import su.a71.new_soviet.blocks.lamps.LampBlock;
public class TableLampBlock extends LampBlock {
protected final VoxelShape SHAPE = getStandingShape();;
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 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;
}
}

View file

@ -0,0 +1,24 @@
package su.a71.new_soviet.blocks.lamps;
import net.minecraft.util.shape.VoxelShape;
import net.minecraft.util.shape.VoxelShapes;
import su.a71.new_soviet.blocks.lamps.TableLampBlock;
public class VintageLampBlock extends TableLampBlock {
public VintageLampBlock(Settings settings) {
super(settings);
}
@Override
public VoxelShape getStandingShape(){
VoxelShape shape = VoxelShapes.empty();
shape = VoxelShapes.union(shape, VoxelShapes.cuboid(0.28125, 0, 0.28125, 0.71875, 0.125, 0.71875));
shape = VoxelShapes.union(shape, VoxelShapes.cuboid(0.40625, 0.125, 0.40625, 0.59375, 0.625, 0.59375));
shape = VoxelShapes.union(shape, VoxelShapes.cuboid(0.4375, 0.625, 0.4375, 0.5625, 0.875, 0.5625));
shape = VoxelShapes.union(shape, VoxelShapes.cuboid(0.1875, 0.875, 0.1875, 0.8125, 1.125, 0.8125));
shape = VoxelShapes.union(shape, VoxelShapes.cuboid(0.25, 1.125, 0.25, 0.75, 1.1875, 0.75));
shape.simplify();
return shape;
}
}