Fly cancels wrong time.

Discussion in 'Plugin Development' started by sunny.zhang, Aug 16, 2014.

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

    sunny.zhang

    Hey guys. Its my first time working with events. I made a plugin for a server to stop player fly when two players pvp. The problem is if a mob damages player, player looses fly too. When you attack a entity, you still keep fly. Please help? Here is the code:
    Code:java
    1. package thebawsgamer.fly;
    2.  
    3. import org.bukkit.entity.Player;
    4. import org.bukkit.event.EventHandler;
    5. import org.bukkit.event.Listener;
    6. import org.bukkit.event.entity.EntityDamageByEntityEvent;
    7.  
    8. public class listener implements Listener {
    9. @EventHandler
    10. public void onAttack(EntityDamageByEntityEvent event){
    11. if(event.getEntity() instanceof Player){
    12. Player damaged = (Player) event.getEntity();
    13. damaged.setAllowFlight(false);
    14. damaged = (Player) event.getEntity();
    15. if(event.getDamager() instanceof Player){
    16. Player damager = (Player) event.getDamager();
    17. damager.setAllowFlight(false);
    18. damager = (Player) event.getEntity();
    19.  
    20. }
    21.  
    22. }
    23. }
    24. }
    25.  
     
  2. Offline

    rfsantos1996

    Try to do:

    PlayerToggleFlightEvent {
    // You can create a WeakHashMap with Player, Long with the player and the last time it was attacked using System.currentTimeInMillis()
    // and use TimeUnit.SECONDS.toMillis(<time in seconds since last attack>), do basic math:
    if(System.currentTimeInMillis() - weakHashMap.getOrDefault(player, 0L) < TimeUnit.SECONDS.toMillis(15)) { // the differente between the last attack and now is less than 15 seconds, disable fly
    // Set the event's flight variable to false here and he wouldn't be able to fly when he is attacked
    }
    }

    But I belive that the thingie on the EntityDamageEvent is still necessary or the player would be able to attack you if he is already flying
     
  3. Offline

    sunny.zhang

    I don't think i worded it well but my question is: How do i stop cancelling flight if a MOB and not PLAYER is attacking someone. Thanks for trying to help though :D
     
  4. Offline

    DinosParkour

    Code:java
    1. if(!(event.getDamager() instanceof Player)){
    2. //then you keep flight enabled
    3. }
     
  5. Offline

    Necrodoom

    sunny.zhang you start off checking if the victim is player, then cancel his flight. If the attacker is also a player, cancel his too. I believe the checks where supposed to be in reverse?
     
  6. Offline

    sunny.zhang

    So first check if Attacker then Attacked?

    And is there a way to turn off Fall damage event for 20 seconds on the event of player damage player?

    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: Jun 9, 2016
Thread Status:
Not open for further replies.

Share This Page