Found a cool thing.

Discussion in 'Resources' started by Cyrushehe, Jul 5, 2012.

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

    Cyrushehe

    This is not originally mine, I just found parts of this around the forum and made it into a function. You use this to get the first target entity within the players crosshairs. It took me so long to find something that would do this.

    Code:
        public static Entity getTarget(final Player player) {
     
            BlockIterator iterator = new BlockIterator(player.getWorld(), player
                    .getLocation().toVector(), player.getEyeLocation()
                    .getDirection(), 0, 100);
            Entity target = null;
            while (iterator.hasNext()) {
                Block item = iterator.next();
                for (Entity entity : player.getNearbyEntities(100, 100, 100)) {
                    int acc = 2;
                    for (int x = -acc; x < acc; x++)
                        for (int z = -acc; z < acc; z++)
                            for (int y = -acc; y < acc; y++)
                                if (entity.getLocation().getBlock()
                                        .getRelative(x, y, z).equals(item)) {
                                    return target = entity;
                                }
                }
            }
            return target;
        }
    If you know who to credit you should tell me! :3
     
    RentAMonkey likes this.
  2. Well, I know this, you should name this thread to reflect what the code otherwise it won't be easily found by searching and people who read it might read it out of pure curiosity... like I just did :}

    Also, you might want to not call entity.getLocation().getBlock() each iteration of those loops as it's basically doing getWorld().getBlockAt(blockX, blockY, blockZ), so you should store it :p
     
    ferrybig likes this.
Thread Status:
Not open for further replies.

Share This Page