MatchTool .java

net.minecraft.world.level.storage.loot.predicates.MatchTool (record): members: predicate, toolMatches, getReferencedContextParams, test, MAP_CODEC, codec.

net.minecraft.world.level.storage.loot.predicatesrecord37 linescommon1.3 KB

Implements

API Surface (4)

MapCodec<MatchTool> codec()
Set<ContextKey<?>> getReferencedContextParams()
boolean test(final LootContext context)
LootItemCondition.Builder toolMatches(final ItemPredicate.Builder predicate)

Fields & Constants (1)

MAP_CODEC

String literals (1)

"predicate"

Code Graph: Dependencies (7)

Edges (calls / imports / inherits / references) resolved by graphify from the AST. Every chip opens the class source.

Full Source

MatchTool.java37 lines · Minecraft 26.3 · verbatim
1
package net.minecraft.world.level.storage.loot.predicates;
2
3
import com.mojang.serialization.MapCodec;
4
import com.mojang.serialization.codecs.RecordCodecBuilder;
5
import java.util.Optional;
6
import java.util.Set;
7
import net.minecraft.advancements.predicates.ItemPredicate;
8
import net.minecraft.util.context.ContextKey;
9
import net.minecraft.world.item.ItemInstance;
10
import net.minecraft.world.level.storage.loot.LootContext;
11
import net.minecraft.world.level.storage.loot.parameters.LootContextParams;
12
13
public record MatchTool(Optional<ItemPredicate> predicate) implements LootItemCondition {
14
	public static final MapCodec<MatchTool> MAP_CODEC = RecordCodecBuilder.mapCodec(
15
		i -> i.group(ItemPredicate.CODEC.optionalFieldOf("predicate").forGetter(MatchTool::predicate)).apply(i, MatchTool::new)
16
	);
17
18
	@Override
19
	public MapCodec<MatchTool> codec() {
20
		return MAP_CODEC;
21
	}
22
23
	@Override
24
	public Set<ContextKey<?>> getReferencedContextParams() {
25
		return Set.of(LootContextParams.TOOL);
26
	}
27
28
	public boolean test(final LootContext context) {
29
		ItemInstance tool = context.getOptional(LootContextParams.TOOL);
30
		return tool != null && (this.predicate.isEmpty() || this.predicate.get().test(tool));
31
	}
32
33
	public static LootItemCondition.Builder toolMatches(final ItemPredicate.Builder predicate) {
34
		return () -> new MatchTool(Optional.of(predicate.build()));
35
	}
36
}
37

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