Jump to content

[1.7.10] Crash when killing mobs with nothing in hands.


Ms_Raven

Recommended Posts

I have an event that makes mobs drop certain items when killed with certain weapons, but killing them with nothing in your hand causes a NullPointerException even though I check to make sure the currently equipped item is not null.

 

What am I doing wrong?

 

 

public class MobDrops

{

public static Random random;

public static int dropped;

 

@SubscribeEvent

public void onDeath(LivingDeathEvent event)

{

 

random = new Random();

dropped = random.nextInt(2) + 1;

 

if (!(event.source instanceof EntityDamageSource))

{

return;

}

EntityDamageSource damageSource = (EntityDamageSource)event.source;

     

Entity entity = damageSource.getSourceOfDamage();

if (!(entity instanceof EntityPlayer))

{

return;

}

EntityPlayer player = (EntityPlayer)entity;

 

if(event.entityLiving instanceof EntityBombskit)

{

  player.triggerAchievement(FandomAchievementList.bombskit);

}

if(event.entityLiving instanceof EntityArgorok)

{

  player.triggerAchievement(FandomAchievementList.dragon2);

}

ItemStack stack = player.getCurrentEquippedItem();

 

 

//CRASH POINTS TO THIS LINE:

if(player.inventory.getCurrentItem().getItem() != null && player.inventory.getCurrentItem().getItem() == Main.keyKingdom|| player.inventory.getCurrentItem().getItem() == Main.keyKingdomD|| player.inventory.getCurrentItem().getItem() == Main.jungleKing|| player.inventory.getCurrentItem().getItem() == Main.oblivion|| player.inventory.getCurrentItem().getItem() == Main.ultima|| player.inventory.getCurrentItem().getItem() == Main.oneWingedAngel|| player.inventory.getCurrentItem().getItem() == Main.destinyEmbrace|| player.inventory.getCurrentItem().getItem() == Main.oathkeeper|| player.inventory.getCurrentItem().getItem() == Main.photonDebugger|| player.inventory.getCurrentItem().getItem() == Main.metalChocobo|| player.inventory.getCurrentItem().getItem() == Main.dreamSword)

{

        if(event.entityLiving instanceof EntityZombie)

        {

        event.entityLiving.entityDropItem(new ItemStack(Main.darkHeart), dropped);

        }

        if(event.entityLiving instanceof EntitySkeleton)

        {

        event.entityLiving.entityDropItem(new ItemStack(Main.darkHeart), dropped);

        }

        if(event.entityLiving instanceof EntityPigZombie)

        {

        event.entityLiving.entityDropItem(new ItemStack(Main.darkHeart), dropped);

        }

        if(event.entityLiving instanceof EntityGhast)

        {

        event.entityLiving.entityDropItem(new ItemStack(Main.darkHeart), dropped);

        }

        if(event.entityLiving instanceof EntityWitch)

        {

        event.entityLiving.entityDropItem(new ItemStack(Main.darkHeart), dropped);

        }

        if(event.entityLiving instanceof EntityEnderman)

        {

        event.entityLiving.entityDropItem(new ItemStack(Main.darkHeart), dropped);

        }

        if(event.entityLiving instanceof EntityWither)

        {

        event.entityLiving.entityDropItem(new ItemStack(Main.crown), dropped);

        }

        else

        {

        return;

        }

        }

if(stack.getItem() == Main.soulEater)

{

        if(event.entityLiving instanceof EntityCow)

        {

        event.entityLiving.entityDropItem(new ItemStack(Main.soul), dropped);

        }

        if(event.entityLiving instanceof EntityPig)

        {

        event.entityLiving.entityDropItem(new ItemStack(Main.soul), dropped);

        }

        if(event.entityLiving instanceof EntitySheep)

        {

        event.entityLiving.entityDropItem(new ItemStack(Main.soul), dropped);

        }

        if(event.entityLiving instanceof EntityCreeper)

        {

        event.entityLiving.entityDropItem(new ItemStack(Main.soul), dropped);

        }

        if(event.entityLiving instanceof EntityCaveSpider)

        {

        event.entityLiving.entityDropItem(new ItemStack(Main.soul), dropped);

        }

        if(event.entityLiving instanceof EntitySpider)

        {

        event.entityLiving.entityDropItem(new ItemStack(Main.soul), dropped);

        }

        if(event.entityLiving instanceof EntityWitch)

        {

        event.entityLiving.entityDropItem(new ItemStack(Main.ragingSoul), dropped);

        }

        else

        {

        return;

        }

        }

if(stack.getItem() == Main.fourSword)

{

        if(event.entityLiving instanceof EntitySlime)

        {

        event.entityLiving.entityDropItem(new ItemStack(Main.jellyBlue), dropped);

        }

}

if(stack.getItem() == Main.fourSword_2)

{

        if(event.entityLiving instanceof EntitySlime)

        {

        event.entityLiving.entityDropItem(new ItemStack(Main.jellyRed), dropped);

        }

        if(event.entityLiving instanceof EntityChicken)

        {

        event.entityLiving.entityDropItem(new ItemStack(Main.goldenFeather), dropped);

        }

}

if(stack.getItem() == Main.masterSword)

{

        if(event.entityLiving instanceof EntitySlime)

        {

        event.entityLiving.entityDropItem(new ItemStack(Main.jellyGreen), dropped);

        }

}

if(stack.getItem() == Main.starHammer)

{

        if(event.entityLiving instanceof EntityGoomba)

        {

        event.entityLiving.entityDropItem(new ItemStack(Main.coin), dropped);

        }

        if(event.entityLiving instanceof EntityKoopa)

        {

        event.entityLiving.entityDropItem(new ItemStack(Main.coin), dropped);

        }

}

else

{

int c = random.nextInt(3000);

if(c < 2)

{

event.entityLiving.entityDropItem(new ItemStack(Main.dinsPearl), dropped);

}

else

{

event.entityLiving.entityDropItem(new ItemStack(Main.rupeeGreen), dropped);

}

}

    if(event.entityLiving instanceof EntityBeastGannon)

    {

 

  player.triggerAchievement(FandomAchievementList.masterQuest);

    }

 

}

 

}

 

