| 1 | package net.minecraft.world.level.block;
|
| 2 |
|
| 3 | import java.util.UUID;
|
| 4 | import net.minecraft.core.BlockPos;
|
| 5 | import net.minecraft.core.Direction;
|
| 6 | import net.minecraft.server.level.ServerLevel;
|
| 7 | import net.minecraft.server.level.ServerPlayer;
|
| 8 | import net.minecraft.sounds.SoundSource;
|
| 9 | import net.minecraft.stats.Stats;
|
| 10 | import net.minecraft.util.RandomSource;
|
| 11 | import net.minecraft.util.Util;
|
| 12 | import net.minecraft.world.InteractionHand;
|
| 13 | import net.minecraft.world.InteractionResult;
|
| 14 | import net.minecraft.world.entity.LivingEntity;
|
| 15 | import net.minecraft.world.entity.player.Player;
|
| 16 | import net.minecraft.world.item.ItemStack;
|
| 17 | import net.minecraft.world.item.SignApplicator;
|
| 18 | import net.minecraft.world.level.BlockGetter;
|
| 19 | import net.minecraft.world.level.Level;
|
| 20 | import net.minecraft.world.level.LevelReader;
|
| 21 | import net.minecraft.world.level.ScheduledTickAccess;
|
| 22 | import net.minecraft.world.level.block.entity.BlockEntity;
|
| 23 | import net.minecraft.world.level.block.entity.BlockEntityTicker;
|
| 24 | import net.minecraft.world.level.block.entity.BlockEntityType;
|
| 25 | import net.minecraft.world.level.block.entity.BlockEntityTypes;
|
| 26 | import net.minecraft.world.level.block.entity.SignBlockEntity;
|
| 27 | import net.minecraft.world.level.block.entity.SignTextSlot;
|
| 28 | import net.minecraft.world.level.block.state.BlockBehaviour;
|
| 29 | import net.minecraft.world.level.block.state.BlockState;
|
| 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.WoodType;
|
| 33 | import net.minecraft.world.level.gameevent.GameEvent;
|
| 34 | import net.minecraft.world.level.material.FluidState;
|
| 35 | import net.minecraft.world.level.material.Fluids;
|
| 36 | import net.minecraft.world.phys.BlockHitResult;
|
| 37 | import net.minecraft.world.phys.Vec3;
|
| 38 | import net.minecraft.world.phys.shapes.CollisionContext;
|
| 39 | import net.minecraft.world.phys.shapes.VoxelShape;
|
| 40 | import org.jspecify.annotations.Nullable;
|
| 41 |
|
| 42 | public abstract class SignBlock extends BaseEntityBlock implements SimpleWaterloggedBlock {
|
| 43 | public static final BooleanProperty WATERLOGGED = BlockStateProperties.WATERLOGGED;
|
| 44 | private static final VoxelShape SHAPE = Block.column(8.0, 0.0, 16.0);
|
| 45 | private final WoodType type;
|
| 46 |
|
| 47 | protected SignBlock(final WoodType type, final BlockBehaviour.Properties properties) {
|
| 48 | super(properties);
|
| 49 | this.type = type;
|
| 50 | }
|
| 51 |
|
| 52 | @Override
|
| 53 | protected BlockState updateShape(
|
| 54 | final BlockState state,
|
| 55 | final LevelReader level,
|
| 56 | final ScheduledTickAccess ticks,
|
| 57 | final BlockPos pos,
|
| 58 | final Direction directionToNeighbour,
|
| 59 | final BlockPos neighbourPos,
|
| 60 | final BlockState neighbourState,
|
| 61 | final RandomSource random
|
| 62 | ) {
|
| 63 | if (state.getValue(WATERLOGGED)) {
|
| 64 | ticks.scheduleTick(pos, Fluids.WATER, Fluids.WATER.getTickDelay(level));
|
| 65 | }
|
| 66 |
|
| 67 | return super.updateShape(state, level, ticks, pos, directionToNeighbour, neighbourPos, neighbourState, random);
|
| 68 | }
|
| 69 |
|
| 70 | @Override
|
| 71 | protected VoxelShape getShape(final BlockState state, final BlockGetter level, final BlockPos pos, final CollisionContext context) {
|
| 72 | return SHAPE;
|
| 73 | }
|
| 74 |
|
| 75 | @Override
|
| 76 | public boolean isPossibleToRespawnInThis(final BlockState state) {
|
| 77 | return true;
|
| 78 | }
|
| 79 |
|
| 80 | @Override
|
| 81 | public BlockEntity newBlockEntity(final BlockPos worldPosition, final BlockState blockState) {
|
| 82 | return new SignBlockEntity(worldPosition, blockState);
|
| 83 | }
|
| 84 |
|
| 85 | @Override
|
| 86 | protected InteractionResult useItemOn(
|
| 87 | final ItemStack itemStack,
|
| 88 | final BlockState state,
|
| 89 | final Level level,
|
| 90 | final BlockPos pos,
|
| 91 | final Player player,
|
| 92 | final InteractionHand hand,
|
| 93 | final BlockHitResult hitResult
|
| 94 | ) {
|
| 95 | if (level.getBlockEntity(pos) instanceof SignBlockEntity sign) {
|
| 96 | SignApplicator signApplicator = itemStack.getItem() instanceof SignApplicator applicator ? applicator : null;
|
| 97 | boolean hasApplicatorToUse = signApplicator != null && player.mayBuild();
|
| 98 | if (level instanceof ServerLevel serverLevel) {
|
| 99 | if (hasApplicatorToUse && !sign.isWaxed() && !this.otherPlayerIsEditingSign(player, sign)) {
|
| 100 | SignTextSlot textSlot = sign.getSlotPlayerIsFacing(player);
|
| 101 | if (signApplicator.canApplyToSign(sign.getText(textSlot), itemStack, player)
|
| 102 | && signApplicator.tryApplyToSign(serverLevel, sign, textSlot, itemStack, player)) {
|
| 103 | sign.executeClickCommandsIfPresent(serverLevel, player, pos, textSlot);
|
| 104 | player.awardStat(Stats.ITEM_USED.get(itemStack.getItem()));
|
| 105 | serverLevel.gameEvent(GameEvent.BLOCK_CHANGE, sign.getBlockPos(), GameEvent.Context.of(player, sign.getBlockState()));
|
| 106 | itemStack.consume(1, player);
|
| 107 | return InteractionResult.SUCCESS;
|
| 108 | } else {
|
| 109 | return InteractionResult.TRY_WITH_EMPTY_HAND;
|
| 110 | }
|
| 111 | } else {
|
| 112 | return InteractionResult.TRY_WITH_EMPTY_HAND;
|
| 113 | }
|
| 114 | } else {
|
| 115 | return !hasApplicatorToUse && !sign.isWaxed() ? InteractionResult.CONSUME : InteractionResult.SUCCESS;
|
| 116 | }
|
| 117 | } else {
|
| 118 | return InteractionResult.PASS;
|
| 119 | }
|
| 120 | }
|
| 121 |
|
| 122 | @Override
|
| 123 | protected InteractionResult useWithoutItem(final BlockState state, final Level level, final BlockPos pos, final Player player, final BlockHitResult hitResult) {
|
| 124 | if (level.getBlockEntity(pos) instanceof SignBlockEntity sign) {
|
| 125 | if (level instanceof ServerLevel serverLevel) {
|
| 126 | SignTextSlot textSlot = sign.getSlotPlayerIsFacing(player);
|
| 127 | boolean executedClickCommand = sign.executeClickCommandsIfPresent(serverLevel, player, pos, textSlot);
|
| 128 | if (sign.isWaxed()) {
|
| 129 | serverLevel.playSound(null, sign.getBlockPos(), sign.getSignInteractionFailedSoundEvent(), SoundSource.BLOCKS);
|
| 130 | return InteractionResult.SUCCESS_SERVER;
|
| 131 | } else if (executedClickCommand) {
|
| 132 | return InteractionResult.SUCCESS_SERVER;
|
| 133 | } else if (!this.otherPlayerIsEditingSign(player, sign) && player.mayBuild() && sign.getText(textSlot).hasEditableText(player.isTextFilteringEnabled())) {
|
| 134 | this.openTextEdit(player, sign, textSlot);
|
| 135 | return InteractionResult.SUCCESS_SERVER;
|
| 136 | } else {
|
| 137 | return InteractionResult.PASS;
|
| 138 | }
|
| 139 | } else {
|
| 140 | Util.pauseInIde(new IllegalStateException("Expected to only call this on server"));
|
| 141 | return InteractionResult.CONSUME;
|
| 142 | }
|
| 143 | } else {
|
| 144 | return InteractionResult.PASS;
|
| 145 | }
|
| 146 | }
|
| 147 |
|
| 148 | public abstract float getYRotationDegrees(final BlockState state);
|
| 149 |
|
| 150 | public Vec3 getSignHitboxCenterPosition(final BlockState state) {
|
| 151 | return new Vec3(0.5, 0.5, 0.5);
|
| 152 | }
|
| 153 |
|
| 154 | @Override
|
| 155 | protected FluidState getFluidState(final BlockState state) {
|
| 156 | return state.getValue(WATERLOGGED) ? Fluids.WATER.getSource(false) : super.getFluidState(state);
|
| 157 | }
|
| 158 |
|
| 159 | public WoodType type() {
|
| 160 | return this.type;
|
| 161 | }
|
| 162 |
|
| 163 | public static WoodType getWoodType(final Block block) {
|
| 164 | return block instanceof SignBlock signBlock ? signBlock.type() : WoodType.OAK;
|
| 165 | }
|
| 166 |
|
| 167 | public void openTextEdit(final Player player, final SignBlockEntity sign, final SignTextSlot slot) {
|
| 168 | sign.setAllowedPlayerEditor(player.getUUID());
|
| 169 | player.openTextEdit(sign, slot);
|
| 170 | }
|
| 171 |
|
| 172 | private boolean otherPlayerIsEditingSign(final Player player, final SignBlockEntity sign) {
|
| 173 | UUID playerWhoMayEdit = sign.getPlayerWhoMayEdit();
|
| 174 | return playerWhoMayEdit != null && !playerWhoMayEdit.equals(player.getUUID());
|
| 175 | }
|
| 176 |
|
| 177 | @Override
|
| 178 | public <T extends BlockEntity> @Nullable BlockEntityTicker<T> getTicker(final Level level, final BlockState blockState, final BlockEntityType<T> type) {
|
| 179 | return createTickerHelper(type, BlockEntityTypes.SIGN, SignBlockEntity::tick);
|
| 180 | }
|
| 181 |
|
| 182 | @Override
|
| 183 | public void setPlacedBy(final Level level, final BlockPos pos, final BlockState state, final @Nullable LivingEntity by, final ItemStack itemStack) {
|
| 184 | if (!level.isClientSide()
|
| 185 | && by instanceof ServerPlayer player
|
| 186 | && state.getBlock() instanceof SignBlock sign
|
| 187 | && level.getBlockEntity(pos) instanceof SignBlockEntity signEntity
|
| 188 | && !signEntity.isWaxed()
|
| 189 | && signEntity.getText(SignTextSlot.FRONT).hasEditableText(player.isTextFilteringEnabled())) {
|
| 190 | sign.openTextEdit(player, signEntity, SignTextSlot.FRONT);
|
| 191 | }
|
| 192 | }
|
| 193 | }
|
| 194 |
|