Get the block that is "x" away from the player (in his direction)

Discussion in 'Plugin Development' started by orange451, Aug 1, 2012.

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

    orange451

    Code:
     public static double lengthdir_x(double len, double dir) {
    return len * Math.cos(Math.toRadians(dir));
    }
     
    public static double lengthdir_y(double len, double dir) {
    return -len * Math.sin(Math.toRadians(dir));
    }
     
    public Block getBlockXAway(Player p, double distance) {
    Location ploc = p.getLocation().add(0,2,0);
    double pitch = -ploc.getPitch();
    double direction = -ploc.getYaw()-90;
    System.out.println(pitch);
    System.out.println(direction);
    int xx = (int) (ploc.getBlockX() + ( lengthdir_x(distance, direction) * lengthdir_x(1, pitch)));
    int yy = (int) (ploc.getBlockY() + (-lengthdir_y(distance, pitch)));
    int zz = (int) (ploc.getBlockZ() + ( lengthdir_y(distance, direction) * lengthdir_x(1, pitch)));
    Location nloc = new Location(ploc.getWorld(), xx, yy, zz);
    Block b = nloc.getBlock();
    return b;
    }
    What I wrote above works perfectly. It finds the player, his direction, and gets the block that is "x" away from the player and still is in his line of sight. However, I want to know whether or not this is the fastest way to go about this. Is it?
     
  2. Offline

    pzxc

    You're not using any loops, so the difference between your method and any other method will be pretty negligible.
     
Thread Status:
Not open for further replies.

Share This Page