FenceBlock .java

net.minecraft.world.level.block.FenceBlock (class): members: occlusionShapes, connectsTo, getOcclusionShape, getVisualShape, useWithoutItem, updateShape.

net.minecraft.world.level.blockclass117 linescommonextends CrossCollisionBlock5.0 KB

API Surface (8)

VoxelShape getOcclusionShape(final BlockState state)
VoxelShape getVisualShape(final BlockState state, final BlockGetter level, final BlockPos pos, final CollisionContext context)
boolean isPathfindable(final BlockState state, final PathComputationType type)
boolean connectsTo(final BlockState state, final boolean faceSolid, final Direction direction)
InteractionResult useWithoutItem(final BlockState state, final Level level, final BlockPos pos, final Player player, final BlockHitResult hitResult)
BlockState getStateForPlacement(final BlockPlaceContext 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)
void createBlockStateDefinition(final StateDefinition.Builder<Block, BlockState> builder)

Fields & Constants (8)

occlusionShapes
state
level
ticks
pos
directionToNeighbour
neighbourPos
neighbourState

Full Source

FenceBlock.java117 lines · Minecraft 26.3 · verbatim
1
package net.minecraft.world.level.block;
2
3
import java.util.function.Function;
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.InteractionResult;
9
import net.minecraft.world.entity.player.Player;
10
import net.minecraft.world.item.LeadItem;
11
import net.minecraft.world.item.context.BlockPlaceContext;
12
import net.minecraft.world.level.BlockGetter;
13
import net.minecraft.world.level.Level;
14
import net.minecraft.world.level.LevelReader;
15
import net.minecraft.world.level.ScheduledTickAccess;
16
import net.minecraft.world.level.block.state.BlockBehaviour;
17
import net.minecraft.world.level.block.state.BlockState;
18
import net.minecraft.world.level.block.state.StateDefinition;
19
import net.minecraft.world.level.material.FluidState;
20
import net.minecraft.world.level.material.Fluids;
21
import net.minecraft.world.level.pathfinder.PathComputationType;
22
import net.minecraft.world.phys.BlockHitResult;
23
import net.minecraft.world.phys.shapes.CollisionContext;
24
import net.minecraft.world.phys.shapes.VoxelShape;
25
26
public class FenceBlock extends CrossCollisionBlock {
27
	private final Function<BlockState, VoxelShape> occlusionShapes;
28
29
	public FenceBlock(final BlockBehaviour.Properties properties) {
30
		super(4.0F, 16.0F, 4.0F, 16.0F, 24.0F, properties);
31
		this.registerDefaultState(
32
			this.stateDefinition.any().setValue(NORTH, false).setValue(EAST, false).setValue(SOUTH, false).setValue(WEST, false).setValue(WATERLOGGED, false)
33
		);
34
		this.occlusionShapes = this.makeShapes(4.0F, 16.0F, 2.0F, 6.0F, 15.0F);
35
	}
36
37
	@Override
38
	protected VoxelShape getOcclusionShape(final BlockState state) {
39
		return this.occlusionShapes.apply(state);
40
	}
41
42
	@Override
43
	protected VoxelShape getVisualShape(final BlockState state, final BlockGetter level, final BlockPos pos, final CollisionContext context) {
44
		return this.getShape(state, level, pos, context);
45
	}
46
47
	@Override
48
	protected boolean isPathfindable(final BlockState state, final PathComputationType type) {
49
		return false;
50
	}
51
52
	public boolean connectsTo(final BlockState state, final boolean faceSolid, final Direction direction) {
53
		Block block = state.getBlock();
54
		boolean sameFence = this.isSameFence(state);
55
		boolean gate = block instanceof FenceGateBlock && FenceGateBlock.connectsToDirection(state, direction);
56
		return !isExceptionForConnection(state) && faceSolid || sameFence || gate;
57
	}
58
59
	private boolean isSameFence(final BlockState state) {
60
		return state.is(BlockTags.FENCES) && state.is(BlockTags.WOODEN_FENCES) == this.defaultBlockState().is(BlockTags.WOODEN_FENCES);
61
	}
62
63
	@Override
64
	protected InteractionResult useWithoutItem(final BlockState state, final Level level, final BlockPos pos, final Player player, final BlockHitResult hitResult) {
65
		return !level.isClientSide() ? LeadItem.bindPlayerMobs(player, level, pos) : InteractionResult.PASS;
66
	}
67
68
	@Override
69
	public BlockState getStateForPlacement(final BlockPlaceContext context) {
70
		BlockGetter level = context.getLevel();
71
		BlockPos pos = context.getClickedPos();
72
		FluidState replacedFluidState = context.getLevel().getFluidState(context.getClickedPos());
73
		BlockPos north = pos.north();
74
		BlockPos east = pos.east();
75
		BlockPos south = pos.south();
76
		BlockPos west = pos.west();
77
		BlockState northState = level.getBlockState(north);
78
		BlockState eastState = level.getBlockState(east);
79
		BlockState southState = level.getBlockState(south);
80
		BlockState westState = level.getBlockState(west);
81
		return super.getStateForPlacement(context)
82
			.setValue(NORTH, this.connectsTo(northState, northState.isFaceSturdy(level, north, Direction.SOUTH), Direction.SOUTH))
83
			.setValue(EAST, this.connectsTo(eastState, eastState.isFaceSturdy(level, east, Direction.WEST), Direction.WEST))
84
			.setValue(SOUTH, this.connectsTo(southState, southState.isFaceSturdy(level, south, Direction.NORTH), Direction.NORTH))
85
			.setValue(WEST, this.connectsTo(westState, westState.isFaceSturdy(level, west, Direction.EAST), Direction.EAST))
86
			.setValue(WATERLOGGED, replacedFluidState.is(Fluids.WATER));
87
	}
88
89
	@Override
90
	protected BlockState updateShape(
91
		final BlockState state,
92
		final LevelReader level,
93
		final ScheduledTickAccess ticks,
94
		final BlockPos pos,
95
		final Direction directionToNeighbour,
96
		final BlockPos neighbourPos,
97
		final BlockState neighbourState,
98
		final RandomSource random
99
	) {
100
		if (state.getValue(WATERLOGGED)) {
101
			ticks.scheduleTick(pos, Fluids.WATER, Fluids.WATER.getTickDelay(level));
102
		}
103
104
		return directionToNeighbour.getAxis().isHorizontal()
105
			? state.setValue(
106
				PROPERTY_BY_DIRECTION.get(directionToNeighbour),
107
				this.connectsTo(neighbourState, neighbourState.isFaceSturdy(level, neighbourPos, directionToNeighbour.getOpposite()), directionToNeighbour.getOpposite())
108
			)
109
			: super.updateShape(state, level, ticks, pos, directionToNeighbour, neighbourPos, neighbourState, random);
110
	}
111
112
	@Override
113
	protected void createBlockStateDefinition(final StateDefinition.Builder<Block, BlockState> builder) {
114
		builder.add(NORTH, EAST, WEST, SOUTH, WATERLOGGED);
115
	}
116
}
117

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