Jump to content

Recommended Posts

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!

Posted

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...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Announcements



×
×
  • Create New...

Important Information

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