if(player.isJumping?){

Discussion in 'Plugin Development' started by MariusAlexanderWinsjansen, Mar 18, 2012.

Thread Status:
Not open for further replies.
  1. Greetings,

    I wonder if someone could give me some ideas and tips on how I can define if a player is jumping, on the MoveEvent! I presume there is no other ways to check if the player is jumping, then check if he is moving? There is no event for it, and I could find any good methods!

    Hope someone can help me whit this issue!

    Best Regards,
    Marius Alexander Winsjansen
     
  2. Offline

    iMint

    Coding requires logic. So let's do this logically ;)

    Think of what happens when a player jumps. Well, he moves up one block right?
    And when he's moved up, what's directly below him? NOTHING. Air.
    So...
    We can check if the block below the player is air. If it is, he must be jumping! (or flying)

    if(player.getWorld().getBlockAt(player.getX(), player.getY() - 1, player.getZ()).getType() == Material.AIR){

    lets break this down really quick:

    player.getWorld().getBlockAt(x,y,z); this gets the Block at a certain location. we use x,y,z instead of player.getLocation() because we need to change the Y.

    player.getY() - 1 we're subtracting 1 from the Y value because we're getting the block DIRECTLY BELOW the player.

    ... == Material.AIR this is testing if the block is air. While a player is jumping, he has air below him.

    Thats all! :D I hope you enjoyed my mini-guide. ;) Feel free to copy/paste my guide as long as you give me credit lol :D

    - iMint
     
    treestompz likes this.
  3. Offline

    user_43347

    You can always do this...
    Code:
    if (player.getLocation().subtract(0, 1, 0).getBlock().getType().equals(Material.AIR)) {
    
    Also, compare with .equals whenever possible.
    However, this does not account if the player is falling, so you may want to try this.
    Code:
    Block a = player.getLocation().subtract(0, 1, 0).getBlock();
    Block b = player.getLocation().subtract(0, 2, 0).getBlock();
     
    if (a.getType().equals(Material.AIR) && !b.getType().equals(Material.AIR)) {
    
    In this case, we check if the block thats 2 below is not air, and if it isn't and the one below the player is, they are jumping.
     
    Iron_Crystal likes this.
  4. Offline

    bleachisback

    Actually that would still trigger if the player is falling, but only when he is one block off the ground. Try this instead:
    Code:java
    1. @EventHandler
    2. public void onPlayerMove(PlayerMoveEvent e)
    3. {
    4. if(e.getTo().getBlockY()>e.getFrom().getBlockY())
    5. {
    6. //do stuff
    7. }
    8. }


    However, this will only keep track of when people jump one block up. If they jump half a block up (Such as if a half slap is above them, upside-down), then it won't fire. If you instead want to know them jumping half-way (or less):

    Code:java
    1. @EventHandler
    2. public void onPlayerMove(PlayerMoveEvent e)
    3. {
    4. if(e.getTo().getY()>e.getFrom().getY())
    5. {
    6. //do stuff
    7. }
    8. }


    However, this will trigger multiple times during the jump.


    Hope this helps!
     
    hammale likes this.
  5. Offline

    AVOCARDO

    Try this...

    Code:
    @EventHandler(priority = EventPriority.NORMAL)
      public void onPlayerMoveEvent(PlayerMoveEvent event) {
           
        if (event.isCancelled()) {
          return;
        }
           
        if(event.getTo().getY() > event.getFrom().getY()) {
          Player p = event.getPlayer();
          if (p.getLocation().subtract(0, 1, 0).getBlock().getType().equals(Material.AIR)) {
            if (!p.isFlying()) {
              // Do Stuff
            }
          }
        }
           
    }
     
  6. Offline

    Hoolean

    This is what I've used to check if the player is on the ground or not:
    Code:
    boolean onGround = player.getLocation().getBlockY()==player.getLocation().getY();
     
  7. Offline

    fireblast709

    For starters, you might want to use the Y-axis
     
  8. Offline

    Hoolean

    Derp moment. Sorry. Changed.

    Anyways that's the code I use in CloneZ for seeing if the player is off the ground :)
     
Thread Status:
Not open for further replies.

Share This Page