Jump to content

  • Curse Sites
Become a Premium Member! Help
Latest News Article

How to make lightning spawn Out of Item range


  • Please log in to reply
14 replies to this topic

#1

UltimateSpartan
  • Location: brb goin to narnia
  • Minecraft: TheOldTimer

Posted 12 January 2012 - 04:33 AM

I'm making an Item and I want lightning to spawn when right clicking and it works fine, but it wont spawn anywhere outside of the Item's range. I found this code in some other topic and anyways its supposed to be able to see a block that's outside of the Range so that the item can shoot lightning. I put this in my code and I got some errors so I wanted to know if anyone else knew a better way or something?

 MovingObjectPosition movingobjectposition = rayTrace(100, 1.0F);
			if(movingobjectposition == null)
			{
				return;
			}
			Vec3D temp = movingobjectposition.hitVec;
			EntityLightningBolt lightning = new EntityLightningBolt(world, this, temp.xCoord - posX, temp.yCoord - posY, temp.zCoord - posZ);
			

Posted Image


Register or log in to remove.

#2

jghgjb790

Posted 12 January 2012 - 04:38 AM

View PostUltimateSpartan, on 12 January 2012 - 04:33 AM, said:

I put this in my code and I got some errors so I wanted to know if anyone else knew a better way or something?

 MovingObjectPosition movingobjectposition = rayTrace(100, 1.0F);
			if(movingobjectposition == null)
			{
				return;
			}
			Vec3D temp = movingobjectposition.hitVec;
			EntityLightningBolt lightning = new EntityLightningBolt(world, this, temp.xCoord - posX, temp.yCoord - posY, temp.zCoord - posZ);
			
You'll have to post the errors if you want help. Also, the entire java file causing the problem would be great.

#3

Stewiecraft
  • Location: Canada
  • Minecraft: Stewiecraft

Posted 12 January 2012 - 05:48 AM

I was going to do a tutorial for this exactly but I've been busy with my diploma today and I'm about to go to bed. I'll write it sometime tomorrow if you don't get your answer by then.

Posted Image


#4

UltimateSpartan
  • Location: brb goin to narnia
  • Minecraft: TheOldTimer

Posted 12 January 2012 - 02:20 PM

View PostStewiecraft, on 12 January 2012 - 05:48 AM, said:

I was going to do a tutorial for this exactly but I've been busy with my diploma today and I'm about to go to bed. I'll write it sometime tomorrow if you don't get your answer by then.

That would be great!! Thanks!! I assume you'll post it to the tutorial section so I look for it there.

Posted Image


#5

UltimateSpartan
  • Location: brb goin to narnia
  • Minecraft: TheOldTimer

Posted 12 January 2012 - 07:41 PM

View PostStewiecraft, on 12 January 2012 - 05:48 AM, said:

I was going to do a tutorial for this exactly but I've been busy with my diploma today and I'm about to go to bed. I'll write it sometime tomorrow if you don't get your answer by then.
so did you make the tutorial yet? I thought you'd post it in the tutorial section but I don't see anything new from you about this.

Posted Image


#6

UltimateSpartan
  • Location: brb goin to narnia
  • Minecraft: TheOldTimer

Posted 12 January 2012 - 11:01 PM

View Postjghgjb790, on 12 January 2012 - 04:38 AM, said:

You'll have to post the errors if you want help. Also, the entire java file causing the problem would be great.

I think I copied what they had in the topic that I found that other peice of code (it was awile back so I dont know fo sho) but here it is


Item class
package net.minecraft.src;
import net.minecraft.client.Minecraft;
public class ItemThorHammer extends Item
{
	Minecraft mc = ModLoader.getMinecraftInstance();
	public ItemThorHammer(int i, EnumToolMaterial lightningshard)
	{
		super(i);
		maxStackSize = 1;
		setMaxDamage(0);
	}
	
	
	
