TripWireBlock .java

net.minecraft.world.level.block.TripWireBlock (class): members: SHAPE_NOT_ATTACHED, SHAPE_ATTACHED, hook, shouldConnectTo, RECHECK_PERIOD, DISARMED, NORTH.

net.minecraft.world.level.blockclass227 linescommonextends Block8.6 KB

API Surface (13)

VoxelShape getShape(final BlockState state, final BlockGetter level, final BlockPos pos, final CollisionContext context)
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 onPlace(final BlockState state, final Level level, final BlockPos pos, final BlockState oldState, final boolean movedByPiston)
void affectNeighborsAfterRemoval(final BlockState state, final ServerLevel level, final BlockPos pos, final boolean movedByPiston)
BlockState playerWillDestroy(final Level level, final BlockPos pos, final BlockState state, final Player player)
VoxelShape getEntityInsideCollisionShape(final BlockState state, final BlockGetter level, final BlockPos pos, final Entity entity)
void entityInside(final BlockState state, final Level level, final BlockPos pos, final Entity entity, final InsideBlockEffectApplier effectApplier, final boolean isPrecise)
void tick(final BlockState state, final ServerLevel level, final BlockPos pos, final RandomSource random)
boolean shouldConnectTo(final BlockState blockState, final Direction direction)
BlockState rotate(final BlockState state, final Rotation rotation)
BlockState mirror(final BlockState state, final Mirror mirror)
void createBlockStateDefinition(final StateDefinition.Builder<Block, BlockState> builder)

Fields & Constants (20)

POWERED
ATTACHED
DISARMED
NORTH
EAST
SOUTH
WEST
PROPERTY_BY_DIRECTION
SHAPE_ATTACHED
SHAPE_NOT_ATTACHED
RECHECK_PERIOD
hook
state
level
ticks
pos
directionToNeighbour
neighbourPos
neighbourState
effectApplier

Full Source

