MatchBlock .java

net.minecraft.world.level.storage.loot.predicates.MatchBlock (record, 46 lines): members: predicate, blockMatches, getReferencedContextParams, test, codec.

net.minecraft.world.level.storage.loot.predicatesrecord46 linescommon2.1 KB

Implements

API Surface (6)

MapCodec<MatchBlock> codec()
Set<ContextKey<?>> getReferencedContextParams()
boolean test(final LootContext context)
LootItemCondition.Builder blockMatches(final BlockPredicate.Builder predicate)
LootItemCondition.Builder blockMatches(final HolderGetter<Block> lookup, final Block block)
LootItemCondition.Builder blockMatches(final HolderGetter<Block> lookup, final Block block, final StatePropertiesPredicate.Builder properties)

Fields & Constants (1)

MAP_CODEC

Full Source

MatchBlock.java46 lines · Minecraft 26.3 · verbatim
1
package net.minecraft.world.level.storage.loot.predicates;
2
3
import com.mojang.serialization.MapCodec;
4
import java.util.Set;
5
import net.minecraft.advancements.predicates.BlockPredicate;
6
import net.minecraft.advancements.predicates.StatePropertiesPredicate;
7
import net.minecraft.core.HolderGetter;
8
import net.minecraft.util.context.ContextKey;
9
import net.minecraft.world.level.block.Block;
10
import net.minecraft.world.level.block.entity.BlockEntity;
11
import net.minecraft.world.level.block.state.BlockState;
12
import net.minecraft.world.level.storage.loot.LootContext;
13
import net.minecraft.world.level.storage.loot.parameters.LootContextParams;
14
15
public record MatchBlock(BlockPredicate predicate) implements LootItemCondition {
16
	public static final MapCodec<MatchBlock> MAP_CODEC = BlockPredicate.MAP_CODEC.xmap(MatchBlock::new, MatchBlock::predicate);
17
18
	@Override
19
	public MapCodec<MatchBlock> codec() {
20
		return MAP_CODEC;
21
	}
22
23
	@Override
24
	public Set<ContextKey<?>> getReferencedContextParams() {
25
		return this.predicate.willMatchBlockEntity() ? Set.of(LootContextParams.BLOCK_STATE, LootContextParams.BLOCK_ENTITY) : Set.of(LootContextParams.BLOCK_STATE);
26
	}
27
28
	public boolean test(final LootContext context) {
29
		BlockState blockState = context.getOptional(LootContextParams.BLOCK_STATE);
30
		BlockEntity blockEntity = context.getOptional(LootContextParams.BLOCK_ENTITY);
31
		return blockState != null && this.predicate.matchesState(blockState) && this.predicate.matchesBlockEntity(context.getLevel(), blockEntity);
32
	}
33
34
	public static LootItemCondition.Builder blockMatches(final BlockPredicate.Builder predicate) {
35
		return () -> new MatchBlock(predicate.build());
36
	}
37
38
	public static LootItemCondition.Builder blockMatches(final HolderGetter<Block> lookup, final Block block) {
39
		return blockMatches(BlockPredicate.Builder.block().of(lookup, block));
40
	}
41
42
	public static LootItemCondition.Builder blockMatches(final HolderGetter<Block> lookup, final Block block, final StatePropertiesPredicate.Builder properties) {
43
		return blockMatches(BlockPredicate.Builder.block().of(lookup, block).setProperties(properties));
44
	}
45
}
46

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