Skip to content

How to give a mob a new AI goal in a Minecraft mod

A mob’s behaviour is a list of goals. A Goal has canUse (should it start), tick (what it does each tick) and flags for what it needs (on 1.12.2 an EntityAIBase with shouldExecute and updateTask). The mob’s goalSelector.addGoal(priority, goal) adds one (tasks.addTask on 1.12.2); a lower priority number wins. To change a vanilla mob you add the goal when the entity loads: ServerEntityEvents.ENTITY_LOAD on Fabric, EntityJoinLevelEvent on Forge and NeoForge (EntityJoinWorldEvent before 1.19). This example heals a calm mob. For your own mob, put addGoal in its registerGoals.

Works on Minecraft 1.20.1 to 26.3 on NeoForge; Minecraft 1.16.5 to 26.3 on Fabric; Minecraft 1.16.5 to 1.20.1 on Forge; Minecraft 1.12.2 on Forge. Each example was compiled against its loader and version before it shipped in Linen.

  1. Write a class that extends Goal and fill in canUse and tick.
  2. Pick a priority: 0 runs before 5.
  3. Add it to the mob’s goalSelector when the entity loads, on the server only.
  4. Hurt the mob and wait to watch it heal.

The example uses these values. Change them in the code.

Setting In the example What it does
mob ZOMBIE The vanilla mob that gets the goal.
seconds 2 Seconds between heals.

The example mod is examplemod in the package com.example.examplemod. Use your own mod id and package.

Fabric 1.18 to 1.21.1, Fabric 1.21.2 to 26.1.2

Section titled “Fabric 1.18 to 1.21.1, Fabric 1.21.2 to 26.1.2”

Uses:

  • Goal.canUse
  • Goal.tick
  • Mob.goalSelector
  • GoalSelector.addGoal
  • ServerEntityEvents.ENTITY_LOAD

Fabric: goalSelector is protected in Mob, so this adds one line to your access widener to reach it. NeoForge already makes it public.

src/main/java/com/example/examplemod/event/CalmHealGoals.java

package com.example.examplemod.event;
import net.fabricmc.fabric.api.event.lifecycle.v1.ServerEntityEvents;
import net.minecraft.world.entity.EntityType;
import net.minecraft.world.entity.PathfinderMob;
import net.minecraft.world.entity.ai.goal.Goal;
// Calm Heal: every ZOMBIE that loads gets a goal that heals it 1 health every 2 seconds while it has no target.
public final class CalmHealGoals {
private CalmHealGoals() {
}
// Called from the mod's onInitialize.
public static void register() {
ServerEntityEvents.ENTITY_LOAD.register((entity, world) -> {
if (entity.getType() == EntityType.ZOMBIE && entity instanceof PathfinderMob mob) {
mob.goalSelector.addGoal(3, new CalmHeal(mob));
}
});
}
private static final class CalmHeal extends Goal {
private final PathfinderMob mob;
private int timer;
CalmHeal(PathfinderMob mob) {
this.mob = mob;
}
@Override
public boolean canUse() {
return mob.getTarget() == null && mob.getHealth() < mob.getMaxHealth();
}
@Override
public boolean requiresUpdateEveryTick() {
return true;
}
@Override
public void tick() {
if (++timer % (2 * 20) == 0) {
mob.heal(1.0F);
}
}
}
}

In your main mod class, inside onInitialize:

CalmHealGoals.register();

Add this line to your access widener file:

accessible field net/minecraft/world/entity/Mob goalSelector Lnet/minecraft/world/entity/ai/goal/GoalSelector;

NeoForge 1.20.2 to 1.21.1, NeoForge 1.21.2 to 26.1.2

Section titled “NeoForge 1.20.2 to 1.21.1, NeoForge 1.21.2 to 26.1.2”

Uses:

  • EntityJoinLevelEvent
  • Goal.canUse
  • Goal.tick
  • Mob.goalSelector
  • GoalSelector.addGoal

src/main/java/com/example/examplemod/event/CalmHealGoals.java

