Solved Get highest block underneath player

Discussion in 'Plugin Development' started by 808sFinest, May 14, 2016.

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

    808sFinest

    Hey guys! Can someone show me how to get the highest block underneath a player, im working on a factions fly plugin and need to know this!
     
  2. world.getHighestBlockAt(x, z)
    or loop through all blocks starting from y = playerY - 0.5 to 0
     
  3. Offline

    ChipDev

    Exactly- but you cannot use that Method becuase it gets the highest block no matter if it is above the player, so, I think, there SHOULD be a way to find the highest block BENEATH a y value.
     
  4. Offline

    Zombie_Striker

    Inside, you should subtract 1. By subtracting a double, you would have to cast each y value to an int to get the block, and you would get the same block twice.


    @808sFinest
    To get the highest block under a player, get the player's location and create a for loop. Each time, subtract 1 from the y. If the block at the new location is equal to something that is not air/null, then you can return that block at that location.
     
  5. Is it that hard to cast to int?

    What if you stand on a half slab: when starting with -1 it would get the block below it, not the one the player is standing on. That's why I chose -0.5
     
  6. Offline

    Zombie_Striker

    @FisheyLP
    True, but you are using up twice the amount of memory to check for the same blocks twice just on the off chance the player is standing on a slab. If you want to check for slabs, you could also include a check for the player's current Y value as well.
     
  7. *subtracting 0.5 from a number* *server crashes with OutOfMemoryException*
    seriously that takes up not very much memory and I don't think he'll run that code 20 times a second with 5000 players online.
     
  8. Offline

    808sFinest

    sorry guys :( but im lost, im new to for loops. Heres wat i got

    Block b = p.getLocation().getBlock();

    for(int y = p.getLocation().getBlockY(); b.getType() != Material.AIR; y--) {
     
  9. Offline

    I Al Istannen

    @808sFinest
    What do you do inside the for loop body (Inside the brackets)?
    If you change b to the block one down (b = b.getRelative(BlockFace.DOWN) or subtract one from the location and then call getBlock() again or use World#getBlockAt(int x, int y, int z)) your code should work. Just make sure, that y is NEVER smaller than zero.
     
  10. That's how it could work

    Code:
    Block b;
    for (int y = p location blockY - 0.5; y > 0; y--) {
    set b to block at location y
    if (block is not air) break;
    }
    
    //do what you want with that block
     
  11. Offline

    808sFinest

    do i initialize block by adding =null? sorry.. and btw im trying to tp the player on top of the block
    @FisheyLP
     
  12. Offline

    I Al Istannen

    @808sFinest
    Yes, you do it.
    You might want to put this project on hold for a/some week(s) or so, while you learn a bit more about the basics of Java though.

    On the top. What is the problem? You may need to add 1 to the y coordinate.
     
  13. Offline

    808sFinest

    Why is this tping me to the lowest y(0.5)? theres blocks at y 1-4 btw

    Location pl = p.getLocation();

    Location b = new Location(p.getWorld(), pl.getBlockX(), pl.getBlockY(), pl.getBlockZ());

    if (cmd.getName().equalsIgnoreCase("for")) {

    for (double y = p.getLocation().getBlockY() - 0.5; y > 0; y--) {

    if (!b.getBlock().getType().equals(Material.AIR)) break;

    p.teleport(new Location(p.getWorld(), p.getLocation().getX(), y, p.getLocation().getZ()));

    p.sendMessage("" + y);
     
  14. Offline

    DoggyCode™

    Code:
    Player p;
    Block lowestBlock = null;
    Location loc = p.getLocation();
    for(int y = loc.getBlockY() - 0.5; y > 0; y--) {
      lowestBlock = loc.getWorld().getBlockAt(loc.add(0, -0.5, 0));
      if(b.getType() != Material.AIR) {
        break;
      }
    }
    
    if(lowestBlock == null) {
      //block was not found
    }
    Should work.
     
  15. Offline

    I Al Istannen

    @DoggyCode™
    You messed up... :p
    "b.getType("
    b is never declared​

    "lowestBlock == null"
    You assign it a value at every iteration. Will never be null​

    "add(0, -0.5, 0)"
    Why not use subtract(d,d,d)?
    "int y = loc.getBlockY() - 0.5; y > 0; y--"
    you never use y. Check if loc.getY() > 0;
    If you give code, working one would be nice :p

    Now, this is also code. Bad or not? At least working (and honestly not much magic involved). The explanation in words was already given.

    I would make a method for that. Optional is just there to force you to think about the result :p I could return null, but a NoSuchElement exception (if you choose to totally ignore everything) is much more descriptive than a NPE.
    Code:
        public Optional<Block> getNextBlockUnderPlayer(Player player) {
            Location loc = player.getLocation();  // get the location of the player
            Block block; // define a block variable. Could also do that in the loop, but here it is possible too.
            while(loc.getY() >= 0) {  // as long as we are not in the void, perform the actions in the body
                loc.subtract(0, 0.5, 0);  // subtract 0.5 from the players location
                block = loc.getBlock();  // get the block at the location (now 0.5 deeper)
                if(block.getType() != Material.AIR) {  // if this is not Air ==> A block!
                    return Optional.of(block);  // return the block
                }
            }
            return Optional.empty(); // nothing found (as we never returned above). Return an empty optional to indicate this.
        }

    EDIT: Okay, after seeing looking at your code a bit further (use [code=java] <code> [/code] tags the next time), the code could look like magic to you. I would suggest you learning some more Java first.
     
    DoggyCode™ likes this.
  16. Offline

    808sFinest

    I got it to work, but it sometimes does nothing when your an specific amount of blocks above ground. Does anybody know y and how i can fix it?
    Code:
    Player p = (Player) sender;
    
            Location ploc = p.getLocation();
    
            Block block;
    
            if (cmd.getName().equalsIgnoreCase("for")) {
    
                for (int y = ploc.getBlockY(); y > 0; y--) {
    
                    ploc.subtract(0, 0.5, 0);
    
                    block = ploc.getBlock();
    
                    if (block.getType() != Material.AIR) {
    
                        p.teleport(new Location(block.getWorld(), block.getX(), block.getY() + 1, block.getZ()));
    
                        break;
    
     
  17. Offline

    I Al Istannen

    @808sFinest
    Well, my method seems to work. You could have a look at it. Also I would encourage you to press CTRL+A and then CTRL+SHIFT+F.

    Your for is also never closed, so you miss some code.
    Also look at my comments to Doggy's code, some of them apply to yours as well.

    AND i realized your error. Just do what I said above (and maybe look at my code) and you will fix it automatically.

    The error is a logic one.
    In your for: "y > 0; y--"
    In your body: "ploc.subtract(0, 0.5, 0);"
    See the error? No? Well, the y coordinate will decrease by one each iteration, but the pLoc one only by 0.5. This is the error.
     
  18. Offline

    808sFinest

    ok i got it thanks to all that helped :D
     
Thread Status:
Not open for further replies.

Share This Page