	public boolean hitEntity(ItemStack itemstack, EntityLiving entityliving, EntityLiving entityliving1)
	{
		itemstack.damageItem(1, entityliving1);
		return true; 
	}
	public ItemStack onItemRightClick(ItemStack itemstack, World world, EntityPlayer entityplayer) {
		
		Minecraft mc = ModLoader.getMinecraftInstance();
		
		if (mc != null && mc.objectMouseOver != null) {
			  	
			
			EntityLightningBolt lightningbolt = new EntityLightningBolt(world, entityplayer.posX, 128, entityplayer.posZ);
			
			if (mc.objectMouseOver.typeOfHit == EnumMovingObjectType.TILE) {
				lightningbolt.setPosition(mc.objectMouseOver.blockX,
						mc.objectMouseOver.blockY + 1,
						mc.objectMouseOver.blockZ);
			} else {
				lightningbolt.setPosition(mc.objectMouseOver.entityHit.posX,
						mc.objectMouseOver.entityHit.posY,
						mc.objectMouseOver.entityHit.posZ);
			}
			world.entityJoinedWorld(lightningbolt);
		}

		return itemstack;
	}
		
		
}


mod class
package net.minecraft.src;

public class mod_Thor extends BaseMod
{
public static final Item thorhammer  = new ItemThorHammer(580, EnumToolMaterial.LIGHTNINGSHARD).setItemName("ThorHammer");

	
	public mod_Thor()
	{
		thorhammer.iconIndex = ModLoader.addOverride("/gui/items.png", "/ThorsHammer.png");
		ModLoader.AddName(thorhammer, "Thor's Hammer");
		
		ModLoader.AddRecipe(new ItemStack(thorhammer, 1), new Object[] {
			"KKK", " K ", " K ", Character.valueOf('K'), Item.stick	
		});
	}	
	public String getVersion()
	{
		return "1.0.1";
	}
	
	public void load(){}
	
	
	
}

Posted Image


#7

Stewiecraft
  • Location: Canada
  • Minecraft: Stewiecraft

Posted 13 January 2012 - 06:36 AM

Sorry I didn't get to the computer much today and I doubt I will tomorrow. If you post your errors someone other than myself can probably help you find the problem if I can't get on.

Posted Image


#8

KMeister
    KMeister

    Diamond Miner

  • Members
  • 829 posts
  • Location: Somewhere in South America...
  • Minecraft: KMEISTER

Posted 13 January 2012 - 03:36 PM

I have this code and it works although its limited range:
package net.minecraft.src;

import net.minecraft.client.Minecraft;
import net.minecraft.src.forge.*;

public class ItemLightning extends Item implements ITextureProvider
{
	public ItemLightning(int i)
	{
		super(i);
		maxStackSize = 1;
	}
	
	public String getTextureFile()
	{
		return "/Aluminium Mod/Items/items.png";
	}

public ItemStack onItemRightClick(ItemStack itemstack, World world, EntityPlayer player) 
	{
	Vec3D vec3d = Vec3D.createVector(player.posX, player.posY, player.posZ);
	double vecX = -Math.sin(Math.toRadians(player.rotationYaw)) * Math.cos(Math.toRadians(player.rotationPitch)) * 5D;
	double vecY = -Math.sin(Math.toRadians(player.rotationPitch)) * 5D;
	double vecZ = Math.cos(Math.toRadians(player.rotationYaw)) * Math.cos(Math.toRadians(player.rotationPitch)) * 5D;
	Vec3D vec3d1 = vec3d.addVector(vecX, vecY, vecZ);
	MovingObjectPosition movingobjectposition = world.rayTraceBlocks_do(vec3d, vec3d1, true);
		
	if(movingobjectposition != null && movingobjectposition.typeOfHit == EnumMovingObjectType.TILE) 
		{
			int x = movingobjectposition.blockX, y = movingobjectposition.blockY, z = movingobjectposition.blockZ;
			EntityLightningBolt entitylightningbolt = new EntityLightningBolt(world, x, y, z);
			entitylightningbolt.setLocationAndAngles(x + 0.5, y + 1 +entitylightningbolt.yOffset, z + 0.5, player.rotationYaw, player.rotationPitch);
			world.spawnEntityInWorld(entitylightningbolt);
		}
	return itemstack;
	}
	
}


#9

UltimateSpartan
  • Location: brb goin to narnia
  • Minecraft: TheOldTimer

Posted 13 January 2012 - 03:45 PM

View PostKMeister, on 13 January 2012 - 03:36 PM, said:

I have this code and it works although its limited range:
package net.minecraft.src;

import net.minecraft.client.Minecraft;
import net.minecraft.src.forge.*;

public class ItemLightning extends Item implements ITextureProvider
{
	public ItemLightning(int i)
	{
		super(i);
		maxStackSize = 1;
	}
	
	public String getTextureFile()
	{
		return "/Aluminium Mod/Items/items.png";
	}

