MC Forge 1.19.2 3.新的实体生物:感染者 实体生物这东西我肝了几个星期终于倒腾明白了做生物时如果想做一种新的需要自己新建模型非常麻烦如果是一种原版生物的变种则只需继承它的类和渲染方式即可不过这种生物将属于被继承的生物的一种种类。例如如果你做了一个僵尸继承了原版僵尸类那么覆盖攻击方式时不能添加攻击Zombie的AI只能添加Husk尸壳和Drowned溺尸否则他们会自相残杀。我做的感染者正是一种僵尸。下面给出代码。新建entity/Entities.javapackage dogegg.infected.entity; import dogegg.infected.Main; import net.minecraft.resources.ResourceLocation; import net.minecraft.world.entity.EntityType; import net.minecraft.world.entity.MobCategory; import net.minecraftforge.registries.DeferredRegister; import net.minecraftforge.registries.ForgeRegistries; import net.minecraftforge.registries.RegistryObject; public class Entities { public static final DeferredRegisterEntityType? ENTITY_TYPES DeferredRegister.create(ForgeRegistries.ENTITY_TYPES, Main.MODID); public static final RegistryObjectEntityTypeInfected INFECTED ENTITY_TYPES.register(infected, () - EntityType.Builder.of(Infected::new, MobCategory.MONSTER).sized(0.6f,1.95f).clientTrackingRange(20) .build(new ResourceLocation(Main.MODID, infected).toString())); }本来有多种僵尸都继承了Infected类但此处不便放入过多代码。只需按同样的方式注册而已也可以参考其他原版生物的代码。在该包下新建Infected.javapackage dogegg.infected.entity; import net.minecraft.sounds.SoundEvent; import net.minecraft.sounds.SoundEvents; import net.minecraft.world.damagesource.DamageSource; import net.minecraft.world.effect.MobEffectInstance; import net.minecraft.world.effect.MobEffects; import net.minecraft.world.entity.*; import net.minecraft.world.entity.ai.attributes.AttributeSupplier; import net.minecraft.world.entity.ai.attributes.Attributes; import net.minecraft.world.entity.ai.goal.*; import net.minecraft.world.entity.ai.goal.target.NearestAttackableTargetGoal; import net.minecraft.world.entity.animal.*; import net.minecraft.world.entity.monster.*; import net.minecraft.world.entity.npc.AbstractVillager; import net.minecraft.world.entity.player.Player; import net.minecraft.world.item.ItemStack; import net.minecraft.world.level.Level; public class Infected extends Zombie { public Infected(EntityType? extends Infected p_32889_, Level p_32890_) { super(p_32889_, p_32890_); } protected boolean isSunSensitive() { return false; } //是否会在太阳下燃烧 protected SoundEvent getAmbientSound() { return SoundEvents.HUSK_AMBIENT; } //使用尸壳的声音 protected SoundEvent getHurtSound(DamageSource p_32903_) { return SoundEvents.HUSK_HURT; } //受伤的声音 protected SoundEvent getDeathSound() { return SoundEvents.HUSK_DEATH; } //死亡声 protected SoundEvent getStepSound() { return SoundEvents.HUSK_STEP; } //脚步声 Override //覆盖僵尸的AI protected void registerGoals() { this.goalSelector.addGoal(11,new RandomLookAroundGoal(this)); this.goalSelector.addGoal(10,new FloatGoal(this)); //在水上浮起的AI this.goalSelector.addGoal(6, new MoveThroughVillageGoal(this, 1.0D, true, 4, this::canBreakDoors)); this.goalSelector.addGoal(7, new WaterAvoidingRandomStrollGoal(this, 1.0D)); this.targetSelector.addGoal(2, new NearestAttackableTargetGoal(this, Player.class, true)); this.targetSelector.addGoal(3,new NearestAttackableTargetGoal(this, Skeleton.class,true)); this.targetSelector.addGoal(3, new NearestAttackableTargetGoal(this, AbstractVillager.class, false)); this.targetSelector.addGoal(3, new NearestAttackableTargetGoal(this, IronGolem.class, true)); this.targetSelector.addGoal(3,new NearestAttackableTargetGoal(this, Animal.class,true)); this.targetSelector.addGoal(3,new ZombieAttackGoal(this,1d,false)); //注意做任何有攻击力的生物都必须有继承自MeleeAttackGoal的AI否则NearestAttackableTargetGoal将不会有作用 } public boolean doHurtTarget(Entity p_32892_) { boolean flag super.doHurtTarget(p_32892_); if (flag this.getMainHandItem().isEmpty() p_32892_ instanceof LivingEntity) { float f this.level.getCurrentDifficultyAt(this.blockPosition()).getEffectiveDifficulty(); ((LivingEntity)p_32892_).addEffect(new MobEffectInstance(MobEffects.HUNGER, 140 * (int)f), this); } return flag; } public static AttributeSupplier.Builder createAttributes() { //属性注册 return Monster.createMonsterAttributes(). add(Attributes.FOLLOW_RANGE, 700.0D). add(Attributes.MOVEMENT_SPEED, (double)0.28F). add(Attributes.ATTACK_DAMAGE, 3.0D). add(Attributes.ARMOR, 2.0D).add(Attributes.SPAWN_REINFORCEMENTS_CHANCE). add(Attributes.MAX_HEALTH,21); } protected boolean convertsInWater() { return true; } //是否在水下转换。 protected void doUnderWaterConversion() { //在水下他将转化成新的Infected... this.convertToZombieType(Entities.INFECTED.get()); if (!this.isSilent()) { this.level.levelEvent((Player)null, 1041, this.blockPosition(), 0); } } protected ItemStack getSkull() { return ItemStack.EMPTY; } }添加语言文件en_us.jsonentity.infected.infected: InfectedHuman注意前面要有四个空格。在Items.java中添加刷怪蛋。public static final RegistryObjectItem INFECTED_SPAWN_EGG ITEMS.register(infected_spawn_egg, () - new ForgeSpawnEggItem(Entities.INFECTED, 3093009, 4390912, new Item.Properties().tab(Main.TUTORIAL_TAB)));此时运行RunClient命令使用命令或刷怪蛋刷出一个Infected会看见一个紫红色的僵尸。他会攻击所有动物骷髅铁傀儡以及玩家。assets/textures/中新建entity目录添加infected.png(64*64),建议在原版材质基础上修改以防止UV坐标对应错乱。