Solved Block fall damage

Discussion in 'Plugin Development' started by mydeblob, Apr 10, 2014.

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

    mydeblob

    Hi,
    I'm making a plugin that allows the players to double jump and get shot up into the air, and when he lands he doesn't take fall damage. But if he falls from another cause (not jumping) he will take block damage. So I got this code
    Code:java
    1. @EventHandler
    2. public void onFly(PlayerToggleFlightEvent event){
    3. Player player = event.getPlayer();
    4. if(jump.containsKey(player.getName())){
    5. if(jump.get(player.getName()) > 0){
    6. if ((player.getGameMode() != GameMode.CREATIVE)) {
    7. event.getPlayer().sendMessage("Toggleflightevent");
    8. event.setCancelled(true);
    9. player.setAllowFlight(false);
    10. player.setFlying(false);
    11. player.setVelocity(player.getLocation().getDirection().multiply(1.0D * this.multiply).setY(1.0D * this.height));
    12. player.getLocation().getWorld().playSound(player.getLocation(), Sound.ENDERDRAGON_WINGS, 1.0F, -5.0F);
    13. jump.put(player.getName(), jump.get(player.getName()) - 1);
    14. if(!nodamage.contains(player.getName())){
    15. nodamage.add(player.getName());
    16. }
    17. }
    18. }
    19. }
    20. }

    nodamage is an arraylist of Strings. The jumping works fine, and your name does get added to the aray (I added some debug code earlier) however when blocking the damage with this code
    Code:java
    1. @EventHandler
    2. public void onDamage(EntityDamageEvent e){
    3. if(e.getEntity() instanceof Player){
    4. Player p = (Player) e.getEntity();
    5. if(nodamage.contains(p.getName())){
    6. if(e.getCause().equals(DamageCause.FALL)){
    7. nodamage.remove(p.getName());
    8. e.setCancelled(true);
    9. }
    10. }
    11. }
    12. }

    it starts to act weird. If I have no jumps at all (determind by the jump hashmap) than I don't take fall damage, which is correct. However if I do have a jump, even if I'm not activating the ToggleFlightEvent (Where the name would be added into the nodamage array) I don't take fall damage. I've tested, and my name isn't in the array even when I have more than 1 jump.
    Thanks.
     
  2. Offline

    spacerocket

    If I recall correctly, I had a problem where I needed the player to get a temporary nofall damage for a plugin. You're correct by using an EntityDamageEvent, but there's no need to set if the player is flying.

    No fall damage:
    Code:
    //call this when the player jumps or whatever
    nodamage.add(player);
    player.setFallDistance(0);
    
    //in your listener
    event.setCancelled(true);
    
    Reset fall damage:
    Code:
    nodamage.remove(player);
    player.setFallDistance(player.getFallDistance()); //resets their fall damage to default
    
    I'm going to assume you know where to put these bits of code, since you seem to know what you're doing.
     
    mydeblob likes this.
  3. Offline

    mydeblob

    Thanks, the setFallDamage was a bit derpy but I got it to work.
     
  4. Offline

    spacerocket

    mydeblob
    No problem. I remember when I had your problem, it was pretty annoying until I figured out the aforementioned solution. Good luck with the rest of your plugin :D
     
Thread Status:
Not open for further replies.

Share This Page