	public ItemStack onItemRightClick(ItemStack itemstack, World world, EntityPlayer player) 
	{
	Vec3D vec3d = Vec3D.createVector(player.posX, player.posY, player.posZ);
	double vecX = -Math.sin(Math.toRadians(player.rotationYaw)) * Math.cos(Math.toRadians(player.rotationPitch)) * 5D;
	double vecY = -Math.sin(Math.toRadians(player.rotationPitch)) * 5D;
	double vecZ = Math.cos(Math.toRadians(player.rotationYaw)) * Math.cos(Math.toRadians(player.rotationPitch)) * 5D;
	Vec3D vec3d1 = vec3d.addVector(vecX, vecY, vecZ);
	MovingObjectPosition movingobjectposition = world.rayTraceBlocks_do(vec3d, vec3d1, true);
		
	if(movingobjectposition != null && movingobjectposition.typeOfHit == EnumMovingObjectType.TILE) 
		{
			int x = movingobjectposition.blockX, y = movingobjectposition.blockY, z = movingobjectposition.blockZ;
			EntityLightningBolt robotTame = new EntityLightningBolt(world, x, y, z);
			robotTame.setLocationAndAngles(x+0.5, y+1+robotTame.yOffset, z+0.5, player.rotationYaw, player.rotationPitch);
			world.spawnEntityInWorld(robotTame);
		   
			itemstack.stackSize--;
		}
	return itemstack;
	}
	
}

Ok great thanks! But how much of it should I copy into my item class?

Posted Image


#10

KMeister
    KMeister

    Diamond Miner

  • Members
  • 829 posts
  • Location: Somewhere in South America...
  • Minecraft: KMEISTER

Posted 13 January 2012 - 04:13 PM

View PostUltimateSpartan, on 13 January 2012 - 03:45 PM, said:

Ok great thanks! But how much of it should I copy into my item class?
You can just copy the onItemRightClick method, and it should work.

#11

UltimateSpartan
  • Location: brb goin to narnia
  • Minecraft: TheOldTimer

Posted 13 January 2012 - 04:45 PM

View PostKMeister, on 13 January 2012 - 04:13 PM, said:

You can just copy the onItemRightClick method, and it should work.

Ok so I don't have to add this to my onitemrightclick I  just have to replace it

Posted Image


#12

KMeister
    KMeister

    Diamond Miner

  • Members
  • 829 posts
  • Location: Somewhere in South America...
  • Minecraft: KMEISTER

Posted 13 January 2012 - 04:46 PM

Yes.

#13

Jonhy05
    Jonhy05

    Zombie Killer

  • Members
  • 204 posts
  • Location: Costa Rica

Posted 13 January 2012 - 06:00 PM

public boolean onItemUse(ItemStack itemstack, EntityPlayer entityplayer, World world, int i, int j, int k, int l){
		if(l==0)j--;
		if(l==1)j++;
		if(l==2)k--;
		if(l==3)k++;
		if(l==4)i--;
		if(l==5)i++;
		world.entityJoinedWorld(new EntityLightningBolt(world, i, j, k));
}
this is for within player's range.
or this
public ItemStack onItemRightClick(ItemStack itemstack, World world, EntityPlayer entityplayer)
	{
		 double maxDistance = 160D;
		 MovingObjectPosition mop = entityplayer.rayTrace(maxDistance, 1.0F);

		 int x = mop.blockX;
		 int y = mop.blockY;
		 int z = mop.blockZ;
		 
		 world.entityJoinedWorld(new EntityLightningBolt(world, x, y, z));
	}
for when u point outside your range.

Posted Image
Mjölnir Hammer Mod

clicky the + button :3

#14

Stewiecraft
  • Location: Canada
  • Minecraft: Stewiecraft

Posted 13 January 2012 - 06:12 PM

It seems like you got what you need, but I wrote up the tutorial herein the second post. I also cover how to make it an item with a cooldown if that catches your fancy.

Posted Image


#15

UltimateSpartan
  • Location: brb goin to narnia
  • Minecraft: TheOldTimer

Posted 13 January 2012 - 07:07 PM

View PostStewiecraft, on 13 January 2012 - 06:12 PM, said:

It seems like you got what you need, but I wrote up the tutorial herein the second post. I also cover how to make it an item with a cooldown if that catches your fancy.


Your walkthrough worked!! thank you so much Stewiecraft and everyone else that helped me. When I finish the mod I'll make sure to give you credit. :D

Posted Image