[Unsolved] Detecting Player Jump

Discussion in 'Plugin Development' started by puyttre, Mar 1, 2013.

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

    puyttre

    Hi,

    I have found and read many topics on how to detect this, but none of them seem to come to a conclusion. I have seen methods like:
    Code:java
    1. @EventHandler
    2. public void onJump(PlayerMoveEvent e) {
    3. if (e.getFrom().getY() < e.getTo().getY()) {
    4. // Player is jumping
    5. }
    6. }
    but this method always detects about 4 jumps, not one and can detect players swimming up water/climbing up ladders.

    I have also seen a post using this method:
    Code:java
    1. @EventHandler
    2. public void onJump(PlayerMoveEvent e) {
    3. if (e.getPlayer().getFallDistance() >= 1) {
    4. // Player is jumping
    5. }
    6. }
    but this method detects when a player is falling, not necessarily jumping.

    I would like to know if any of you have a solution to this problem that I still have no answer to.

    Thanks!
     
  2. Offline

    caseif

    Keep track of players who are jumping in a global List. If a player jumps and is not in it, add them. If they are, don't. If they move, but the y doesn't change or lowers, remove them from the List.
     
    Wizehh likes this.
  3. Offline

    chasechocolate

    Maybe check if the event.getTo().getBlockY() == event.getFrom().getBlockY() + 1, although that may be called multiple times.
     
  4. Offline

    puyttre

    Hmm. That is an interesting method :) However, after testing it, it appears that this also detects when players walk up stairs and when a player is swimming up/climbing ladder.
     
  5. Offline

    Gravity

    Check if the block below them is air.
     
  6. Offline

    chasechocolate

    Specifically event.getTo().getWorld().getBlockAt(event.getTo()).
     
  7. Offline

    Gravity

    .getRelative(BlockFace.DOWN);
     
  8. Offline

    chasechocolate

    Ah, yes, forgot that. Good point.
     
  9. Offline

    MrTwiggy

    Why not keep tracking of all the players and their 'onGround' state, and see on player move event or whatever if their 'onGround' state changes.
     
  10. Offline

    puyttre

    This seems to be the trick! It only leaves one problem though, which is when a player is jumping from a slab or another sort of half block. I just tested some code using e.getFrom().getBlock().getType() == Material.AIR || e.getFrom().getBlock().getType() == Material.STEP but it doesn't seem to work. Any suggestions, anyone?

    Edit: Didn't see new posts

    Edit 2: Same thing with the e.getTo().getWorld().getBlockAt(e.getTo()).getRelative(BlockFace.DOWN).getType() == Material.AIR as the code above. It still doesn't detect when a player has jumped from a half block.
     
  11. Offline

    Gravity

    Wait what? Explain "doesn't detect", because your intent is to see when a player is jumping, no? And they are jumping to a block?
     
  12. Offline

    puyttre

    The code I have right now can detect when a player jumps from a regular block. But if a player jumps on a half block, it won't detect the jump for some reason.

    Here's the code:
    Code:java
    1. @EventHandler
    2. public void onJump(PlayerMoveEvent e) {
    3. if (e.getTo().getBlockY() == e.getFrom().getBlockY() + 1 && e.getTo().getWorld().getBlockAt(e.getTo()).getRelative(BlockFace.DOWN).getType() == Material.AIR) {
    4. // jump
    5. }
    6. }


    Any suggestions, anyone?

    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: May 31, 2016
  13. Offline

    puyttre

    Bump. Still haven't solved this. :p
     
  14. Offline

    mcat95

    Ok,
    1º Checking the block under the player.getType().isTransparent == true
    2º I don't know if player.getVelocity() may help you.
    3º Calculate the time that a jump takes, get the Y before jump and the Y x seconds later, if it has incresased the height of the jump, the player has jump
    4º This may help you: https://github.com/FriedTaco/godPow...o/taco/godPowers/godPowersPlayerListener.java
    5º event.getTo() and event.getFrom() on MoveEvent maybe?
     
  15. Offline

    EcMiner

    @EventHandler
    public void onJump(PlayerMoveEvent e) {
    Player p = e.getPlayer();
    Location loc = p.getLocation();
    Block b = p.getWorld().getBlockAt(loc.getX(), loc.getY() - 1, loc.getZ());
    if(e.getFrom().getY() != e.getTo().getY() && b.getType == Material.AIR) {
    // your code here
    }
    }
     
  16. Offline

    ziimzy

    its getBlockAt(loc.getBlockX(), loc.getBlockY() - 1, loc.getBlockZ() They are ints not doubles.
     
  17. Offline

    Deathmarine

    I did a project a while ago that would enhance gravity and the way it functions; superjump / slowing fall rate. The best recommendation I've found was to use a check for onGround however this also shows true for players falling which a simple check for .getFrom().getY() < .getTo().getY() this will show the upward movement of the player. However after the apex of the jump this will not hold true. Do keep in mind that this listener will mostly be called several times a sec while moving hence the need for a simple boolean like onGround with less logic.
     
  18. Offline

    EcMiner

    Idk, this is just the method i use, and for me it works perfect
     
  19. Offline

    ziimzy

    eh it threw errors for me
     
  20. there is a entity.isonground(), maybe you can see if it goes from true to false?
     
  21. Offline

    EcMiner

    Ohh yeah i forgot to cast loc.getX(), loc.getY() and loc.getZ() to an int, and i mis spelled the getType, it had to be be: getType(), it should be like this:

    Code:java
    1. @EventHandler
    2. public void onJump(PlayerMoveEvent e) {
    3. Player p = e.getPlayer();
    4. Location loc = p.getLocation();
    5. Block b = p.getWorld().getBlockAt((int)loc.getX(), (int)loc.getY() - 1, (int)loc.getZ());
    6. if (e.getFrom().getY() != e.getTo().getY() && b.getType() == Material.AIR) {
    7. // Your code here
    8. }
    9. }
     
  22. Offline

    ziimzy

    Looks about right :) Gonna test it soon thanks
     
  23. Offline

    ziimzy

    Nope :(
     
Thread Status:
Not open for further replies.

Share This Page