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.

Featured Replies

Posted

Hello!  I am looking to use ray tracing to find the position of the block the player is looking at.  Then, when the player right clicks the item, it summons a lightning bolt at that block's location.  I have created my own code for this, but nothing is currently working (not summoning lightning at all).  I am new to ray tracing but apparently this is not how it is done (question mark?).  I am just looking to see what I have done wrong and to build on knowledge for the future.

 

import net.minecraft.block.Block;
import net.minecraft.block.Blocks;
import net.minecraft.entity.Entity;
import net.minecraft.entity.EntityType;
import net.minecraft.entity.effect.LightningBoltEntity;
import net.minecraft.entity.player.PlayerEntity;
import net.minecraft.item.Item;
import net.minecraft.item.ItemStack;
import net.minecraft.stats.Stats;
import net.minecraft.util.ActionResult;
import net.minecraft.util.ActionResultType;
import net.minecraft.util.Hand;
import net.minecraft.util.math.BlockPos;
import net.minecraft.util.math.BlockRayTraceResult;
import net.minecraft.util.math.EntityRayTraceResult;
import net.minecraft.util.math.MathHelper;
import net.minecraft.util.math.RayTraceContext;
import net.minecraft.util.math.RayTraceContext.FluidMode;
import net.minecraft.util.math.RayTraceResult;
import net.minecraft.util.math.vector.Vector3d;
import net.minecraft.world.World;
import net.minecraft.world.server.ServerWorld;

public class LightningWandItem extends Item
{
	public LightningWandItem(Properties properties) 
	{
		super(properties);
	}

	private RayTraceResult rayTrace(World world, PlayerEntity player)
	{
		float rotationPitch = player.getYHeadRot();
		float rotationYaw = player.xRot;
		Vector3d eyePosition = player.getEyePosition(1.0F);
		
		// look vector
		float f2 = MathHelper.cos(-rotationYaw * ((float)Math.PI / 180F) - (float)Math.PI);
		float f3 = MathHelper.sin(-rotationYaw * ((float)Math.PI / 180F) - (float)Math.PI);
		float f4 = -MathHelper.cos(-rotationPitch * ((float)Math.PI / 180F));
		float f5 = MathHelper.sin(-rotationPitch * ((float)Math.PI / 180F));
		float f6 = f3 * f4;
		float f7 = f2 * f4;
		double rayTraceDistance = 54;
		
		// from start pos, add look vector multiplied by rayTraceDistance
		Vector3d endPosition = eyePosition.add((double)f6 * rayTraceDistance, (double)f5 * rayTraceDistance, (double)f7 * rayTraceDistance);
		return world.clip(new RayTraceContext(eyePosition, endPosition, RayTraceContext.BlockMode.OUTLINE, FluidMode.NONE, player));
	}
	
	public ActionResult<ItemStack> onItemRightClick(World worldIn, PlayerEntity playerIn, Hand handIn) 
	{
		if(!worldIn.isClientSide)
		{
			ItemStack stack = playerIn.getItemInHand(handIn);
			RayTraceResult result = this.rayTrace(worldIn, playerIn);
			if(result instanceof BlockRayTraceResult)
			{
				BlockPos pos = ((BlockRayTraceResult) result).getBlockPos();
				Block block = worldIn.getBlockState(pos).getBlock();
				if(block != Blocks.AIR)
				{
					if(worldIn instanceof ServerWorld)
					{
						LightningBoltEntity lightningEntity = new LightningBoltEntity(EntityType.LIGHTNING_BOLT, worldIn);
						lightningEntity.setPos(pos.getX(), pos.getY(), pos.getZ());
						
						playerIn.awardStat(Stats.ITEM_USED.get(this));
						if (!playerIn.abilities.mayfly) {
							stack.hurtAndBreak(1, playerIn, (p_219998_1_) -> {
								p_219998_1_.broadcastBreakEvent(handIn);
							});
						}	
					}
				}
				return new ActionResult<ItemStack>(ActionResultType.SUCCESS, stack);
				
			} else if(result instanceof EntityRayTraceResult)
			{
				Entity entity = ((EntityRayTraceResult) result).getEntity();
				if(worldIn instanceof ServerWorld)
				{
					Vector3d entityPos = entity.position();
					LightningBoltEntity lightningEntity = new LightningBoltEntity(EntityType.LIGHTNING_BOLT, worldIn);
					lightningEntity.setPos(entityPos.x(), entityPos.y(), entityPos.z());
				
					playerIn.awardStat(Stats.ITEM_USED.get(this));
					if (!playerIn.abilities.mayfly) {
						stack.hurtAndBreak(1, playerIn, (p_219998_1_) -> {
							p_219998_1_.broadcastBreakEvent(handIn);
						});
					}
				}
				return new ActionResult<ItemStack>(ActionResultType.SUCCESS, stack);
			}
			
			playerIn.getItemInHand(handIn);
		}
		return super.use(worldIn, playerIn, handIn);
	}
}

 

I can't tell if you are doing ray tracing right, but there's already a method in Entity called getLookVec or something similar. Secondly, you've create the lightning entities but had never add them to the world. Use World#addEntity

  • Author

I do now realize that I wasn't using the correct method.  I have changed "onItemRightClick" to "use" and now the item does function.  It functions because I also added the entity to the world, as you had mentioned that I had not added it (oops).  Now, it does summon lightning, but only whenever I look certain ways and my ray tracing does not work well because the lightning is not summoned where I am looking.  I will try to use getLookVec (or whatever the name is) to trace instead and see how that goes.

  • Author

I have changed the code so that it now overrides the method.  However, I think there are some problems with my raytracing method.  It does not properly trace where the player is looking and grabbing that block pos.

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.