DoubleJump is buggy

Discussion in 'Plugin Development' started by Robin Bi, Apr 16, 2014.

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

    Robin Bi

    I've recently made a doublejump-plugin. This is my code: http://hastebin.com/cihucoqigi.avrasm
    The problem seems to be related to the HashMap DoubleJumpCooldown, because the DoubleJump itself works just fine.

    The HashMap's sense is to prevent the user from doing more than two DoubleJumps before they hit the ground again.
    After the first jump, a new entry with the key p.getName() is created and set to 1. After the second jump, it's set to 2. When they then want to jump again, the code notices that the value if 2 and should only send that annoying sound. When the player then hits the ground again, his entry in the Map should be deleted.

    But what the code really does: It allows the player to doublejump twice, then plays the sound but seems to allow the player to fly for a small amount of time, because the free fall is slowed down and you're standing still in the air for the twinkling of an eye.
    When one hits the ground, it seems to not delete the entry, because when trying to doublejump again, the annoying sound comes up again.


    Hopefully someone knows how to solve that problem.




    Greetings,
    Robin
     
  2. Offline

    Konkz

    If you're allowing a single jump, the best way to do it is make an ArrayList, once player jumps add their name to the List and once they land remove it. If player is in the List and they try to jump just play the noise.

    I see no need for the HashMap here. :S
     
  3. Offline

    Robin Bi

    Konkz Thanks for your fast answer :)
    Unfortunately, i want to allow two doublejumps in a row. That's why i need bothes a key and a value. If i wanted to allow only one, i could also check for the block below the player.



    Greetings
     
  4. Offline

    Konkz

    So you mean that they jump into the air, and they can do it again?
     
  5. Offline

    Robin Bi

    Konkz Actually, they can do one normal jump, and assumed they jumped off a cliff, they can do two doublejumps in the air whenever they want. That's the goal.



    Greetings
     
  6. Offline

    Konkz

    Okay. They can jump normally, that's fine - but there's an event that is triggered when a player double taps space bar which I suppose you made them get boosted in the air.

    What you want to do, is when that event is fired add them to the arraylist and remove once they face plant
     
  7. Offline

    Robin Bi

    Konkz

    I'm afraid i'm already doing this.
    Code:java
    1. @EventHandler
    2. public void onToggleFlight (PlayerToggleFlightEvent ev) {
    3.  
    4. Player p = ev.getPlayer();
    5. if (p.getGameMode() != GameMode.CREATIVE) {
    6. ev.setCancelled(true);
    7.  
    8. p.setAllowFlight(false); // Später löschen! :D
    9. p.setFlying(false);
    10.  
    11.  
    12. if (DoubleJumpCooldown.containsKey(p.getName()) && DoubleJumpCooldown.get(p.getName()) == 2) {
    13. p.playSound(p.getLocation(), Sound.VILLAGER_HAGGLE, 1.0F, 1.0F);
    14. return;
    15. }
    16.  
    17.  
    18. p.setVelocity(p.getLocation().getDirection().multiply(1.0D * this.multiply).setY(1.0D * this.height));
    19. p.playSound(p.getLocation(), Sound.ENDERDRAGON_WINGS, 1.0F, -5.0F);
    20.  
    21.  
    22. if (!DoubleJumpCooldown.containsKey(p.getName())) {
    23. DoubleJumpCooldown.put(p.getName(), 1);
    24. return;
    25. }
    26. else if (DoubleJumpCooldown.get(p.getName()) == 1) {
    27. DoubleJumpCooldown.remove(p.getName());
    28. DoubleJumpCooldown.put(p.getName(), 2);
    29. return;
    30. }
    31. }
    32. }



    In line 22 and 26 I'm adding them to the HashMap.


    Code:java
    1. @EventHandler
    2. public void onFallDamage (EntityDamageEvent ev) {
    3. Entity entity = ev.getEntity();
    4. if (entity instanceof Player && ev.getCause() == EntityDamageEvent.DamageCause.FALL) {
    5. Player p = (Player) entity;
    6. DoubleJumpCooldown.remove(p.getName());
    7. ev.setCancelled(true);
    8. }
    9. }


    And here in line 6 I'm removing them.


    This plugin is driving me crazy ^^

    Konkz Ahh, you still mean i should use ArrayLists! But then i can just add or remove the player and cannot give it a value of how many jumps this player has already made. I don't see the point in using an ArrayList, here.

    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: Jun 7, 2016
  8. Offline

    Konkz

    But if you are removing them what is the point in keeping how many jumps they made...?

    Also, if you really want to use hashmaps for whatever reason, it is probably because of this:
    Code:java
    1. DoubleJumpCooldown.remove(p.getName());
    2. DoubleJumpCooldown.put(p.getName(), 2);


    You re-add them after removing them.

    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: Jun 7, 2016
  9. Offline

    Robin Bi

    Konkz To be honest, i guess you haven't understood me.
    A player who hasn't made a doublejump is not in the HashMap.
    A player who has made one doublejump has the value 1 in the HashMap.
    A player who has made two doublejumps has the value 2 in the HashMap.


    That's not possible with ArrayLists, is it?
     
  10. Offline

    Konkz

    Oh, I see what you're trying to do.
    But the only way you are removing them is in the damage event, if the player does not take damage they don't get removed. Make sure that they're taking damage; also a more efficient way to do this would be checking the block below them instead of damage. If you don't solve this in an hour I will jump on my PC and help you sort this out.

    Ps, this is possible with an ArrayList - not efficient but possible. :)
     
  11. Offline

    Robin Bi

    Konkz I unfortunately don't know a method for getting the block relative to the player. And I also don't quite know what to google to get this.
     
  12. Offline

    pablo67340

    I currently have a shift+click plugin in my eclipse laying around. Depending on the permissions, it allows the user to jump a certain height. ccjump.4 will make you jump up 4 blocks when you press shift+jump and cancels fall damage. The permissions go up to 10. If your interested i can drop-box it here for you? Submit the source as well?
     
  13. Offline

    Konkz


    I quite like how he's done it, could be improved but good enough to get the skeleton of it.

    Robin Bi
     
  14. Offline

    pablo67340

    Robin Bi

    Here's a quick snippet:

    Code:java
    1. Player player = event.getPlayer();
    2. World world = player.getWorld();
    3.  
    4. Material material = Material.DIAMOND_BLOCK;
    5. if (player.getLocation().getBlock().getRelative(BlockFace.DOWN).getType() == material) {
    6. //Do stuff
    7.  
    8. }
     
Thread Status:
Not open for further replies.

Share This Page