Link to comment
Share on other sites

You are not checking if ItemStack is null. You are checking if Item is null which won't do anything, Item will never be null.

 

Also - use else if - will save you a lot of other ifs.

1.7.10 is no longer supported by forge, you are on your own.

Link to comment
Share on other sites

In case you want to know what happened when killing with hands only:

 

********Java checks && before ||**********

(You can check that)

so, when your current "item" is hand (which is null):

 

player.inventory.getCurrentItem().getItem() != null // false, it is null.

&& player.inventory.getCurrentItem().getItem() == Main.keyKingdom) // false because of the statement just now.

|| player.inventory.getCurrentItem().getItem() == Main.keyKingdomD // Cannot compare. LHS is null.

 

If you want to keep your chunk of code, MAKE SO MUCH SURE you add brackets to the item-checking after the null-checking. In this way, if the null-checking finds out it's null, then you wont accidentally access the pointer when checking for its type.

Link to comment
Share on other sites

so, when your current "item" is hand (which is null):

 

1: player.inventory.getCurrentItem().getItem() != null // false, it is null.

2: && player.inventory.getCurrentItem().getItem() == Main.keyKingdom) // false because of the statement just now.

3: || player.inventory.getCurrentItem().getItem() == Main.keyKingdomD // Cannot compare. LHS is null.

 

Null pointer exception on line 1.

Apparently I'm a complete and utter jerk and come to this forum just like to make fun of people, be confrontational, and make your personal life miserable.  If you think this is the case, JUST REPORT ME.  Otherwise you're just going to get reported when you reply to my posts and point it out, because odds are, I was trying to be nice.

 

Exception: If you do not understand Java, I WILL NOT HELP YOU and your thread will get locked.

 

DO NOT PM ME WITH PROBLEMS. No help will be given.

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.