Jump to content

DamageSource.getEntity() always null


Yario

Recommended Posts

I use Player API with and I have a problem:

public boolean attackEntityFrom(DamageSource damageSource, float v) {
    if(damageSource.getEntity() != null)
    {
        //Never Reached
    }
    return super.attackEntityFrom(damageSource, v);
}

I saw that I need to use EntityDamageSource but It is not working.

What can I do?
(It is clientside)

Edited by Yario
Link to comment
Share on other sites

What are you trying to do?
Neither hurting an entity, nor protecting from damage can be done client-side, as that is done server-side.
It would not make sense if the client could overrule and go "nu-uh, that missed", or "I'm gonna attack you with 1 MILLION DAMAGE"

Also previously known as eAndPi.

"Pi, is there a station coming up where we can board your train of thought?" -Kronnn

Published Mods: Underworld

Handy links: Vic_'s Forge events Own WIP Tutorials.

Link to comment
Share on other sites

59 minutes ago, Yario said:

Yes I do:


MinecraftForge.EVENT_BUS.register(this);

Below, I provided a direct example of how to get the attacker, target, etc. and how to use LivingAttackEvent.

	@SubscribeEvent
	public void onAttack(LivingAttackEvent event)
	{
		if (event.getEntity() instanceof EntityPlayer)
		{
			Entity attackerIn = event.getSource().getSourceOfDamage(); // this is your attacker instance, the entity attacking the target		
			EntityPlayer targetIn = (EntityPlayer) event.getEntity(); // this is your target instance, the entity being attacked by the attacker
			/*
             		* Here, you do whatever you want with your attacker and target.
             		* You can make the target set on flame, add potion effects, etc.
             		* Make sure you properly handle sides
             		*/
			targetIn.worldObj.playSound((EntityPlayer) null, targetIn.getPosition(), SoundEvents.BLOCK_METAL_BREAK, SoundCategory.BLOCKS, 2.0F, 1.25F); // I play a sound in my attack event
			
			Random rand = worldIn.rand; // random instance
    		
			for (int i = 0; i < 75; ++i)
			{
              			/* my helper stuff, don't worry about it */
				DinocraftServer.spawnParticle(EnumParticleTypes.BLOCK_CRACK, worldIn, 
					  targetIn.posX + (rand.nextDouble() - 0.5D) * (double)targetIn.width, 
					  targetIn.posY + rand.nextDouble() - (double)targetIn.getYOffset() + 0.25D, 
					  targetIn.posZ + (rand.nextDouble() - 0.5D) * (double)targetIn.width, 
					  Math.random() * 0.2D - 0.1D, Math.random() * 0.25D, Math.random() * 0.2D - 0.1D, 
					  Block.getIdFromBlock(Blocks.REDSTONE_BLOCK)
				   ); // I spawn blood particles in my attack event
			}
		}
	}

I don't even think LivingAttackEvent works on 1.9, last I heard, correct me if I'm wrong. I would recommend updating to at least 1.10.2, which is the version I used above.

Edited by Differentiation
Link to comment
Share on other sites

6 minutes ago, Yario said:

Yes but I will try in another version like you said above

Let me guide you.

1. Create a new class called EventHandler.

2. Create a method called registerEvents() in the class and register your event inside. Like so:

public class EventHandler 
{
	public void registerEvents()
	{
      		/* All your events will be registered here */
      ...
		MinecraftForge.EVENT_BUS.register(new YourClassNameWhereTheEventIsFound());
      ...
	}
}

3. Call this method in your mod's initialization phase.

MAIN CLASS:
......
  	EventHandler eventHandler = new EventHandler();	
  
	@EventHandler
	public void registerEvents(FMLInitializationEvent event)
	{
      ...
		eventHandler.init();
      ...
	}
......

You may use these steps to guide you.

I hope I helped.

Edited by Differentiation
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.

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.