Jump to content

[1.7.2] Mob AI


Recommended Posts


I know this is really REALLY simple and i'm ashamed for asking this but


I had the mob working fine with the attacking system, same as a zombie pigman but without the group attacking you


But I felt the code was really repetative so moved it into another Entity class so all mobs that work like that can extend it so i wont have the same code over and over again


So I've extended the class with the exact code from the mob class, but it's not attacking anything after it gets attacked





public abstract class EntityPeacefullUntillAttacked extends EntityDivineRPGMob {

private int angerLevel = 0;

public EntityPeacefullUntillAttacked(World w) {

public void writeEntityToNBT(NBTTagCompound nbt) {
	nbt.setShort("Anger", (short)this.angerLevel);

public void readEntityFromNBT(NBTTagCompound nbt) {
	this.angerLevel = nbt.getShort("Anger");

public Entity findPlayerToAttack() {
	return this.angerLevel == 0 ? null : super.findPlayerToAttack();

public boolean attackEntityFrom(DamageSource src, float dmg) {
        if(this.isEntityInvulnerable()) {
            return false;
        } else {
            Entity entity = src.getEntity();
            if(entity instanceof EntityPlayer)
            return super.attackEntityFrom(src, dmg);

public boolean becomeAngryAt(Entity entity) {
	this.entityToAttack = entity;
	this.angerLevel = 1;
	return true;





And EntityDivineRPGMob if you wanted to see that class aswell



public EntityDivineRPGMob(World par1World) {

public void setMaxHP(double d){getEntityAttribute(SharedMonsterAttributes.maxHealth).setBaseValue(100);}

public void setMoveSpeed(double d){getEntityAttribute(SharedMonsterAttributes.movementSpeed).setBaseValue(0.7);}

public void setAttackDamage(double d){getEntityAttribute(SharedMonsterAttributes.attackDamage).setBaseValue(13);}

public void setFollowRange(double d){getEntityAttribute(SharedMonsterAttributes.followRange).setBaseValue(100);}

public void setKnockbackResistance(double d){getEntityAttribute(SharedMonsterAttributes.knockbackResistance).setBaseValue(100);}

public double getHP(){return getEntityAttribute(SharedMonsterAttributes.maxHealth).getAttributeValue();}

public double getMoveSpeed(){return getEntityAttribute(SharedMonsterAttributes.movementSpeed).getAttributeValue();}

public double getAttackDamage(){return getEntityAttribute(SharedMonsterAttributes.attackDamage).getAttributeValue();}

public double getFollowRange(){return getEntityAttribute(SharedMonsterAttributes.followRange).getAttributeValue();}

public double getKnockbackResistance(){return getEntityAttribute(SharedMonsterAttributes.knockbackResistance).getAttributeValue();}

public abstract String mobName();

protected void addAttackingAI(){
        this.tasks.addTask(5, new EntityAIAttackOnCollide(this, EntityPlayer.class, getMoveSpeed(), false));
	this.targetTasks.addTask(6, new EntityAINearestAttackableTarget(this, EntityPlayer.class, 0, true));


protected void addBasicAI(){
	this.tasks.addTask(0, new EntityAISwimming(this));
        this.tasks.addTask(1, new EntityAIWander(this, getMoveSpeed()));
        this.tasks.addTask(2, new EntityAIWatchClosest(this, EntityPlayer.class, 8.0F));
        this.tasks.addTask(3, new EntityAILookIdle(this));

protected boolean isAIEnabled() {
	return true;

public void onDeath(DamageSource d) {
	if(!worldObj.isRemote && ConfigurationHelper.canShowDeathChat){
		if(d.getSourceOfDamage() != null && d.getSourceOfDamage() instanceof EntityPlayer){
			EntityPlayer p = (EntityPlayer)d.getSourceOfDamage();
			Util.sendMessageToAll(p.getDisplayName() + " has slain a " + mobName() + ".");

			if(d.getEntity() instanceof EntityTwins){
				Util.sendMessageToAll(p.getDisplayName() + " has slain the " + mobName() + ".");




As i stated before, this is really simple and probably me being blind to see the problem, but if anyone can help, that would be great :)

Former developer for DivineRPG, Pixelmon and now the maker of Essence of the Gods

Link to comment
Share on other sites



I would suggest you drill further into here


public Entity findPlayerToAttack() {

return this.angerLevel == 0 ? null : super.findPlayerToAttack();



perhaps you angerLevel is wrong in this method, or alternatively super.findPlayerToAttack() is not what you think (are you extending EntityMob?)


Do you know how to use breakpoints?

If not, and you're using Eclipse, try this link...





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.