Eternaldoom Posted January 24, 2015 Share Posted January 24, 2015 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; } Quote Check out my mod, Realms of Chaos, here. If I helped you, be sure to press the "Thank You" button! Link to comment Share on other sites More sharing options...
TheGreyGhost Posted January 24, 2015 Share Posted January 24, 2015 It's because rayTraceBlocks only raytraces blocks, not entities. Look in EntityRenderer.getMouseOver() for the code which raytraces entities as well... -TGG Quote Link to comment Share on other sites More sharing options...
Eternaldoom Posted January 25, 2015 Author Share Posted January 25, 2015 Oops. Thanks. I'd imagine you'd then send a packet? Quote Check out my mod, Realms of Chaos, here. If I helped you, be sure to press the "Thank You" button! Link to comment Share on other sites More sharing options...
TheGreyGhost Posted January 25, 2015 Share Posted January 25, 2015 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 Quote Link to comment Share on other sites More sharing options...
Recommended Posts
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.