ComposterBlock .java

net.minecraft.world.level.block.ComposterBlock (class): members: OutputContainer, EmptyContainer, InputContainer, extractProduce, HOLE_WIDTH, handleFill.

net.minecraft.world.level.blockclass354 linescommonextends Block12.6 KB

Implements

API Surface (28)

void handleFill(final Level level, final BlockPos pos, final boolean success)
VoxelShape getShape(final BlockState state, final BlockGetter level, final BlockPos pos, final CollisionContext context)
VoxelShape getInteractionShape(final BlockState state, final BlockGetter level, final BlockPos pos)
VoxelShape getCollisionShape(final BlockState state, final BlockGetter level, final BlockPos pos, final CollisionContext context)
void onPlace(final BlockState state, final Level level, final BlockPos pos, final BlockState oldState, final boolean movedByPiston)
InteractionResult useItemOn(final ItemStack itemStack, final BlockState state, final Level level, final BlockPos pos, final Player player, final InteractionHand hand, final BlockHitResult hitResult)
InteractionResult useWithoutItem(final BlockState state, final Level level, final BlockPos pos, final Player player, final BlockHitResult hitResult)
BlockState insertItem(final Entity sourceEntity, final BlockState state, final ServerLevel level, final ItemStack itemStack, final BlockPos pos)
BlockState extractProduce(final Entity sourceEntity, final BlockState state, final Level level, final BlockPos pos)
void tick(final BlockState state, final ServerLevel level, final BlockPos pos, final RandomSource random)
boolean hasAnalogOutputSignal(final BlockState state)
int getAnalogOutputSignal(final BlockState state, final Level level, final BlockPos pos, final Direction direction)
void createBlockStateDefinition(final StateDefinition.Builder<Block, BlockState> builder)
boolean isPathfindable(final BlockState state, final PathComputationType type)
WorldlyContainer getContainer(final BlockState state, final LevelAccessor level, final BlockPos pos)
int[] getSlotsForFace(final Direction direction)
boolean canPlaceItemThroughFace(final int slot, final ItemStack itemStack, final @Nullable Direction direction)
boolean canTakeItemThroughFace(final int slot, final ItemStack itemStack, final Direction direction)
int getMaxStackSize()
int[] getSlotsForFace(final Direction direction)
boolean canPlaceItemThroughFace(final int slot, final ItemStack itemStack, final @Nullable Direction direction)
boolean canTakeItemThroughFace(final int slot, final ItemStack itemStack, final Direction direction)
void setChanged()
int getMaxStackSize()
int[] getSlotsForFace(final Direction direction)
boolean canPlaceItemThroughFace(final int slot, final ItemStack itemStack, final @Nullable Direction direction)
boolean canTakeItemThroughFace(final int slot, final ItemStack itemStack, final Direction direction)
void setChanged()

Fields & Constants (12)

READY
MIN_LEVEL
MAX_LEVEL
LEVEL
HOLE_WIDTH
SHAPES
itemStack
state
level
pos
player
hand

Full Source

