Flandre923
220 words
1 minutes
19 如何获得实体的药水效果

19 如何获得实体的药水效果#

如何给实体添加药水效果,以及获得实体的药水效果,移除实体的药水效果,

package com.mafuyu33.neomafishmod.mixin.effectMixin.antidote;


import java.util.Map;

@Mixin(LivingEntity.class) // mixin 进入LivingEntity
public abstract class LivingEntityMixin extends Entity implements Attackable {

    public LivingEntityMixin(EntityType<?> entityType, Level level) {
        super(entityType, level);
    }

    @Final
    @Shadow private Map<MobEffect, MobEffectInstance> activeEffects;

    @Shadow public abstract boolean hasEffect(Holder<MobEffect> effect);

    @Shadow protected abstract void onEffectRemoved(MobEffectInstance effectInstance);

    @Inject(at=@At("HEAD"),method = "tick")
    private void init(CallbackInfo ci){
        if(this.hasEffect(ModEffects.ANTIDOTE_EFFECT)){ // 判断是否有某个药水效果
            if(!this.level().isClientSide){
                var iterator = this.activeEffects.values().iterator();  // 获得所有的药水效果
                boolean b1;
                for(b1 = false;iterator.hasNext();b1=true){
                    MobEffectInstance next = iterator.next();
                    if(mafishmod$isNegativeEffect(next)){
                        this.onEffectRemoved(next); // 调用onStatusEffectRemoved方法处理当前状态效果
                        iterator.remove(); // 从活跃状态效果列表中移除当前状态效果
                    }
                }
            }
        }
    }


    @Unique
    private boolean mafishmod$isNegativeEffect(MobEffectInstance effect) {
        // 你需要根据实际情况实现此方法,判断效果类型是否为负面效果
        // 例如,根据效果类型进行判断
        return effect.getEffect().value().getCategory()== MobEffectCategory.HARMFUL;
    }
}
19 如何获得实体的药水效果
https://fuwari.vercel.app/posts/minecraft1_21_0/19_如何获得实体的药水效果/
Author
Flandre923
Published at
2024-08-24