Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How would I detect if a player is looking at another entity?

Tags:

bukkit

I have a player and of course able to grab the direction the player is facing in vector form.

Using this vector, I need to calculate if the player is looking at an entity within x blocks and if a 'spell' would hit it. I would also need to take in account if there is anything in front of the entity.

So far my thought process has been to first, get a list of all entities in x blocks of the player. But from there, I have no clue.

Any help leading me in the right direction would be awesome.

like image 889
SexyToad Avatar asked Jul 07 '15 02:07

SexyToad


People also ask

What is the command to see entities in Minecraft?

EntityData Command in Minecraft Java Edition (PC/Mac) It can be either the UUID for an entity or you can use the @e target selector to target all entities or a type of entity. (See Minecraft Entities). dataTag is optional. It is the data tag that you want to set.


1 Answers

I edited your code to be more precise and less complicated, it doesn't go through blocks. But your method is fine to :D

public static Entity getNearestEntityInSight(Player player, int range) {
    ArrayList<Entity> entities = (ArrayList<Entity>) player.getNearbyEntities(range, range, range);
    ArrayList<Block> sightBlock = (ArrayList<Block>) player.getLineOfSight( (Set<Material>) null, range);
    ArrayList<Location> sight = new ArrayList<Location>();
    for (int i = 0;i<sightBlock.size();i++)
        sight.add(sightBlock.get(i).getLocation());
    for (int i = 0;i<sight.size();i++) {
        for (int k = 0;k<entities.size();k++) {
            if (Math.abs(entities.get(k).getLocation().getX()-sight.get(i).getX())<1.3) {
                if (Math.abs(entities.get(k).getLocation().getY()-sight.get(i).getY())<1.5) {
                    if (Math.abs(entities.get(k).getLocation().getZ()-sight.get(i).getZ())<1.3) {
                        return entities.get(k);
                    }
                }
            }
        }
    }
    return null; //Return null/nothing if no entity was found
}
like image 187
PhantomUnicorns Avatar answered Sep 18 '22 17:09

PhantomUnicorns