minecraft-src/net/minecraft/world/level/block/WallHangingSignBlock.java
2025-07-04 01:41:11 +03:00

185 lines
7.9 KiB
Java

package net.minecraft.world.level.block;
import com.google.common.collect.ImmutableMap;
import com.google.common.collect.Maps;
import com.mojang.serialization.MapCodec;
import com.mojang.serialization.codecs.RecordCodecBuilder;
import java.util.Map;
import net.minecraft.core.BlockPos;
import net.minecraft.core.Direction;
import net.minecraft.tags.BlockTags;
import net.minecraft.world.InteractionHand;
import net.minecraft.world.ItemInteractionResult;
import net.minecraft.world.entity.player.Player;
import net.minecraft.world.item.HangingSignItem;
import net.minecraft.world.item.ItemStack;
import net.minecraft.world.item.context.BlockPlaceContext;
import net.minecraft.world.level.BlockGetter;
import net.minecraft.world.level.Level;
import net.minecraft.world.level.LevelAccessor;
import net.minecraft.world.level.LevelReader;
import net.minecraft.world.level.block.entity.BlockEntity;
import net.minecraft.world.level.block.entity.BlockEntityTicker;
import net.minecraft.world.level.block.entity.BlockEntityType;
import net.minecraft.world.level.block.entity.HangingSignBlockEntity;
import net.minecraft.world.level.block.entity.SignBlockEntity;
import net.minecraft.world.level.block.state.BlockBehaviour;
import net.minecraft.world.level.block.state.BlockState;
import net.minecraft.world.level.block.state.StateDefinition;
import net.minecraft.world.level.block.state.properties.DirectionProperty;
import net.minecraft.world.level.block.state.properties.WoodType;
import net.minecraft.world.level.material.FluidState;
import net.minecraft.world.level.material.Fluids;
import net.minecraft.world.level.pathfinder.PathComputationType;
import net.minecraft.world.phys.BlockHitResult;
import net.minecraft.world.phys.shapes.CollisionContext;
import net.minecraft.world.phys.shapes.Shapes;
import net.minecraft.world.phys.shapes.VoxelShape;
import org.jetbrains.annotations.Nullable;
public class WallHangingSignBlock extends SignBlock {
public static final MapCodec<WallHangingSignBlock> CODEC = RecordCodecBuilder.mapCodec(
instance -> instance.group(WoodType.CODEC.fieldOf("wood_type").forGetter(SignBlock::type), propertiesCodec()).apply(instance, WallHangingSignBlock::new)
);
public static final DirectionProperty FACING = HorizontalDirectionalBlock.FACING;
public static final VoxelShape PLANK_NORTHSOUTH = Block.box(0.0, 14.0, 6.0, 16.0, 16.0, 10.0);
public static final VoxelShape PLANK_EASTWEST = Block.box(6.0, 14.0, 0.0, 10.0, 16.0, 16.0);
public static final VoxelShape SHAPE_NORTHSOUTH = Shapes.or(PLANK_NORTHSOUTH, Block.box(1.0, 0.0, 7.0, 15.0, 10.0, 9.0));
public static final VoxelShape SHAPE_EASTWEST = Shapes.or(PLANK_EASTWEST, Block.box(7.0, 0.0, 1.0, 9.0, 10.0, 15.0));
private static final Map<Direction, VoxelShape> AABBS = Maps.newEnumMap(
ImmutableMap.of(Direction.NORTH, SHAPE_NORTHSOUTH, Direction.SOUTH, SHAPE_NORTHSOUTH, Direction.EAST, SHAPE_EASTWEST, Direction.WEST, SHAPE_EASTWEST)
);
@Override
public MapCodec<WallHangingSignBlock> codec() {
return CODEC;
}
public WallHangingSignBlock(WoodType type, BlockBehaviour.Properties properties) {
super(type, properties.sound(type.hangingSignSoundType()));
this.registerDefaultState(this.stateDefinition.any().setValue(FACING, Direction.NORTH).setValue(WATERLOGGED, false));
}
@Override
protected ItemInteractionResult useItemOn(
ItemStack stack, BlockState state, Level level, BlockPos pos, Player player, InteractionHand hand, BlockHitResult hitResult
) {
return level.getBlockEntity(pos) instanceof SignBlockEntity signBlockEntity
&& this.shouldTryToChainAnotherHangingSign(state, player, hitResult, signBlockEntity, stack)
? ItemInteractionResult.SKIP_DEFAULT_BLOCK_INTERACTION
: super.useItemOn(stack, state, level, pos, player, hand, hitResult);
}
private boolean shouldTryToChainAnotherHangingSign(BlockState state, Player player, BlockHitResult hitResult, SignBlockEntity sign, ItemStack stack) {
return !sign.canExecuteClickCommands(sign.isFacingFrontText(player), player)
&& stack.getItem() instanceof HangingSignItem
&& !this.isHittingEditableSide(hitResult, state);
}
private boolean isHittingEditableSide(BlockHitResult hitResult, BlockState state) {
return hitResult.getDirection().getAxis() == ((Direction)state.getValue(FACING)).getAxis();
}
@Override
public String getDescriptionId() {
return this.asItem().getDescriptionId();
}
@Override
protected VoxelShape getShape(BlockState state, BlockGetter level, BlockPos pos, CollisionContext context) {
return (VoxelShape)AABBS.get(state.getValue(FACING));
}
@Override
protected VoxelShape getBlockSupportShape(BlockState state, BlockGetter level, BlockPos pos) {
return this.getShape(state, level, pos, CollisionContext.empty());
}
@Override
protected VoxelShape getCollisionShape(BlockState state, BlockGetter level, BlockPos pos, CollisionContext context) {
switch ((Direction)state.getValue(FACING)) {
case EAST:
case WEST:
return PLANK_EASTWEST;
default:
return PLANK_NORTHSOUTH;
}
}
public boolean canPlace(BlockState state, LevelReader level, BlockPos pos) {
Direction direction = ((Direction)state.getValue(FACING)).getClockWise();
Direction direction2 = ((Direction)state.getValue(FACING)).getCounterClockWise();
return this.canAttachTo(level, state, pos.relative(direction), direction2) || this.canAttachTo(level, state, pos.relative(direction2), direction);
}
public boolean canAttachTo(LevelReader level, BlockState state, BlockPos pos, Direction direction) {
BlockState blockState = level.getBlockState(pos);
return blockState.is(BlockTags.WALL_HANGING_SIGNS)
? ((Direction)blockState.getValue(FACING)).getAxis().test(state.getValue(FACING))
: blockState.isFaceSturdy(level, pos, direction, SupportType.FULL);
}
@Nullable
@Override
public BlockState getStateForPlacement(BlockPlaceContext context) {
BlockState blockState = this.defaultBlockState();
FluidState fluidState = context.getLevel().getFluidState(context.getClickedPos());
LevelReader levelReader = context.getLevel();
BlockPos blockPos = context.getClickedPos();
for (Direction direction : context.getNearestLookingDirections()) {
if (direction.getAxis().isHorizontal() && !direction.getAxis().test(context.getClickedFace())) {
Direction direction2 = direction.getOpposite();
blockState = blockState.setValue(FACING, direction2);
if (blockState.canSurvive(levelReader, blockPos) && this.canPlace(blockState, levelReader, blockPos)) {
return blockState.setValue(WATERLOGGED, fluidState.getType() == Fluids.WATER);
}
}
}
return null;
}
@Override
protected BlockState updateShape(BlockState state, Direction direction, BlockState neighborState, LevelAccessor level, BlockPos pos, BlockPos neighborPos) {
return direction.getAxis() == ((Direction)state.getValue(FACING)).getClockWise().getAxis() && !state.canSurvive(level, pos)
? Blocks.AIR.defaultBlockState()
: super.updateShape(state, direction, neighborState, level, pos, neighborPos);
}
@Override
public float getYRotationDegrees(BlockState state) {
return ((Direction)state.getValue(FACING)).toYRot();
}
@Override
protected BlockState rotate(BlockState state, Rotation rotation) {
return state.setValue(FACING, rotation.rotate(state.getValue(FACING)));
}
@Override
protected BlockState mirror(BlockState state, Mirror mirror) {
return state.rotate(mirror.getRotation(state.getValue(FACING)));
}
@Override
protected void createBlockStateDefinition(StateDefinition.Builder<Block, BlockState> builder) {
builder.add(FACING, WATERLOGGED);
}
@Override
public BlockEntity newBlockEntity(BlockPos pos, BlockState state) {
return new HangingSignBlockEntity(pos, state);
}
@Override
protected boolean isPathfindable(BlockState state, PathComputationType pathComputationType) {
return false;
}
@Nullable
@Override
public <T extends BlockEntity> BlockEntityTicker<T> getTicker(Level level, BlockState state, BlockEntityType<T> blockEntityType) {
return createTickerHelper(blockEntityType, BlockEntityType.HANGING_SIGN, SignBlockEntity::tick);
}
}