NMS Entityv1.0.0已發布
NMS 自定義實體
NMS Custom Entity
繼承 NMS Mob 類別並覆寫 registerGoals(),加入自訂 PathfinderGoal 實現 Boss、NPC 或守衛的 AI 行為。
nmsentityaipathfindermobbosspaperweight
更新:2026年4月19日GitHub
NMS Custom Entity
目的
透過繼承 NMS Mob 類別並覆寫 registerGoals() 加入自訂 PathfinderGoal,實現自定義 AI 行為。適用於客製化 Boss、NPC、守衛等場景。
平台需求
- Paper 1.21 – 1.21.3
- Paperweight userdev 1.7.2+
paper-plugin.yml(確保 NMS 早於 Bukkit plugin 載入)
產生的代碼
CustomZombie.java
@SuppressWarnings("UnstableApiUsage")
public class CustomZombie extends Zombie {
@Override
protected void registerGoals() {
this.goalSelector.addGoal(0, new FloatGoal(this));
this.goalSelector.addGoal(1, new FollowClosestPlayerGoal(this, 1.2D, 32.0D));
this.goalSelector.addGoal(2, new MeleeAttackGoal(this, 1.0D, false));
this.targetSelector.addGoal(1,
new NearestAttackableTargetGoal<>(this, Player.class, true));
}
public static AttributeSupplier.Builder createAttributes() {
return Zombie.createAttributes()
.add(Attributes.MAX_HEALTH, 40.0D)
.add(Attributes.ATTACK_DAMAGE, 8.0D);
}
}
FollowClosestPlayerGoal.java(自訂 PathfinderGoal)
public class FollowClosestPlayerGoal extends Goal {
@Override public boolean canUse() { /* 找最近玩家 */ }
@Override public void start() { mob.getNavigation().moveTo(target, speed); }
@Override public void tick() { /* 每 10 tick 重新規劃路徑 */ }
}
執行緒安全
- 實體建立、
addFreshEntity()必須在主執行緒 PathfinderGoal.tick()由 NMS tick 迴圈在主執行緒呼叫