Get highest block at Y that below the player

Discussion in 'Plugin Development' started by Deckerz, Jun 6, 2013.

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


    .getHighestBlockAtY() returns the highest block but i want it to return the highest block like that but it has to be under the player.


    .getHighestBlockAtY() returns 134 but the player is standing at 100Y

    .getHighestBlockAtYBelowPlayer() is a new method that checks the highest block below the player and returns 45 for example

    Hopefully that explains it enough
  2. Offline

    Windy Day

    Something along the lines of this should work. Hope it helps!

    NOTE: Didn't test this but I have used something like it before and of course it doesn't need to be it's own method you can just put it in something like a command too.

        public static void teleportTopBlock (Player player) {
            Location plocation = player.getLocation();
            int topX =plocation.getBlockX();
            int topZ = plocation.getBlockZ();
            int topY = plocation.getWorld().getHighestYAt(topX, topZ);
            player.teleport(new Location(player.getWorld(), topX, topY + 1, topZ, plocation.getYaw(), plocation.getPitch()));
  3. Offline


    Thank for the help but that isnt what i wanted, i wanted it so say at a location the highest point is Y = 65 BUT the player is located below Y = 65. i want it to get the highest Y BELOW the player
  4. Offline

    Windy Day

    So you want the highest Y at the block below their feet?

    EDIT: Because this should get the highest block at their location because it gets the world, then it gets highest block at topX and topZ which where found by getting the players location then getting the X and Z coordinate.
  5. Offline


    what if their underground? i want to find the highest Y BELOW them so a plugin im making can be used underground
  6. Offline


    iterate over every Y integer that is below the player's Y position, and see if the block there is of Material.AIR...if it is NOT, you've got your block.
  7. Offline


    i forgot to mention its a area like a wand and i want it to make the ground full with grass but if your under a arch it doesn't place where you looking
  8. Offline

    Windy Day

    So you want it placing grass around the player unless they are under something? Or do you want it placing grass around them at the next air block (for this you wouldn't get highest block at Y)?
  9. Offline


    No, say i right click a bone and get the block im looking at then say its a hill i want it to cover the surface of the hill with grass. but say the hill is under a cliff or something the .getHighestBlockAtY will return the first block ontop of the cliff not the the hill
  10. Offline

    Windy Day

    Yes, so do you want help getting that to work? For the block clicked?
  11. Offline


    no just hoe to calculate the highest Y
  12. Offline

    Windy Day

    But, by highest Y you mean the next block of air?
    Something like this will find the highest Y that is "on the hill" not the cliff.
    I hope this is what you are looking for.

    public void whateveryouwant (Block block) {
            Location blocation = player.getLocation();
            int topX = blocation.getBlockX();
            int topZ = blocation.getBlockZ();
            int topY = blocation.getBlockY();
            String type = blocation.getBlock().getType().toString();
            if (!(type=="AIR")) {    //chekcing if the block is air
                while (!(type=="AIR")) { //if it is not air ...
                    topY++; //add one to Y
                    Location newy = new Location (block.getWorld(), topX, topY, topZ); //make a new location that is above the last
                    type = newy.getBlock().getType().toString(); //get the new locations block tpye
                if ((type=="AIR")) { //check if it is air if it is do this
                    //Do whatever here if the block is equal to air
                } //otherwise repeat
    EDIT: Made it so that is checks for a block sense you said you wanted blocks (you could change to player).
  13. Offline


    Ty for helping but imanged to make my brain figure it out, Ty anyway xD
  14. Offline


    public Location getBlockBelowLoc(Location loc) {
        Location locBelow = loc.subtract(0, 1, 0);
        if(locBelow.getBlock().getType() == Material.AIR) {
            locBelow = getBlockBelowLoc(locBelow);
        return locBelow;
    This is a recursive method that will return the first location underneath any other location that is not air.
Thread Status:
Not open for further replies.

Share This Page