Jump to content
View in the app

A better way to browse. Learn more.

Forge Forums

A full-screen app on your home screen with push notifications, badges and more.

To install this app on iOS and iPadOS
  1. Tap the Share icon in Safari
  2. Scroll the menu and tap Add to Home Screen.
  3. Tap Add in the top-right corner.
To install this app on Android
  1. Tap the 3-dot menu (⋮) in the top-right corner of the browser.
  2. Tap Add to Home screen or Install app.
  3. Confirm by tapping Install.

Featured Replies

Posted

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

I never casted the source, are you telling me too or..?

I'm saying that you can't do this:

EntityPlayer p = (EntityPlayer) event.source.getEntity();
if (p.someMethod()) { 
// CRASH because event.source.getEntity() might not be a palyer

 

Welcome to the forums, btw - I've seen you around on MCF.

  • Author

Than how would a good STABLE way to check if the player is the source of damage and he/she is holding a sword?

 

And ty, iv'e been told that people are much more helpful on these forums :P

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

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
}
}

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...

Important Information

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

Configure browser push notifications

Chrome (Android)
  1. Tap the lock icon next to the address bar.
  2. Tap Permissions → Notifications.
  3. Adjust your preference.
Chrome (Desktop)
  1. Click the padlock icon in the address bar.
  2. Select Site settings.
  3. Find Notifications and adjust your preference.