Begin fixing cig and handrail code

This commit is contained in:
Andrew-71 2023-09-05 21:57:28 +03:00
parent d2631b7714
commit 0bf6453e69
2 changed files with 37 additions and 68 deletions

View file

@ -20,24 +20,24 @@ import net.minecraft.util.shape.VoxelShape;
import net.minecraft.util.shape.VoxelShapes;
import net.minecraft.world.BlockView;
import net.minecraft.world.WorldAccess;
import net.minecraft.world.gen.feature.util.CaveSurface;
import su.a71.new_soviet.entity.TVBlockEntity;
import su.a71.new_soviet.registration.NSE_Blocks;
import su.a71.new_soviet.util.Shapes;
import su.a71.new_soviet.entity.TVBlockEntity;
import su.a71.new_soviet.util.Shapes;
import java.util.List;
import java.util.Objects;
import static net.minecraft.block.StairsBlock.HALF;
import static net.minecraft.block.StairsBlock.SHAPE;
public class HandrailBlock extends HorizontalFacingBlock implements BlockEntityProvider {
// FIXME: 05.09.2023 This class can probably be improved further.
// By the way, Blockstate not updating when removing stairs is a "creative decision" apparently
// - Andrew71
public class HandrailBlock extends HorizontalFacingBlock implements BlockEntityProvider, Waterloggable {
protected static final Shapes.HorizontalShape2 SHAPE1;
protected static final Shapes.HorizontalShape2 SHAPE2;
protected static final Shapes.HorizontalShape2 SHAPE3;
public static final BooleanProperty WATERLOGGED;
public static final IntProperty ROTATE;
public static final IntProperty ROTATION;
public HandrailBlock(Settings settings) {
super(settings.sounds(BlockSoundGroup.METAL).pistonBehavior(PistonBehavior.BLOCK).strength(1f, 2f));
@ -46,17 +46,17 @@ public class HandrailBlock extends HorizontalFacingBlock implements BlockEntityP
@Override
protected void appendProperties(StateManager.Builder<Block, BlockState> builder) {
builder.add(Properties.HORIZONTAL_FACING, WATERLOGGED, ROTATE);
builder.add(Properties.HORIZONTAL_FACING, WATERLOGGED, ROTATION);
}
public FluidState getFluidState(BlockState state) {
return (Boolean)state.get(WATERLOGGED) ? Fluids.WATER.getStill(false) : super.getFluidState(state);
return state.get(WATERLOGGED) ? Fluids.WATER.getStill(false) : super.getFluidState(state);
}
@Override
public VoxelShape getOutlineShape(BlockState state, BlockView world, BlockPos pos, ShapeContext ctx) {
Direction dir = state.get(FACING);
int rot = state.get(ROTATE);
int rot = state.get(ROTATION);
return switch (dir) {
case NORTH -> switch (rot) {
case 1 -> SHAPE1.north();
@ -88,54 +88,31 @@ public class HandrailBlock extends HorizontalFacingBlock implements BlockEntityP
@Override
public BlockState getPlacementState(ItemPlacementContext ctx) {
FluidState fluidState = ctx.getWorld().getFluidState(ctx.getBlockPos());
boolean bl = fluidState.getFluid() == Fluids.WATER;
int rot = 2;
BlockPos under = new BlockPos(ctx.getBlockPos().getX(), ctx.getBlockPos().getY() - 1, ctx.getBlockPos().getZ());
if (istruestairs1(ctx.getWorld().getBlockState(ctx.getBlockPos().down(1)), ctx)) {
rot = 1;
} else if (istruestairs2(ctx.getWorld().getBlockState(ctx.getBlockPos().down(1)), ctx)) {
rot = 3;
}
return (BlockState)this.getDefaultState()
int rot = getRotation(ctx.getWorld().getBlockState(ctx.getBlockPos().down()), ctx);
return this.getDefaultState()
.with(WATERLOGGED, ctx.getWorld().getFluidState(ctx.getBlockPos()).getFluid() == Fluids.WATER)
.with(Properties.HORIZONTAL_FACING, ctx.getHorizontalPlayerFacing().getOpposite())
.with(ROTATE, rot);
.with(ROTATION, rot);
}
private boolean istruestairs1(BlockState blockState, ItemPlacementContext ctx) {
boolean output = false;
if (blockState.isIn(BlockTags.STAIRS)) {
boolean h = blockState.get(SHAPE) == StairShape.STRAIGHT;
if (h && blockState.get(HALF) == BlockHalf.BOTTOM && returnFacing(blockState.get(FACING).getName()) == ctx.getHorizontalPlayerFacing().getOpposite().getName()) {
output = true;
private int getRotation(BlockState state, ItemPlacementContext ctx) {
if (state.isIn(BlockTags.STAIRS) && ((state.get(SHAPE) == StairShape.STRAIGHT) && state.get(HALF) == BlockHalf.BOTTOM)) {
if (returnFacing(state.get(FACING)) == ctx.getHorizontalPlayerFacing().getOpposite()) {
return 1;
} else if (state.get(FACING) == returnFacing(ctx.getHorizontalPlayerFacing().getOpposite())) {
return 3;
}
}
return output;
return 2;
}
private boolean istruestairs2(BlockState blockState, ItemPlacementContext ctx) {
boolean output = false;
if (blockState.isIn(BlockTags.STAIRS)) {
boolean h = blockState.get(SHAPE) == StairShape.STRAIGHT;
if (h && blockState.get(HALF) == BlockHalf.BOTTOM && blockState.get(FACING).getName() == returnFacing(ctx.getHorizontalPlayerFacing().getOpposite().getName())) {
output = true;
}
}
return output;
}
private String returnFacing(String facing1) {
String facing2 = "north";
if (facing1 == "north") {
facing2 = "west";
} else if (facing1 == "west") {
facing2 = "south";
} else if (facing1 == "south") {
facing2 = "east";
}
return facing2;
private Direction returnFacing(Direction facing) {
return switch (facing) {
case NORTH -> Direction.WEST;
case WEST -> Direction.SOUTH;
case SOUTH -> Direction.EAST;
default -> Direction.NORTH;
};
}
@Override
@ -144,22 +121,17 @@ public class HandrailBlock extends HorizontalFacingBlock implements BlockEntityP
}
public BlockState getStateForNeighborUpdate(BlockState state, Direction direction, BlockState neighborState, WorldAccess world, BlockPos pos, BlockPos neighborPos) {
if (!state.canPlaceAt(world, pos)) {
return Blocks.AIR.getDefaultState();
} else {
if ((Boolean)state.get(WATERLOGGED)) {
world.scheduleFluidTick(pos, Fluids.WATER, Fluids.WATER.getTickRate(world));
}
return super.getStateForNeighborUpdate(state, direction, neighborState, world, pos, 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);
}
static {
SHAPE2 = new Shapes.HorizontalShape2(List.of(List.of(0.0, 0.0, 15.0, 16.0, 17.0, 16.0), List.of(0.0, 17.0, 14.5, 16.0, 19.0, 16.5)));
SHAPE1 = new Shapes.HorizontalShape2(List.of(List.of(0.0, 0.0, 15.0, 8.0, 18.0, 16.0), List.of(8.0, -8.0, 15.0, 16.0, 10.0, 16.0)));
SHAPE3 = new Shapes.HorizontalShape2(List.of(List.of(0.0, -8.0, 15.0, 8.0, 10.0, 16.0), List.of(8.0, 0.0, 15.0, 16.0, 18.0, 16.0)));
ROTATE = IntProperty.of("rotate", 1, 3);;
ROTATION = IntProperty.of("rotate", 1, 3);;
WATERLOGGED = Properties.WATERLOGGED;
}
}

View file

@ -1,25 +1,20 @@
package su.a71.new_soviet.items;
import net.minecraft.client.item.TooltipContext;
import net.minecraft.entity.Entity;
import net.minecraft.entity.EquipmentSlot;
import net.minecraft.entity.player.PlayerEntity;
import net.minecraft.item.*;
import net.minecraft.particle.DustParticleEffect;
import net.minecraft.particle.ParticleEffect;
import net.minecraft.particle.ParticleTypes;
import net.minecraft.sound.SoundCategory;
import net.minecraft.stat.Stats;
import net.minecraft.text.Text;
import net.minecraft.util.*;
import net.minecraft.util.math.BlockPos;
import net.minecraft.util.math.random.Random;
import net.minecraft.world.World;
import net.minecraft.entity.LivingEntity;
import net.minecraft.item.Item;
import net.minecraft.item.ItemStack;
import net.minecraft.sound.SoundEvents;
import net.minecraft.util.Hand;
import net.minecraft.util.TypedActionResult;
import org.jetbrains.annotations.Nullable;
@ -82,8 +77,9 @@ public class CigaretteItem extends Item {
@Override
public void usageTick(World world, LivingEntity user, ItemStack stack, int remainingUseTicks) {
user.setMovementSpeed(1.1f);
if (stack.getDamage() < durationInTicks - 1)
stack.damage(1, user, e -> e.sendEquipmentBreakStatus(EquipmentSlot.MAINHAND));
if (stack.getDamage() < durationInTicks - 1) {
stack.damage(1, user, e -> e.sendEquipmentBreakStatus(!(user.getMainHandStack() == stack) ? EquipmentSlot.OFFHAND : EquipmentSlot.MAINHAND));
}
double d = user.getX();
double e = user.getY() + user.getHeight();
@ -103,10 +99,11 @@ public class CigaretteItem extends Item {
@Override
public void onStoppedUsing(ItemStack stack, World world, LivingEntity user, int remainingUseTicks) {
if (stack.getDamage() < (durationInTicks - 1)) {
stack.damage(1, user, e -> e.sendEquipmentBreakStatus(EquipmentSlot.MAINHAND));
stack.damage(1, user, e -> e.sendEquipmentBreakStatus(!(user.getMainHandStack() == stack) ? EquipmentSlot.OFFHAND : EquipmentSlot.MAINHAND));
world.playSound(user, BlockPos.ofFloored(user.getPos()), NSE_Sounds.CIGARETTE_PAUSE, SoundCategory.PLAYERS, 1.0f, 1.0f);
} else if (stack.getDamage() >= (durationInTicks - 2)) {
user.equipStack(EquipmentSlot.MAINHAND, new ItemStack(returnedItem));
user.equipStack((!(user.getMainHandStack() == stack) ? EquipmentSlot.OFFHAND : EquipmentSlot.MAINHAND), new ItemStack(returnedItem));
world.playSound(user, BlockPos.ofFloored(user.getPos()), NSE_Sounds.CIGARETTE_STOPPED, SoundCategory.PLAYERS, 1.0f, 1.0f);
}
double d = user.getX();