Jump to content

[1.12.2] How Do I Remove An Entity's AI Task


Grapefruit

Recommended Posts

So what I'm trying to do is make it so that when a mob gets below a certain percentage of its original health, it flees from the player. I've managed to make it run away, but it's still trying to target the player, and it can still hurt the player if they get to close. Is there any way to change this?

 

Spoiler

package com.doggo.moboverhaul.handlers;

import net.minecraft.entity.ai.EntityAIAvoidEntity;
import net.minecraft.entity.monster.EntitySpider;
import net.minecraft.entity.monster.EntityZombie;
import net.minecraft.entity.player.EntityPlayer;
import net.minecraftforge.event.entity.EntityJoinWorldEvent;
import net.minecraftforge.event.entity.living.LivingHurtEvent;
import net.minecraftforge.fml.common.Mod.EventBusSubscriber;
import net.minecraftforge.fml.common.eventhandler.SubscribeEvent;

@EventBusSubscriber

public class MobAIHandler {
    
    @SubscribeEvent
    public static void onEntitySpawn(EntityJoinWorldEvent event)
    {
        if(event.getEntity() instanceof EntitySpider)
        {
            System.out.println("Spider spawned");
            EntitySpider spider = (EntitySpider)event.getEntity();
            spider.tasks.addTask(5, new EntityAIAvoidEntity<EntityPlayer>(spider, EntityPlayer.class, 3.0F, 1.0D, 1.2D));
        }
    }
    
    @SubscribeEvent
    public static void onEntityHurt(LivingHurtEvent event)
    {
        if(event.getEntity() instanceof EntitySpider)
        {
            System.out.println("Spider hurt");
            EntitySpider spider = (EntitySpider)event.getEntity();
            if(spider.getHealth() <= (spider.getMaxHealth()/2))
            {
                System.out.println("Spider's health is "+spider.getHealth());
                spider.tasks.addTask(1, new EntityAIAvoidEntity<EntityPlayer>(spider, EntityPlayer.class, 6.0F, 1.0D, 1.2D));
                
            }
        }
        
        if(event.getEntity() instanceof EntityZombie)
        {
            System.out.println("Zombie hurt");
            EntityZombie zombie = (EntityZombie)event.getEntity();
            if(zombie.getHealth() <= (zombie.getMaxHealth()/2))
            {
                System.out.println("Zombie's health is "+zombie.getHealth());
                zombie.tasks.addTask(1, new EntityAIAvoidEntity<EntityPlayer>(zombie, EntityPlayer.class, 6.0F, 1.0D, 1.2D));
            }
        }
    }
}
 

 

 

Link to comment
Share on other sites

You can use EntityAITasks#removeTask.

Alternatively you can abuse the mutex bits of tasks. Basically each AI task has an int that represents a collection of bits(so 32 bits total). When the game wants to make the AI run it checks whether it conflicts with another higher priority AI by comparing their mutex bits field. The game performs a bitwise AND operation on them. If the AND returns 0 then both tasks can run concurrently, otherwise the one with the higher priority is the one that will run. The mutex of the targeting AI is 1.

Link to comment
Share on other sites

Thanks. I tried both of them, these were my results, I'm clearly doing something wrong with both of them

 

Spoiler

@SubscribeEvent
    public static void onEntityHurt(LivingHurtEvent event)
    {
        if(event.getEntity() instanceof EntitySpider)
        {
            System.out.println("Spider hurt");
            EntitySpider spider = (EntitySpider)event.getEntity();
            if(spider.getHealth() <= (spider.getMaxHealth()/2))
            {
                System.out.println("Spider's health is "+spider.getHealth());
                spider.tasks.addTask(1, new EntityAIAvoidEntity<EntityPlayer>(spider, EntityPlayer.class, 6.0F, 1.0D, 1.2D));


                //My attempt of setMutexBits (error: "Cannot make a static reference to the non-static method setMutexBits(int) from the type EntityAIBase". This could be because my class is static)
                EntityAINearestAttackableTarget.setMutexBits(1);


                //My attempt of removeTask (error: "The method removeTask(EntityAIBase) in the type EntityAITasks is not applicable for the arguments (EntitySpider)" because spider is an EntitySpider and not an EntityAIBase)
                EntityAITasks.removeTask(spider);
            }
        }

 

 

Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Unfortunately, your content contains terms that we do not allow. Please edit your content to remove the highlighted words below.
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.



×
×
  • Create New...

Important Information

By using this site, you agree to our Terms of Use.