ShelfBlock .java

net.minecraft.world.level.block.ShelfBlock (class): members: hitSlot, getMaxChainLength, getSideChainPart, setSideChainPart, SIDE_CHAIN_PART, getFacing.

net.minecraft.world.level.blockclass327 linescommonextends BaseEntityBlock11.9 KB

Implements

API Surface (22)

VoxelShape getShape(final BlockState state, final BlockGetter level, final BlockPos pos, final CollisionContext context)
boolean useShapeForLightOcclusion(final BlockState state)
boolean isPathfindable(final BlockState state, final PathComputationType type)
void createBlockStateDefinition(final StateDefinition.Builder<Block, BlockState> builder)
void affectNeighborsAfterRemoval(final BlockState state, final ServerLevel level, final BlockPos pos, final boolean movedByPiston)
void neighborChanged(final BlockState state, final Level level, final BlockPos pos, final Block block, final @Nullable Orientation orientation, final boolean movedByPiston)
BlockState getStateForPlacement(final BlockPlaceContext context)
BlockState rotate(final BlockState state, final Rotation rotation)
BlockState mirror(final BlockState state, final Mirror mirror)
int getRows()
int getColumns()
InteractionResult useItemOn(final ItemStack itemStack, final BlockState state, final Level level, final BlockPos pos, final Player player, final InteractionHand hand, final BlockHitResult hitResult)
SideChainPart getSideChainPart(final BlockState state)
BlockState setSideChainPart(final BlockState state, final SideChainPart newPart)
Direction getFacing(final BlockState state)
boolean isConnectable(final BlockState state)
int getMaxChainLength()
void onPlace(final BlockState state, final Level level, final BlockPos pos, final BlockState oldState, final boolean movedByPiston)
FluidState getFluidState(final BlockState state)
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)
boolean hasAnalogOutputSignal(final BlockState state)
int getAnalogOutputSignal(final BlockState state, final Level level, final BlockPos pos, final Direction direction)

Fields & Constants (17)

POWERED
FACING
SIDE_CHAIN_PART
WATERLOGGED
SHAPES
orientation
itemStack
state
level
pos
player
hand
hitSlot
ticks
directionToNeighbour
neighbourPos
neighbourState

Full Source

