title: How to Make Minecraft Player's looking position be recorded in java
body: <p>I know this title is confusing. I am making a minecraft mod in java. I made an Item that explodes and spawns lightning when you right click. Currently, it explodes and makes lightning at the position of the player. How would I make it explode at the position the player looks? My item class currently looks like this:</p>
  
  <pre><code>package com.mod.elliotmod;
  
  import net.minecraft.creativetab.CreativeTabs;
  import net.minecraft.entity.effect.EntityLightningBolt;
  import net.minecraft.entity.player.EntityPlayer;
  import net.minecraft.item.Item;
  import net.minecraft.item.ItemStack;
  import net.minecraft.world.World;
  import net.minecraftforge.fml.common.registry.GameRegistry;
  
  public class ItemMyTNT extends Item {
  
      public ItemMyTNT(String name, int stackSize) {
          GameRegistry.registerItem(this, name);
          setUnlocalizedName(ElliotMod.MODID + "_" + name);
          setCreativeTab(CreativeTabs.tabMisc);
          setMaxStackSize(stackSize);
      }
  
      public ItemStack onItemRightClick(ItemStack item, World world, EntityPlayer player) {
  
          world.spawnEntityInWorld(new EntityLightningBolt(world, player.posX, player.posY, player.posZ));
          world.createExplosion(player, player.posX, player.posY, player.posZ, 5.0F, true);
  
          return item;
      }
  }
  </code></pre>
  
  <p><strong>EDIT:</strong>
  Here is what I now have:</p>
  
  <pre><code>package com.mod.elliotmod;
  
  import net.minecraft.creativetab.CreativeTabs;
  import net.minecraft.entity.effect.EntityLightningBolt;
  import net.minecraft.entity.player.EntityPlayer;
  import net.minecraft.item.Item;
  import net.minecraft.item.ItemStack;
  import net.minecraft.world.World;
  import net.minecraftforge.fml.common.registry.GameRegistry;
  
  public class ItemMyTNT extends Item {
  
      public ItemMyTNT(String name, int stackSize) {
          GameRegistry.registerItem(this, name);
          setUnlocalizedName(ElliotMod.MODID + "_" + name);
          setCreativeTab(CreativeTabs.tabMisc);
          setMaxStackSize(stackSize);
      }
  
      public ItemStack onItemRightClick(ItemStack item, World world, EntityPlayer player) {
  
          double x = player.getLookVec().xCoord;
          double y = player.getLookVec().yCoord;
          double z = player.getLookVec().zCoord; 
  
          world.spawnEntityInWorld(new EntityLightningBolt(world, x, y, z));
          world.createExplosion(player, x, y, z, 5.0F, true);
  
          return item;
      }
  }
  </code></pre>
  
  <p>I found this in the Entity Player class. It may help</p>
  
  <pre><code>        Vec3 vec3 = new Vec3(((double)this.rand.nextFloat() - 0.5D) * 0.1D, Math.random() * 0.1D + 0.1D, 0.0D);
          vec3 = vec3.rotatePitch(-this.rotationPitch * (float)Math.PI / 180.0F);
          vec3 = vec3.rotateYaw(-this.rotationYaw * (float)Math.PI / 180.0F);
          double d0 = (double)(-this.rand.nextFloat()) * 0.6D - 0.3D;
          Vec3 vec31 = new Vec3(((double)this.rand.nextFloat() - 0.5D) * 0.3D, d0, 0.6D);
          vec31 = vec31.rotatePitch(-this.rotationPitch * (float)Math.PI / 180.0F);
          vec31 = vec31.rotateYaw(-this.rotationYaw * (float)Math.PI / 180.0F);
          vec31 = vec31.addVector(this.posX, this.posY + (double)this.getEyeHeight(), this.posZ);
  </code></pre>
  
  <p>I used this class and my code looks like this, but doesn't work:</p>
  
  <pre><code>public ItemStack onItemRightClick(ItemStack item, World world, EntityPlayer player) {
           Random rand = new Random();
           Vec3 vec3 = new Vec3(((double)rand.nextFloat() - 0.5D) * 0.1D, Math.random() * 0.1D + 0.1D, 0.0D);
           vec3 = vec3.rotatePitch(-player.rotationPitch * (float)Math.PI / 180.0F);
           vec3 = vec3.rotateYaw(-player.rotationYaw * (float)Math.PI / 180.0F);
           double d0 = (double)(rand.nextFloat()) * 0.6D - 0.3D;
           Vec3 vec31 = new Vec3(((double)rand.nextFloat() - 0.5D) * 0.3D, d0, 0.6D);
           vec31 = vec31.rotatePitch(-player.rotationPitch * (float)Math.PI / 180.0F);
           vec31 = vec31.rotateYaw(-player.rotationYaw * (float)Math.PI / 180.0F);
           vec31 = vec31.addVector(player.posX, player.posY + (double)player.getEyeHeight(), player.posZ);
  
          MovingObjectPosition movingobjectposition = world.rayTraceBlocks(vec3, vec31, false, true, false);
          if (movingobjectposition == null) {
              //nothing was under the player's gaze
              return item;
          }
          if (movingobjectposition.typeOfHit == MovingObjectPosition.MovingObjectType.BLOCK) {
              int ix = movingobjectposition.getBlockPos().getX();
              int iy = movingobjectposition.getBlockPos().getY();
              int iz = movingobjectposition.getBlockPos().getZ();
              if (!world.isBlockNormalCube(movingobjectposition.getBlockPos(), true)) {
                  --iy;
              }
              world.spawnEntityInWorld(new EntityLightningBolt(world, ix, iy, iz));
          }
          if (movingobjectposition.typeOfHit == MovingObjectPosition.MovingObjectType.ENTITY) {
              double ix = movingobjectposition.hitVec.xCoord;
              double iy = movingobjectposition.hitVec.yCoord;
              double iz = movingobjectposition.hitVec.zCoord;
              world.spawnEntityInWorld(new EntityLightningBolt(world, ix, iy, iz));
          }
  
          return item;
  
  
      }
  </code></pre>