PotionsPredicate .java

net.minecraft.core.component.predicates.PotionsPredicate (record, 63 lines): members: potions, effects, ofPotions, ofPotion, componentType, matches, CODEC.

net.minecraft.core.component.predicatesrecord63 linescommon2.7 KB

Implements

API Surface (4)

DataComponentType<PotionContents> componentType()
boolean matches(final PotionContents potionContents)
PotionsPredicate ofPotions(final HolderSet<Potion> potions)
PotionsPredicate ofPotion(final Holder<Potion> potion)

Fields & Constants (2)

CODEC
STREAM_CODEC

String literals (2)

"potions"
"effects"

Full Source

PotionsPredicate.java63 lines · Minecraft 26.3 · verbatim
1
package net.minecraft.core.component.predicates;
2
3
import com.mojang.serialization.Codec;
4
import com.mojang.serialization.codecs.RecordCodecBuilder;
5
import java.util.Optional;
6
import net.minecraft.advancements.predicates.CollectionPredicate;
7
import net.minecraft.advancements.predicates.MobEffectsPredicate;
8
import net.minecraft.advancements.predicates.SingleComponentItemPredicate;
9
import net.minecraft.core.Holder;
10
import net.minecraft.core.HolderSet;
11
import net.minecraft.core.component.DataComponentType;
12
import net.minecraft.core.component.DataComponents;
13
import net.minecraft.core.registries.Registries;
14
import net.minecraft.core.registries.codec.RegistryCodecs;
15
import net.minecraft.network.RegistryFriendlyByteBuf;
16
import net.minecraft.network.codec.ByteBufCodecs;
17
import net.minecraft.network.codec.StreamCodec;
18
import net.minecraft.world.effect.MobEffectInstance;
19
import net.minecraft.world.item.alchemy.Potion;
20
import net.minecraft.world.item.alchemy.PotionContents;
21
22
public record PotionsPredicate(Optional<HolderSet<Potion>> potions, Optional<CollectionPredicate<MobEffectInstance, MobEffectsPredicate>> effects)
23
	implements SingleComponentItemPredicate<PotionContents> {
24
	public static final Codec<PotionsPredicate> CODEC = RecordCodecBuilder.create(
25
		i -> i.group(
26
				RegistryCodecs.holderSet(Registries.POTION).optionalFieldOf("potions").forGetter(PotionsPredicate::potions),
27
				CollectionPredicate.<MobEffectInstance, MobEffectsPredicate>codec(MobEffectsPredicate.CODEC)
28
					.optionalFieldOf("effects")
29
					.forGetter(PotionsPredicate::effects)
30
			)
31
			.apply(i, PotionsPredicate::new)
32
	);
33
	public static final StreamCodec<RegistryFriendlyByteBuf, PotionsPredicate> STREAM_CODEC = StreamCodec.composite(
34
		ByteBufCodecs.optional(ByteBufCodecs.holderSet(Registries.POTION)),
35
		PotionsPredicate::potions,
36
		ByteBufCodecs.optional(ByteBufCodecs.fromCodecTrusted(CollectionPredicate.codec(MobEffectsPredicate.CODEC))),
37
		PotionsPredicate::effects,
38
		PotionsPredicate::new
39
	);
40
41
	@Override
42
	public DataComponentType<PotionContents> componentType() {
43
		return DataComponents.POTION_CONTENTS;
44
	}
45
46
	public boolean matches(final PotionContents potionContents) {
47
		Optional<Holder<Potion>> potion = potionContents.potion();
48
		if (!this.potions.isPresent() || !potion.isEmpty() && this.potions.get().contains(potion.get())) {
49
			return this.effects.isPresent() ? this.effects.get().test(potionContents.getAllEffects()) : true;
50
		} else {
51
			return false;
52
		}
53
	}
54
55
	public static PotionsPredicate ofPotions(final HolderSet<Potion> potions) {
56
		return new PotionsPredicate(Optional.of(potions), Optional.empty());
57
	}
58
59
	public static PotionsPredicate ofPotion(final Holder<Potion> potion) {
60
		return ofPotions(HolderSet.direct(potion));
61
	}
62
}
63

Decompiled Minecraft 26.3 source of PotionsPredicate (net/minecraft/core/component/predicates/PotionsPredicate.java) with Mojang official mappings, stored verbatim on the site.