| 1 | package net.minecraft.world.level.block;
|
| 2 |
|
| 3 | import java.util.Map;
|
| 4 | import net.minecraft.core.BlockPos;
|
| 5 | import net.minecraft.core.Direction;
|
| 6 | import net.minecraft.tags.BlockTags;
|
| 7 | import net.minecraft.util.RandomSource;
|
| 8 | import net.minecraft.world.InteractionHand;
|
| 9 | import net.minecraft.world.InteractionResult;
|
| 10 | import net.minecraft.world.entity.player.Player;
|
| 11 | import net.minecraft.world.item.HangingSignItem;
|
| 12 | import net.minecraft.world.item.ItemStack;
|
| 13 | import net.minecraft.world.item.context.BlockPlaceContext;
|
| 14 | import net.minecraft.world.level.BlockGetter;
|
| 15 | import net.minecraft.world.level.Level;
|
| 16 | import net.minecraft.world.level.LevelReader;
|
| 17 | import net.minecraft.world.level.ScheduledTickAccess;
|
| 18 | import net.minecraft.world.level.block.entity.BlockEntity;
|
| 19 | import net.minecraft.world.level.block.entity.BlockEntityTicker;
|
| 20 | import net.minecraft.world.level.block.entity.BlockEntityType;
|
| 21 | import net.minecraft.world.level.block.entity.BlockEntityTypes;
|
| 22 | import net.minecraft.world.level.block.entity.HangingSignBlockEntity;
|
| 23 | import net.minecraft.world.level.block.entity.SignBlockEntity;
|
| 24 | import net.minecraft.world.level.block.state.BlockBehaviour;
|
| 25 | import net.minecraft.world.level.block.state.BlockState;
|
| 26 | import net.minecraft.world.level.block.state.StateDefinition;
|
| 27 | import net.minecraft.world.level.block.state.properties.EnumProperty;
|
| 28 | import net.minecraft.world.level.block.state.properties.WoodType;
|
| 29 | import net.minecraft.world.level.material.FluidState;
|
| 30 | import net.minecraft.world.level.material.Fluids;
|
| 31 | import net.minecraft.world.level.pathfinder.PathComputationType;
|
| 32 | import net.minecraft.world.phys.BlockHitResult;
|
| 33 | import net.minecraft.world.phys.shapes.CollisionContext;
|
| 34 | import net.minecraft.world.phys.shapes.Shapes;
|
| 35 | import net.minecraft.world.phys.shapes.VoxelShape;
|
| 36 | import org.jspecify.annotations.Nullable;
|
| 37 |
|
| 38 | public class WallHangingSignBlock extends SignBlock implements HangingSignBlock {
|
| 39 | public static final EnumProperty<Direction> FACING = HorizontalDirectionalBlock.FACING;
|
| 40 | private static final Map<Direction.Axis, VoxelShape> SHAPES_PLANK = Shapes.rotateHorizontalAxis(Block.column(16.0, 4.0, 14.0, 16.0));
|
| 41 | private static final Map<Direction.Axis, VoxelShape> SHAPES = Shapes.rotateHorizontalAxis(
|
| 42 | Shapes.or(SHAPES_PLANK.get(Direction.Axis.Z), Block.column(14.0, 2.0, 0.0, 10.0))
|
| 43 | );
|
| 44 |
|
| 45 | public WallHangingSignBlock(final WoodType type, final BlockBehaviour.Properties properties) {
|
| 46 | super(type, properties.sound(type.hangingSignSoundType()));
|
| 47 | this.registerDefaultState(this.stateDefinition.any().setValue(FACING, Direction.NORTH).setValue(WATERLOGGED, false));
|
| 48 | }
|
| 49 |
|
| 50 | @Override
|
| 51 | protected InteractionResult useItemOn(
|
| 52 | final ItemStack itemStack,
|
| 53 | final BlockState state,
|
| 54 | final Level level,
|
| 55 | final BlockPos pos,
|
| 56 | final Player player,
|
| 57 | final InteractionHand hand,
|
| 58 | final BlockHitResult hitResult
|
| 59 | ) {
|
| 60 | return level.getBlockEntity(pos) instanceof SignBlockEntity signEntity
|
| 61 | && this.shouldTryToChainAnotherHangingSign(state, player, hitResult, signEntity, itemStack)
|
| 62 | ? InteractionResult.PASS
|
| 63 | : super.useItemOn(itemStack, state, level, pos, player, hand, hitResult);
|
| 64 | }
|
| 65 |
|
| 66 | private boolean shouldTryToChainAnotherHangingSign(
|
| 67 | final BlockState state, final Player player, final BlockHitResult hitResult, final SignBlockEntity signEntity, final ItemStack itemStack
|
| 68 | ) {
|
| 69 | return !signEntity.canExecuteClickCommands(signEntity.getSlotPlayerIsFacing(player), player)
|
| 70 | && itemStack.getItem() instanceof HangingSignItem
|
| 71 | && !this.isHittingEditableSide(hitResult, state);
|
| 72 | }
|
| 73 |
|
| 74 | private boolean isHittingEditableSide(final BlockHitResult hitResult, final BlockState state) {
|
| 75 | return hitResult.getDirection().getAxis() == state.getValue(FACING).getAxis();
|
| 76 | }
|
| 77 |
|
| 78 | @Override
|
| 79 | protected VoxelShape getShape(final BlockState state, final BlockGetter level, final BlockPos pos, final CollisionContext context) {
|
| 80 | return SHAPES.get(state.getValue(FACING).getAxis());
|
| 81 | }
|
| 82 |
|
| 83 | @Override
|
| 84 | protected VoxelShape getBlockSupportShape(final BlockState state, final BlockGetter level, final BlockPos pos) {
|
| 85 | return this.getShape(state, level, pos, CollisionContext.empty());
|
| 86 | }
|
| 87 |
|
| 88 | @Override
|
| 89 | protected VoxelShape getCollisionShape(final BlockState state, final BlockGetter level, final BlockPos pos, final CollisionContext context) {
|
| 90 | return SHAPES_PLANK.get(state.getValue(FACING).getAxis());
|
| 91 | }
|
| 92 |
|
| 93 | public boolean canPlace(final BlockState state, final LevelReader level, final BlockPos pos) {
|
| 94 | Direction clockwise = state.getValue(FACING).getClockWise();
|
| 95 | Direction counterClockwise = state.getValue(FACING).getCounterClockWise();
|
| 96 | return this.canAttachTo(level, state, pos.relative(clockwise), counterClockwise) || this.canAttachTo(level, state, pos.relative(counterClockwise), clockwise);
|
| 97 | }
|
| 98 |
|
| 99 | public boolean canAttachTo(final LevelReader level, final BlockState state, final BlockPos attachPos, final Direction attachFace) {
|
| 100 | BlockState attachState = level.getBlockState(attachPos);
|
| 101 | return attachState.is(BlockTags.WALL_HANGING_SIGNS)
|
| 102 | ? attachState.getValue(FACING).getAxis().test(state.getValue(FACING))
|
| 103 | : attachState.isFaceSturdy(level, attachPos, attachFace, SupportType.FULL);
|
| 104 | }
|
| 105 |
|
| 106 | @Override
|
| 107 | public @Nullable BlockState getStateForPlacement(final BlockPlaceContext context) {
|
| 108 | BlockState state = this.defaultBlockState();
|
| 109 | FluidState replacedFluidState = context.getLevel().getFluidState(context.getClickedPos());
|
| 110 | LevelReader level = context.getLevel();
|
| 111 | BlockPos pos = context.getClickedPos();
|
| 112 |
|
| 113 | for (Direction direction : context.getNearestLookingDirections()) {
|
| 114 | if (direction.getAxis().isHorizontal() && !direction.getAxis().test(context.getClickedFace())) {
|
| 115 | Direction facing = direction.getOpposite();
|
| 116 | state = state.setValue(FACING, facing);
|
| 117 | if (state.canSurvive(level, pos) && this.canPlace(state, level, pos)) {
|
| 118 | return state.setValue(WATERLOGGED, replacedFluidState.is(Fluids.WATER));
|
| 119 | }
|
| 120 | }
|
| 121 | }
|
| 122 |
|
| 123 | return null;
|
| 124 | }
|
| 125 |
|
| 126 | @Override
|
| 127 | protected BlockState updateShape(
|
| 128 | final BlockState state,
|
| 129 | final LevelReader level,
|
| 130 | final ScheduledTickAccess ticks,
|
| 131 | final BlockPos pos,
|
| 132 | final Direction directionToNeighbour,
|
| 133 | final BlockPos neighbourPos,
|
| 134 | final BlockState neighbourState,
|
| 135 | final RandomSource random
|
| 136 | ) {
|
| 137 | return directionToNeighbour.getAxis() == state.getValue(FACING).getClockWise().getAxis() && !state.canSurvive(level, pos)
|
| 138 | ? Blocks.AIR.defaultBlockState()
|
| 139 | : super.updateShape(state, level, ticks, pos, directionToNeighbour, neighbourPos, neighbourState, random);
|
| 140 | }
|
| 141 |
|
| 142 | @Override
|
| 143 | public float getYRotationDegrees(final BlockState state) {
|
| 144 | return state.getValue(FACING).toYRot();
|
| 145 | }
|
| 146 |
|
| 147 | @Override
|
| 148 | protected BlockState rotate(final BlockState state, final Rotation rotation) {
|
| 149 | return state.setValue(FACING, rotation.rotate(state.getValue(FACING)));
|
| 150 | }
|
| 151 |
|
| 152 | @Override
|
| 153 | protected BlockState mirror(final BlockState state, final Mirror mirror) {
|
| 154 | return state.rotate(mirror.getRotation(state.getValue(FACING)));
|
| 155 | }
|
| 156 |
|
| 157 | @Override
|
| 158 | protected void createBlockStateDefinition(final StateDefinition.Builder<Block, BlockState> builder) {
|
| 159 | builder.add(FACING, WATERLOGGED);
|
| 160 | }
|
| 161 |
|
| 162 | @Override
|
| 163 | public BlockEntity newBlockEntity(final BlockPos worldPosition, final BlockState blockState) {
|
| 164 | return new HangingSignBlockEntity(worldPosition, blockState);
|
| 165 | }
|
| 166 |
|
| 167 | @Override
|
| 168 | protected boolean isPathfindable(final BlockState state, final PathComputationType type) {
|
| 169 | return false;
|
| 170 | }
|
| 171 |
|
| 172 | @Override
|
| 173 | public <T extends BlockEntity> @Nullable BlockEntityTicker<T> getTicker(final Level level, final BlockState blockState, final BlockEntityType<T> type) {
|
| 174 | return createTickerHelper(type, BlockEntityTypes.HANGING_SIGN, SignBlockEntity::tick);
|
| 175 | }
|
| 176 |
|
| 177 | @Override
|
| 178 | public HangingSignBlock.Attachment attachmentPoint(final BlockState state) {
|
| 179 | return HangingSignBlock.Attachment.WALL;
|
| 180 | }
|
| 181 | }
|
| 182 |
|