FenceGateBlock .java

net.minecraft.world.level.block.FenceGateBlock (class): members: SHAPE_OCCLUSION_WALL, connectsToDirection, SHAPE_OCCLUSION, SHAPE_SUPPORT, SHAPES_WALL.

net.minecraft.world.level.blockclass198 linescommonextends HorizontalDirectionalBlock8.6 KB

API Surface (12)

VoxelShape getShape(final BlockState state, final BlockGetter level, final BlockPos pos, final CollisionContext context)
BlockState updateShape(final BlockState state, final LevelReader level, final ScheduledTickAccess ticks, final BlockPos pos, final Direction directionToNeighbour, final BlockPos neighbourPos, final BlockState neighbourState, final RandomSource random)
VoxelShape getBlockSupportShape(final BlockState state, final BlockGetter level, final BlockPos pos)
VoxelShape getCollisionShape(final BlockState state, final BlockGetter level, final BlockPos pos, final CollisionContext context)
VoxelShape getOcclusionShape(final BlockState state)
boolean isPathfindable(final BlockState state, final PathComputationType type)
BlockState getStateForPlacement(final BlockPlaceContext context)
InteractionResult useWithoutItem(BlockState state, final Level level, final BlockPos pos, final Player player, final BlockHitResult hitResult)
void onExplosionHit(final BlockState state, final ServerLevel level, final BlockPos pos, final Explosion explosion, final BiConsumer<ItemStack, BlockPos> onHit)
void neighborChanged(final BlockState state, final Level level, final BlockPos pos, final Block block, final @Nullable Orientation orientation, final boolean movedByPiston)
void createBlockStateDefinition(final StateDefinition.Builder<Block, BlockState> builder)
boolean connectsToDirection(final BlockState state, final Direction direction)

Fields & Constants (19)

OPEN
POWERED
IN_WALL
SHAPES
SHAPES_WALL
SHAPE_COLLISION
SHAPE_SUPPORT
SHAPE_OCCLUSION
SHAPE_OCCLUSION_WALL
type
state
level
ticks
pos
directionToNeighbour
neighbourPos
neighbourState
explosion
orientation

Full Source

