Solved Detect Player Jump

Discussion in 'Plugin Development' started by MrGeneralQ, Feb 18, 2017.

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

    MrGeneralQ

    Okay so since there is someone with a plugin request that requires player jumps, I thinking on a sollution on how to detect a player jump. This is what I'm thinking off

    in the PlayerMoveEvent I detect if the player subtract block is air. Would that be a good way to check it?
     
  2. Offline

    Zombie_Striker

    @MrGeneralQ
    If event.getTo().getY() > event.getFrom().getY(), then the player is moving up.

    I'll also post this on that thread.
     
  3. Offline

    MrGeneralQ

    Is there no other event that will triger the player to increase Y? If so then would this indeed be the solution I need.
     
  4. Online

    timtower Administrator Administrator Moderator

  5. @MrGeneralQ
    You could use a repeating task, but other than that, no. Also, this Y checking method has one flaw, and it's that you can't tell the difference between the player moving up (explosion, slimeblock, moved by pistons etc) and the player jumping. I can't think of a good solution off the top of my head though.
     
  6. Offline

    MrGeneralQ

    @AlvinB
    They should fix that. This should be something they can easily create A class for. Detecting a jump sounds so simple and yet it is soo complicated

    @timtower
    Yes indeed, so @Zombie_Striker your solution will cause problems.
     
  7. @MrGeneralQ
    Why don't you test for the Y value in their vector?
     
  8. Offline

    MrGeneralQ

    @AlvinB
    They should fix that. This should be something they can easily create A class for. Detecting a jump sounds so simple and yet it is soo complicated

    @timtower
    Yes indeed, so @Zombie_Striker your solution will cause problems.
    @BananaPuncher714
    Same as @timtower said. Climbing a ladder, stair or even stepping on a single block would trigger it.
     
  9. @MrGeneralQ
    What if you tested for their Y velocity and if they're on the ground at the same time? That way, if they're going up stairs or ladders, they're on the ground, or else they're probably jumping.
     
  10. Offline

    PhantomUnicorns

    Test if their Y cord has a block in it??
    Like the slab stair ladder?
     
  11. @PhantomUnicorns
    if ( player.getVelocity().getY() >= 0 && !player.isOnGround() )

    or something like that
     
    MrGeneralQ likes this.
  12. Offline

    MrGeneralQ

    That might work!
     
  13. You could try something like:
    Code:
    if (player.getVelocity().getY() > 0 && blockBelow(player).getType().equals(Material.LAPIS_BLOCK)
    Note that the function blockBelow has to be made, but it should be very ease
     
  14. Offline

    MrGeneralQ

    Yea that wouldn't be a problem.
     
  15. Offline

    dNiym

    For block below simply use.

    Block b =
    player.getLocation().getBlock().getRelative(BlockFace.DOWN)

    Then check to see if the block below is air. If so and they're moving up they can't be on a slab so that would take stairs and slabs out of the equation.


    Sent from my iPhone using Tapatalk
     
  16. Offline

    MrGeneralQ


    I knew that, read my post again ;) . And when they are falling the block below is also air, however that shouldn't be a problem to fix.
     
  17. @MrGeneralQ
    Alright, I have been researching the best way to do this, and this is surprisingly hard. There is no actual information sent to the server that the player has pressed jump, all that's sent is the player's new position. Here's the code I came up with (along with an explanation of each of the checks):
    Code:java
    1. private Set<UUID> prevPlayersOnGround = Sets.newHashSet();
    2.  
    3. @EventHandler
    4. public void onMove(PlayerMoveEvent e) {
    5. Player player = e.getPlayer();
    6. if (player.getVelocity().getY() > 0) {
    7. double jumpVelocity = (double) 0.42F;
    8. if (player.hasPotionEffect(PotionEffectType.JUMP)) {
    9. jumpVelocity += (double) ((float) (player.getPotionEffect(PotionEffectType.JUMP).getAmplifier() + 1) * 0.1F);
    10. }
    11. if (e.getPlayer().getLocation().getBlock().getType() != Material.LADDER && prevPlayersOnGround.contains(player.getUniqueId())) {
    12. if (!player.isOnGround() && Double.compare(player.getVelocity().getY(), jumpVelocity) == 0) {
    13. player.sendMessage("Jumping!");
    14. }
    15. }
    16. }
    17. if (player.isOnGround()) {
    18. prevPlayersOnGround.add(player.getUniqueId());
    19. } else {
    20. prevPlayersOnGround.remove(player.getUniqueId());
    21. }
    22. }

    • player.getVelocity().getY() > 0 - pretty self-explanatory, just check if the player is moving upwards.
    • e.getPlayer().getLocation().getBlock().getType() != Material.LADDER - Check if the player isn't on a ladder (I had to manually check this, since the jump velocity is also added for ladders).
    • prevPlayersOnGround.contains(player.getUniqueId()) - Check if the player was on ground the previous time the event was fired. This is so the if statement only fires once. The reason it's a Set is because we need to get the previous onGround state, and player.isOnGround() is already updated to the new position when PlayerMoveEvent is fired.
    • !player.isOnGround() - Check if the player currently isn't on ground.
    • Double.compare(player.getVelocity().getY(), jumpVelocity) == 0 - Check if the player's velocity is equal to the predicted jumpVelocity (see EntityLiving#cm() for the equations). This is to eliminate pushing from various sources (slime blocks, explosions, etc)
     
  18. Offline

    MrGeneralQ

    Thats some huge code you got over There just for jump detection , they should implement a class or methode for it.
     
  19. @MrGeneralQ
    Yeah, I would submit a PullRequest to Spigot adding something like this, but apparently I'm not old enough to sign their contributor agreement :p
     
    MrGeneralQ likes this.
  20. Offline

    MrGeneralQ

    That's a pain in the ass. I can't believe that it isn't implemented yet :p
     
  21. Offline

    ipodtouch0218

    @AlvinB
    Wouldn't that need new number if the player happens to have Jump Boost?
     
  22. @ipodtouch0218
    Look carefully at the code - I have accounted for that :p
     
    ipodtouch0218 likes this.
  23. Offline

    MrGeneralQ

    I think I can change this thread to solved since there are many possibilities and many answer were given. Thanks to anyone that provided a usefull answer.
     
Thread Status:
Not open for further replies.

Share This Page