Jump to content

Recommended Posts

Posted

THIS PART HAS BEEN SOLVED! SEE BELOW FOR NEW PROBLEMS!

 

I'm trying to make a mob that flies toward you and attacks, but every 5 seconds or so, it'll throw a nullpointerexception saying the entity that it is attacking doesn't have a position.

Here is all the relevant code:

public boolean attackEntityFrom(DamageSource par1DamageSource, float par2)
{
	if (this.isEntityInvulnerable())
	{
		return false;
	}
	else
	{
		Entity entity = par1DamageSource.getEntity();

		if (entity instanceof Entity)
		{
			List list = this.worldObj.getEntitiesWithinAABBExcludingEntity(this, this.boundingBox.expand(16.0D, 16.0D, 16.0D));

			for (int i = 0; i < list.size(); ++i)
			{
				Entity entity1 = (Entity)list.get(i);

				if (entity1 instanceof EntityScarfy)
				{
					EntityScarfy entityscarfy = (EntityScarfy)entity1;
					entityscarfy.becomeAngryAt(entity);
				}
			}
			this.becomeAngryAt(entity);
		}

		return super.attackEntityFrom(par1DamageSource, par2);
	}
}

private void becomeAngryAt(Entity par1Entity)
{
	this.isAngry = true;
	this.entityToAttack = par1Entity;
}
public void updateAITasks()
{
	super.updateAITasks();
	if (this.currentFlightTarget != null && (!this.worldObj.isAirBlock(this.currentFlightTarget.posX, this.currentFlightTarget.posY, this.currentFlightTarget.posZ) || this.currentFlightTarget.posY < 1))
	{
		this.currentFlightTarget = null;
	}

	if (this.currentFlightTarget == null || this.rand.nextInt(30) == 0 || this.currentFlightTarget.getDistanceSquared((int)this.posX, (int)this.posY, (int)this.posZ) < 4.0F)
	{
		if(this.entityToAttack != null)
		{
//HERE IS THE PROBLEM		this.currentFlightTarget.set((int)this.entityToAttack.posX, (int)(this.entityToAttack.posY + 0.5F), (int)this.entityToAttack.posZ);
		}
		else
		{
			this.currentFlightTarget = new ChunkCoordinates((int)this.posX + this.rand.nextInt(7) - this.rand.nextInt(7), (int)this.posY + this.rand.nextInt(5) - 2, (int)this.posZ + this.rand.nextInt(7) - this.rand.nextInt(7));
		}
	}
	double d0 = (double)this.currentFlightTarget.posX - this.posX;
	double d1 = (double)this.currentFlightTarget.posY + 0.5F - this.posY;
	double d2 = (double)this.currentFlightTarget.posZ - this.posZ;
	this.motionX += (Math.signum(d0) * 0.5D - this.motionX) * 0.10000000149011612D;
	this.motionY += (Math.signum(d1) * 0.699999988079071D - this.motionY) * 0.10000000149011612D;
	this.motionZ += (Math.signum(d2) * 0.5D - this.motionZ) * 0.10000000149011612D;
	float f = (float)(Math.atan2(this.motionZ, this.motionX) * 180.0D / Math.PI) - 90.0F;
	float f1 = MathHelper.wrapAngleTo180_float(f - this.rotationYaw);
	this.moveForward = 0.5F;
	this.rotationYaw += f1;
}

It doesn't always throw the null pointer, so I don't know why it doesn't work all the time.

Posted

	if (this.currentFlightTarget == null || this.rand.nextInt(30) == 0 || this.currentFlightTarget.getDistanceSquared((int)this.posX, (int)this.posY, (int)this.posZ) < 4.0F)
	{
		if(this.entityToAttack != null)
		{
//HERE IS THE PROBLEM		this.currentFlightTarget.set((int)this.entityToAttack.posX, (int)(this.entityToAttack.posY + 0.5F), (int)this.entityToAttack.posZ);
		}
		else
		{
			this.currentFlightTarget = new ChunkCoordinates((int)this.posX + this.rand.nextInt(7) - this.rand.nextInt(7), (int)this.posY + this.rand.nextInt(5) - 2, (int)this.posZ + this.rand.nextInt(7) - this.rand.nextInt(7));
		}
	}

 

so, you indicated the problem line

 

the reason it is sometimes null, is because you are explicitly executing that branch when the target IS null with the first bit...so you need to check if it executed that branch because of null, and un-nullifiy it (give it a reference to SOMETHING), such as:

 

 

	if (this.currentFlightTarget == null || this.rand.nextInt(30) == 0 || this.currentFlightTarget.getDistanceSquared((int)this.posX, (int)this.posY, (int)this.posZ) < 4.0F)
	{
		if(this.entityToAttack != null)
		{
                             if(this.currentFlightTarget==null)
                             {
                                          this.currentFlightTarget = new ChunkCoordinantes(0,0,0);
                             }
                     this.currentFlightTarget.set((int)this.entityToAttack.posX, (int)(this.entityToAttack.posY + 0.5F), (int)this.entityToAttack.posZ);
		}
		else
		{
			this.currentFlightTarget = new ChunkCoordinates((int)this.posX + this.rand.nextInt(7) - this.rand.nextInt(7), (int)this.posY + this.rand.nextInt(5) - 2, (int)this.posZ + this.rand.nextInt(7) - this.rand.nextInt(7));
		}
	}

 

Posted

Thanks tons! It works beautifully! Maybe you could help me with one more thing... or 2, but they're both small.

When the mob didn't have flying, it was easily able to attack me, but now with the flying I never take damage.

Also the mob flies wayy to fast. :(

Posted

Obviously the motion parameters deal with how fast the entity is.

 

And how the entity attacks is your choice.

 

The whole point of me asking is because I've tried and can't get the flying speed to change.

 

And yes, I know than I control how it attacks. I want it to attack by touching you just like zombies do. But that is t working now that the mob flies.

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.

Announcements



×
×
  • Create New...

Important Information

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