Jump to content

[SOLVED] getSourceOfDamage not working


Mister_

Recommended Posts

I have the following if statement in my code, in a LivingDeathEvent:

if (event.source.getSourceOfDamage() instanceof EntityPlayer) {

For some reason, it always returns false, and when I try to print event.source.getSourceOfDamage().getName(), it gives me a NullPointer error.

Has something changed about getSourceOfDamage in 1.8 (I'm using 1.8.9)? If so, how can I update my code? 

Edited by Mister_
Link to comment
Share on other sites

@diesieben07 Eh, just haven't gotten around to updated yet. And sorry, I should have clarified, it doesn't fire even when I, as the player, kill a mob with a sword (it's a custom sword if that makes any difference).

Here's the full event code:

 

	@SubscribeEvent(priority=EventPriority.HIGH)
	public void kill(LivingDeathEvent event) {
		if (event.source.getSourceOfDamage() instanceof EntityPlayer) {
		    EntityPlayer playerK = (EntityPlayer) event.source.getSourceOfDamage();
			     if (playerK.getCurrentEquippedItem().getDisplayName().equals("Cursed Sword")) { //Cursed Sword   
					if (playerK.getCurrentEquippedItem().getTagCompound().getInteger("curseMode")==2) { //Guilt
		            	if(!playerK.worldObj.isRemote) { 
		            		if (rand.nextInt(5)+1==1) {
		            			playerK.addChatMessage(new ChatComponentText(rndSobStory(event.entity.getName().toLowerCase())));
		            		}
		            	}
					}
			     }
		    }
		}

(It's a sword that makes you feel guilty about what you did - don't judge)

Link to comment
Share on other sites

@diesieben07 getEntity has the same problem. The thing that isn't triggering is the if statement:

event.source.getSourceOfDamage() instanceof EntityPlayer

Always returns false. Same thing happens if you replace getSourceOfDamage with getEntity.

 

Waaaait a second I just realized something while thinking about this. I have custom damage, in that every time I hit a mob with that sword, it inflicts normal damage but also uses attackEntityFrom to inflict a bunch of extra damage. What I believe is happening is that attackEntityFrom is killing the mob, and it isn't registering that the player's hit caused it. So - now the question becomes, if I use attackEntityFrom in a different class to kill a mob, how can I possibly set up a way to know if the player killed the mob? 

Link to comment
Share on other sites

@diesieben07 Ok, I'm trying to set up my Damage Source now. This is all new to me. I have the following class:

public class CursedDamage extends EntityDamageSource {
	public CursedDamage(String str, Entity damageSource) {
		super(str, damageSource);
	    this.setDamageBypassesArmor();
	    
	}
	public static CursedDamage causeCursedDamage(Entity source) {
	    return new CursedDamage("cursed_sword.item", source);
	}
}

Now how can I use this to attack an entity as a damage source?

Link to comment
Share on other sites

 

@diesieben07 From checking the code,  you pass DamageSource.causePlayerDamage a player entity and it returns a damage source of type player. I can then use that in my Event Handler by checking the type of the Damage Source, and if it's player, I know it came from my cursed sword. Is that correct or am I fundamentally misunderstanding this? 

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.



×
×
  • Create New...

Important Information

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