ShulkerBoxBlock .java

net.minecraft.world.level.block.ShulkerBoxBlock (class): members: SHAPES_OPEN_SUPPORT, CONTENTS, getDrops, getBlockSupportShape, playerWillDestroy, FACING.

net.minecraft.world.level.blockclass175 linescommonextends BaseEntityBlock7.1 KB

API Surface (14)

BlockEntity newBlockEntity(final BlockPos worldPosition, final BlockState blockState)
InteractionResult useWithoutItem(final BlockState state, final Level level, final BlockPos pos, final Player player, final BlockHitResult hitResult)
BlockState getStateForPlacement(final BlockPlaceContext context)
void createBlockStateDefinition(final StateDefinition.Builder<Block, BlockState> builder)
BlockState playerWillDestroy(final Level level, final BlockPos pos, final BlockState state, final Player player)
List<ItemStack> getDrops(final BlockState state, LootParams.Builder params)
void affectNeighborsAfterRemoval(final BlockState state, final ServerLevel level, final BlockPos pos, final boolean movedByPiston)
VoxelShape getBlockSupportShape(final BlockState state, final BlockGetter level, final BlockPos pos)
VoxelShape getShape(final BlockState state, final BlockGetter level, final BlockPos pos, final CollisionContext context)
boolean propagatesSkylightDown(final BlockState state)
boolean hasAnalogOutputSignal(final BlockState state)
int getAnalogOutputSignal(final BlockState state, final Level level, final BlockPos pos, final Direction direction)
BlockState rotate(final BlockState state, final Rotation rotation)
BlockState mirror(final BlockState state, final Mirror mirror)

Fields & Constants (4)

SHAPES_OPEN_SUPPORT
FACING
CONTENTS
color

String literals (1)

"contents"

Full Source

