Solved Player on ground

Discussion in 'Plugin Development' started by Pugmatt, Jun 28, 2014.

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


    There are lots of threads that ask this problem: How can I get if a player is on the ground? For some reason, I can't get anyway to work. I have tried several ways of doing this:

    1. Check for fall damage. The flaw: I want to get if it hits the ground even if it's just a small fall, which won't cause fall damage. So with this method it does not catch every time the player is on the ground.

    2. Simply check the block below player on Player Movement Event. Flaw: Players can crouch and go to the very edge of a block, to where they are basically floating on a AIR block. And that is the major flaw.[​IMG]
    I did some debug messages in Player Movement Event to see if any of these work (Which are all called while on the very edge of the block):
    1. event.getPlayer().sendMessage("getBlock with round: " + new Location(event.getPlayer().getWorld(), Math.round(event.getPlayer().getLocation().getBlockX()), Math.round(event.getPlayer().getLocation().getBlockY() - 1), Math.round(event.getPlayer().getLocation().getBlockZ())).getBlock().getType().name());
    2. event.getPlayer().sendMessage("getBlock: " + new Location(event.getPlayer().getWorld(), event.getPlayer().getLocation().getBlockX(), event.getPlayer().getLocation().getBlockY() - 1 , event.getPlayer().getLocation().getBlockZ()).getBlock().getType().name());
    3. event.getPlayer().sendMessage("get with round: " + new Location(event.getPlayer().getWorld(), Math.round(event.getPlayer().getLocation().getX()), Math.round(event.getPlayer().getLocation().getY() - 1), Math.round(event.getPlayer().getLocation().getZ())).getBlock().getType().name());
    4. event.getPlayer().sendMessage("get: " + new Location(event.getPlayer().getWorld(), event.getPlayer().getLocation().getX(), event.getPlayer().getLocation().getY() - 1, event.getPlayer().getLocation().getZ()).getBlock().getType().name());
    5. event.getPlayer().sendMessage("BlockFace: " + event.getPlayer().getLocation().getBlock().getRelative(BlockFace.DOWN).getType().name());

    They all return AIR. I have tried and tried, and can't seem to solve this. I don't usually post questions on here, because 99% of the time I usually find a working answer on an old thread. This though I am stumped with. So if anyone knows the answer to this, I would be super grateful, I've been trying to figure this out for too long. :p

  2. Offline


    I doubt this is the best way to do this but you could make a repeating task that runs every tick and check if the player is on the ground.
  3. Offline


    Jogy34 Doesn't really fix my problem, since the part with getting whether or not the player is on the ground is the problem. If the player stands on the block normally without being on the edge I can get whether or not it's on the ground just fine. But if they go on the edge of the block as displayed in the picture in the thread it gives back AIR.

    EDIT: I think this is impossible to fix. I'm probably just going to have to try to work with fall damage or something. If someone knows if it is indeed possible though, please tell. :)
  4. Offline


    Is it possible with the .isOnGround method? I think it works... [Even though its deprecated]
  5. Offline


    BlazingBroGamer According to some threads it's not recommended, because it can be called by "NoFall" on hacked clients or something. I'd rather play it safe if possible.

    @Jogy34 Not sure if this if this was your intended idea, but I might try to use a timer like you said and just check whether or not the Y location of the player hasn't changed for a certain amount of time. Seems kind of messy though, but I'll see if it works.
    EDIT: Damn you posted right when I tagged you :p
  6. Offline


    Sorry, I misunderstood your question.

    Check 0.3 from the players position in every X and Z direction (The player's bounding box is 0.6 wide). If that increments or decrements the player's truncated coordinates that means that they are standing on that block as well. Check all of the blocks and if any of them are solid then they should be on the ground.

    For instance if the player is standing at X = 10.2 and Z = 73.8 then they are standing on [(10, 73), (9, 73), (10, 74), (9, 74)] with whatever Y coordinate. If any of those are solid blocks then they are standing on the ground.
    Pugmatt likes this.
  7. Offline


    Pugmatt Well if you don't want to use the isOnGround() method, just check the location below the player to see if it is air/water/lava/whatever other blocks aren't solid.
  8. Offline


    Pugmatt When a player is off the ground (unless they're flying), not only would the block under them be air but they'd also be moving with velocity, especially along the y-axis. You'd probably want to examine both of these things.
  9. Offline


    You're a life saver! I had thought of maybe doing something like this once, but wasn't sure the exact way of doing it. But your way works perfectly! Thank you very much. :D
Thread Status:
Not open for further replies.

Share This Page