如何将 MobEffects 应用于实体命中

How to apply MobEffects to entity hit

我希望当我用我的 Minecraft Forge 1.11.2 自定义弓击中玩家时,它会给击中玩家一个 PotionEffect。

我试过了

public boolean hitEntity(ItemStack stack, EntityLivingBase target, EntityLivingBase attacker) {
        target.addPotionEffect(new PotionEffect(MobEffects.SPEED, Utils.SECS2TICKS(3), 1));
        return true;
        }

它只在我用弓击中而不是射击实体时有效。

我当前的密码是

package revdomain.mod.items;


import revdomain.mod.renders.RenderItems;
import net.minecraft.creativetab.CreativeTabs;
import net.minecraft.item.ItemBow;

public class bowclass extends ItemBow {
    public bowclass(String Name) {
        this.setCreativeTab(CreativeTabs.COMBAT);
        this.setRegistryName(Name);
        this.setUnlocalizedName(Name);
        RenderItems.ITEMS.put(Name, this);
    }

}

我覆盖了createArrow

package revdomain.mod.items;

import revdomain.mod.renders.RenderItems;
import revdomain.mod.Utils;
import net.minecraft.creativetab.CreativeTabs;
import net.minecraft.entity.EntityLivingBase;
import net.minecraft.entity.projectile.EntityArrow;
import net.minecraft.entity.projectile.EntityTippedArrow;
import net.minecraft.init.MobEffects;
import net.minecraft.item.ItemArrow;
import net.minecraft.item.ItemStack;
import net.minecraft.potion.PotionEffect;
import net.minecraft.world.World;

        public class bowclass extends ItemBow {
            public bowclass(String Name) {
                this.setCreativeTab(CreativeTabs.COMBAT);
                this.setRegistryName(Name);
                this.setUnlocalizedName(Name);
                RenderItems.ITEMS.put(Name, this);
            }

        }
        // Bit to add
        public EntityArrow createArrow(World worldIn, ItemStack stack, EntityLivingBase shooter)
        {
            EntityTippedArrow entitytippedarrow = new EntityTippedArrow(worldIn, shooter);
            entitytippedarrow.setPotionEffect(stack);
            entitytippedarrow.addEffect(new PotionEffect(MobEffects.effectname, Utils.SECS2TICKS(10), 1)); // Add custom PotionEffect
            return entitytippedarrow;
        }

要转换报价,这是我的工具

public static int MINS2TICKS(int mins) {
return mins * 1200;
}

public static int SECS2TICKS(int secs) {
return secs * 20;
}

如果有人需要这方面的帮助,请随时发表评论