Jump to content
View in the app

A better way to browse. Learn more.

Forge Forums

A full-screen app on your home screen with push notifications, badges and more.

To install this app on iOS and iPadOS
  1. Tap the Share icon in Safari
  2. Scroll the menu and tap Add to Home Screen.
  3. Tap Add in the top-right corner.
To install this app on Android
  1. Tap the 3-dot menu (⋮) in the top-right corner of the browser.
  2. Tap Add to Home screen or Install app.
  3. Confirm by tapping Install.

[1.7.2]Wand of Lightning: Finding out which block im looking at[Solved]

Featured Replies

Posted

Hello guys. So for my mod I am making a wand of lightning. I need to find out which block the player is looking at and spawn a lightning bolt there. How would I go and do that? I've seen a good deal about raytracing. But how exactly would you do this?

I'm back from being gone for... I think its been about a year. I'm pretty sure nobody remembers me, but hello anybody who does!

  • Author

Here is some code I have already got.

 


	 MovingObjectPosition objectMouseOver = world.rayTraceBlocks(300, 1);
         		//world.func_147447_a(vec3, addedVector, true, true, false);

        
         
         
         if (objectMouseOver != null && objectMouseOver.typeOfHit == MovingObjectType.BLOCK) {

            int i = objectMouseOver.blockX;
            
            int j = objectMouseOver.blockY;
            
            int k = objectMouseOver.blockZ;
            
            	world.spawnEntityInWorld(new EntityLightningBolt(world, i, j, k));
            	System.out.println(objectMouseOver.blockX);
            	System.out.println(objectMouseOver.blockY);
            	System.out.println(objectMouseOver.blockZ);

The raytrace command above doesn't work.

 

I'm back from being gone for... I think its been about a year. I'm pretty sure nobody remembers me, but hello anybody who does!

  • Author

I can not make any sense of his code.

I'm back from being gone for... I think its been about a year. I'm pretty sure nobody remembers me, but hello anybody who does!

What about it? The code or the trig?

Check out my mod, Realms of Chaos, here.

 

If I helped you, be sure to press the "Thank You" button!

  • Author

The code.

Also, I seem to have something working.

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

	if(!world.isRemote){
	 Vec3 look = player.getLookVec();
	 MovingObjectPosition Coord = player.rayTrace(300, 1);
	 if(Coord != null && Coord.typeOfHit == MovingObjectType.BLOCK){
	          EntityLightningBolt Lightning = new EntityLightningBolt(world, 1, 1, 1);
	          Lightning.setPosition(Coord.blockX,Coord.blockY,Coord.blockZ);
	          world.spawnEntityInWorld(Lightning);
	 }         
	 }

This works. Only, thing is lightning and fire is invisible. Should I spawn things in on if(world.isremote)?

 

Edit: I just realized I have a vec3 in there for no reason but that shouldnt matter.

I'm back from being gone for... I think its been about a year. I'm pretty sure nobody remembers me, but hello anybody who does!

  • Author

You need to use world.addWeatherEffect(entity); not spawnEntityinWorld.

Sorry, this didn't work either. Any other suggestions?

I'm back from being gone for... I think its been about a year. I'm pretty sure nobody remembers me, but hello anybody who does!

  • Author

OHHH YEAH!!! :) :) :)

I got it working!!!!

I was actually updating this from 1.6.4 and it was working then. So this is what I did. I took my 1.6.4 code and replace the fuction

world.rayTraceBlocks_do_do

with

 world.rayTraceBlocks

 

So heres my final code if anyone wants it.

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_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;
    }

I'm back from being gone for... I think its been about a year. I'm pretty sure nobody remembers me, but hello anybody who does!

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Unfortunately, your content contains terms that we do not allow. Please edit your content to remove the highlighted words below.
Reply to this topic...

Important Information

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

Configure browser push notifications

Chrome (Android)
  1. Tap the lock icon next to the address bar.
  2. Tap Permissions → Notifications.
  3. Adjust your preference.
Chrome (Desktop)
  1. Click the padlock icon in the address bar.
  2. Select Site settings.
  3. Find Notifications and adjust your preference.