ShelfBlock.java327 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 java.util.OptionalInt;
6
import net.minecraft.core.BlockPos;
7
import net.minecraft.core.Direction;
8
import net.minecraft.core.component.DataComponents;
9
import net.minecraft.server.level.ServerLevel;
10
import net.minecraft.sounds.SoundEvent;
11
import net.minecraft.sounds.SoundEvents;
12
import net.minecraft.sounds.SoundSource;
13
import net.minecraft.tags.BlockTags;
14
import net.minecraft.tags.FluidTags;
15
import net.minecraft.util.RandomSource;
16
import net.minecraft.world.Containers;
17
import net.minecraft.world.InteractionHand;
18
import net.minecraft.world.InteractionResult;
19
import net.minecraft.world.entity.player.Inventory;
20
import net.minecraft.world.entity.player.Player;
21
import net.minecraft.world.item.ItemStack;
22
import net.minecraft.world.item.context.BlockPlaceContext;
23
import net.minecraft.world.level.BlockGetter;
24
import net.minecraft.world.level.Level;
25
import net.minecraft.world.level.LevelAccessor;
26
import net.minecraft.world.level.LevelReader;
27
import net.minecraft.world.level.ScheduledTickAccess;
28
import net.minecraft.world.level.block.entity.BlockEntity;
29
import net.minecraft.world.level.block.entity.ShelfBlockEntity;
30
import net.minecraft.world.level.block.state.BlockBehaviour;
31
import net.minecraft.world.level.block.state.BlockState;
32
import net.minecraft.world.level.block.state.StateDefinition;
33
import net.minecraft.world.level.block.state.properties.BlockStateProperties;
34
import net.minecraft.world.level.block.state.properties.BooleanProperty;
35
import net.minecraft.world.level.block.state.properties.EnumProperty;
36
import net.minecraft.world.level.block.state.properties.SideChainPart;
37
import net.minecraft.world.level.gameevent.GameEvent;
38
import net.minecraft.world.level.material.FluidState;
39
import net.minecraft.world.level.material.Fluids;
40
import net.minecraft.world.level.pathfinder.PathComputationType;
41
import net.minecraft.world.level.redstone.Orientation;
42
import net.minecraft.world.phys.BlockHitResult;
43
import net.minecraft.world.phys.shapes.CollisionContext;
44
import net.minecraft.world.phys.shapes.Shapes;
45
import net.minecraft.world.phys.shapes.VoxelShape;
46
import org.jspecify.annotations.Nullable;
47
48
public class ShelfBlock extends BaseEntityBlock implements SelectableSlotContainer, SideChainPartBlock, SimpleWaterloggedBlock {
49
	public static final BooleanProperty POWERED = BlockStateProperties.POWERED;
50
	public static final EnumProperty<Direction> FACING = BlockStateProperties.HORIZONTAL_FACING;
51
	public static final EnumProperty<SideChainPart> SIDE_CHAIN_PART = BlockStateProperties.SIDE_CHAIN_PART;
52
	public static final BooleanProperty WATERLOGGED = BlockStateProperties.WATERLOGGED;
53
	private static final Map<Direction, VoxelShape> SHAPES = Shapes.rotateHorizontal(
54
		Shapes.or(Block.box(0.0, 12.0, 11.0, 16.0, 16.0, 13.0), Block.box(0.0, 0.0, 13.0, 16.0, 16.0, 16.0), Block.box(0.0, 0.0, 11.0, 16.0, 4.0, 13.0))
55
	);
56
57
	public ShelfBlock(final BlockBehaviour.Properties properties) {
58
		super(properties);
59
		this.registerDefaultState(
60
			this.stateDefinition
61
				.any()
62
				.setValue(FACING, Direction.NORTH)
63
				.setValue(POWERED, false)
64
				.setValue(SIDE_CHAIN_PART, SideChainPart.UNCONNECTED)
65
				.setValue(WATERLOGGED, false)
66
		);
67
	}
68
69
	@Override
70
	protected VoxelShape getShape(final BlockState state, final BlockGetter level, final BlockPos pos, final CollisionContext context) {
71
		return SHAPES.get(state.getValue(FACING));
72
	}
73
74
	@Override
75
	protected boolean useShapeForLightOcclusion(final BlockState state) {
76
		return true;
77
	}
78
79
	@Override
80
	protected boolean isPathfindable(final BlockState state, final PathComputationType type) {
81
		return type == PathComputationType.WATER && state.getFluidState().is(FluidTags.WATER);
82
	}
83
84
	@Override
85
	public @Nullable BlockEntity newBlockEntity(final BlockPos worldPosition, final BlockState blockState) {
86
		return new ShelfBlockEntity(worldPosition, blockState);
87
	}
88
89
	@Override
90
	protected void createBlockStateDefinition(final StateDefinition.Builder<Block, BlockState> builder) {
91
		builder.add(FACING, POWERED, SIDE_CHAIN_PART, WATERLOGGED);
92
	}
93
94
	@Override
95
	protected void affectNeighborsAfterRemoval(final BlockState state, final ServerLevel level, final BlockPos pos, final boolean movedByPiston) {
96
		Containers.updateNeighboursAfterDestroy(state, level, pos);
97
		this.updateNeighborsAfterPoweringDown(level, pos, state);
98
	}
99
100
	@Override
101
	protected void neighborChanged(
102
		final BlockState state, final Level level, final BlockPos pos, final Block block, final @Nullable Orientation orientation, final boolean movedByPiston
103
	) {
104
		if (!level.isClientSide()) {
105
			boolean signal = level.hasNeighborSignal(pos);
106
			if (state.getValue(POWERED) != signal) {
107
				BlockState newState = state.setValue(POWERED, signal);
108
				if (!signal) {
109
					newState = newState.setValue(SIDE_CHAIN_PART, SideChainPart.UNCONNECTED);
110
				}
111
112
				level.setBlockAndUpdate(pos, newState);
113
				this.playSound(level, pos, signal ? SoundEvents.SHELF_ACTIVATE : SoundEvents.SHELF_DEACTIVATE);
114
				level.gameEvent(signal ? GameEvent.BLOCK_ACTIVATE : GameEvent.BLOCK_DEACTIVATE, pos, GameEvent.Context.of(newState));
115
			}
116
		}
117
	}
118
119
	@Override
120
	public BlockState getStateForPlacement(final BlockPlaceContext context) {
121
		FluidState replacedFluidState = context.getLevel().getFluidState(context.getClickedPos());
122
		return this.defaultBlockState()
123
			.setValue(FACING, context.getHorizontalDirection().getOpposite())
124
			.setValue(POWERED, context.getLevel().hasNeighborSignal(context.getClickedPos()))
125
			.setValue(WATERLOGGED, replacedFluidState.is(Fluids.WATER));
126
	}
127
128
	@Override
129
	public BlockState rotate(final BlockState state, final Rotation rotation) {
130
		return state.setValue(FACING, rotation.rotate(state.getValue(FACING)));
131
	}
132
133
	@Override
134
	public BlockState mirror(final BlockState state, final Mirror mirror) {
135
		return state.rotate(mirror.getRotation(state.getValue(FACING)));
136
	}
137
138
	@Override
139
	public int getRows() {
140
		return 1;
141
	}
142
143
	@Override
144
	public int getColumns() {
145
		return 3;
146
	}
147
148
	@Override
149
	protected InteractionResult useItemOn(
150
		final ItemStack itemStack,
151
		final BlockState state,
152
		final Level level,
153
		final BlockPos pos,
154
		final Player player,
155
		final InteractionHand hand,
156
		final BlockHitResult hitResult
157
	) {
158
		if (level.getBlockEntity(pos) instanceof ShelfBlockEntity shelfBlockEntity && !hand.equals(InteractionHand.OFF_HAND)) {
159
			OptionalInt hitSlot = this.getHitSlot(hitResult, state.getValue(FACING));
160
			if (hitSlot.isEmpty()) {
161
				return InteractionResult.PASS;
162
			}
163
164
			Inventory inventory = player.getInventory();
165
			if (level.isClientSide()) {
166
				return inventory.getSelectedItem().isEmpty() ? InteractionResult.PASS : InteractionResult.CONSUME;
167
			}
168
169
			if (!state.getValue(POWERED)) {
170
				boolean itemRemoved = swapSingleItem(itemStack, player, shelfBlockEntity, hitSlot.getAsInt(), inventory);
171
				if (itemRemoved) {
172
					this.playSound(level, pos, itemStack.isEmpty() ? SoundEvents.SHELF_TAKE_ITEM : SoundEvents.SHELF_SINGLE_SWAP);
173
				} else {
174
					if (itemStack.isEmpty()) {
175
						return InteractionResult.PASS;
176
					}
177
178
					this.playSound(level, pos, SoundEvents.SHELF_PLACE_ITEM);
179
				}
180
181
				return InteractionResult.SUCCESS_SERVER.heldItemTransformedTo(itemStack);
182
			} else {
183
				ItemStack previousItem = inventory.getSelectedItem();
184
				boolean anySwapped = this.swapHotbar(level, pos, inventory);
185
				if (!anySwapped) {
186
					return InteractionResult.CONSUME;
187
				}
188
189
				this.playSound(level, pos, SoundEvents.SHELF_MULTI_SWAP);
190
				return previousItem == inventory.getSelectedItem()
191
					? InteractionResult.SUCCESS_SERVER
192
					: InteractionResult.SUCCESS_SERVER.heldItemTransformedTo(inventory.getSelectedItem());
193
			}
194
		} else {
195
			return InteractionResult.PASS;
196
		}
197
	}
198
199
	private static boolean swapSingleItem(
200
		final ItemStack itemStack, final Player player, final ShelfBlockEntity shelfBlockEntity, final int hitSlot, final Inventory inventory
201
	) {
202
		ItemStack removedItem = shelfBlockEntity.swapItemNoUpdate(hitSlot, itemStack);
203
		ItemStack newInventoryItem = player.hasInfiniteMaterials() && removedItem.isEmpty() ? itemStack.copy() : removedItem;
204
		inventory.setItem(inventory.getSelectedSlot(), newInventoryItem);
205
		inventory.setChanged();
206
		shelfBlockEntity.setChanged(
207
			newInventoryItem.has(DataComponents.USE_EFFECTS) && !newInventoryItem.get(DataComponents.USE_EFFECTS).interactVibrations()
208
				? null
209
				: GameEvent.ITEM_INTERACT_FINISH
210
		);
211
		return !removedItem.isEmpty();
212
	}
213
214
	private boolean swapHotbar(final Level level, final BlockPos pos, final Inventory inventory) {
215
		List<BlockPos> connectedBlocks = this.getAllBlocksConnectedTo(level, pos);
216
		if (connectedBlocks.isEmpty()) {
217
			return false;
218
		}
219
220
		boolean anySwapped = false;
221
222
		for (int shelfPartIndex = 0; shelfPartIndex < connectedBlocks.size(); shelfPartIndex++) {
223
			ShelfBlockEntity shelfPart = (ShelfBlockEntity)level.getBlockEntity(connectedBlocks.get(shelfPartIndex));
224
			if (shelfPart != null) {
225
				for (int slot = 0; slot < shelfPart.getContainerSize(); slot++) {
226
					int inventorySlot = 9 - (connectedBlocks.size() - shelfPartIndex) * shelfPart.getContainerSize() + slot;
227
					if (inventorySlot >= 0 && inventorySlot <= inventory.getContainerSize()) {
228
						ItemStack placedInventoryItem = inventory.removeItemNoUpdate(inventorySlot);
229
						ItemStack removedShelfItem = shelfPart.swapItemNoUpdate(slot, placedInventoryItem);
230
						if (!placedInventoryItem.isEmpty() || !removedShelfItem.isEmpty()) {
231
							inventory.setItem(inventorySlot, removedShelfItem);
232
							anySwapped = true;
233
						}
234
					}
235
				}
236
237
				inventory.setChanged();
238
				shelfPart.setChanged(GameEvent.ENTITY_INTERACT);
239
			}
240
		}
241
242
		return anySwapped;
243
	}
244
245
	@Override
246
	public SideChainPart getSideChainPart(final BlockState state) {
247
		return state.getValue(SIDE_CHAIN_PART);
248
	}
249
250
	@Override
251
	public BlockState setSideChainPart(final BlockState state, final SideChainPart newPart) {
252
		return state.setValue(SIDE_CHAIN_PART, newPart);
253
	}
254
255
	@Override
256
	public Direction getFacing(final BlockState state) {
257
		return state.getValue(FACING);
258
	}
259
260
	@Override
261
	public boolean isConnectable(final BlockState state) {
262
		return state.is(BlockTags.WOODEN_SHELVES) && state.hasProperty(POWERED) && state.getValue(POWERED);
263
	}
264
265
	@Override
266
	public int getMaxChainLength() {
267
		return 3;
268
	}
269
270
	@Override
271
	protected void onPlace(final BlockState state, final Level level, final BlockPos pos, final BlockState oldState, final boolean movedByPiston) {
272
		if (state.getValue(POWERED)) {
273
			this.updateSelfAndNeighborsOnPoweringUp(level, pos, state, oldState);
274
		} else {
275
			this.updateNeighborsAfterPoweringDown(level, pos, state);
276
		}
277
	}
278
279
	private void playSound(final LevelAccessor level, final BlockPos pos, final SoundEvent sound) {
280
		level.playSound(null, pos, sound, SoundSource.BLOCKS, 1.0F, 1.0F);
281
	}
282
283
	@Override
284
	protected FluidState getFluidState(final BlockState state) {
285
		return state.getValue(WATERLOGGED) ? Fluids.WATER.getSource(false) : super.getFluidState(state);
286
	}
287
288
	@Override
289
	protected BlockState updateShape(
290
		final BlockState state,
291
		final LevelReader level,
292
		final ScheduledTickAccess ticks,
293
		final BlockPos pos,
294
		final Direction directionToNeighbour,
295
		final BlockPos neighbourPos,
296
		final BlockState neighbourState,
297
		final RandomSource random
298
	) {
299
		if (state.getValue(WATERLOGGED)) {
300
			ticks.scheduleTick(pos, Fluids.WATER, Fluids.WATER.getTickDelay(level));
301
		}
302
303
		return super.updateShape(state, level, ticks, pos, directionToNeighbour, neighbourPos, neighbourState, random);
304
	}
305
306
	@Override
307
	protected boolean hasAnalogOutputSignal(final BlockState state) {
308
		return true;
309
	}
310
311
	@Override
312
	protected int getAnalogOutputSignal(final BlockState state, final Level level, final BlockPos pos, final Direction direction) {
313
		if (level.isClientSide()) {
314
			return 0;
315
		} else if (direction != state.getValue(FACING).getOpposite()) {
316
			return 0;
317
		} else if (level.getBlockEntity(pos) instanceof ShelfBlockEntity blockEntity) {
318
			int item1Bit = blockEntity.getItem(0).isEmpty() ? 0 : 1;
319
			int item2Bit = blockEntity.getItem(1).isEmpty() ? 0 : 1;
320
			int item3Bit = blockEntity.getItem(2).isEmpty() ? 0 : 1;
321
			return item1Bit | item2Bit << 1 | item3Bit << 2;
322
		} else {
323
			return 0;
324
		}
325
	}
326
}
327

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