Jump to content

Recommended Posts

Posted

Hey guys,

 

I am trying to make a mob that reflects all damage taken to its attacker, I got almost all of it working except the projectiles. The projectiles should change directions and return to the attacker but I just can't seem to get this to work properly.

 

Here is some code from the Entities file

 

@Override
public boolean attackEntityFrom(DamageSource source, float amount)
{
	Entity attacker = source.getSourceOfDamage();

	if(source.isProjectile() && !this.worldObj.isRemote)
	{
		if(attacker instanceof EntityArrow)
		{
			Entity Aggressor = ((EntityArrow) attacker).shootingEntity;

			EntityArrow attackerclone = new EntityArrow(this.worldObj, this, (EntityLivingBase) Aggressor, 16.0F, 0.0F);
		}
		else
	}
	else if(!this.worldObj.isRemote && !source.isExplosion() && !source.isFireDamage() && !source.isMagicDamage() && !source.isCreativePlayer() && !source.isUnblockable() && source.getDamageType() != "fall" && source.getDamageType() != "lightningBolt" && source.getDamageType() != "inWall" && source.getDamageType() != "cactus" && source.getDamageType() != "drown" && source.getDamageType() != "outOfWorld" && source.getDamageType() != "anvil" && source.getDamageType() != "fallingBlock")
	{
		attacker.attackEntityFrom(source, amount * 3);
	}
	else if(source.isCreativePlayer())
	{
		Minecraft.getMinecraft().thePlayer.addChatMessage(new ChatComponentText("<Accelerator> And they claim that I use hacks."));
		this.setDead();
	}
	else if(source.damageType == "fall")
	{
		explode(amount/2);
	}
	return false;
}

 

The arrow is ably to change directions fine but no matter how much I try I just can never make it gain velocity, it seems as if it is spawning with the minimum velocity possible. I tried to take a look at EntitySkeleton and yet here I am. I tried to change the 16.0F in the spawn code but it doesn't change anything.

 

Thanks!

if(pain == 0)

{

   gain = 0;

}

Posted

It doesn't look like you're spawning the arrow entity at all, so I don't see how you can say it's having no problems changing directions...

 

Furthermore, your Java is a bit messy - you have a trailing 'else' with no brackets and you're casting the arrow's shooting entity to an EntityArrow and then to an EntityLivingBase.

 

Anyway, you need to spawn the new arrow entity, or use the existing one and just modify its position and motion to the new direction.

Posted

It doesn't look like you're spawning the arrow entity at all, so I don't see how you can say it's having no problems changing directions...

 

Furthermore, your Java is a bit messy - you have a trailing 'else' with no brackets and you're casting the arrow's shooting entity to an EntityArrow and then to an EntityLivingBase.

 

Anyway, you need to spawn the new arrow entity, or use the existing one and just modify its position and motion to the new direction.

 

Oh poop, I edited the code from the original to remove a huge chunk that has nothing to do with the problem whatsoever, I will clean up the code after I get it to work :P.

 

The issue is that if I spawn a new entityarrow then I would need to do the same code for each type of throwable entity. Is there another way to simply change the entities velocity vector? I tried setVelocity but it doesn't do anything either

if(pain == 0)

{

   gain = 0;

}

Posted

You can use reflection to generalize your construction of a new entity, e.g.:

Entity newProjectile = oldProjectile.getConstructor(World.class, EntityLivingBase.class, float.class).newInstance(world, shooter, charge);

Then your code will handle any type of projectile entity so long as it has a constructor with that signature (taken from EntityArrow, so you may need to instead use the one that doesn't take the float, or have a different case for EntityArrow vs. EntityThrowable sub-classes).

 

A problem with that, though, is that you lose any extra information that may have been stored in the original class instance, i.e. any custom fields, such as damage, that were set previously.

 

Another option, as I already mentioned, is to just change the heading at any time by setting the projectile entity's motionX/Y/Z values to whatever you want. That's what #setVelocity does, mostly, but the problem with that is you also need to make sure the clients all know about it, too, or else the client version of the entity (which is the one you see) will still be headed in the original direction.

 

A further problem is that, since the projectile has already impacted, chances are it has had #setDead called and will be cleaned up very shortly. You need to undo that and also make sure it is no longer colliding with any entity, otherwise it will just re-collide on the next tick and do its onImpact business.

 

Take a look at #setThrowableHeading (or some version thereof, including pretty much any constructor that takes the shooting / throwing entity) in EntityArrow / EntityThrowable to get an idea of the code you'll need. That and a way to avoid hitting the same entity again, which most projectiles do by using an 'if (this.ticksExisted < 5) { ignore the shooting entity when checking for impact }'

 

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.