Jump to content
View in the app

A better way to browse. Learn more.

Forge Forums

A full-screen app on your home screen with push notifications, badges and more.

To install this app on iOS and iPadOS
  1. Tap the Share icon in Safari
  2. Scroll the menu and tap Add to Home Screen.
  3. Tap Add in the top-right corner.
To install this app on Android
  1. Tap the 3-dot menu (⋮) in the top-right corner of the browser.
  2. Tap Add to Home screen or Install app.
  3. Confirm by tapping Install.

Featured Replies

Posted

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) {
	super(par1World);
	 this.getNavigator().setBreakDoors(true);
        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()
    {
        super.applyEntityAttributes(); 
        this.getEntityAttribute(SharedMonsterAttributes.followRange).setBaseValue(40.0D);
        this.getEntityAttribute(SharedMonsterAttributes.movementSpeed).setBaseValue(0.2D);
        this.getEntityAttribute(SharedMonsterAttributes.attackDamage).setBaseValue(3.0D);
        this.getEntityAttribute(SharedMonsterAttributes.maxHealth).setBaseValue(15.0D);
    }


   public boolean isAIEnabled()
    {
        return true;
    }


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


            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; 

        }
    	}
    	else{
    		if (HaveBombed){
    			this.tasks.addTask(4, this.aiAvoideEntity);
    			this.tasks.removeTask(this.aiAttackOnCollide);

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

    		    		
    			}
    	
    	
    	
    		}
    
   

    	}
    	
    }
}








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);
this.tasks.addTask(this.aiAvoideEntity);
} else {
this.tasks.addTask(4, this.aiAttackOnCollide);
this.tasks.removeTask(this.aiAvoideEntity);
}

 

 

 

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
}

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

@Override
public void readEntityFromNBT(NBTTagCompound nbttc) {
super.readEntityFromNBT(nbttc);
this.setHasBombed(nbttc.getBoolean("HasBombed"));
}

 

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

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/

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...

Important Information

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

Configure browser push notifications

Chrome (Android)
  1. Tap the lock icon next to the address bar.
  2. Tap Permissions → Notifications.
  3. Adjust your preference.
Chrome (Desktop)
  1. Click the padlock icon in the address bar.
  2. Select Site settings.
  3. Find Notifications and adjust your preference.