Prevent player from receiving damage from teleport

Discussion in 'Plugin Development' started by CubieX, Nov 23, 2014.

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

    CubieX

    My current problem is:
    I'm teleporting the player to a safe spot in case he would take lethal damage, IF he has a special item in his inventory.
    For example if he is falling too deep or is hit by an arrow or similar.
    I then remove the item.
    But after the teleport, the EntityDamageEvent fires again for that player and this time he is not teleported, because he does no longer have the item. So he dies.
    If he has more than one of those items, he may get teleported until those items run out,
    because each teleport tiggers a damage event.

    Is there a good way to prevent the player from receiving damage from this teleport?
    Normally this does not happen, right?
    I tried to setVelocity() to 0 for all directions prior to the teleport.
    I tried to set the players fallDistance to 0.
    I tried setting NoDamageTicks(20). And some other things.
    But none of this works reliable.
    I would like to prevent setting a player to creative mode, if possible. And I also do not want to use a delayed task to remove the item. Because he may then still be teleported multiple times.
     
  2. Offline

    Gingerbreadman

    CubieX Cancel the EntityDamageEvent
     
  3. Offline

    Rocoty

    Have you checked the damage cause. Try figuring out why it happens before trying to fix it.
     
  4. Offline

    Avygeil

    CubieX Is the damage cause the same after teleport ? Do you cancel the damage when you teleport the player after detecting lethal damage ?
    If yes, I'd delay the teleport itself (not the item removal) by 1 tick after you cancel the damage event.
     
  5. Offline

    CubieX

    I do, if my conditions are met. (Player has special item in inventory)
    Otherwise he would not get teleported in the first place.

    It depends.
    If I jump into lava from great hight, a "FIRE" damage triggers and then a "FALL" damage. Sometimes also a "FIRE_TICK" triggers after the "FALL", even when I setFireTicks(0) and setNoDamageTicks(20) in the first event.

    Delaying the teleport does improve the handling.
    But there are multiple damage events triggered, depending on what the player did to die.

    I now "sort of" solved it.
    My call order is now:
    Code:
    e.setCancelled(true);
    p.setFireTicks(0);
    p.setFallDistance(0.0f);
    p.setNoDamageTicks(20);
    removeDamagingPotionEffects(p);
    p.setHealth(2.0);
    doBlink(p);
    And the teleport (blink) itself is delayed by 1 tick and does again a player.setFireTicks(0);
    Alot of testing showed, that this configuration is fairly reliable.

    It's still not perfect. Sometimes I get stuck in a teleport-loop (until my special items run out)
    when jumping into a lava ocean from great height.
    I have not yet figured out what exactly is happening there. But I guess its kind of a "worst case" scenario that fires alot of damage events.
    FALL, LAVA, SUFFOCATION and FIRE.
    But for now, it's usable, I would say.

    Thanks for your input. :)
     
  6. Offline

    Jaaakee224

    CubieX
    For using damage ticks, you should use
    Code:java
    1. p.setNoDamageTicks(Integer.MAX_VALUE);


    To remove the no damage ticks
    Code:java
    1. p.setNoDamageTicks(0);
     
  7. Offline

    CubieX

    Why would I want to use MAX_VALUE? I only want a slight amount of time for invincibility. ;)
     
  8. Offline

    Jaaakee224

    CubieX
    You can easily make them invincible to any type of damage with MAX_VALUE damage ticks, and you can schedule a runnable to set the player ticks back to 0.
     
Thread Status:
Not open for further replies.

Share This Page