| 1 | package net.minecraft.advancements.predicates;
|
| 2 |
|
| 3 | import com.google.common.collect.ImmutableMap;
|
| 4 | import com.mojang.serialization.Codec;
|
| 5 | import com.mojang.serialization.codecs.RecordCodecBuilder;
|
| 6 | import java.util.HashMap;
|
| 7 | import java.util.Map;
|
| 8 | import java.util.Optional;
|
| 9 | import java.util.Map.Entry;
|
| 10 | import java.util.function.Predicate;
|
| 11 | import net.minecraft.core.Holder;
|
| 12 | import net.minecraft.network.RegistryFriendlyByteBuf;
|
| 13 | import net.minecraft.network.codec.ByteBufCodecs;
|
| 14 | import net.minecraft.network.codec.StreamCodec;
|
| 15 | import net.minecraft.world.effect.MobEffect;
|
| 16 | import net.minecraft.world.effect.MobEffectInstance;
|
| 17 | import net.minecraft.world.entity.Entity;
|
| 18 | import net.minecraft.world.entity.LivingEntity;
|
| 19 | import org.jspecify.annotations.Nullable;
|
| 20 |
|
| 21 | public record MobEffectsPredicate(Map<Holder<MobEffect>, MobEffectsPredicate.MobEffectInstancePredicate> effectMap) implements Predicate<MobEffectInstance> {
|
| 22 | public static final Codec<MobEffectsPredicate> CODEC = Codec.unboundedMap(MobEffect.CODEC, MobEffectsPredicate.MobEffectInstancePredicate.CODEC)
|
| 23 | .xmap(MobEffectsPredicate::new, MobEffectsPredicate::effectMap);
|
| 24 | public static final StreamCodec<RegistryFriendlyByteBuf, MobEffectsPredicate> STREAM_CODEC = MobEffectsPredicate.MobEffectInstancePredicate.MAP_STREAM_CODEC
|
| 25 | .map(MobEffectsPredicate::new, MobEffectsPredicate::effectMap);
|
| 26 |
|
| 27 | public boolean matches(final Entity entity) {
|
| 28 | return entity instanceof LivingEntity living && this.matches(living.getActiveEffectsMap());
|
| 29 | }
|
| 30 |
|
| 31 | public boolean matches(final LivingEntity entity) {
|
| 32 | return this.matches(entity.getActiveEffectsMap());
|
| 33 | }
|
| 34 |
|
| 35 | public boolean matches(final Map<Holder<MobEffect>, MobEffectInstance> effects) {
|
| 36 | for (Entry<Holder<MobEffect>, MobEffectsPredicate.MobEffectInstancePredicate> entry : this.effectMap.entrySet()) {
|
| 37 | MobEffectInstance instance = effects.get(entry.getKey());
|
| 38 | if (!entry.getValue().matches(instance)) {
|
| 39 | return false;
|
| 40 | }
|
| 41 | }
|
| 42 |
|
| 43 | return true;
|
| 44 | }
|
| 45 |
|
| 46 | public boolean test(final MobEffectInstance mobEffect) {
|
| 47 | MobEffectsPredicate.MobEffectInstancePredicate predicate = this.effectMap.get(mobEffect.getEffect());
|
| 48 | return predicate != null && predicate.matches(mobEffect);
|
| 49 | }
|
| 50 |
|
| 51 | public static class Builder {
|
| 52 | private final ImmutableMap.Builder<Holder<MobEffect>, MobEffectsPredicate.MobEffectInstancePredicate> effectMap = ImmutableMap.builder();
|
| 53 |
|
| 54 | public static MobEffectsPredicate.Builder effects() {
|
| 55 | return new MobEffectsPredicate.Builder();
|
| 56 | }
|
| 57 |
|
| 58 | public MobEffectsPredicate.Builder and(final Holder<MobEffect> effect) {
|
| 59 | this.effectMap.put(effect, new MobEffectsPredicate.MobEffectInstancePredicate());
|
| 60 | return this;
|
| 61 | }
|
| 62 |
|
| 63 | public MobEffectsPredicate.Builder and(final Holder<MobEffect> effect, final MobEffectsPredicate.MobEffectInstancePredicate predicate) {
|
| 64 | this.effectMap.put(effect, predicate);
|
| 65 | return this;
|
| 66 | }
|
| 67 |
|
| 68 | public MobEffectsPredicate build() {
|
| 69 | return new MobEffectsPredicate(this.effectMap.build());
|
| 70 | }
|
| 71 | }
|
| 72 |
|
| 73 | public record MobEffectInstancePredicate(MinMaxBounds.Ints amplifier, MinMaxBounds.Ints duration, Optional<Boolean> ambient, Optional<Boolean> visible) {
|
| 74 | public static final Codec<MobEffectsPredicate.MobEffectInstancePredicate> CODEC = RecordCodecBuilder.create(
|
| 75 | i -> i.group(
|
| 76 | MinMaxBounds.Ints.CODEC.optionalFieldOf("amplifier", MinMaxBounds.Ints.ANY).forGetter(MobEffectsPredicate.MobEffectInstancePredicate::amplifier),
|
| 77 | MinMaxBounds.Ints.CODEC.optionalFieldOf("duration", MinMaxBounds.Ints.ANY).forGetter(MobEffectsPredicate.MobEffectInstancePredicate::duration),
|
| 78 | Codec.BOOL.optionalFieldOf("ambient").forGetter(MobEffectsPredicate.MobEffectInstancePredicate::ambient),
|
| 79 | Codec.BOOL.optionalFieldOf("visible").forGetter(MobEffectsPredicate.MobEffectInstancePredicate::visible)
|
| 80 | )
|
| 81 | .apply(i, MobEffectsPredicate.MobEffectInstancePredicate::new)
|
| 82 | );
|
| 83 | public static final StreamCodec<RegistryFriendlyByteBuf, MobEffectsPredicate.MobEffectInstancePredicate> STREAM_CODEC = StreamCodec.composite(
|
| 84 | MinMaxBounds.Ints.STREAM_CODEC,
|
| 85 | MobEffectsPredicate.MobEffectInstancePredicate::amplifier,
|
| 86 | MinMaxBounds.Ints.STREAM_CODEC,
|
| 87 | MobEffectsPredicate.MobEffectInstancePredicate::duration,
|
| 88 | ByteBufCodecs.optional(ByteBufCodecs.BOOL),
|
| 89 | MobEffectsPredicate.MobEffectInstancePredicate::ambient,
|
| 90 | ByteBufCodecs.optional(ByteBufCodecs.BOOL),
|
| 91 | MobEffectsPredicate.MobEffectInstancePredicate::visible,
|
| 92 | MobEffectsPredicate.MobEffectInstancePredicate::new
|
| 93 | );
|
| 94 | public static final StreamCodec<RegistryFriendlyByteBuf, Map<Holder<MobEffect>, MobEffectsPredicate.MobEffectInstancePredicate>> MAP_STREAM_CODEC = ByteBufCodecs.map(
|
| 95 | HashMap::new, MobEffect.STREAM_CODEC, STREAM_CODEC
|
| 96 | );
|
| 97 |
|
| 98 | public MobEffectInstancePredicate() {
|
| 99 | this(MinMaxBounds.Ints.ANY, MinMaxBounds.Ints.ANY, Optional.empty(), Optional.empty());
|
| 100 | }
|
| 101 |
|
| 102 | public boolean matches(final @Nullable MobEffectInstance instance) {
|
| 103 | if (instance == null) {
|
| 104 | return false;
|
| 105 | } else if (!this.amplifier.matches(instance.getAmplifier())) {
|
| 106 | return false;
|
| 107 | } else if (!this.duration.matches(instance.getDuration())) {
|
| 108 | return false;
|
| 109 | } else {
|
| 110 | return this.ambient.isPresent() && this.ambient.get() != instance.isAmbient()
|
| 111 | ? false
|
| 112 | : !this.visible.isPresent() || this.visible.get() == instance.isVisible();
|
| 113 | }
|
| 114 | }
|
| 115 | }
|
| 116 | }
|
| 117 |
|