| 1 | package net.minecraft.world.level.block;
|
| 2 |
|
| 3 | import java.util.Map;
|
| 4 | import java.util.function.BiConsumer;
|
| 5 | import net.minecraft.core.BlockPos;
|
| 6 | import net.minecraft.core.Direction;
|
| 7 | import net.minecraft.server.level.ServerLevel;
|
| 8 | import net.minecraft.sounds.SoundSource;
|
| 9 | import net.minecraft.util.RandomSource;
|
| 10 | import net.minecraft.world.InteractionResult;
|
| 11 | import net.minecraft.world.entity.player.Player;
|
| 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.Explosion;
|
| 16 | import net.minecraft.world.level.Level;
|
| 17 | import net.minecraft.world.level.LevelReader;
|
| 18 | import net.minecraft.world.level.ScheduledTickAccess;
|
| 19 | import net.minecraft.world.level.block.state.BlockBehaviour;
|
| 20 | import net.minecraft.world.level.block.state.BlockState;
|
| 21 | import net.minecraft.world.level.block.state.StateDefinition;
|
| 22 | import net.minecraft.world.level.block.state.properties.BlockSetType;
|
| 23 | import net.minecraft.world.level.block.state.properties.BlockStateProperties;
|
| 24 | import net.minecraft.world.level.block.state.properties.BooleanProperty;
|
| 25 | import net.minecraft.world.level.block.state.properties.EnumProperty;
|
| 26 | import net.minecraft.world.level.block.state.properties.Half;
|
| 27 | import net.minecraft.world.level.gameevent.GameEvent;
|
| 28 | import net.minecraft.world.level.material.FluidState;
|
| 29 | import net.minecraft.world.level.material.Fluids;
|
| 30 | import net.minecraft.world.level.pathfinder.PathComputationType;
|
| 31 | import net.minecraft.world.level.redstone.Orientation;
|
| 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 | /**
|
| 39 | * Access widened by fabric-transitive-access-wideners-v1 to accessible
|
| 40 | */
|
| 41 | public class TrapDoorBlock extends HorizontalDirectionalBlock implements SimpleWaterloggedBlock {
|
| 42 | public static final BooleanProperty OPEN = BlockStateProperties.OPEN;
|
| 43 | public static final EnumProperty<Half> HALF = BlockStateProperties.HALF;
|
| 44 | public static final BooleanProperty POWERED = BlockStateProperties.POWERED;
|
| 45 | public static final BooleanProperty WATERLOGGED = BlockStateProperties.WATERLOGGED;
|
| 46 | private static final Map<Direction, VoxelShape> SHAPES = Shapes.rotateAll(Block.boxZ(16.0, 13.0, 16.0));
|
| 47 | private final BlockSetType type;
|
| 48 |
|
| 49 | /**
|
| 50 | * Access widened by fabric-transitive-access-wideners-v1 to accessible
|
| 51 | */
|
| 52 | public TrapDoorBlock(final BlockSetType type, final BlockBehaviour.Properties properties) {
|
| 53 | super(properties.sound(type.soundType()));
|
| 54 | this.type = type;
|
| 55 | this.registerDefaultState(
|
| 56 | this.stateDefinition
|
| 57 | .any()
|
| 58 | .setValue(FACING, Direction.NORTH)
|
| 59 | .setValue(OPEN, false)
|
| 60 | .setValue(HALF, Half.BOTTOM)
|
| 61 | .setValue(POWERED, false)
|
| 62 | .setValue(WATERLOGGED, false)
|
| 63 | );
|
| 64 | }
|
| 65 |
|
| 66 | @Override
|
| 67 | protected VoxelShape getShape(final BlockState state, final BlockGetter level, final BlockPos pos, final CollisionContext context) {
|
| 68 | return SHAPES.get(state.getValue(OPEN) ? state.getValue(FACING) : (state.getValue(HALF) == Half.TOP ? Direction.DOWN : Direction.UP));
|
| 69 | }
|
| 70 |
|
| 71 | @Override
|
| 72 | protected boolean isPathfindable(final BlockState state, final PathComputationType type) {
|
| 73 | return switch (type) {
|
| 74 | case LAND -> state.getValue(OPEN);
|
| 75 | case WATER -> state.getValue(WATERLOGGED);
|
| 76 | case AIR -> state.getValue(OPEN);
|
| 77 | default -> false;
|
| 78 | };
|
| 79 | }
|
| 80 |
|
| 81 | @Override
|
| 82 | protected InteractionResult useWithoutItem(final BlockState state, final Level level, final BlockPos pos, final Player player, final BlockHitResult hitResult) {
|
| 83 | if (!this.type.canOpenByHand()) {
|
| 84 | return InteractionResult.PASS;
|
| 85 | }
|
| 86 |
|
| 87 | this.toggle(state, level, pos, player);
|
| 88 | return InteractionResult.SUCCESS;
|
| 89 | }
|
| 90 |
|
| 91 | @Override
|
| 92 | protected void onExplosionHit(
|
| 93 | final BlockState state, final ServerLevel level, final BlockPos pos, final Explosion explosion, final BiConsumer<ItemStack, BlockPos> onHit
|
| 94 | ) {
|
| 95 | if (explosion.canTriggerBlocks() && this.type.canOpenByWindCharge() && !state.getValue(POWERED)) {
|
| 96 | this.toggle(state, level, pos, null);
|
| 97 | }
|
| 98 |
|
| 99 | super.onExplosionHit(state, level, pos, explosion, onHit);
|
| 100 | }
|
| 101 |
|
| 102 | private void toggle(final BlockState state, final Level level, final BlockPos pos, final @Nullable Player player) {
|
| 103 | BlockState updated = state.cycle(OPEN);
|
| 104 | level.setBlock(pos, updated, 2);
|
| 105 | if (updated.getValue(WATERLOGGED)) {
|
| 106 | level.scheduleTick(pos, Fluids.WATER, Fluids.WATER.getTickDelay(level));
|
| 107 | }
|
| 108 |
|
| 109 | this.playSound(player, level, pos, updated.getValue(OPEN));
|
| 110 | }
|
| 111 |
|
| 112 | protected void playSound(final @Nullable Player player, final Level level, final BlockPos pos, final boolean opening) {
|
| 113 | level.playSound(
|
| 114 | player, pos, opening ? this.type.trapdoorOpen() : this.type.trapdoorClose(), SoundSource.BLOCKS, 1.0F, level.getRandom().nextFloat() * 0.1F + 0.9F
|
| 115 | );
|
| 116 | level.gameEvent(player, opening ? GameEvent.BLOCK_OPEN : GameEvent.BLOCK_CLOSE, pos);
|
| 117 | }
|
| 118 |
|
| 119 | @Override
|
| 120 | protected void neighborChanged(
|
| 121 | BlockState state, final Level level, final BlockPos pos, final Block block, final @Nullable Orientation orientation, final boolean movedByPiston
|
| 122 | ) {
|
| 123 | if (!level.isClientSide()) {
|
| 124 | boolean signal = level.hasNeighborSignal(pos);
|
| 125 | if (signal != state.getValue(POWERED)) {
|
| 126 | if (state.getValue(OPEN) != signal) {
|
| 127 | state = state.setValue(OPEN, signal);
|
| 128 | this.playSound(null, level, pos, signal);
|
| 129 | }
|
| 130 |
|
| 131 | level.setBlock(pos, state.setValue(POWERED, signal), 2);
|
| 132 | if (state.getValue(WATERLOGGED)) {
|
| 133 | level.scheduleTick(pos, Fluids.WATER, Fluids.WATER.getTickDelay(level));
|
| 134 | }
|
| 135 | }
|
| 136 | }
|
| 137 | }
|
| 138 |
|
| 139 | @Override
|
| 140 | public BlockState getStateForPlacement(final BlockPlaceContext context) {
|
| 141 | BlockState state = this.defaultBlockState();
|
| 142 | FluidState replacedFluidState = context.getLevel().getFluidState(context.getClickedPos());
|
| 143 | Direction clickedFace = context.getClickedFace();
|
| 144 | if (!context.replacingClickedOnBlock() && clickedFace.getAxis().isHorizontal()) {
|
| 145 | state = state.setValue(FACING, clickedFace).setValue(HALF, context.getClickLocation().y - context.getClickedPos().getY() > 0.5 ? Half.TOP : Half.BOTTOM);
|
| 146 | } else {
|
| 147 | state = state.setValue(FACING, context.getHorizontalDirection().getOpposite()).setValue(HALF, clickedFace == Direction.UP ? Half.BOTTOM : Half.TOP);
|
| 148 | }
|
| 149 |
|
| 150 | if (context.getLevel().hasNeighborSignal(context.getClickedPos())) {
|
| 151 | state = state.setValue(OPEN, true).setValue(POWERED, true);
|
| 152 | }
|
| 153 |
|
| 154 | return state.setValue(WATERLOGGED, replacedFluidState.is(Fluids.WATER));
|
| 155 | }
|
| 156 |
|
| 157 | @Override
|
| 158 | protected void createBlockStateDefinition(final StateDefinition.Builder<Block, BlockState> builder) {
|
| 159 | builder.add(FACING, OPEN, HALF, POWERED, WATERLOGGED);
|
| 160 | }
|
| 161 |
|
| 162 | @Override
|
| 163 | protected FluidState getFluidState(final BlockState state) {
|
| 164 | return state.getValue(WATERLOGGED) ? Fluids.WATER.getSource(false) : super.getFluidState(state);
|
| 165 | }
|
| 166 |
|
| 167 | @Override
|
| 168 | protected BlockState updateShape(
|
| 169 | final BlockState state,
|
| 170 | final LevelReader level,
|
| 171 | final ScheduledTickAccess ticks,
|
| 172 | final BlockPos pos,
|
| 173 | final Direction directionToNeighbour,
|
| 174 | final BlockPos neighbourPos,
|
| 175 | final BlockState neighbourState,
|
| 176 | final RandomSource random
|
| 177 | ) {
|
| 178 | if (state.getValue(WATERLOGGED)) {
|
| 179 | ticks.scheduleTick(pos, Fluids.WATER, Fluids.WATER.getTickDelay(level));
|
| 180 | }
|
| 181 |
|
| 182 | return super.updateShape(state, level, ticks, pos, directionToNeighbour, neighbourPos, neighbourState, random);
|
| 183 | }
|
| 184 |
|
| 185 | protected BlockSetType getType() {
|
| 186 | return this.type;
|
| 187 | }
|
| 188 | }
|
| 189 |
|