FenceGateBlock.java198 lines · Minecraft 26.3 · verbatim
1
package net.minecraft.world.level.block;
2
3
import com.google.common.collect.Maps;
4
import java.util.Map;
5
import java.util.function.BiConsumer;
6
import net.minecraft.core.BlockPos;
7
import net.minecraft.core.Direction;
8
import net.minecraft.server.level.ServerLevel;
9
import net.minecraft.sounds.SoundSource;
10
import net.minecraft.tags.BlockTags;
11
import net.minecraft.util.RandomSource;
12
import net.minecraft.util.Util;
13
import net.minecraft.world.InteractionResult;
14
import net.minecraft.world.entity.player.Player;
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.Explosion;
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.state.BlockBehaviour;
23
import net.minecraft.world.level.block.state.BlockState;
24
import net.minecraft.world.level.block.state.StateDefinition;
25
import net.minecraft.world.level.block.state.properties.BlockStateProperties;
26
import net.minecraft.world.level.block.state.properties.BooleanProperty;
27
import net.minecraft.world.level.block.state.properties.WoodType;
28
import net.minecraft.world.level.gameevent.GameEvent;
29
import net.minecraft.world.level.pathfinder.PathComputationType;
30
import net.minecraft.world.level.redstone.Orientation;
31
import net.minecraft.world.phys.BlockHitResult;
32
import net.minecraft.world.phys.shapes.BooleanOp;
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 FenceGateBlock extends HorizontalDirectionalBlock {
39
	public static final BooleanProperty OPEN = BlockStateProperties.OPEN;
40
	public static final BooleanProperty POWERED = BlockStateProperties.POWERED;
41
	public static final BooleanProperty IN_WALL = BlockStateProperties.IN_WALL;
42
	private static final Map<Direction.Axis, VoxelShape> SHAPES = Shapes.rotateHorizontalAxis(Block.cube(16.0, 16.0, 4.0));
43
	private static final Map<Direction.Axis, VoxelShape> SHAPES_WALL = Maps.newEnumMap(
44
		Util.mapValues(SHAPES, v -> Shapes.join(v, Block.column(16.0, 13.0, 16.0), BooleanOp.ONLY_FIRST))
45
	);
46
	private static final Map<Direction.Axis, VoxelShape> SHAPE_COLLISION = Shapes.rotateHorizontalAxis(Block.column(16.0, 4.0, 0.0, 24.0));
47
	private static final Map<Direction.Axis, VoxelShape> SHAPE_SUPPORT = Shapes.rotateHorizontalAxis(Block.column(16.0, 4.0, 5.0, 24.0));
48
	private static final Map<Direction.Axis, VoxelShape> SHAPE_OCCLUSION = Shapes.rotateHorizontalAxis(
49
		Shapes.or(Block.box(0.0, 5.0, 7.0, 2.0, 16.0, 9.0), Block.box(14.0, 5.0, 7.0, 16.0, 16.0, 9.0))
50
	);
51
	private static final Map<Direction.Axis, VoxelShape> SHAPE_OCCLUSION_WALL = Maps.newEnumMap(
52
		Util.mapValues(SHAPE_OCCLUSION, v -> v.move(0.0, -0.1875, 0.0).optimize())
53
	);
54
	private final WoodType type;
55
56
	public FenceGateBlock(final WoodType type, final BlockBehaviour.Properties properties) {
57
		super(properties.sound(type.soundType()));
58
		this.type = type;
59
		this.registerDefaultState(this.stateDefinition.any().setValue(OPEN, false).setValue(POWERED, false).setValue(IN_WALL, false));
60
	}
61
62
	@Override
63
	protected VoxelShape getShape(final BlockState state, final BlockGetter level, final BlockPos pos, final CollisionContext context) {
64
		Direction.Axis axis = state.getValue(FACING).getAxis();
65
		return (state.getValue(IN_WALL) ? SHAPES_WALL : SHAPES).get(axis);
66
	}
67
68
	@Override
69
	protected BlockState updateShape(
70
		final BlockState state,
71
		final LevelReader level,
72
		final ScheduledTickAccess ticks,
73
		final BlockPos pos,
74
		final Direction directionToNeighbour,
75
		final BlockPos neighbourPos,
76
		final BlockState neighbourState,
77
		final RandomSource random
78
	) {
79
		Direction.Axis axis = directionToNeighbour.getAxis();
80
		if (state.getValue(FACING).getClockWise().getAxis() != axis) {
81
			return super.updateShape(state, level, ticks, pos, directionToNeighbour, neighbourPos, neighbourState, random);
82
		}
83
84
		boolean inWall = this.isWall(neighbourState) || this.isWall(level.getBlockState(pos.relative(directionToNeighbour.getOpposite())));
85
		return state.setValue(IN_WALL, inWall);
86
	}
87
88
	@Override
89
	protected VoxelShape getBlockSupportShape(final BlockState state, final BlockGetter level, final BlockPos pos) {
90
		Direction.Axis axis = state.getValue(FACING).getAxis();
91
		return state.getValue(OPEN) ? Shapes.empty() : SHAPE_SUPPORT.get(axis);
92
	}
93
94
	@Override
95
	protected VoxelShape getCollisionShape(final BlockState state, final BlockGetter level, final BlockPos pos, final CollisionContext context) {
96
		Direction.Axis axis = state.getValue(FACING).getAxis();
97
		return state.getValue(OPEN) ? Shapes.empty() : SHAPE_COLLISION.get(axis);
98
	}
99
100
	@Override
101
	protected VoxelShape getOcclusionShape(final BlockState state) {
102
		Direction.Axis axis = state.getValue(FACING).getAxis();
103
		return (state.getValue(IN_WALL) ? SHAPE_OCCLUSION_WALL : SHAPE_OCCLUSION).get(axis);
104
	}
105
106
	@Override
107
	protected boolean isPathfindable(final BlockState state, final PathComputationType type) {
108
		return switch (type) {
109
			case LAND -> state.getValue(OPEN);
110
			case WATER -> false;
111
			case AIR -> state.getValue(OPEN);
112
			default -> false;
113
		};
114
	}
115
116
	@Override
117
	public BlockState getStateForPlacement(final BlockPlaceContext context) {
118
		Level level = context.getLevel();
119
		BlockPos pos = context.getClickedPos();
120
		boolean isOpen = level.hasNeighborSignal(pos);
121
		Direction direction = context.getHorizontalDirection();
122
		Direction.Axis axis = direction.getAxis();
123
		boolean inWall = axis == Direction.Axis.Z && (this.isWall(level.getBlockState(pos.west())) || this.isWall(level.getBlockState(pos.east())))
124
			|| axis == Direction.Axis.X && (this.isWall(level.getBlockState(pos.north())) || this.isWall(level.getBlockState(pos.south())));
125
		return this.defaultBlockState().setValue(FACING, direction).setValue(OPEN, isOpen).setValue(POWERED, isOpen).setValue(IN_WALL, inWall);
126
	}
127
128
	private boolean isWall(final BlockState state) {
129
		return state.is(BlockTags.WALLS);
130
	}
131
132
	@Override
133
	protected InteractionResult useWithoutItem(BlockState state, final Level level, final BlockPos pos, final Player player, final BlockHitResult hitResult) {
134
		if (state.getValue(OPEN)) {
135
			state = state.setValue(OPEN, false);
136
			level.setBlock(pos, state, 10);
137
		} else {
138
			Direction direction = player.getDirection();
139
			if (state.getValue(FACING) == direction.getOpposite()) {
140
				state = state.setValue(FACING, direction);
141
			}
142
143
			state = state.setValue(OPEN, true);
144
			level.setBlock(pos, state, 10);
145
		}
146
147
		boolean opens = state.getValue(OPEN);
148
		level.playSound(
149
			player, pos, opens ? this.type.fenceGateOpen() : this.type.fenceGateClose(), SoundSource.BLOCKS, 1.0F, level.getRandom().nextFloat() * 0.1F + 0.9F
150
		);
151
		level.gameEvent(player, opens ? GameEvent.BLOCK_OPEN : GameEvent.BLOCK_CLOSE, pos);
152
		return InteractionResult.SUCCESS;
153
	}
154
155
	@Override
156
	protected void onExplosionHit(
157
		final BlockState state, final ServerLevel level, final BlockPos pos, final Explosion explosion, final BiConsumer<ItemStack, BlockPos> onHit
158
	) {
159
		if (explosion.canTriggerBlocks() && !state.getValue(POWERED)) {
160
			boolean open = state.getValue(OPEN);
161
			level.setBlockAndUpdate(pos, state.setValue(OPEN, !open));
162
			level.playSound(
163
				null, pos, open ? this.type.fenceGateClose() : this.type.fenceGateOpen(), SoundSource.BLOCKS, 1.0F, level.getRandom().nextFloat() * 0.1F + 0.9F
164
			);
165
			level.gameEvent(open ? GameEvent.BLOCK_CLOSE : GameEvent.BLOCK_OPEN, pos, GameEvent.Context.of(state));
166
		}
167
168
		super.onExplosionHit(state, level, pos, explosion, onHit);
169
	}
170
171
	@Override
172
	protected void neighborChanged(
173
		final BlockState state, final Level level, final BlockPos pos, final Block block, final @Nullable Orientation orientation, final boolean movedByPiston
174
	) {
175
		if (!level.isClientSide()) {
176
			boolean hasPower = level.hasNeighborSignal(pos);
177
			if (state.getValue(POWERED) != hasPower) {
178
				level.setBlock(pos, state.setValue(POWERED, hasPower).setValue(OPEN, hasPower), 2);
179
				if (state.getValue(OPEN) != hasPower) {
180
					level.playSound(
181
						null, pos, hasPower ? this.type.fenceGateOpen() : this.type.fenceGateClose(), SoundSource.BLOCKS, 1.0F, level.getRandom().nextFloat() * 0.1F + 0.9F
182
					);
183
					level.gameEvent(null, hasPower ? GameEvent.BLOCK_OPEN : GameEvent.BLOCK_CLOSE, pos);
184
				}
185
			}
186
		}
187
	}
188
189
	@Override
190
	protected void createBlockStateDefinition(final StateDefinition.Builder<Block, BlockState> builder) {
191
		builder.add(FACING, OPEN, POWERED, IN_WALL);
192
	}
193
194
	public static boolean connectsToDirection(final BlockState state, final Direction direction) {
195
		return state.getValue(FACING).getAxis() == direction.getClockWise().getAxis();
196
	}
197
}
198

Decompiled Minecraft 26.3 source of FenceGateBlock (net/minecraft/world/level/block/FenceGateBlock.java) with Mojang official mappings, stored verbatim on the site.