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