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

Hi,

I'm trying to use raytracing for an item but when I check if the typeOfHit is an entity, its always false. Do you know why this is?

Heres my code:

@Override
public ItemStack onItemRightClick(ItemStack stack, World world, EntityPlayer player) {
	final double eyeHeight = 1.62;
	final double reachDistance = 300;
	Vec3 startPos = Vec3.createVectorHelper(player.posX, player.posY, player.posZ);
	if (!world.isRemote) startPos = startPos.addVector(0, eyeHeight, 0);
	Vec3 look = player.getLook(1.0F);
	Vec3 endPos = startPos.addVector(look.xCoord * reachDistance, look.yCoord * reachDistance, look.zCoord * reachDistance);
	MovingObjectPosition rarTrace = world.rayTraceBlocks(startPos, endPos);

	if (rarTrace != null) {
		int x = rarTrace.blockX;
		int y = rarTrace.blockY;
		int z = rarTrace.blockZ;
		List e = world.getEntitiesWithinAABBExcludingEntity(player, AxisAlignedBB.getBoundingBox(x-1, y-1, z-1, x+1, y+1, z+1));
		if(Math.abs(Math.sqrt(player.posX*player.posX + player.posY*player.posY + player.posZ*player.posZ) - Math.sqrt(x*x+y*y+z*z)) < 100){
			if(!player.capabilities.isCreativeMode)stack.damageItem(1, player);
			for(Object o : e){
				if(o instanceof EntityLivingBase)((EntityLivingBase)o).addPotionEffect(new PotionEffect(Potion.moveSlowdown.id, 100, 2, true));
			}
		}
	}
	return stack;
}

Check out my mod, Realms of Chaos, here.

ย 

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

It's because rayTraceBlocks only raytraces blocks, not entities.

ย 

Look in EntityRenderer.getMouseOver() for the code which raytraces entities as well...

ย 

-TGG

  • Author

Oops. Thanks. I'd imagine you'd then send a packet?

Check out my mod, Realms of Chaos, here.

ย 

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

Hi

ย 

If you need server-side code, personally I'd just copy the entity-ray-tracing code to your own class and tweak it to do what you need, it's pretty straight forward.ย  Packet from client to server would also work.

ย 

Depending on what you're trying to do, Item.itemInteractionForEntity() might also work best of all.ย  See EntityPlayer.interactWith() for the relevant vanilla code related to interacting with Entity you've just clicked on.

Alternatively attack - see EntityPlayer.attackTargetEntityWithCurrentItem() such as AttackEntityEvent

ย 

-TGG

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.