Solved Invisible Projectiles without NMS?

Discussion in 'Spigot Plugin Development' started by Ugleh, Mar 8, 2018.

  1. Trying to create a wand, and this wand needs to select a target. Since Minecraft doesn't have any Ray Casting ability I figured I would just have an invisible projectile be my "ray caster". From what I knew already and what I googled to do this I would have to send a PacketPlayOutEntityDestroy packet to every player online so it looks invisible. The only problem is I do not want to have to use reflections to support multiple Spigot versions. Is there another way to do this? Either it be not using invisible projectiles or anything?

    My end goal is to left_click_air a Material.Stick and be able to find the entity target the player is aiming at.
     
  2. Another option, much more work but ultimately cleaner and gives you a lot more control, is to do the raycasting yourself.

    It's not super hard, you can start with a BlockIterator, walk forward one block at a time and then use World.getNearbyEntities to see if there is an entity standing in that block. Repeat until you hit an entity, hit a solid block, or hit the range of your iterator.

    This will give you a very basic projectile. Better, but more complicated, is to use a ray/box intersection test for entities, using their hitboxes. This will require some reflection/NMS to get hitboxes, though you can roughly approximate them without NMS.

    This technique would involve separately using a BlockIterator to get the closest block in the player's view, and then also do a ray/box test to find the closest entity intersecting the player's line of sight. Whichever is closer is your target.

    Less complex (if you're not up for the ray/box match), do the BlockIterator and then also get all entities near the player within a radius that matches your range. Sort this list by some combination of distance to player and angle from player's look direction. Basically what you want is to choose the entity that is most in their line of sight (smallest angle from eyes to entity versus player direction) and closest to them.

    The last method is pretty simple, it was the first targeting method I implemented and though it's a little fuzzy, with some tweaking (mainly weights of angle vs range) it worked pretty well.

    So you do have some options that don't rely on hacking around with vanilla projectiles, there are going to be a lot of drawbacks that are hard to work around using that method (arrows don't fly perfectly straight, do odd things at high velocities, etc).
     
    • Agree Agree x 1
    • Winner Winner x 1
  3. The block iterator seams very hacky but probably the way I am going to go. Thanks.
     
    • Like Like x 1

Share This Page