MobEffectsPredicate.java

net.minecraft.advancements.criterion.MobEffectsPredicate

信息

  • 全限定名:net.minecraft.advancements.criterion.MobEffectsPredicate
  • 类型:public record
  • 包:net.minecraft.advancements.criterion
  • 源码路径:src/main/java/net/minecraft/advancements/criterion/MobEffectsPredicate.java
  • 起始行号:L16
  • 职责:

    TODO

字段/常量

  • CODEC
    • 类型: Codec<MobEffectsPredicate>
    • 修饰符: public static final
    • 源码定位: L17
    • 说明:

      TODO

内部类/嵌套类型

  • net.minecraft.advancements.criterion.MobEffectsPredicate.Builder

    • 类型: class
    • 修饰符: public static
    • 源码定位: L39
    • 说明:

      TODO

  • net.minecraft.advancements.criterion.MobEffectsPredicate.MobEffectInstancePredicate

    • 类型: record
    • 修饰符: public
    • 源码定位: L61
    • 说明:

      TODO

构造器

方法

下面的方法块按源码顺序生成。

public boolean matches(Entity entity) @ L20

  • 方法名:matches
  • 源码定位:L20
  • 返回类型:boolean
  • 修饰符:public

参数:

  • entity: Entity

说明:

TODO

public boolean matches(LivingEntity entity) @ L24

  • 方法名:matches
  • 源码定位:L24
  • 返回类型:boolean
  • 修饰符:public

参数:

  • entity: LivingEntity

说明:

TODO

public boolean matches(Map<Holder<MobEffect>,MobEffectInstance> effects) @ L28

  • 方法名:matches
  • 源码定位:L28
  • 返回类型:boolean
  • 修饰符:public

参数:

  • effects: Map<Holder,MobEffectInstance>

说明:

TODO

代码

public record MobEffectsPredicate(Map<Holder<MobEffect>, MobEffectsPredicate.MobEffectInstancePredicate> effectMap) {
    public static final Codec<MobEffectsPredicate> CODEC = Codec.unboundedMap(MobEffect.CODEC, MobEffectsPredicate.MobEffectInstancePredicate.CODEC)
        .xmap(MobEffectsPredicate::new, MobEffectsPredicate::effectMap);
 
    public boolean matches(Entity entity) {
        return entity instanceof LivingEntity living && this.matches(living.getActiveEffectsMap());
    }
 
    public boolean matches(LivingEntity entity) {
        return this.matches(entity.getActiveEffectsMap());
    }
 
    public boolean matches(Map<Holder<MobEffect>, MobEffectInstance> effects) {
        for (Entry<Holder<MobEffect>, MobEffectsPredicate.MobEffectInstancePredicate> entry : this.effectMap.entrySet()) {
            MobEffectInstance instance = effects.get(entry.getKey());
            if (!entry.getValue().matches(instance)) {
                return false;
            }
        }
 
        return true;
    }
 
    public static class Builder {
        private final ImmutableMap.Builder<Holder<MobEffect>, MobEffectsPredicate.MobEffectInstancePredicate> effectMap = ImmutableMap.builder();
 
        public static MobEffectsPredicate.Builder effects() {
            return new MobEffectsPredicate.Builder();
        }
 
        public MobEffectsPredicate.Builder and(Holder<MobEffect> effect) {
            this.effectMap.put(effect, new MobEffectsPredicate.MobEffectInstancePredicate());
            return this;
        }
 
        public MobEffectsPredicate.Builder and(Holder<MobEffect> effect, MobEffectsPredicate.MobEffectInstancePredicate predicate) {
            this.effectMap.put(effect, predicate);
            return this;
        }
 
        public Optional<MobEffectsPredicate> build() {
            return Optional.of(new MobEffectsPredicate(this.effectMap.build()));
        }
    }
 
    public record MobEffectInstancePredicate(MinMaxBounds.Ints amplifier, MinMaxBounds.Ints duration, Optional<Boolean> ambient, Optional<Boolean> visible) {
        public static final Codec<MobEffectsPredicate.MobEffectInstancePredicate> CODEC = RecordCodecBuilder.create(
            i -> i.group(
                    MinMaxBounds.Ints.CODEC
                        .optionalFieldOf("amplifier", MinMaxBounds.Ints.ANY)
                        .forGetter(MobEffectsPredicate.MobEffectInstancePredicate::amplifier),
                    MinMaxBounds.Ints.CODEC
                        .optionalFieldOf("duration", MinMaxBounds.Ints.ANY)
                        .forGetter(MobEffectsPredicate.MobEffectInstancePredicate::duration),
                    Codec.BOOL.optionalFieldOf("ambient").forGetter(MobEffectsPredicate.MobEffectInstancePredicate::ambient),
                    Codec.BOOL.optionalFieldOf("visible").forGetter(MobEffectsPredicate.MobEffectInstancePredicate::visible)
                )
                .apply(i, MobEffectsPredicate.MobEffectInstancePredicate::new)
        );
 
        public MobEffectInstancePredicate() {
            this(MinMaxBounds.Ints.ANY, MinMaxBounds.Ints.ANY, Optional.empty(), Optional.empty());
        }
 
        public boolean matches(@Nullable MobEffectInstance instance) {
            if (instance == null) {
                return false;
            } else if (!this.amplifier.matches(instance.getAmplifier())) {
                return false;
            } else if (!this.duration.matches(instance.getDuration())) {
                return false;
            } else {
                return this.ambient.isPresent() && this.ambient.get() != instance.isAmbient()
                    ? false
                    : !this.visible.isPresent() || this.visible.get() == instance.isVisible();
            }
        }
    }
}

引用的其他类