Get Entities In Your Eye Location

Discussion in 'Plugin Development' started by mkezar, Jan 21, 2015.

Thread Status:
Not open for further replies.
  1. Offline

    mkezar

    Hi. I am trying to make an hp plug in that any player you look at, you get their hp in a scoreboard. I am capable of doing the other things but im new to trying to get entities in your eye location. So my question is, how do you get the list of entities in your cross hair / eye location? Please help! Thank you! :)
     
  2. Offline

    pie_flavor

    @mkezar Not sure if you knew this, but you can make the HP of players shown below their name using code. If you don't mind the format being Health: <1-20>, you can even do it in vanilla with /scoreboard objectives setdisplay belowName. You can probably put custom text below their names with packet and whatnot.
    As to your actual question, I'm pretty sure you're going to have to manually calculate the position of every player and decide if any player is actually looking at any other player based on the center of their screen.
     
  3. Offline

    mkezar

    I knew that but it would be a lot cooler if you could look at a player from such a far distance and get their health.
     
  4. Offline

    pie_flavor

    @mkezar You can also put their health (not the title "Health", just the value) in the tab-list using the vanilla tabList display slot.
     
  5. Offline

    mkezar

    I know this @pie_flavor but it would be awesome and a bit more impressive to have it so whenever you look at a player (maybe if the player is 50-100 blocks away) and you get their health
     
  6. Offline

    1Rogue

    -edit- wouldn't work.
     
  7. Offline

    mkezar

    Why? Is it client side?
     
  8. Offline

    1Rogue

    No I thought I had a solution at first and then realized it wouldn't. Also I'm a bit busy myself so I really should be avoiding posting on here right now.
     
  9. Offline

    sirrus86

    I use these methods to get a target entity. I can't say it's the most efficient, but so far it works:
    Code:
    @SuppressWarnings("unchecked")
    public <E extends Entity> E getTargetEntity(Class<E> clazz, double range) {
        Vector dir = player.getLocation().getDirection();
        for (Location loc = player.getEyeLocation().clone(); player.getEyeLocation().distanceSquared(loc) <= range * range; loc.add(dir)) {
            if (loc.getBlock().getType().isOccluding()) return null; // Block is in the way
            for (Entity entity : plugin.getPowerTools().getNearbyEntities(clazz, loc, 1.25D)) {
                if (entity != player) return (E) entity;
            }
        }
        return null;
    }
       
    @SuppressWarnings("unchecked")
    public <E extends Entity> Set<E> getNearbyEntities(Class<E> clazz, Location loc, double radius) {
        Set<E> tmp = new HashSet<E>();
        int cRad = radius < 16 ? 1 : (int)((radius - (radius % 16)) / 16);
        for (int x = -cRad; x < cRad; x ++) {
            for (int z = -cRad; z < cRad; z ++) {
                for (Entity entity : new Location(loc.getWorld(), loc.getX() + (x * 16), loc.getY(), loc.getZ() + (z * 16)).getChunk().getEntities()) {
                    if (entity.getLocation().distanceSquared(loc) <= radius * radius
                            && clazz.isInstance(entity)) {
                        tmp.add((E) entity);
                    }
                }
            }
        }
        return tmp;
    }
     
  10. Offline

    mkezar

    Wow thank you! I can't wait to test it out! So what this does is that it gets any entity in a 16 block radius of your eyesight? And now, how would make it so it sends a message after you look at the entity? I'm taking a guess here and I'm guessing you add the player.sendMessage(); To this if statement,

    if (entity.getLocation().distanceSquared(loc) <= radius * radius
    && clazz.isInstance(entity)) {
    tmp.add((E) entity);
    }
     
  11. @mkezar some time ago i made a mmrog plugin where i needed a method to find the exact target a player is aming at. because in the normal case the most ways will fuck up if there are two entities near together. but i created a method which works almost perfect:
    Code:
        private Player getTarget(Player source) {
              @SuppressWarnings("deprecation")
            // Sichtlinie und mögliche Ziele
            List<Block> blocksInSight = source.getLineOfSight(noCollisionBlocks, range);
            List<Entity> nearEntities = source.getNearbyEntities(range, range, range);
            // Ziele werden auf Sichtlinie geprüft
            if (blocksInSight != null && nearEntities != null) {
                for (Block block : blocksInSight) {
                    int xBlock = block.getX();
                    int yBlock = block.getY();
                    int zBlock = block.getZ();
    
                    for (Entity entity : nearEntities) {
                        if (entity instanceof Player) {
                            Location entityLocation = entity.getLocation();
                            int xEntity = entityLocation.getBlockX();
                            int yEntity = entityLocation.getBlockY();
                            int zEntity = entityLocation.getBlockZ();
                            // Überprüft den Standpunkt
                            if (xEntity == xBlock && (yEntity <= yBlock && yEntity >= yBlock - 1) && zEntity == zBlock) {
                                return (Player) entity;
                            }
                        }
                    }
                }
            }
            return null;
        }
    if the player aims at something this is the right way to get his target :) if you call the method and he isnt aiming at a Player it will return null. Combine this method with a move listener or sth like this and you re done :)

    GOD DAMN I HATE SPOONFEEDING xD but it took like forever to get up with this stuff, because you can't find anything like this on google so i made an exception for you :p
     
  12. Offline

    mkezar

    Wow thank you! :)
     
  13. Offline

    NathanWolf

    I'll just point out that this sort of thing is going to be pretty expensive- I suggest you make this feature work on a click (maybe a special "inspect player" item?)

    You don't want to be running entity targeting code constantly for all players while they're looking around.. I'd bet. Shrug :)
     
  14. Offline

    Not2EXceL

    SCC
    You do realize there's a research paper on the math behind ray intersection? Aandk used it in an example class already. Not hard to use and its way more efficient than this laggy mess.

    Seriously, iteration to retrieve blocks, iteration to get nearby entities, and finally let's take more time to iterate and compare with locations.
     
  15. Offline

    Experminator

    I say one thing:
    Player#getLineOfSight() and iterate that between 16 blocks (Or something).

    - Check if there's a entity and then check if the entity is a player.
    - Then it can return that player.

    And set the player data to a scoreboard/item/message or something.
    Works fine for me.
     
Thread Status:
Not open for further replies.

Share This Page