ChestBlockEntity .java

net.minecraft.world.level.block.entity.ChestBlockEntity (class): members: getOpenCount, swapContents, chestLidController, signalOpenCount, lidAnimateTick.

net.minecraft.world.level.block.entityclass193 linescommonextends RandomizableContainerBlockEntity6.8 KB

Implements

API Surface (21)

void onOpen(final Level level, final BlockPos pos, final BlockState blockState)
void onClose(final Level level, final BlockPos pos, final BlockState blockState)
void openerCountChanged(final Level level, final BlockPos pos, final BlockState blockState, final int previous, final int current)
boolean isOwnContainer(final Player player)
int getContainerSize()
Component getDefaultName()
void loadAdditional(final ValueInput input)
void saveAdditional(final ValueOutput output)
void lidAnimateTick(final Level level, final BlockPos pos, final BlockState state, final ChestBlockEntity entity)
boolean triggerEvent(final int b0, final int b1)
void startOpen(final ContainerUser containerUser)
void stopOpen(final ContainerUser containerUser)
List<ContainerUser> getEntitiesWithContainerOpen()
NonNullList<ItemStack> getItems()
void setItems(final NonNullList<ItemStack> items)
float getOpenNess(final float a)
int getOpenCount(final BlockGetter level, final BlockPos pos)
void swapContents(final ChestBlockEntity one, final ChestBlockEntity two)
AbstractContainerMenu createMenu(final int containerId, final Inventory inventory)
void recheckOpen()
void signalOpenCount(final Level level, final BlockPos pos, final BlockState blockState, final int previous, final int current)

Fields & Constants (5)

EVENT_SET_OPEN_COUNT
DEFAULT_NAME
items
openersCounter
chestLidController

String literals (1)

"container.chest"

Full Source

