I know this has been a while, but either i'm blind and can't see the solution in the given thread or it isn't there. Could someone pls point it out to me?
Here's how I would do it in a nutshell (little to no code): Get the block the player is staring at. Get the nearby entities to the location of said block within a tolerance of 1 block. ??? Profit!
I was thinking... 1. Get nearby entities and put them in a list 2. Do a foreach and if the entity equals the entity of herobrine 3. Profit??? But that doesn't cover them looking at him... Edit trying Taco's method...
Isn't there a method for that in the Location interface? I haven't messed with plugin coding for a while, so I'm a little rusty.
Only in player. Edit, just getting that 1 final piece: Code: Block targetBlock = p.getTargetBlock(null, 50); for (Entity e : targetBlock.) { if (e == heroEntity) { e.remove(); } }
For one, I recommend changing that == to instanceof. Second, let me look and see what I can find in the API. Try something like.... for(Entity e : block.getChunk().getEntities()) { if(e instanceof heroEntity && block.getLocation().distance(e.getLocation()) < 2) { e.remove(); } }
Hmm what happens if you directly look at the entity, does it return the air-block then? I think i'll have to take a look into the java-docs to see what i can achieve with targetBlock... Thanks for your answer, it brings me closer to a solution
The only mobs I see this being an issue with are flying mobs because I'm not sure if line of sight would return the airblock that stops at that mob.
Another trick that I'm using is just firing a projectile (arrow/snowball) at very high speed. The entity that it hits is what you are looking at You can set the projectile damage to zero as well. Another idea: maybe you can make this projectile invisible, same way people make the vanish mods, so you don't even see the projectile. Then there's just a little delay caused by travel time of the projectile
@Lathanael This is what I use to get the entity the player is looking at. Someone better at Java could probably improve on this, but this does work. Code: void getTarget() { List<Entity> nearbyE = plugin.player.getNearbyEntities(plugin.range, plugin.range, plugin.range); ArrayList<LivingEntity> livingE = new ArrayList<LivingEntity>(); for (Entity e : nearbyE) { if (e instanceof LivingEntity) { livingE.add((LivingEntity) e); } } plugin.target = null; BlockIterator bItr = new BlockIterator(plugin.player, plugin.range); Block block; Location loc; int bx, by, bz; double ex, ey, ez; // loop through player's line of sight while (bItr.hasNext()) { block = bItr.next(); bx = block.getX(); by = block.getY(); bz = block.getZ(); // check for entities near this block in the line of sight for (LivingEntity e : livingE) { loc = e.getLocation(); ex = loc.getX(); ey = loc.getY(); ez = loc.getZ(); if ((bx-.75 <= ex && ex <= bx+1.75) && (bz-.75 <= ez && ez <= bz+1.75) && (by-1 <= ey && ey <= by+2.5)) { // entity is close enough, set target and stop plugin.target = e; break; } } } } target is a LivingEntity variable. range is an int. Basically it adds all entities near the player (set by the range) to a list, and gets the living entities from them. Then it checks the location of the LivingEntity against the location of the blocks. If its close enough, then you have your entity, which you can then do whatever you want with.