Minecraft / Blogs

Modding Tutorial: Making an Item Spawn Lightning

  • 14
  • 5
  • comment41
  • playlist_add
  • share
  • more_horiz
avatar Surseance
Level 67 : High Grandmaster Modder
284
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
Tags

Comments : 41

star Login or register to post a comment.

Show Comments

1 - 41 of 41

  • Verdigris
  • Level 1
  • New Miner
  • September 20, 2017, 9:38 am
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.
  • SirVolmz
  • Level 1
  • New Miner
  • August 30, 2015, 2:18 pm
I have made my own code similair to this but when i do it it puts fire beneath me, how do i remove it?
[deleted]
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;
}
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
  • EnderHamner
  • Level 4
  • Apprentice Network
  • August 22, 2015, 6:57 pm
where do i paste it?
does this work for 1.7.10?
  • C_M
  • Level 1
  • New Miner
  • December 31, 2013, 8:15 am
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.
  • Surseance
  • Level 67
  • High Grandmaster Modder
  • January 3, 2014, 7:04 pm
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
  • gidelix
  • Level 1
  • New Miner
  • December 22, 2013, 4:23 pm
Getting errors at World world and Entityplayer entityPlayer. Help? BTW: I'm a total 100% noob at coding
  • gidelix
  • Level 1
  • New Miner
  • December 22, 2013, 4:23 pm
And there is no "MathHelper"
  • Surseance
  • Level 67
  • High Grandmaster Modder
  • December 25, 2013, 7:17 am
Obviously, this code has changed significantly due to the version for which it was intended. Try to figure it out (it helps to make you a better coder).
I'm using Forge for 1.6.2, would that require any changes to this code?
I am getting errors under f7 * d3 and things like that
For some reason the addvector and vec 3d are giving me some errors?
  • mrbliss1
  • Level 7
  • Apprentice Modder
  • August 4, 2013, 10:27 am
vec3d has been changed to vec3
  • mrbliss1
  • Level 7
  • Apprentice Modder
  • August 4, 2013, 10:31 am
oh and createVector is createHelperVector
its not working for me
  • dof12345
  • Level 65
  • High Grandmaster Modder
  • February 17, 2013, 11:10 am
Awesome work man, thanks.
  • +RedSnivy14+
  • Level 22
  • Expert Modder
  • November 27, 2012, 5:52 am
is this using forge modloader coding or modloader coding
  • bonamb
  • Level 14
  • Journeyman Modder
  • November 17, 2012, 12:42 pm
Dimond for you
  • Ichthus95
  • Level 37
  • Artisan Pixel Painter
  • October 22, 2012, 6:13 pm
This will be infinitely useful for the Magic Staff in my mod.
Thanks!
<@{
hey im having some trouble with this for 1.3.1 could someone pm me their source code? and im trying to make it a regular sword as well as it shooting lightning so if that makes a difference plz pm me!
  • Surseance
  • Level 67
  • High Grandmaster Modder
  • October 2, 2012, 10:58 am
The error lies in the vec3d.addVector part. I'll post an update to this code soon.
Ok could you do it shortly? i was hoping to put a lightning sword in my Swords+ mod and just pm me when you post the update plz!
  • Surseance
  • Level 67
  • High Grandmaster Modder
  • October 3, 2012, 1:46 am
Look at the comment below this one. That member shows how to fix your error. (It's a temporary solution.)
Hey i also have a question to ask of you. If you wouldn't mind if your not too busy, could you download my mods and see how you like them? And make some suggestions were you think they need to be made.
Thanks,
+RedSnivy14+
  • mentin2
  • Level 44
  • Master Modder
  • August 5, 2012, 12:49 am
I got an error on "Vec3D", you know why?

Edit: You have to change this:

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

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

to this for it to work with 1.3.1:

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

Vec3 vec3d1 = vec3d.addVector((double)f7 * d3, (double)f8 * d3, (double)f9 * d3);
  • papertazer
  • Level 76
  • Legendary Creator
  • July 6, 2012, 4:57 pm
Boss
  • Kjordo711
  • Level 43
  • Master Lad
  • July 6, 2012, 7:19 am
I love you, no homo. This saved my life. As my Gratitude, DIAMOND TO YOU!
  • Surseance
  • Level 67
  • High Grandmaster Modder
  • July 6, 2012, 8:03 am
Thanks very much!
  • Kjordo711
  • Level 43
  • Master Lad
  • July 6, 2012, 11:23 am
Could you make a tutorial on how to do a similar thing exept it would place fire. And can you make one for fireballs? If you dont want to make one could you please explain what I would change using this as a template? Thanks, and i favorited and subbed.
  • Surseance
  • Level 67
  • High Grandmaster Modder
  • July 6, 2012, 12:41 pm
Yeah sure! Please note: all my future mod tutorials will be moved to the PMC mod section where they can receive more viewer traffic.
  • Kjordo711
  • Level 43
  • Master Lad
  • July 6, 2012, 12:50 pm
Do you want to like, talk on skype too? If that would be easier? Dark.attackz is my skype if you want to talk there. And Thanks in advance.
  • Surseance
  • Level 67
  • High Grandmaster Modder
  • July 6, 2012, 1:05 pm
Okay.
Could you make a mod that increases jump height, similar to smart moving mod except without as much lag, mostly so players can jump higher.
  • Surseance
  • Level 67
  • High Grandmaster Modder
  • July 2, 2012, 12:21 pm
I could, but that would require editing base class files (such as the Minecraft.class). This is not practical when it would be easier to simply add an armor piece that makes you jump higher. By doing that, you would eliminate the need of editing the Minecraft files. Look at my armor tutorial to see how to make an armor piece that allows you greater jump height. Thanks, MrArcane111
  • 0mnipotent
  • Level 63
  • High Grandmaster Programmer
  • May 31, 2012, 6:45 pm
Thanks! Been looking for that lightning spawning code for a bit. Diamond for you!
  • Surseance
  • Level 67
  • High Grandmaster Modder
  • June 1, 2012, 2:31 am
Thank-you kind sir!
ty
  • Surseance
  • Level 67
  • High Grandmaster Modder
  • May 31, 2012, 1:00 am
Leave a diamond if it was helpful!

1 - 41 of 41

Show Comments

Planet Minecraft

Browse

Site

© 2010 - 2019
planetminecraft.com

Welcome