ShulkerBoxBlock.java175 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.resources.Identifier;
8
import net.minecraft.server.level.ServerLevel;
9
import net.minecraft.stats.Stats;
10
import net.minecraft.world.Containers;
11
import net.minecraft.world.InteractionResult;
12
import net.minecraft.world.entity.item.ItemEntity;
13
import net.minecraft.world.entity.monster.Shulker;
14
import net.minecraft.world.entity.monster.piglin.PiglinAi;
15
import net.minecraft.world.entity.player.Player;
16
import net.minecraft.world.inventory.AbstractContainerMenu;
17
import net.minecraft.world.item.DyeColor;
18
import net.minecraft.world.item.ItemStack;
19
import net.minecraft.world.item.context.BlockPlaceContext;
20
import net.minecraft.world.level.BlockGetter;
21
import net.minecraft.world.level.Level;
22
import net.minecraft.world.level.block.entity.BlockEntity;
23
import net.minecraft.world.level.block.entity.BlockEntityTicker;
24
import net.minecraft.world.level.block.entity.BlockEntityType;
25
import net.minecraft.world.level.block.entity.BlockEntityTypes;
26
import net.minecraft.world.level.block.entity.ShulkerBoxBlockEntity;
27
import net.minecraft.world.level.block.state.BlockBehaviour;
28
import net.minecraft.world.level.block.state.BlockState;
29
import net.minecraft.world.level.block.state.StateDefinition;
30
import net.minecraft.world.level.block.state.properties.EnumProperty;
31
import net.minecraft.world.level.storage.loot.LootParams;
32
import net.minecraft.world.level.storage.loot.parameters.LootContextParams;
33
import net.minecraft.world.phys.AABB;
34
import net.minecraft.world.phys.BlockHitResult;
35
import net.minecraft.world.phys.Vec3;
36
import net.minecraft.world.phys.shapes.CollisionContext;
37
import net.minecraft.world.phys.shapes.Shapes;
38
import net.minecraft.world.phys.shapes.VoxelShape;
39
import org.jspecify.annotations.Nullable;
40
41
public class ShulkerBoxBlock extends BaseEntityBlock {
42
	public static final Map<Direction, VoxelShape> SHAPES_OPEN_SUPPORT = Shapes.rotateAll(Block.boxZ(16.0, 0.0, 1.0));
43
	public static final EnumProperty<Direction> FACING = DirectionalBlock.FACING;
44
	public static final Identifier CONTENTS = Identifier.withDefaultNamespace("contents");
45
	private final @Nullable DyeColor color;
46
47
	public ShulkerBoxBlock(final @Nullable DyeColor color, final BlockBehaviour.Properties properties) {
48
		super(properties);
49
		this.color = color;
50
		this.registerDefaultState(this.stateDefinition.any().setValue(FACING, Direction.UP));
51
	}
52
53
	@Override
54
	public BlockEntity newBlockEntity(final BlockPos worldPosition, final BlockState blockState) {
55
		return new ShulkerBoxBlockEntity(this.color, worldPosition, blockState);
56
	}
57
58
	@Override
59
	public <T extends BlockEntity> @Nullable BlockEntityTicker<T> getTicker(final Level level, final BlockState blockState, final BlockEntityType<T> type) {
60
		return createTickerHelper(type, BlockEntityTypes.SHULKER_BOX, ShulkerBoxBlockEntity::tick);
61
	}
62
63
	@Override
64
	protected InteractionResult useWithoutItem(final BlockState state, final Level level, final BlockPos pos, final Player player, final BlockHitResult hitResult) {
65
		if (level instanceof ServerLevel serverLevel
66
			&& level.getBlockEntity(pos) instanceof ShulkerBoxBlockEntity shulkerBoxBlockEntity
67
			&& canOpen(state, level, pos, shulkerBoxBlockEntity)) {
68
			player.openMenu(shulkerBoxBlockEntity);
69
			player.awardStat(Stats.OPEN_SHULKER_BOX);
70
			PiglinAi.angerNearbyPiglins(serverLevel, player, true);
71
		}
72
73
		return InteractionResult.SUCCESS;
74
	}
75
76
	private static boolean canOpen(final BlockState state, final Level level, final BlockPos pos, final ShulkerBoxBlockEntity blockEntity) {
77
		if (blockEntity.getAnimationStatus() != ShulkerBoxBlockEntity.AnimationStatus.CLOSED) {
78
			return true;
79
		}
80
81
		AABB lidOpenBoundingBox = Shulker.getProgressDeltaAabb(1.0F, state.getValue(FACING), 0.0F, 0.5F, Vec3.atBottomCenterOf(pos)).deflate(1.0E-6);
82
		return level.noCollision(lidOpenBoundingBox);
83
	}
84
85
	@Override
86
	public BlockState getStateForPlacement(final BlockPlaceContext context) {
87
		return this.defaultBlockState().setValue(FACING, context.getClickedFace());
88
	}
89
90
	@Override
91
	protected void createBlockStateDefinition(final StateDefinition.Builder<Block, BlockState> builder) {
92
		builder.add(FACING);
93
	}
94
95
	@Override
96
	public BlockState playerWillDestroy(final Level level, final BlockPos pos, final BlockState state, final Player player) {
97
		BlockEntity blockEntity = level.getBlockEntity(pos);
98
		if (blockEntity instanceof ShulkerBoxBlockEntity shulkerBoxBlockEntity) {
99
			if (!level.isClientSide() && player.preventsBlockDrops() && !shulkerBoxBlockEntity.isEmpty()) {
100
				ItemStack itemStack = new ItemStack(state.getBlock());
101
				itemStack.applyComponents(blockEntity.collectComponents());
102
				ItemEntity entity = new ItemEntity(level, pos.getX() + 0.5, pos.getY() + 0.5, pos.getZ() + 0.5, itemStack);
103
				entity.setDefaultPickUpDelay();
104
				level.addFreshEntity(entity);
105
			} else {
106
				shulkerBoxBlockEntity.unpackLootTable(player);
107
			}
108
		}
109
110
		return super.playerWillDestroy(level, pos, state, player);
111
	}
112
113
	@Override
114
	protected List<ItemStack> getDrops(final BlockState state, LootParams.Builder params) {
115
		BlockEntity blockEntity = params.getOptionalParameter(LootContextParams.BLOCK_ENTITY);
116
		if (blockEntity instanceof ShulkerBoxBlockEntity shulkerBoxBlockEntity) {
117
			params = params.withDynamicDrop(CONTENTS, output -> {
118
				for (int i = 0; i < shulkerBoxBlockEntity.getContainerSize(); i++) {
119
					output.accept(shulkerBoxBlockEntity.getItem(i));
120
				}
121
			});
122
		}
123
124
		return super.getDrops(state, params);
125
	}
126
127
	@Override
128
	protected void affectNeighborsAfterRemoval(final BlockState state, final ServerLevel level, final BlockPos pos, final boolean movedByPiston) {
129
		Containers.updateNeighboursAfterDestroy(state, level, pos);
130
	}
131
132
	@Override
133
	protected VoxelShape getBlockSupportShape(final BlockState state, final BlockGetter level, final BlockPos pos) {
134
		return level.getBlockEntity(pos) instanceof ShulkerBoxBlockEntity shulker && !shulker.isClosed()
135
			? SHAPES_OPEN_SUPPORT.get(state.getValue(FACING).getOpposite())
136
			: Shapes.block();
137
	}
138
139
	@Override
140
	protected VoxelShape getShape(final BlockState state, final BlockGetter level, final BlockPos pos, final CollisionContext context) {
141
		return level.getBlockEntity(pos) instanceof ShulkerBoxBlockEntity shulkerBoxBlockEntity
142
			? Shapes.create(shulkerBoxBlockEntity.getBoundingBox(state))
143
			: Shapes.block();
144
	}
145
146
	@Override
147
	protected boolean propagatesSkylightDown(final BlockState state) {
148
		return false;
149
	}
150
151
	@Override
152
	protected boolean hasAnalogOutputSignal(final BlockState state) {
153
		return true;
154
	}
155
156
	@Override
157
	protected int getAnalogOutputSignal(final BlockState state, final Level level, final BlockPos pos, final Direction direction) {
158
		return AbstractContainerMenu.getRedstoneSignalFromBlockEntity(level.getBlockEntity(pos));
159
	}
160
161
	public @Nullable DyeColor getColor() {
162
		return this.color;
163
	}
164
165
	@Override
166
	protected BlockState rotate(final BlockState state, final Rotation rotation) {
167
		return state.setValue(FACING, rotation.rotate(state.getValue(FACING)));
168
	}
169
170
	@Override
171
	protected BlockState mirror(final BlockState state, final Mirror mirror) {
172
		return state.rotate(mirror.getRotation(state.getValue(FACING)));
173
	}
174
}
175

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