package com.example.examplemod.event;
import net.minecraft.world.entity.EntityType;
import net.minecraft.world.entity.PathfinderMob;
import net.minecraft.world.entity.ai.goal.Goal;
import net.neoforged.neoforge.common.NeoForge;
import net.neoforged.neoforge.event.entity.EntityJoinLevelEvent;
// Calm Heal: every ZOMBIE that joins a level gets a goal that heals it 1 health every 2 seconds while it has no target.
public final class CalmHealGoals {
private CalmHealGoals() {
}
// Called from the mod's constructor.
public static void register() {
NeoForge.EVENT_BUS.addListener(CalmHealGoals::onJoin);
}
private static void onJoin(EntityJoinLevelEvent event) {
if (!event.getLevel().isClientSide() && event.getEntity().getType() == EntityType.ZOMBIE && event.getEntity() instanceof PathfinderMob mob) {
mob.goalSelector.addGoal(3, new CalmHeal(mob));
}
}
private static final class CalmHeal extends Goal {
private final PathfinderMob mob;
private int timer;
CalmHeal(PathfinderMob mob) {
this.mob = mob;
}
@Override
public boolean canUse() {
return mob.getTarget() == null && mob.getHealth() < mob.getMaxHealth();
}
@Override
public boolean requiresUpdateEveryTick() {
return true;
}
@Override
public void tick() {
if (++timer % (2 * 20) == 0) {
mob.heal(1.0F);
}
}
}
}

In your main mod class, inside <init>:

CalmHealGoals.register();

Uses:

  • EntityJoinLevelEvent
  • Goal.canUse
  • Goal.tick
  • Mob.goalSelector
  • GoalSelector.addGoal

src/main/java/com/example/examplemod/event/CalmHealGoals.java

package com.example.examplemod.event;
import net.minecraft.world.entity.EntityTypes;
import net.minecraft.world.entity.PathfinderMob;
import net.minecraft.world.entity.ai.goal.Goal;
import net.neoforged.neoforge.common.NeoForge;
import net.neoforged.neoforge.event.entity.EntityJoinLevelEvent;
// Calm Heal: every ZOMBIE that joins a level gets a goal that heals it 1 health every 2 seconds while it has no target.
public final class CalmHealGoals {
private CalmHealGoals() {
}
// Called from the mod's constructor.
public static void register() {
NeoForge.EVENT_BUS.addListener(CalmHealGoals::onJoin);
}
private static void onJoin(EntityJoinLevelEvent event) {
if (!event.getLevel().isClientSide() && event.getEntity().getType() == EntityTypes.ZOMBIE && event.getEntity() instanceof PathfinderMob mob) {
mob.goalSelector.addGoal(3, new CalmHeal(mob));
}
}
private static final class CalmHeal extends Goal {
private final PathfinderMob mob;
private int timer;
CalmHeal(PathfinderMob mob) {
this.mob = mob;
}
@Override
public boolean canUse() {
return mob.getTarget() == null && mob.getHealth() < mob.getMaxHealth();
}
@Override
public boolean requiresUpdateEveryTick() {
return true;
}
@Override
public void tick() {
if (++timer % (2 * 20) == 0) {
mob.heal(1.0F);
}
}
}
}

In your main mod class, inside <init>:

CalmHealGoals.register();

Uses:

  • Goal.canUse
  • Goal.tick
  • Mob.goalSelector
  • GoalSelector.addGoal
  • ServerEntityEvents.ENTITY_LOAD

Fabric: goalSelector is protected in Mob, so this adds one line to your access widener to reach it. NeoForge already makes it public.

src/main/java/com/example/examplemod/event/CalmHealGoals.java