TripWireBlock.java227 lines · Minecraft 26.3 · verbatim
1
package net.minecraft.world.level.block;
2
3
import java.util.List;
4
import java.util.Map;
5
import net.minecraft.core.BlockPos;
6
import net.minecraft.core.Direction;
7
import net.minecraft.server.level.ServerLevel;
8
import net.minecraft.util.RandomSource;
9
import net.minecraft.world.entity.Entity;
10
import net.minecraft.world.entity.InsideBlockEffectApplier;
11
import net.minecraft.world.entity.player.Player;
12
import net.minecraft.world.item.Items;
13
import net.minecraft.world.item.context.BlockPlaceContext;
14
import net.minecraft.world.level.BlockGetter;
15
import net.minecraft.world.level.Level;
16
import net.minecraft.world.level.LevelReader;
17
import net.minecraft.world.level.ScheduledTickAccess;
18
import net.minecraft.world.level.block.state.BlockBehaviour;
19
import net.minecraft.world.level.block.state.BlockState;
20
import net.minecraft.world.level.block.state.StateDefinition;
21
import net.minecraft.world.level.block.state.properties.BlockStateProperties;
22
import net.minecraft.world.level.block.state.properties.BooleanProperty;
23
import net.minecraft.world.level.gameevent.GameEvent;
24
import net.minecraft.world.phys.shapes.CollisionContext;
25
import net.minecraft.world.phys.shapes.VoxelShape;
26
27
public class TripWireBlock extends Block {
28
	public static final BooleanProperty POWERED = BlockStateProperties.POWERED;
29
	public static final BooleanProperty ATTACHED = BlockStateProperties.ATTACHED;
30
	public static final BooleanProperty DISARMED = BlockStateProperties.DISARMED;
31
	public static final BooleanProperty NORTH = PipeBlock.NORTH;
32
	public static final BooleanProperty EAST = PipeBlock.EAST;
33
	public static final BooleanProperty SOUTH = PipeBlock.SOUTH;
34
	public static final BooleanProperty WEST = PipeBlock.WEST;
35
	private static final Map<Direction, BooleanProperty> PROPERTY_BY_DIRECTION = CrossCollisionBlock.PROPERTY_BY_DIRECTION;
36
	private static final VoxelShape SHAPE_ATTACHED = Block.column(16.0, 1.0, 2.5);
37
	private static final VoxelShape SHAPE_NOT_ATTACHED = Block.column(16.0, 0.0, 8.0);
38
	private static final int RECHECK_PERIOD = 10;
39
	private final Block hook;
40
41
	public TripWireBlock(final Block hook, final BlockBehaviour.Properties properties) {
42
		super(properties);
43
		this.registerDefaultState(
44
			this.stateDefinition
45
				.any()
46
				.setValue(POWERED, false)
47
				.setValue(ATTACHED, false)
48
				.setValue(DISARMED, false)
49
				.setValue(NORTH, false)
50
				.setValue(EAST, false)
51
				.setValue(SOUTH, false)
52
				.setValue(WEST, false)
53
		);
54
		this.hook = hook;
55
	}
56
57
	@Override
58
	protected VoxelShape getShape(final BlockState state, final BlockGetter level, final BlockPos pos, final CollisionContext context) {
59
		return state.getValue(ATTACHED) ? SHAPE_ATTACHED : SHAPE_NOT_ATTACHED;
60
	}
61
62
	@Override
63
	public BlockState getStateForPlacement(final BlockPlaceContext context) {
64
		BlockGetter level = context.getLevel();
65
		BlockPos pos = context.getClickedPos();
66
		return this.defaultBlockState()
67
			.setValue(NORTH, this.shouldConnectTo(level.getBlockState(pos.north()), Direction.NORTH))
68
			.setValue(EAST, this.shouldConnectTo(level.getBlockState(pos.east()), Direction.EAST))
69
			.setValue(SOUTH, this.shouldConnectTo(level.getBlockState(pos.south()), Direction.SOUTH))
70
			.setValue(WEST, this.shouldConnectTo(level.getBlockState(pos.west()), Direction.WEST));
71
	}
72
73
	@Override
74
	protected BlockState updateShape(
75
		final BlockState state,
76
		final LevelReader level,
77
		final ScheduledTickAccess ticks,
78
		final BlockPos pos,
79
		final Direction directionToNeighbour,
80
		final BlockPos neighbourPos,
81
		final BlockState neighbourState,
82
		final RandomSource random
83
	) {
84
		return directionToNeighbour.getAxis().isHorizontal()
85
			? state.setValue(PROPERTY_BY_DIRECTION.get(directionToNeighbour), this.shouldConnectTo(neighbourState, directionToNeighbour))
86
			: super.updateShape(state, level, ticks, pos, directionToNeighbour, neighbourPos, neighbourState, random);
87
	}
88
89
	@Override
90
	protected void onPlace(final BlockState state, final Level level, final BlockPos pos, final BlockState oldState, final boolean movedByPiston) {
91
		if (!oldState.is(state.getBlock())) {
92
			this.updateSource(level, pos, state);
93
		}
94
	}
95
96
	@Override
97
	protected void affectNeighborsAfterRemoval(final BlockState state, final ServerLevel level, final BlockPos pos, final boolean movedByPiston) {
98
		if (!movedByPiston) {
99
			this.updateSource(level, pos, state.setValue(POWERED, true));
100
		}
101
	}
102
103
	@Override
104
	public BlockState playerWillDestroy(final Level level, final BlockPos pos, final BlockState state, final Player player) {
105
		if (!level.isClientSide() && !player.getMainHandItem().isEmpty() && player.getMainHandItem().is(Items.SHEARS)) {
106
			level.setBlock(pos, state.setValue(DISARMED, true), 260);
107
			level.gameEvent(player, GameEvent.SHEAR, pos);
108
		}
109
110
		return super.playerWillDestroy(level, pos, state, player);
111
	}
112
113
	private void updateSource(final Level level, final BlockPos pos, final BlockState state) {
114
		for (Direction direction : new Direction[]{Direction.SOUTH, Direction.WEST}) {
115
			for (int i = 1; i < 42; i++) {
116
				BlockPos testPos = pos.relative(direction, i);
117
				BlockState block = level.getBlockState(testPos);
118
				if (block.is(this.hook)) {
119
					if (block.getValue(TripWireHookBlock.FACING) == direction.getOpposite()) {
120
						TripWireHookBlock.calculateState(level, testPos, block, false, true, i, state);
121
					}
122
					break;
123
				}
124
125
				if (!block.is(this)) {
126
					break;
127
				}
128
			}
129
		}
130
	}
131
132
	@Override
133
	protected VoxelShape getEntityInsideCollisionShape(final BlockState state, final BlockGetter level, final BlockPos pos, final Entity entity) {
134
		return state.getShape(level, pos);
135
	}
136
137
	@Override
138
	protected void entityInside(
139
		final BlockState state, final Level level, final BlockPos pos, final Entity entity, final InsideBlockEffectApplier effectApplier, final boolean isPrecise
140
	) {
141
		if (!level.isClientSide()) {
142
			if (!state.getValue(POWERED) && !level.getBlockTicks().hasScheduledTick(pos, this)) {
143
				this.checkPressed(level, pos, List.of(entity));
144
			}
145
		}
146
	}
147
148
	@Override
149
	protected void tick(final BlockState state, final ServerLevel level, final BlockPos pos, final RandomSource random) {
150
		if (level.getBlockState(pos).getValue(POWERED)) {
151
			this.checkPressed(level, pos);
152
		}
153
	}
154
155
	private void checkPressed(final Level level, final BlockPos pos) {
156
		BlockState state = level.getBlockState(pos);
157
		List<? extends Entity> entities = level.getEntities(null, state.getShape(level, pos).bounds().move(pos));
158
		this.checkPressed(level, pos, entities);
159
	}
160
161
	private void checkPressed(final Level level, final BlockPos pos, final List<? extends Entity> entities) {
162
		BlockState state = level.getBlockState(pos);
163
		boolean wasPressed = state.getValue(POWERED);
164
		boolean shouldBePressed = false;
165
		if (!entities.isEmpty()) {
166
			for (Entity entity : entities) {
167
				if (!entity.isIgnoringBlockTriggers()) {
168
					shouldBePressed = true;
169
					break;
170
				}
171
			}
172
		}
173
174
		if (shouldBePressed != wasPressed) {
175
			state = state.setValue(POWERED, shouldBePressed);
176
			level.setBlockAndUpdate(pos, state);
177
			this.updateSource(level, pos, state);
178
		}
179
180
		if (shouldBePressed) {
181
			level.scheduleTick(pos, this, 10);
182
		} else if (wasPressed) {
183
			level.scheduleTick(pos, this, 1);
184
		}
185
	}
186
187
	public boolean shouldConnectTo(final BlockState blockState, final Direction direction) {
188
		return blockState.is(this.hook) ? blockState.getValue(TripWireHookBlock.FACING) == direction.getOpposite() : blockState.is(this);
189
	}
190
191
	@Override
192
	protected BlockState rotate(final BlockState state, final Rotation rotation) {
193
		return switch (rotation) {
194
			case CLOCKWISE_180 -> (BlockState)state.setValue(NORTH, state.getValue(SOUTH))
195
				.setValue(EAST, state.getValue(WEST))
196
				.setValue(SOUTH, state.getValue(NORTH))
197
				.setValue(WEST, state.getValue(EAST));
198
			case COUNTERCLOCKWISE_90 -> (BlockState)state.setValue(NORTH, state.getValue(EAST))
199
				.setValue(EAST, state.getValue(SOUTH))
200
				.setValue(SOUTH, state.getValue(WEST))
201
				.setValue(WEST, state.getValue(NORTH));
202
			case CLOCKWISE_90 -> (BlockState)state.setValue(NORTH, state.getValue(WEST))
203
				.setValue(EAST, state.getValue(NORTH))
204
				.setValue(SOUTH, state.getValue(EAST))
205
				.setValue(WEST, state.getValue(SOUTH));
206
			default -> state;
207
		};
208
	}
209
210
	@Override
211
	protected BlockState mirror(final BlockState state, final Mirror mirror) {
212
		switch (mirror) {
213
			case LEFT_RIGHT:
214
				return state.setValue(NORTH, state.getValue(SOUTH)).setValue(SOUTH, state.getValue(NORTH));
215
			case FRONT_BACK:
216
				return state.setValue(EAST, state.getValue(WEST)).setValue(WEST, state.getValue(EAST));
217
			default:
218
				return super.mirror(state, mirror);
219
		}
220
	}
221
222
	@Override
223
	protected void createBlockStateDefinition(final StateDefinition.Builder<Block, BlockState> builder) {
224
		builder.add(POWERED, ATTACHED, DISARMED, NORTH, EAST, WEST, SOUTH);
225
	}
226
}
227

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