Jump to content

[1.7.2]Changing AI using a boolean


Recommended Posts

hello there i am working on a mob that would bomb the player than run away



but it is not working ,its not attacking or running away


here is the class

package thrizzo.minibots.entities;

import net.minecraft.entity.SharedMonsterAttributes;
import net.minecraft.entity.ai.EntityAIAttackOnCollide;
import net.minecraft.entity.ai.EntityAIAvoidEntity;
import net.minecraft.entity.ai.EntityAIHurtByTarget;
import net.minecraft.entity.ai.EntityAILookIdle;
import net.minecraft.entity.ai.EntityAIMoveTowardsRestriction;
import net.minecraft.entity.ai.EntityAINearestAttackableTarget;
import net.minecraft.entity.ai.EntityAISwimming;
import net.minecraft.entity.ai.EntityAIWander;
import net.minecraft.entity.ai.EntityAIWatchClosest;
import net.minecraft.entity.monster.EntityMob;
import net.minecraft.entity.player.EntityPlayer;
import net.minecraft.world.World;

public class EntityMiniBomber extends EntityMob{

private boolean HaveBombed ;

private EntityAIAttackOnCollide aiAttackOnCollide = new EntityAIAttackOnCollide(this, 2.0D, false);
private EntityAIAvoidEntity aiAvoideEntity = new EntityAIAvoidEntity(this, EntityPlayer.class, 16.0F, 0.8D, 1.33D);

//this.tasks.addTask(4, new EntityAIAvoidEntity(this, EntityPlayer.class, 16.0F, 0.8D, 1.33D));

public EntityMiniBomber(World par1World) {
        this.tasks.addTask(0, new EntityAISwimming(this));
        this.tasks.addTask(2, new EntityAIAttackOnCollide(this, EntityPlayer.class, 1.0D, false));
        this.tasks.addTask(5, new EntityAIMoveTowardsRestriction(this, 1.0D));
        this.tasks.addTask(7, new EntityAIWander(this, 1.0D));
        this.tasks.addTask(8, new EntityAIWatchClosest(this, EntityPlayer.class, 8.0F));
        this.tasks.addTask(8, new EntityAILookIdle(this));
        this.targetTasks.addTask(1, new EntityAIHurtByTarget(this, true));
        this.targetTasks.addTask(2, new EntityAINearestAttackableTarget(this, EntityPlayer.class, 0, true));

 protected void applyEntityAttributes()

   public boolean isAIEnabled()
        return true;

    protected void updateAITasks(){
    	if (!HaveBombed){
		this.tasks.addTask(4, this.aiAttackOnCollide);

            boolean flag = this.worldObj.getGameRules().getGameRuleBooleanValue("mobGriefing");

    		if (this.worldObj.getClosestPlayerToEntity(this, 2.0D) != null){
    			this.worldObj.createExplosion(this, posX, posY , posZ, 2, flag);
    			this.HaveBombed = true; 

    		if (HaveBombed){
    			this.tasks.addTask(4, this.aiAvoideEntity);

    			if (this.worldObj.getClosestPlayerToEntity(this, 20.0D) == null){
        			this.HaveBombed = false; 



Link to comment
Share on other sites

1. does your ai even work?

2. remove all other ai's you have set in your mob's constructor eg wander ai. this most definitely will cause a conflict.

3. you add a task when the entity has bombed with the id being a value of 4, but before you remove the previous one, there is already a task with that id present.




//remove the task before adding a new one, especially when the ai has the same id!
private void switchTasks(boolean flag);
if (flag) 		
this.tasks.removeTask(4, this.aiAttackOnCollide);
} else {
this.tasks.addTask(4, this.aiAttackOnCollide);




I haven't used updateAITasks, I would rather set hasBombed to true in the ai, save it in a nbttagcompound and make the ai run if hasBombed is false.


private boolean _hasBombed;

public void setHasBombed(boolean flag) {
_hasBombed = flag;
this.switchTasks(flag); //might be usefull if it has to run instantly after it has bombed.

public boolean hasBombed() {
return _hasBombed

public void writeEntityToNBT(NBTTagCompound nbttc) {
nbttc.setBoolean("HasBombed", this.hasBombed());

public void readEntityFromNBT(NBTTagCompound nbttc) {


you can now just call ((EntityMiniBomber)entity).setHasBombed(true) in your ai class.

Link to comment
Share on other sites

You're not really approaching the problem the right way.  You normally don't add and remove the AI task, rather you always have the task on the list but you use the shouldExecute() method in the AI class to determine whether it should in fact execute.  So the hasBombed would be checked in the shouldExecute().


Also, the priority and the mutexBits() settings are important to avoid conflicting AI.  I posted this elsewhere, but the mutexBits() are set in each AI to help indicate which other AI tasks should not execute concurrently.  It turns out that most movement AI is mutexBits set to 1, so I would set the same to yours.  Otherwise the entity may choose to wander in the middle of running away!


Next, the task number indicates the priority (sort of) with lower numbers going first.  So I would put yours at a fairly high priority (low number) on the list to ensure it is checked early and then once it starts executing it will prevent lower priority movement AI from executing.


Anway, the main point is if you have a Boolean controlling the AI, you should use it in the shouldExecute() method not to add the AI to the list.

Check out my tutorials here: http://jabelarminecraft.blogspot.com/

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.

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.