package com.example.examplemod.event;
import net.fabricmc.fabric.api.event.lifecycle.v1.ServerEntityEvents;
import net.minecraft.world.entity.EntityTypes;
import net.minecraft.world.entity.PathfinderMob;
import net.minecraft.world.entity.ai.goal.Goal;
// Calm Heal: every ZOMBIE that loads gets a goal that heals it 1 health every 2 seconds while it has no target.
public final class CalmHealGoals {
private CalmHealGoals() {
}
// Called from the mod's onInitialize.
public static void register() {
ServerEntityEvents.ENTITY_LOAD.register((entity, world) -> {
if (entity.getType() == EntityTypes.ZOMBIE && entity instanceof PathfinderMob mob) {
mob.goalSelector.addGoal(3, new CalmHeal(mob));
}
});
}
private static final class CalmHeal extends Goal {
private final PathfinderMob mob;
private int timer;
CalmHeal(PathfinderMob mob) {
this.mob = mob;
}
@Override
public boolean canUse() {
return mob.getTarget() == null && mob.getHealth() < mob.getMaxHealth();
}
@Override
public boolean requiresUpdateEveryTick() {
return true;
}
@Override
public void tick() {
if (++timer % (2 * 20) == 0) {
mob.heal(1.0F);
}
}
}
}

In your main mod class, inside onInitialize:

CalmHealGoals.register();

Add this line to your access widener file:

accessible field net/minecraft/world/entity/Mob goalSelector Lnet/minecraft/world/entity/ai/goal/GoalSelector;

Uses:

  • EntityJoinLevelEvent
  • Goal.canUse
  • Goal.tick
  • Mob.goalSelector
  • GoalSelector.addGoal

src/main/java/com/example/examplemod/event/CalmHealGoals.java

package com.example.examplemod.event;
import net.minecraft.world.entity.EntityType;
import net.minecraft.world.entity.PathfinderMob;
import net.minecraft.world.entity.ai.goal.Goal;
import net.minecraftforge.common.MinecraftForge;
import net.minecraftforge.event.entity.EntityJoinWorldEvent;
// Calm Heal: every ZOMBIE that joins a level gets a goal that heals it 1 health every 2 seconds while it has no target.
public final class CalmHealGoals {
private CalmHealGoals() {
}
// Called from the mod's constructor.
public static void register() {
MinecraftForge.EVENT_BUS.addListener(CalmHealGoals::onJoin);
}
private static void onJoin(EntityJoinWorldEvent event) {
if (!event.getWorld().isClientSide() && event.getEntity().getType() == EntityType.ZOMBIE && event.getEntity() instanceof PathfinderMob mob) {
mob.goalSelector.addGoal(3, new CalmHeal(mob));
}
}
private static final class CalmHeal extends Goal {
private final PathfinderMob mob;
private int timer;
CalmHeal(PathfinderMob mob) {
this.mob = mob;
}
@Override
public boolean canUse() {
return mob.getTarget() == null && mob.getHealth() < mob.getMaxHealth();
}
@Override
public void tick() {
if (++timer % (2 * 20) == 0) {
mob.heal(1.0F);
}
}
}
}

In your main mod class, inside <init>:

CalmHealGoals.register();

Uses:

  • EntityJoinLevelEvent
  • Goal.canUse
  • Goal.tick
  • Mob.goalSelector
  • GoalSelector.addGoal

src/main/java/com/example/examplemod/event/CalmHealGoals.java

package com.example.examplemod.event;
import net.minecraft.world.entity.EntityType;
import net.minecraft.world.entity.PathfinderMob;
import net.minecraft.world.entity.ai.goal.Goal;
import net.minecraftforge.common.MinecraftForge;
import net.minecraftforge.event.entity.EntityJoinWorldEvent;
// Calm Heal: every ZOMBIE that joins a level gets a goal that heals it 1 health every 2 seconds while it has no target.
public final class CalmHealGoals {
private CalmHealGoals() {
}
// Called from the mod's constructor.
public static void register() {
MinecraftForge.EVENT_BUS.addListener(CalmHealGoals::onJoin);
}
private static void onJoin(EntityJoinWorldEvent event) {
if (!event.getWorld().isClientSide() && event.getEntity().getType() == EntityType.ZOMBIE && event.getEntity() instanceof PathfinderMob mob) {
mob.goalSelector.addGoal(3, new CalmHeal(mob));
}
}
private static final class CalmHeal extends Goal {
private final PathfinderMob mob;
private int timer;
CalmHeal(PathfinderMob mob) {
this.mob = mob;
}
@Override
public boolean canUse() {
return mob.getTarget() == null && mob.getHealth() < mob.getMaxHealth();
}
@Override
public boolean requiresUpdateEveryTick() {
return true;
}
@Override
public void tick() {
if (++timer % (2 * 20) == 0) {
mob.heal(1.0F);
}
}
}
}

