Minecraft Blogs / Tutorial

Modding Tutorial: Making an Item Spawn Lightning

  • 20,340 views, 3 today
  • 14
  • 5
  • 41
Surseance's Avatar Surseance
Level 67 : High Grandmaster Modder
288
Here is how to make your item spawn lightning. It checks to see if the thing you right clicked is either a block or an entity. Hope you enjoy!

*Make sure you leave constructive criticism/feeback and a diamond!*
Also make sure you comment about all your errors and what not.

Click to reveal


package net.minecraft.src;




public class ItemNameHere extends Item


{


public ItemNameHere(int i)


{


super(i);


maxStackSize = 1;


}


public ItemStack onItemRightClick(ItemStack itemstack, World world, EntityPlayer entityplayer)


{


float f = 1.0F;


float f1 = entityplayer.prevRotationPitch + (entityplayer.rotationPitch - entityplayer.prevRotationPitch) * f;


float f2 = entityplayer.prevRotationYaw + (entityplayer.rotationYaw - entityplayer.prevRotationYaw) * f;


double d = entityplayer.prevPosX + (entityplayer.posX - entityplayer.prevPosX) * (double)f;


double d1 = (entityplayer.prevPosY + (entityplayer.posY - entityplayer.prevPosY) * (double)f + 1.6200000000000001D) - (double)entityplayer.yOffset;


double d2 = entityplayer.prevPosZ + (entityplayer.posZ - entityplayer.prevPosZ) * (double)f;


Vec3D vec3d = Vec3D.createVector(d, d1, d2);


float f3 = MathHelper.cos(-f2 * 0.01745329F - 3.141593F);


float f4 = MathHelper.sin(-f2 * 0.01745329F - 3.141593F);


float f5 = -MathHelper.cos(-f1 * 0.01745329F);


float f6 = MathHelper.sin(-f1 * 0.01745329F);


float f7 = f4 * f5;


float f8 = f6;


float f9 = f3 * f5;


double d3 = 5000D;


Vec3D vec3d1 = vec3d.addVector((double)f7 * d3, (double)f8 * d3, (double)f9 * d3);


MovingObjectPosition movingobjectposition = world.rayTraceBlocks_do_do(vec3d, vec3d1, false, true);


if (movingobjectposition == null)


{


return itemstack;


}


if (movingobjectposition.typeOfHit == EnumMovingObjectType.TILE)


{


int i = movingobjectposition.blockX;


int j = movingobjectposition.blockY;


int k = movingobjectposition.blockZ;


world.spawnEntityInWorld(new EntityLightningBolt(world, i, j, k));


}


return itemstack;


}


}
Tags

Create an account or sign in to comment.

1
09/20/2017 1:38 pm
Level 1 : New Miner
Verdigris
Verdigris's Avatar
so I found this ancient post and decided to see if it actually works still in 1.7.10, which is outdated anyway but less than this post, so as you may expect I got an absolute bucketload of errors. well I decided to update it to 1.7.10 anyway just because I wanted to have a staff that creates lightning on right click. Ladies and gentlemen, here it is! this bit of code updated to 1.7.10!

@Override
public ItemStack onItemRightClick(ItemStack itemstack, World world, EntityPlayer entityplayer){
float f = 1.0f;

float f1 = entityplayer.prevRotationPitch + (entityplayer.rotationPitch - entityplayer.prevRotationPitch) * f;

float f2 = entityplayer.prevRotationYaw + (entityplayer.rotationYaw - entityplayer.prevRotationYaw) * f;

double d = entityplayer.prevPosX + (entityplayer.posX - entityplayer.prevPosX) * (double)f;

double d1 = (entityplayer.prevPosY + (entityplayer.posY - entityplayer.prevPosY) * (double)f + 1.6200000000000001D) - (double)entityplayer.yOffset;

double d2 = entityplayer.prevPosZ + (entityplayer.posZ - entityplayer.prevPosZ) * (double)f;

Vec3 vec3d = Vec3.createVectorHelper(d, d1, d2);

float f3 = MathHelper.cos(-f2 * 0.01745329F - 3.141593F);

float f4 = MathHelper.sin(-f2 * 0.01745329F - 3.141593F);

float f5 = -MathHelper.cos(-f1 * 0.01745329F);

float f6 = MathHelper.sin(-f1 * 0.01745329F);

float f7 = f4 * f5;

float f8 = f6;

float f9 = f3 * f5;

double d3 = 5000D;

Vec3 vec3d1 = vec3d.addVector((double)f7 * d3, (double)f8 * d3, (double)f9 * d3);

MovingObjectPosition movingobjectposition = world.rayTraceBlocks(vec3d, vec3d1, false);

if(movingobjectposition == null){
return itemstack;
}

if(movingobjectposition.typeOfHit == MovingObjectType.BLOCK){
int i = movingobjectposition.blockX;


int j = movingobjectposition.blockY;


int k = movingobjectposition.blockZ;

world.spawnEntityInWorld(new EntityLightningBolt(world, i, j, k));
}
return itemstack;
}


So as you can see, the Vec3Ds have changed to simply Vec3s, and the method createVector into createVectorHelper. The method rayTraceBlocks_do_do has changed to rayTraceBlocks and the EnumMovingObjectType into MovingObjectType. I hope this was helpful, even though outdated.
1
08/30/2015 6:18 pm
Level 1 : New Miner
SirVolmz
SirVolmz's Avatar
I have made my own code similair to this but when i do it it puts fire beneath me, how do i remove it?
1
06/25/2015 11:08 am
Level 1 : New Miner
anonpmc1936308
anonpmc1936308's Avatar
[deleted]
1
10/02/2014 7:03 pm
Level 9 : Apprentice Dragon
Silvercrafted Owner
Silvercrafted Owner's Avatar
In 1.6.4 I found that you can do the same thing by using this code

public boolean onBlockActivated(World par1World, int par2, int par3, int par4, EntityPlayer par5EntityPlayer, int par6, float par7, float par8, float par9)
{
par1World.spawnEntityInWorld(new EntityLightningBolt(par1World, par2, par3, par4));
return true;
}
1
08/23/2015 4:58 pm
Level 9 : Apprentice Dragon
Silvercrafted Owner
Silvercrafted Owner's Avatar
its a block event, when the player clicks that block it will spawn lightning near the block but honestly I cant give you a good answer for that because i have not modded in over a year
1
08/22/2015 10:57 pm
Level 6 : Apprentice Network
EnderHamner
EnderHamner's Avatar
where do i paste it?
1
08/19/2014 6:28 pm
Level 49 : Master Wizard
epicanthony76
epicanthony76's Avatar
does this work for 1.7.10?
1
12/31/2013 1:15 pm
Level 1 : New Miner
C_M
C_M's Avatar
Dude, MrArcane, update this to 1.6.4, please! It kind of kills me, like World class doesn't have rayTraceBlock_do_do() method anymore. You can really save my head if you response in 2-4 days.
1
01/04/2014 12:04 am
Level 67 : High Grandmaster Modder
Surseance
Surseance's Avatar
Just look at rayTrace examples in other classes. Actually there is a mouse over Event in forge...you can mosey around the javadocs for info
1
12/22/2013 9:23 pm
Level 1 : New Miner
gidelix
gidelix's Avatar
Getting errors at World world and Entityplayer entityPlayer. Help? BTW: I'm a total 100% noob at coding
Planet Minecraft

Website

© 2010 - 2024
www.planetminecraft.com

Welcome