Jump to content

Null Pointer Exception - Help :(


Smoth48

Recommended Posts

I'm working on creating an Item in my custom mod that spawns a lightning bolt at whichever block the player right clicks on whilst holding a certain item.

 

The item works flawlessly when the player clicks on a block within their range (a block you could normally mine/interact with). However, when the player clicks a block out of that range, Minecraft crashes with a Null Pointer Exception.

 

If anyone could tell me what is causing this error, or might know a possible solution, I would be very much appreciative. Thanks!

 

The code:

package smith.tyler.hackCraft.item;

import net.minecraft.creativetab.CreativeTabs;
import net.minecraft.entity.effect.EntityLightningBolt;
import net.minecraft.entity.player.EntityPlayer;
import net.minecraft.entity.player.EntityPlayerMP;
import net.minecraft.item.Item;
import net.minecraft.item.ItemStack;
import net.minecraft.util.MathHelper;
import net.minecraft.util.MovingObjectPosition;
import net.minecraft.util.Vec3;
import net.minecraft.world.World;

public class ForGiggles extends Item {

public ForGiggles(int par1) {
	super(par1);

	setUnlocalizedName("forGiggles");
	setCreativeTab(CreativeTabs.tabMisc);
}

public ItemStack onItemRightClick(ItemStack itemStack, World world, EntityPlayer player){

	MovingObjectPosition movingobjectposition = getMovingObjectPositionFromPlayer(world, player, false);

	int x = movingobjectposition.blockX;
	int y = movingobjectposition.blockY;
	int z = movingobjectposition.blockZ;
	world.spawnEntityInWorld(new EntityLightningBolt(world, x, y, z));

	System.out.println("Target X: " + x);
	System.out.println("Target Y: " + y);
	System.out.println("Target Z: " + z);

	return itemStack;
}

public MovingObjectPosition getMovingObjectPositionFromPlayer(World par1World, EntityPlayer par2EntityPlayer, boolean par3)
    {
        float f = 1.0F;
        float f1 = par2EntityPlayer.prevRotationPitch + (par2EntityPlayer.rotationPitch - par2EntityPlayer.prevRotationPitch) * f;
        float f2 = par2EntityPlayer.prevRotationYaw + (par2EntityPlayer.rotationYaw - par2EntityPlayer.prevRotationYaw) * f;
        double d0 = par2EntityPlayer.prevPosX + (par2EntityPlayer.posX - par2EntityPlayer.prevPosX) * (double)f;
        double d1 = par2EntityPlayer.prevPosY + (par2EntityPlayer.posY - par2EntityPlayer.prevPosY) * (double)f + (double)(par1World.isRemote ? par2EntityPlayer.getEyeHeight() - par2EntityPlayer.getDefaultEyeHeight() : par2EntityPlayer.getEyeHeight()); // isRemote check to revert changes to ray trace position due to adding the eye height clientside and player yOffset differences
        double d2 = par2EntityPlayer.prevPosZ + (par2EntityPlayer.posZ - par2EntityPlayer.prevPosZ) * (double)f;
        Vec3 vec3 = par1World.getWorldVec3Pool().getVecFromPool(d0, d1, d2);
        float f3 = MathHelper.cos(-f2 * 0.017453292F - (float)Math.PI);
        float f4 = MathHelper.sin(-f2 * 0.017453292F - (float)Math.PI);
        float f5 = -MathHelper.cos(-f1 * 0.017453292F);
        float f6 = MathHelper.sin(-f1 * 0.017453292F);
        float f7 = f4 * f5;
        float f8 = f3 * f5;
        double d3 = 5.0D;
        if (par2EntityPlayer instanceof EntityPlayerMP)
        {
            d3 = ((EntityPlayerMP)par2EntityPlayer).theItemInWorldManager.getBlockReachDistance();
        }
        Vec3 vec31 = vec3.addVector((double)f7 * d3, (double)f6 * d3, (double)f8 * d3);
        return par1World.rayTraceBlocks_do_do(vec3, vec31, par3, !par3);
    }
}

Link to comment
Share on other sites

Fer the love of god.

 

MovingObjectPosition movingobjectposition = getMovingObjectPositionFromPlayer(world, player, false);
	if(movingobjectposition != null) {
		//If the object it isn't fucking NULL, then we can work with it
		int x = movingobjectposition.blockX;
		int y = movingobjectposition.blockY;
		int z = movingobjectposition.blockZ;
		world.spawnEntityInWorld(new EntityLightningBolt(world, x, y, z));

		System.out.println("Target X: " + x);
		System.out.println("Target Y: " + y);
		System.out.println("Target Z: " + z);
	}
	return itemStack;

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

Guest
This topic is now closed to further replies.

Announcements



×
×
  • Create New...

Important Information

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