Jump to content

Recommended Posts

Posted

I am making my own projectile, and I took quite some code from the EntityArrow class. However, my projectile is unable to inflict damage.

I've been staring and testing my code, and for some reason attackEntityFrom() always returns false.

Now I think I need to solve one of these 3 problems:

    1. Within attackEntityFrom() something with a ForgeHook is being called at the start of the function, and I need to do something with that.

    2. I accidentally removed a function I shouldn't have

    3. I still need to add some stuff

 

As I said before, most of my code is from EntityArrow.onUpdate(), however I removed some major stuff, this includes getting stopped by blocks and everything with arrowshake, yaw and pitch, inGround. And because I want my projectile spawned by a block, I removed this.shootingEntity as well.

I think that last might be my problem, however I'm not sure because an arrow still does damage, even though its not from an entity.

 

My code basically does the following:

    Look for entities, like the arrow does

    See if it collides with one

    Do damage if necessary

    Update position and motion (homing projectile)

 

Here is my code in case you would like to see it:

 

public void onUpdate() {
super.onUpdate();

if(this.ticksInAir > 1200) {
	this.setDead();
}
++this.ticksInAir;

/* Get MOP */
Vec3 vec31 = this.worldObj.getWorldVec3Pool().getVecFromPool(this.posX, this.posY, this.posZ);
Vec3 vec3 = this.worldObj.getWorldVec3Pool().getVecFromPool(this.posX + this.motionX, this.posY + this.motionY, this.posZ + this.motionZ);
MovingObjectPosition movingobjectposition = this.worldObj.func_147447_a(vec31, vec3, false, true, false);
vec31 = this.worldObj.getWorldVec3Pool().getVecFromPool(this.posX, this.posY, this.posZ);
vec3 = this.worldObj.getWorldVec3Pool().getVecFromPool(this.posX + this.motionX, this.posY + this.motionY, this.posZ + this.motionZ);

if (movingobjectposition != null) {
	vec3 = this.worldObj.getWorldVec3Pool().getVecFromPool(movingobjectposition.hitVec.xCoord, movingobjectposition.hitVec.yCoord, movingobjectposition.hitVec.zCoord);
}

Entity entity = null;
List list = this.worldObj.getEntitiesWithinAABBExcludingEntity(this, this.boundingBox.addCoord(this.motionX, this.motionY, this.motionZ).expand(1.0D, 1.0D, 1.0D));
double d0 = 0.0D;
int i;
float f1;

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

	if (entity1.canBeCollidedWith()) {
		f1 = 0.3F;
		AxisAlignedBB axisalignedbb1 = entity1.boundingBox.expand((double)f1, (double)f1, (double)f1);
		MovingObjectPosition movingobjectposition1 = axisalignedbb1.calculateIntercept(vec31, vec3);

		if (movingobjectposition1 != null) {
			double d1 = vec31.distanceTo(movingobjectposition1.hitVec);

			if (d1 < d0 || d0 == 0.0D) {
				entity = entity1;
				d0 = d1;
			}
		}
	}
}

if (entity != null) {
	movingobjectposition = new MovingObjectPosition(entity);
}

if (movingobjectposition != null && movingobjectposition.entityHit != null && movingobjectposition.entityHit instanceof EntityPlayer) {
	EntityPlayer entityplayer = (EntityPlayer)movingobjectposition.entityHit;

	if (entityplayer.capabilities.disableDamage) {
		movingobjectposition = null;
		this.setDead();
	}
}

/* Inflict damage */
float f2;
//System.out.print(movingobjectposition);
if (movingobjectposition != null) {
	System.out.print("mop != null\n");
	if (movingobjectposition.entityHit != null) {
		System.out.print("entityHit != null\n");

		f2 = MathHelper.sqrt_double(this.motionX * this.motionX + this.motionY * this.motionY + this.motionZ * this.motionZ);
		int k = MathHelper.ceiling_double_int((double)f2 * this.damage);

		System.out.print(this.worldObj.isRemote + "_"+ k + "\n");

		DamageSource damagesource = DamageSource.magic;
		//DamageSource damagesource = DamageSource.causeThrownDamage(this, this.target);

		if (movingobjectposition.entityHit.attackEntityFrom(damagesource, (float)k)) {
			System.out.print("ITS A HIT\n");
		//if (movingobjectposition.entityHit.attackEntityFrom(DamageSource.generic, (float)k)) {
			if (movingobjectposition.entityHit instanceof EntityLivingBase) {
				EntityLivingBase entitylivingbase = (EntityLivingBase)movingobjectposition.entityHit;
			}
		}
		this.setDead();
	}
}

/* Updating position */
this.posX += this.motionX;
this.posY += this.motionY;
this.posZ += this.motionZ;

/* Updating motion to target */
if(this.target != null) {
	this.motionX = this.target.posX - this.posX;
	this.motionY = target.boundingBox.minY + (double)(target.height / 2) - this.posY;//this.target.posY - this.posY;
	this.motionZ = this.target.posZ - this.posZ;

	this.setThrowableHeading(this.motionX, this.motionY, this.motionZ, 0.30F, 1.0F);
}

this.setPosition(this.posX, this.posY, this.posZ);
}

 

 

It's probably something really stupid and easy. However I can't figure it out, even after a couple days, and I'm hoping one of you guys see it..

Posted

Well I did, and as I said, the problem is not dispensing my projectile, but damaging entities. I took the code from EntityArrow and removed some lines, mainly parts with this.shootingEntity. However I don't think I need this, since arrows do hurt even if this.shootingEntity is null.

Posted

Hi

 

So I gather that your code prints

ITS A HIT

but doesn't damage anything?

Are you sure your entityHit is what you think it is?

Have you tried tracing into

if (movingobjectposition.entityHit.attackEntityFrom(DamageSource.generic, (float)k)) {

and seeing why it returns false?  (i.e. maybe the arrow is hitting itself, or something silly like that)

 

-TGG

Posted

uhm, nope... It does not print "ITS A HIT" sadly, sorry I forgot to inlcude it in my post :/

I thought entityHit would be the entity taking damage (and after printing it I know it is), and attackEntityFrom would be the function that inflicts damage, and checks if this type of damage does harm the entity.

I hope I am on the right track.

 

And yes I did try looking into attackEntityFrom, but since you hinted me there, I looked at it again. Previous time something bothered me. Whenever I printed this.worldObj.isRemote within my onUpdate function it returns true.

However EntityLivingBase.attackEntityFrom returns false whenever my world is remote..

But even if I print it at the start of my onUpdate method, it does this. It's never called on the serverside

 

I don't really know why though. I thought my entity was registered properly :/

Posted

Aha

 

That is a good clue.  onUpdate should also be called from the server side if this is working properly.

 

Are you spawning your entity on the server side?  (you should) i.e.

 

            if (!par2World.isRemote)

            {

                par2World.spawnEntityInWorld(entityarrow);

            }

 

I think you are on the right track about entityHit and attackFrom.

 

-TGG

Posted

Yes your post fixed it. I found it out a couple minutes before it.. I feel so dumb for this haha.

Well thanks guys.

 

So for any of you with this problem, Kudos to TGG his post above me, might fix your problem as well.

Guest
This topic is now closed to further replies.

Announcements



×
×
  • Create New...

Important Information

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