Posted March 25, 201411 yr 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
March 25, 201411 yr Your EntityPlayers are not the same: 1. (EntityPlayer) event.source.getEntity() 2. (EntityPlayer) event.source.getSourceOfDamage() You never check if the first one is really an EntityPlayer, yet you try to use it as one right away in your if statement. http://i.imgur.com/NdrFdld.png[/img]
March 25, 201411 yr Author I never casted the source, are you telling me too or..? Former developer for DivineRPG, Pixelmon and now the maker of Essence of the Gods
March 25, 201411 yr 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. http://i.imgur.com/NdrFdld.png[/img]
March 25, 201411 yr Also, you can't do this: p.getHeldItem().getItem() // CRASH if player's held item is null http://i.imgur.com/NdrFdld.png[/img]
March 25, 201411 yr 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 Former developer for DivineRPG, Pixelmon and now the maker of Essence of the Gods
March 25, 201411 yr 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 } } http://i.imgur.com/NdrFdld.png[/img]
March 25, 201411 yr Author Thanks!! It works! And i also have a question about storing/getting ints server side using NBT, do you think you could help me over PM? Former developer for DivineRPG, Pixelmon and now the maker of Essence of the Gods
March 25, 201411 yr Sure, but would you mind using the MCF forums to PM me? I find the interface easier to use. http://i.imgur.com/NdrFdld.png[/img]
March 25, 201411 yr Author Sure Former developer for DivineRPG, Pixelmon and now the maker of Essence of the Gods
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.