ChestBlockEntity.java193 lines · Minecraft 26.3 · verbatim
1
package net.minecraft.world.level.block.entity;
2
3
import java.util.List;
4
import net.minecraft.core.BlockPos;
5
import net.minecraft.core.Direction;
6
import net.minecraft.core.NonNullList;
7
import net.minecraft.network.chat.Component;
8
import net.minecraft.sounds.SoundEvent;
9
import net.minecraft.sounds.SoundSource;
10
import net.minecraft.world.CompoundContainer;
11
import net.minecraft.world.Container;
12
import net.minecraft.world.ContainerHelper;
13
import net.minecraft.world.entity.ContainerUser;
14
import net.minecraft.world.entity.player.Inventory;
15
import net.minecraft.world.entity.player.Player;
16
import net.minecraft.world.inventory.AbstractContainerMenu;
17
import net.minecraft.world.inventory.ChestMenu;
18
import net.minecraft.world.item.ItemStack;
19
import net.minecraft.world.level.BlockGetter;
20
import net.minecraft.world.level.Level;
21
import net.minecraft.world.level.block.Block;
22
import net.minecraft.world.level.block.ChestBlock;
23
import net.minecraft.world.level.block.state.BlockState;
24
import net.minecraft.world.level.block.state.properties.ChestType;
25
import net.minecraft.world.level.storage.ValueInput;
26
import net.minecraft.world.level.storage.ValueOutput;
27
28
public class ChestBlockEntity extends RandomizableContainerBlockEntity implements LidBlockEntity {
29
	private static final int EVENT_SET_OPEN_COUNT = 1;
30
	private static final Component DEFAULT_NAME = Component.translatable("container.chest");
31
	private NonNullList<ItemStack> items = NonNullList.withSize(27, ItemStack.EMPTY);
32
	private final ContainerOpenersCounter openersCounter = new ContainerOpenersCounter() {
33
		@Override
34
		protected void onOpen(final Level level, final BlockPos pos, final BlockState blockState) {
35
			if (blockState.getBlock() instanceof ChestBlock chestBlock) {
36
				ChestBlockEntity.playSound(level, pos, blockState, chestBlock.getOpenChestSound());
37
			}
38
		}
39
40
		@Override
41
		protected void onClose(final Level level, final BlockPos pos, final BlockState blockState) {
42
			if (blockState.getBlock() instanceof ChestBlock chestBlock) {
43
				ChestBlockEntity.playSound(level, pos, blockState, chestBlock.getCloseChestSound());
44
			}
45
		}
46
47
		@Override
48
		protected void openerCountChanged(final Level level, final BlockPos pos, final BlockState blockState, final int previous, final int current) {
49
			ChestBlockEntity.this.signalOpenCount(level, pos, blockState, previous, current);
50
		}
51
52
		@Override
53
		public boolean isOwnContainer(final Player player) {
54
			if (!(player.containerMenu instanceof ChestMenu)) {
55
				return false;
56
			}
57
58
			Container container = ((ChestMenu)player.containerMenu).getContainer();
59
			return container == ChestBlockEntity.this || container instanceof CompoundContainer compoundContainer && compoundContainer.contains(ChestBlockEntity.this);
60
		}
61
	};
62
	private final ChestLidController chestLidController = new ChestLidController();
63
64
	protected ChestBlockEntity(final BlockEntityType<?> type, final BlockPos worldPosition, final BlockState blockState) {
65
		super(type, worldPosition, blockState);
66
	}
67
68
	public ChestBlockEntity(final BlockPos worldPosition, final BlockState blockState) {
69
		this(BlockEntityTypes.CHEST, worldPosition, blockState);
70
	}
71
72
	@Override
73
	public int getContainerSize() {
74
		return 27;
75
	}
76
77
	@Override
78
	protected Component getDefaultName() {
79
		return DEFAULT_NAME;
80
	}
81
82
	@Override
83
	protected void loadAdditional(final ValueInput input) {
84
		super.loadAdditional(input);
85
		this.items = NonNullList.withSize(this.getContainerSize(), ItemStack.EMPTY);
86
		if (!this.tryLoadLootTable(input)) {
87
			ContainerHelper.loadAllItems(input, this.items);
88
		}
89
	}
90
91
	@Override
92
	protected void saveAdditional(final ValueOutput output) {
93
		super.saveAdditional(output);
94
		if (!this.trySaveLootTable(output)) {
95
			ContainerHelper.saveAllItems(output, this.items);
96
		}
97
	}
98
99
	public static void lidAnimateTick(final Level level, final BlockPos pos, final BlockState state, final ChestBlockEntity entity) {
100
		entity.chestLidController.tickLid();
101
	}
102
103
	private static void playSound(final Level level, final BlockPos worldPosition, final BlockState blockState, final SoundEvent event) {
104
		ChestType type = blockState.getValue(ChestBlock.TYPE);
105
		if (type != ChestType.LEFT) {
106
			double x = worldPosition.getX() + 0.5;
107
			double y = worldPosition.getY() + 0.5;
108
			double z = worldPosition.getZ() + 0.5;
109
			if (type == ChestType.RIGHT) {
110
				Direction direction = ChestBlock.getConnectedDirection(blockState);
111
				x += direction.getStepX() * 0.5;
112
				z += direction.getStepZ() * 0.5;
113
			}
114
115
			level.playSound(null, x, y, z, event, SoundSource.BLOCKS, 0.5F, level.getRandom().nextFloat() * 0.1F + 0.9F);
116
		}
117
	}
118
119
	@Override
120
	public boolean triggerEvent(final int b0, final int b1) {
121
		if (b0 == 1) {
122
			this.chestLidController.shouldBeOpen(b1 > 0);
123
			return true;
124
		} else {
125
			return super.triggerEvent(b0, b1);
126
		}
127
	}
128
129
	@Override
130
	public void startOpen(final ContainerUser containerUser) {
131
		if (!this.remove && !containerUser.getLivingEntity().isSpectator()) {
132
			this.openersCounter
133
				.incrementOpeners(containerUser.getLivingEntity(), this.getLevel(), this.getBlockPos(), this.getBlockState(), containerUser.getContainerInteractionRange());
134
		}
135
	}
136
137
	@Override
138
	public void stopOpen(final ContainerUser containerUser) {
139
		if (!this.remove && !containerUser.getLivingEntity().isSpectator()) {
140
			this.openersCounter.decrementOpeners(containerUser.getLivingEntity(), this.getLevel(), this.getBlockPos(), this.getBlockState());
141
		}
142
	}
143
144
	@Override
145
	public List<ContainerUser> getEntitiesWithContainerOpen() {
146
		return this.openersCounter.getEntitiesWithContainerOpen(this.getLevel(), this.getBlockPos());
147
	}
148
149
	@Override
150
	protected NonNullList<ItemStack> getItems() {
151
		return this.items;
152
	}
153
154
	@Override
155
	protected void setItems(final NonNullList<ItemStack> items) {
156
		this.items = items;
157
	}
158
159
	@Override
160
	public float getOpenNess(final float a) {
161
		return this.chestLidController.getOpenness(a);
162
	}
163
164
	public static int getOpenCount(final BlockGetter level, final BlockPos pos) {
165
		BlockState state = level.getBlockState(pos);
166
		return state.hasBlockEntity() && level.getBlockEntity(pos) instanceof ChestBlockEntity chestBlockEntity
167
			? chestBlockEntity.openersCounter.getOpenerCount()
168
			: 0;
169
	}
170
171
	public static void swapContents(final ChestBlockEntity one, final ChestBlockEntity two) {
172
		NonNullList<ItemStack> items = one.getItems();
173
		one.setItems(two.getItems());
174
		two.setItems(items);
175
	}
176
177
	@Override
178
	protected AbstractContainerMenu createMenu(final int containerId, final Inventory inventory) {
179
		return ChestMenu.threeRows(containerId, inventory, this);
180
	}
181
182
	public void recheckOpen() {
183
		if (!this.remove) {
184
			this.openersCounter.recheckOpeners(this.getLevel(), this.getBlockPos(), this.getBlockState());
185
		}
186
	}
187
188
	protected void signalOpenCount(final Level level, final BlockPos pos, final BlockState blockState, final int previous, final int current) {
189
		Block block = blockState.getBlock();
190
		level.blockEvent(pos, block, 1, current);
191
	}
192
}
193

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