ComposterBlock.java354 lines · Minecraft 26.3 · verbatim
1
package net.minecraft.world.level.block;
2
3
import java.util.Optional;
4
import net.minecraft.core.BlockPos;
5
import net.minecraft.core.Direction;
6
import net.minecraft.core.component.DataComponents;
7
import net.minecraft.core.particles.ParticleTypes;
8
import net.minecraft.server.level.ServerLevel;
9
import net.minecraft.sounds.SoundEvents;
10
import net.minecraft.sounds.SoundSource;
11
import net.minecraft.stats.Stats;
12
import net.minecraft.util.Mth;
13
import net.minecraft.util.RandomSource;
14
import net.minecraft.util.Util;
15
import net.minecraft.world.InteractionHand;
16
import net.minecraft.world.InteractionResult;
17
import net.minecraft.world.SimpleContainer;
18
import net.minecraft.world.WorldlyContainer;
19
import net.minecraft.world.WorldlyContainerHolder;
20
import net.minecraft.world.entity.Entity;
21
import net.minecraft.world.entity.item.ItemEntity;
22
import net.minecraft.world.entity.player.Player;
23
import net.minecraft.world.item.ItemStack;
24
import net.minecraft.world.item.Items;
25
import net.minecraft.world.item.component.Compostable;
26
import net.minecraft.world.level.BlockGetter;
27
import net.minecraft.world.level.Level;
28
import net.minecraft.world.level.LevelAccessor;
29
import net.minecraft.world.level.block.state.BlockBehaviour;
30
import net.minecraft.world.level.block.state.BlockState;
31
import net.minecraft.world.level.block.state.StateDefinition;
32
import net.minecraft.world.level.block.state.properties.BlockStateProperties;
33
import net.minecraft.world.level.block.state.properties.IntegerProperty;
34
import net.minecraft.world.level.gameevent.GameEvent;
35
import net.minecraft.world.level.pathfinder.PathComputationType;
36
import net.minecraft.world.level.storage.loot.LootContext;
37
import net.minecraft.world.level.storage.loot.LootParams;
38
import net.minecraft.world.level.storage.loot.parameters.LootContextParamSets;
39
import net.minecraft.world.level.storage.loot.parameters.LootContextParams;
40
import net.minecraft.world.phys.BlockHitResult;
41
import net.minecraft.world.phys.Vec3;
42
import net.minecraft.world.phys.shapes.BooleanOp;
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 ComposterBlock extends Block implements WorldlyContainerHolder {
49
	public static final int READY = 8;
50
	public static final int MIN_LEVEL = 0;
51
	public static final int MAX_LEVEL = 7;
52
	public static final IntegerProperty LEVEL = BlockStateProperties.LEVEL_COMPOSTER;
53
	private static final int HOLE_WIDTH = 12;
54
	private static final VoxelShape[] SHAPES = Util.make(() -> {
55
		VoxelShape[] shapes = Block.boxes(8, level -> Shapes.join(Shapes.block(), Block.column(12.0, Math.clamp(1 + level * 2, 2, 16), 16.0), BooleanOp.ONLY_FIRST));
56
		shapes[8] = shapes[7];
57
		return shapes;
58
	});
59
60
	public ComposterBlock(final BlockBehaviour.Properties properties) {
61
		super(properties);
62
		this.registerDefaultState(this.stateDefinition.any().setValue(LEVEL, 0));
63
	}
64
65
	public static void handleFill(final Level level, final BlockPos pos, final boolean success) {
66
		BlockState state = level.getBlockState(pos);
67
		level.playLocalSound(pos, success ? SoundEvents.COMPOSTER_FILL_SUCCESS : SoundEvents.COMPOSTER_FILL, SoundSource.BLOCKS, 1.0F, 1.0F, false);
68
		double centerHeight = state.getShape(level, pos).max(Direction.Axis.Y, 0.5, 0.5) + 0.03125;
69
		double sideOffsetPixels = 2.0;
70
		double sideOffset = 0.1875;
71
		double width = 0.625;
72
		RandomSource random = level.getRandom();
73
74
		for (int i = 0; i < 10; i++) {
75
			double xa = random.nextGaussian() * 0.02;
76
			double ya = random.nextGaussian() * 0.02;
77
			double za = random.nextGaussian() * 0.02;
78
			level.addParticle(
79
				ParticleTypes.COMPOSTER,
80
				pos.getX() + 0.1875 + 0.625 * random.nextFloat(),
81
				pos.getY() + centerHeight + random.nextFloat() * (1.0 - centerHeight),
82
				pos.getZ() + 0.1875 + 0.625 * random.nextFloat(),
83
				xa,
84
				ya,
85
				za
86
			);
87
		}
88
	}
89
90
	@Override
91
	protected VoxelShape getShape(final BlockState state, final BlockGetter level, final BlockPos pos, final CollisionContext context) {
92
		return SHAPES[state.getValue(LEVEL)];
93
	}
94
95
	@Override
96
	protected VoxelShape getInteractionShape(final BlockState state, final BlockGetter level, final BlockPos pos) {
97
		return Shapes.block();
98
	}
99
100
	@Override
101
	protected VoxelShape getCollisionShape(final BlockState state, final BlockGetter level, final BlockPos pos, final CollisionContext context) {
102
		return SHAPES[0];
103
	}
104
105
	@Override
106
	protected void onPlace(final BlockState state, final Level level, final BlockPos pos, final BlockState oldState, final boolean movedByPiston) {
107
		if (state.getValue(LEVEL) == 7) {
108
			level.scheduleTick(pos, state.getBlock(), 20);
109
		}
110
	}
111
112
	@Override
113
	protected InteractionResult useItemOn(
114
		final ItemStack itemStack,
115
		final BlockState state,
116
		final Level level,
117
		final BlockPos pos,
118
		final Player player,
119
		final InteractionHand hand,
120
		final BlockHitResult hitResult
121
	) {
122
		int fillLevel = state.getValue(LEVEL);
123
		Compostable compostable = itemStack.get(DataComponents.COMPOSTABLE);
124
		if (fillLevel < 8 && compostable != null) {
125
			if (fillLevel < 7 && level instanceof ServerLevel serverLevel) {
126
				BlockState newState = addLayer(player, state, serverLevel, pos, compostable);
127
				level.levelEvent(1500, pos, state != newState ? 1 : 0);
128
				player.awardStat(Stats.ITEM_USED.get(itemStack.getItem()));
129
				itemStack.consume(1, player);
130
			}
131
132
			return InteractionResult.SUCCESS;
133
		} else {
134
			return super.useItemOn(itemStack, state, level, pos, player, hand, hitResult);
135
		}
136
	}
137
138
	@Override
139
	protected InteractionResult useWithoutItem(final BlockState state, final Level level, final BlockPos pos, final Player player, final BlockHitResult hitResult) {
140
		int fillLevel = state.getValue(LEVEL);
141
		if (fillLevel == 8) {
142
			extractProduce(player, state, level, pos);
143
			return InteractionResult.SUCCESS;
144
		} else {
145
			return InteractionResult.PASS;
146
		}
147
	}
148
149
	public static BlockState insertItem(final Entity sourceEntity, final BlockState state, final ServerLevel level, final ItemStack itemStack, final BlockPos pos) {
150
		int fillLevel = state.getValue(LEVEL);
151
		Compostable compostable = itemStack.get(DataComponents.COMPOSTABLE);
152
		if (fillLevel < 7 && compostable != null) {
153
			BlockState newState = addLayer(sourceEntity, state, level, pos, compostable);
154
			itemStack.shrink(1);
155
			return newState;
156
		} else {
157
			return state;
158
		}
159
	}
160
161
	public static BlockState extractProduce(final Entity sourceEntity, final BlockState state, final Level level, final BlockPos pos) {
162
		if (!level.isClientSide()) {
163
			Vec3 itemPos = Vec3.atLowerCornerWithOffset(pos, 0.5, 1.01, 0.5).offsetRandomXZ(level.getRandom(), 0.7F);
164
			ItemEntity entity = new ItemEntity(level, itemPos.x(), itemPos.y(), itemPos.z(), new ItemStack(Items.BONE_MEAL));
165
			entity.setDefaultPickUpDelay();
166
			level.addFreshEntity(entity);
167
		}
168
169
		BlockState emptyState = empty(sourceEntity, state, level, pos);
170
		level.playSound(null, pos, SoundEvents.COMPOSTER_EMPTY, SoundSource.BLOCKS, 1.0F, 1.0F);
171
		return emptyState;
172
	}
173
174
	private static BlockState empty(final @Nullable Entity sourceEntity, final BlockState state, final LevelAccessor level, final BlockPos pos) {
175
		BlockState newState = state.setValue(LEVEL, 0);
176
		level.setBlockAndUpdate(pos, newState);
177
		level.gameEvent(GameEvent.BLOCK_CHANGE, pos, GameEvent.Context.of(sourceEntity, newState));
178
		return newState;
179
	}
180
181
	private static BlockState addLayer(
182
		final @Nullable Entity sourceEntity, final BlockState state, final ServerLevel level, final BlockPos pos, final Compostable compostable
183
	) {
184
		int fillLevel = state.getValue(LEVEL);
185
		LootContext lootContext = new LootContext.Builder(
186
				new LootParams.Builder(level)
187
					.withParameter(LootContextParams.BLOCK_STATE, state)
188
					.withParameter(LootContextParams.ORIGIN, Vec3.atCenterOf(pos))
189
					.withOptionalParameter(LootContextParams.INTERACTING_ENTITY, sourceEntity)
190
					.create(LootContextParamSets.BLOCK_INTERACT)
191
			)
192
			.create(Optional.empty());
193
		int layersToAdd = compostable.layers().get(lootContext, 0);
194
		if (layersToAdd > 0) {
195
			int newLevel = Mth.clamp(fillLevel + layersToAdd, 0, 7);
196
			BlockState newState = state.setValue(LEVEL, newLevel);
197
			level.setBlockAndUpdate(pos, newState);
198
			level.gameEvent(GameEvent.BLOCK_CHANGE, pos, GameEvent.Context.of(sourceEntity, newState));
199
			if (newLevel == 7) {
200
				level.scheduleTick(pos, state.getBlock(), 20);
201
			}
202
203
			return newState;
204
		} else {
205
			return state;
206
		}
207
	}
208
209
	@Override
210
	protected void tick(final BlockState state, final ServerLevel level, final BlockPos pos, final RandomSource random) {
211
		if (state.getValue(LEVEL) == 7) {
212
			level.setBlockAndUpdate(pos, state.cycle(LEVEL));
213
			level.playSound(null, pos, SoundEvents.COMPOSTER_READY, SoundSource.BLOCKS, 1.0F, 1.0F);
214
		}
215
	}
216
217
	@Override
218
	protected boolean hasAnalogOutputSignal(final BlockState state) {
219
		return true;
220
	}
221
222
	@Override
223
	protected int getAnalogOutputSignal(final BlockState state, final Level level, final BlockPos pos, final Direction direction) {
224
		return state.getValue(LEVEL);
225
	}
226
227
	@Override
228
	protected void createBlockStateDefinition(final StateDefinition.Builder<Block, BlockState> builder) {
229
		builder.add(LEVEL);
230
	}
231
232
	@Override
233
	protected boolean isPathfindable(final BlockState state, final PathComputationType type) {
234
		return false;
235
	}
236
237
	@Override
238
	public WorldlyContainer getContainer(final BlockState state, final LevelAccessor level, final BlockPos pos) {
239
		int contentLevel = state.getValue(LEVEL);
240
		if (contentLevel == 8) {
241
			return new ComposterBlock.OutputContainer(state, level, pos, new ItemStack(Items.BONE_MEAL));
242
		} else {
243
			return contentLevel < 7 ? new ComposterBlock.InputContainer(state, level, pos) : new ComposterBlock.EmptyContainer();
244
		}
245
	}
246
247
	private static class EmptyContainer extends SimpleContainer implements WorldlyContainer {
248
		public EmptyContainer() {
249
			super(0);
250
		}
251
252
		@Override
253
		public int[] getSlotsForFace(final Direction direction) {
254
			return new int[0];
255
		}
256
257
		@Override
258
		public boolean canPlaceItemThroughFace(final int slot, final ItemStack itemStack, final @Nullable Direction direction) {
259
			return false;
260
		}
261
262
		@Override
263
		public boolean canTakeItemThroughFace(final int slot, final ItemStack itemStack, final Direction direction) {
264
			return false;
265
		}
266
	}
267
268
	private static class InputContainer extends SimpleContainer implements WorldlyContainer {
269
		private final BlockState state;
270
		private final LevelAccessor level;
271
		private final BlockPos pos;
272
		private boolean changed;
273
274
		public InputContainer(final BlockState state, final LevelAccessor level, final BlockPos pos) {
275
			super(1);
276
			this.state = state;
277
			this.level = level;
278
			this.pos = pos;
279
		}
280
281
		@Override
282
		public int getMaxStackSize() {
283
			return 1;
284
		}
285
286
		@Override
287
		public int[] getSlotsForFace(final Direction direction) {
288
			return direction == Direction.UP ? new int[]{0} : new int[0];
289
		}
290
291
		@Override
292
		public boolean canPlaceItemThroughFace(final int slot, final ItemStack itemStack, final @Nullable Direction direction) {
293
			return !this.changed && direction == Direction.UP && itemStack.has(DataComponents.COMPOSTABLE);
294
		}
295
296
		@Override
297
		public boolean canTakeItemThroughFace(final int slot, final ItemStack itemStack, final Direction direction) {
298
			return false;
299
		}
300
301
		@Override
302
		public void setChanged() {
303
			ItemStack contents = this.getItem(0);
304
			Compostable compostable = contents.get(DataComponents.COMPOSTABLE);
305
			if (!contents.isEmpty() && compostable != null && this.level instanceof ServerLevel serverLevel) {
306
				this.changed = true;
307
				BlockState newState = ComposterBlock.addLayer(null, this.state, serverLevel, this.pos, compostable);
308
				this.level.levelEvent(1500, this.pos, newState != this.state ? 1 : 0);
309
				this.removeItemNoUpdate(0);
310
			}
311
		}
312
	}
313
314
	private static class OutputContainer extends SimpleContainer implements WorldlyContainer {
315
		private final BlockState state;
316
		private final LevelAccessor level;
317
		private final BlockPos pos;
318
		private boolean changed;
319
320
		public OutputContainer(final BlockState state, final LevelAccessor level, final BlockPos pos, final ItemStack contents) {
321
			super(contents);
322
			this.state = state;
323
			this.level = level;
324
			this.pos = pos;
325
		}
326
327
		@Override
328
		public int getMaxStackSize() {
329
			return 1;
330
		}
331
332
		@Override
333
		public int[] getSlotsForFace(final Direction direction) {
334
			return direction == Direction.DOWN ? new int[]{0} : new int[0];
335
		}
336
337
		@Override
338
		public boolean canPlaceItemThroughFace(final int slot, final ItemStack itemStack, final @Nullable Direction direction) {
339
			return false;
340
		}
341
342
		@Override
343
		public boolean canTakeItemThroughFace(final int slot, final ItemStack itemStack, final Direction direction) {
344
			return !this.changed && direction == Direction.DOWN && itemStack.is(Items.BONE_MEAL);
345
		}
346
347
		@Override
348
		public void setChanged() {
349
			ComposterBlock.empty(null, this.state, this.level, this.pos);
350
			this.changed = true;
351
		}
352
	}
353
}
354

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