Jump to content

Living death event


The_SlayerMC

Recommended Posts

On my LivingDeathEvent, it just started to crash after I removed if(!event.world.isRemote) (it was placed above the checker if statement) and now it is crashing when i kill a mob the cause is the

"if(event.source.getSourceOfDamage() instanceof EntityPlayer && p.getHeldItem().getItem() instanceof ItemSword){"

 

And before that was crashing, it would crash when a mob would kill itself in lava or something! I don't know why it would crash on that, because I'm only checking if the source was the player and if the player is holding a sword.

 

Here is my event (I would put it in a code spoiler but I'm new to the forge forums and idk how to!)

 

 

 

@SubscribeEvent

public void onKilledMob(LivingDeathEvent event){

EntityPlayer p = (EntityPlayer)event.source.getEntity();

if(event.source.getSourceOfDamage() instanceof EntityPlayer && p.getHeldItem().getItem() instanceof ItemSword){

final int level = rand.nextInt(2) + 1;

DataHelper.setSwordLevel(p, DataHelper.getSwordLevel(p) + level);

}

}

Former developer for DivineRPG, Pixelmon and now the maker of Essence of the Gods

Link to comment
Share on other sites

Exactly the same way. DamageSource#getEntity() returns the direct source of the damage, which may be an arrow or even null (in the case of fire, for example); DamageSource#getSourceOfDamage() returns the ultimate source of the damage, so if an EntityPlayer shot an arrow, the first method returns the arrow, the second the player.

 

If you want to be sure that the source of the damage is a player, use the latter method only:

if (event.source.getSourceOfDamage() instanceof EntityPlayer) {
EntityPlayer p = (EntityPlayer) event.source.getSourceOfDamage();
if (p.getHeldItem() != null && p.getHeldItem.getItem() instanceof ItemSword) {
// do whatever
}
}

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.