In your main mod class, inside <init>:

CalmHealGoals.register();

Uses:

  • EntityJoinLevelEvent
  • Goal.canUse
  • Goal.tick
  • Mob.goalSelector
  • GoalSelector.addGoal

src/main/java/com/example/examplemod/event/CalmHealGoals.java

package com.example.examplemod.event;
import net.minecraft.world.entity.EntityType;
import net.minecraft.world.entity.PathfinderMob;
import net.minecraft.world.entity.ai.goal.Goal;
import net.minecraftforge.common.MinecraftForge;
import net.minecraftforge.event.entity.EntityJoinLevelEvent;
// Calm Heal: every ZOMBIE that joins a level gets a goal that heals it 1 health every 2 seconds while it has no target.
public final class CalmHealGoals {
private CalmHealGoals() {
}
// Called from the mod's constructor.
public static void register() {
MinecraftForge.EVENT_BUS.addListener(CalmHealGoals::onJoin);
}
private static void onJoin(EntityJoinLevelEvent event) {
if (!event.getLevel().isClientSide() && event.getEntity().getType() == EntityType.ZOMBIE && event.getEntity() instanceof PathfinderMob mob) {
mob.goalSelector.addGoal(3, new CalmHeal(mob));
}
}
private static final class CalmHeal extends Goal {
private final PathfinderMob mob;
private int timer;
CalmHeal(PathfinderMob mob) {
this.mob = mob;
}
@Override
public boolean canUse() {
return mob.getTarget() == null && mob.getHealth() < mob.getMaxHealth();
}
@Override
public boolean requiresUpdateEveryTick() {
return true;
}
@Override
public void tick() {
if (++timer % (2 * 20) == 0) {
mob.heal(1.0F);
}
}
}
}

In your main mod class, inside <init>:

CalmHealGoals.register();

Uses:

  • Goal.canUse
  • Goal.tick
  • Mob.goalSelector
  • GoalSelector.addGoal
  • ServerEntityEvents.ENTITY_LOAD

src/main/java/com/example/examplemod/event/CalmHealGoals.java

package com.example.examplemod.event;
import net.fabricmc.fabric.api.event.lifecycle.v1.ServerEntityEvents;
import net.minecraft.world.entity.EntityType;
import net.minecraft.world.entity.PathfinderMob;
import net.minecraft.world.entity.ai.goal.Goal;
// Calm Heal: every ZOMBIE that loads gets a goal that heals it 1 health every 2 seconds while it has no target.
public final class CalmHealGoals {
private CalmHealGoals() {
}
// Called from the mod's onInitialize.
public static void register() {
ServerEntityEvents.ENTITY_LOAD.register((entity, world) -> {
if (entity.getType() == EntityType.ZOMBIE && entity instanceof PathfinderMob) {
PathfinderMob mob = (PathfinderMob) entity;
mob.goalSelector.addGoal(3, new CalmHeal(mob));
}
});
}
private static final class CalmHeal extends Goal {
private final PathfinderMob mob;
private int timer;
CalmHeal(PathfinderMob mob) {
this.mob = mob;
}
@Override
public boolean canUse() {
return mob.getTarget() == null && mob.getHealth() < mob.getMaxHealth();
}
@Override
public void tick() {
if (++timer % (2 * 20) == 0) {
mob.heal(1.0F);
}
}
}
}

In your main mod class, inside onInitialize:

