注:1.16.5的实体教程和本文基本类似,只是一些类中的函数名称有所不同。
我们今天在1.18.1的模组中添加一个生物实体
1.新建entity文件夹 -> 文件夹中新建一个model文件夹,一个render文件夹,一个我们的生物类(以EntityRe8Dimi.java为例)

我们本次要制作一个boss类怪物,类似于凋零,具有boss血条
EntityRe8Dimi.java
package com.joy187.re8joymod.entity;
import java.util.Random;
import java.util.function.Predicate;
import javax.annotation.Nullable;
import com.joy187.re8joymod.init.EntityInit;
import com.mojang.blaze3d.vertex.PoseStack.Pose;
import net.minecraft.client.renderer.EffectInstance;
import net.minecraft.nbt.CompoundTag;
import net.minecraft.network.chat.Component;
import net.minecraft.network.syncher.EntityDataAccessor;
import net.minecraft.network.syncher.EntityDataSerializers;
import net.minecraft.network.syncher.SynchedEntityData;
import net.minecraft.server.level.ServerBossEvent;
import net.minecraft.server.level.ServerPlayer;
import net.minecraft.sounds.SoundEvent;
import net.minecraft.sounds.SoundEvents;
import net.minecraft.world.BossEvent;
import net.minecraft.world.damagesource.DamageSource;
import net.minecraft.world.effect.MobEffectInstance;
import net.minecraft.world.effect.MobEffects;
import net.minecraft.world.entity.Entity;
import net.minecraft.world.entity.EntityDimensions;
import net.minecraft.world.entity.EntityType;
import net.minecraft.world.entity.LivingEntity;
import net.minecraft.world.entity.MobType;
import net.minecraft.world.entity.ai.attributes.AttributeSupplier;
import net.minecraft.world.entity.ai.attributes.Attributes;
import net.minecraft.world.entity.ai.goal.LookAtPlayerGoal;
import net.minecraft.world.entity.ai.goal.RandomLookAroundGoal;
import net.minecraft.world.entity.ai.goal.WaterAvoidingRandomStrollGoal;
import net.minecraft.world.entity.ai.goal.target.HurtByTargetGoal;
import net.minecraft.world.entity.ai.goal.target.NearestAttackableTargetGoal;
import net.minecraft.world.entity.animal.IronGolem;
import net.minecraft.world.entity.boss.wither.WitherBoss;
import net.minecraft.world.entity.monster.Monster;
import net.minecraft.world.entity.monster.Ravager;
import net.minecraft.world.entity.monster.ZombifiedPiglin;
import net.minecraft.world.entity.npc.AbstractVillager;
import net.minecraft.world.entity.player.Player;
import net.minecraft.world.level.Level;
import net.minecraft.world.level.pathfinder.BlockPathTypes;
import net.minecraftforge.client.event.RenderGameOverlayEvent.BossInfo;
//我们的boss继承自劫掠兽
public class EntityRe8Dimi extends Ravager {
//boss血条信息
private final ServerBossEvent bossInfo = (ServerBossEvent)(new ServerBossEvent(this.getDisplayName(), BossEvent.BossBarColor.PURPLE, BossEvent.BossBarOverlay.PROGRESS)).setDarkenScreen(true);
private static final EntityDataAccessor<Integer> DATA_ID_INV = SynchedEntityData.defineId(EntityRe8Dimi.class, EntityDataSerializers.INT);
//我们的boss不攻击类型为不死亡灵类的生物(凋零骷髅一类)
private static final Predicate<LivingEntity> LIVING_ENTITY_SELECTOR = (p_31504_) -> {
return p_31504_.getMobType() != MobType.UNDEAD && p_31504_.attackable();
};
public EntityRe8Dimi(EntityType<? extends Ravager> type, Level worldIn) {
super(type, worldIn);
this.xpReward = 150;
this.isNonBoss();
this.isSunSensitive();
this.setPathfindingMalus(BlockPathTypes.LAVA, 8.0F);
}
//是否怕阳光
protected boolean isSunSensitive() {
return true;
}
public void aiStep() {
super.aiStep();
}
//设置生物属性(最大血量、移动速度、攻击力、护甲值等)
public static AttributeSupplier.Builder prepareAttributes() {
return Monster.createMonsterAttributes().add(Attributes.MAX_HEALTH, 120.0D).
add(Attributes.MOVEMENT_SPEED, 0.28D).
add(Attributes.FOLLOW_RANGE, 48.0D).
add(Attributes.ARMOR, 8.0D).
add(Attributes.ARMOR_TOUGHNESS, 10.0D).
add(Attributes.KNOCKBACK_RESISTANCE, 0.85D).
add(Attributes.ATTACK_DAMAGE, 9.0D);
}
//给生物设置攻击行为
@Override
protected void registerGoals() {
super.registerGoals();
this.goalSelector.addGoal(1, new RandomLookAroundGoal(this));
this.goalSelector.addGoal(2, new LookAtPlayerGoal(this, Player.class, 8.0F));
this.targetSelector.addGoal(1, new NearestAttackableTargetGoal<>(this, LivingEntity.class, 0, false, false, LIVING_ENTITY_SELECTOR));
this.addBehaviourGoals();
}
//给生物添加其行为
protected void addBehaviourGoals() {
this.goalSelector.addGoal(7, new WaterAvoidingRandomStrollGoal(this, 1.0D));
this.targetSelector.addGoal(1, (new HurtByTargetGoal(this)).setAlertOthers(ZombifiedPiglin.class));
this.targetSelector.addGoal(2, new NearestAttackableTargetGoal<>(this, Player.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(4, new NearestAttackableTargetGoal<>(this, EntityEthan.class, true));
}
//生物攻击其他生物
public boolean doHurtTarget(Entity entityIn) {
if(!super.doHurtTarget(entityIn))
{
return false;
}
else{
//如果攻击的对象是生物,就给这个对象一个凋零效果,持续1s(20ticks=1s)
if(entityIn instanceof LivingEntity)
{
((LivingEntity)entityIn).addEffect(new MobEffectInstance(MobEffects.WITHER, 20,0,true,true));
}
return true;
}
}
//生物死亡音效
@Override
protected SoundEvent getDeathSound() {
// Random ran = new Random();
// int co = ran.nextInt(3);
// if(co==0)
// {
// return SoundInit.ENTITY_DIMI_DEATH1.get();
// }
// else if(co==1) return SoundInit.ENTITY_DIMI_DEATH2.get();
return SoundEvents.BEE_DEATH;
}
// 生物在玩家身边的音效
protected SoundEvent

本文详细指导如何在1.18.1的模组中创建一个类似凋零的BOSS生物,包括实体类的搭建、血条设置、模型制作、渲染注册、属性配置及游戏行为。涉及了生物的攻击行为、伤害音效和视觉效果。
最低0.47元/天 解锁文章
2890

被折叠的 条评论
为什么被折叠?



