Posted July 15, 20178 yr 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 July 15, 20178 yr by Mister_
July 15, 20178 yr Author @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)
July 15, 20178 yr Author @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?
July 15, 20178 yr Author @diesieben07 Sorry, I'm pretty new to this whole thing. What do you mean by my DamageSource class? I don't have a damagesource class, unless you mean to edit the base files which I don't want to have to do.
July 15, 20178 yr Author I thought of one solution - creating a custom damagesource that I damage the player with for my sword and then just detecting that damage source. Oh wait is that what you meant?
July 15, 20178 yr Author @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?
July 15, 20178 yr Author @diesieben07 Could you elaborate or give a code example? I'm not really following.
July 15, 20178 yr Author @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?
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.