CalmHealGoals.register();

Add this line to your access widener file:

accessible field net/minecraft/world/entity/Mob goalSelector Lnet/minecraft/world/entity/ai/goal/GoalSelector;

Uses:

  • EntityJoinWorldEvent
  • Goal.canUse
  • Goal.tick
  • MobEntity.goalSelector
  • GoalSelector.addGoal

src/main/java/com/example/examplemod/event/CalmHealGoals.java

package com.example.examplemod.event;
import net.minecraft.entity.EntityType;
import net.minecraft.entity.CreatureEntity;
import net.minecraft.entity.ai.goal.Goal;
import net.minecraftforge.common.MinecraftForge;
import net.minecraftforge.event.entity.EntityJoinWorldEvent;
// Calm Heal: every ZOMBIE that joins a level gets a goal that heals it 1 health every 2 seconds while it has no target.
public final class CalmHealGoals {
private CalmHealGoals() {
}
// Called from the mod's constructor.
public static void register() {
MinecraftForge.EVENT_BUS.addListener(CalmHealGoals::onJoin);
}
private static void onJoin(EntityJoinWorldEvent event) {
if (!event.getWorld().isClientSide() && event.getEntity().getType() == EntityType.ZOMBIE && event.getEntity() instanceof CreatureEntity) {
CreatureEntity mob = (CreatureEntity) event.getEntity();
mob.goalSelector.addGoal(3, new CalmHeal(mob));
}
}
private static final class CalmHeal extends Goal {
private final CreatureEntity mob;
private int timer;
CalmHeal(CreatureEntity mob) {
this.mob = mob;
}
@Override
public boolean canUse() {
return mob.getTarget() == null && mob.getHealth() < mob.getMaxHealth();
}
@Override
public void tick() {
if (++timer % (2 * 20) == 0) {
mob.heal(1.0F);
}
}
}
}

In your main mod class, inside <init>:

CalmHealGoals.register();

Uses:

  • EntityJoinWorldEvent
  • Goal.canUse
  • Goal.tick
  • EntityLiving.goalSelector
  • GoalSelector.addGoal

src/main/java/com/example/examplemod/event/CalmHealGoals.java

package com.example.examplemod.event;
import com.example.examplemod.ExampleMod;
import net.minecraft.entity.EntityCreature;
import net.minecraft.entity.EntityList;
import net.minecraft.entity.ai.EntityAIBase;
import net.minecraft.util.ResourceLocation;
import net.minecraftforge.event.entity.EntityJoinWorldEvent;
import net.minecraftforge.fml.common.Mod;
import net.minecraftforge.fml.common.eventhandler.SubscribeEvent;
// Calm Heal: every ZOMBIE that joins a world gets a goal that heals it 1 health every 2 seconds while it has no target.
@Mod.EventBusSubscriber(modid = ExampleMod.MOD_ID)
public final class CalmHealGoals {
private CalmHealGoals() {
}
@SubscribeEvent
public static void onJoin(EntityJoinWorldEvent event) {
if (!event.getWorld().isRemote && event.getEntity() instanceof EntityCreature
&& new ResourceLocation("minecraft", "ZOMBIE".toLowerCase()).equals(EntityList.getKey(event.getEntity()))) {
EntityCreature mob = (EntityCreature) event.getEntity();
mob.tasks.addTask(3, new CalmHeal(mob));
}
}
private static final class CalmHeal extends EntityAIBase {
private final EntityCreature mob;
private int timer;
CalmHeal(EntityCreature mob) {
this.mob = mob;
}
@Override
public boolean shouldExecute() {
return mob.getAttackTarget() == null && mob.getHealth() < mob.getMaxHealth();
}
@Override
public void updateTask() {
if (++timer % (2 * 20) == 0) {
mob.heal(1.0F);
}
}
}
}

Linen’s How-tos library has this page too, with the example for your own mod’s loader and Minecraft version. Hover a name in your code and Linen points to the how-tos that explain it. Both are free. Download Linen.