Solved EntityDamageByEntityEvent does not get triggered

Discussion in 'Plugin Development' started by Neymar11, Jul 22, 2014.

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

    Neymar11

    So I am having a problem making my plugin, the event EntityDamageByEntityEvent does not get triggered when a player attacks the other one.

    My code so far:

    Code:java
    1. @EventHandler
    2. public void onPlayerAttack(EntityDamageByEntityEvent event)
    3. { final Player damager = (Player)event.getDamager();
    4. final Player victim = (Player)event.getEntity();
    5.  
    6. if (((event.getDamager() instanceof Player)) && ((event.getEntity() instanceof Player)))
    7. {
    8.  
    9. this.getServer().getScheduler().scheduleSyncRepeatingTask(this, new Runnable(){
    10. public void run(){
    11. String pdamager = damager.getName();
    12. String pvictim = victim.getName();
    13.  
    14. damager.sendMessage(ChatColor.RED + "You have tagged " + ChatColor.GOLD
    15. + victim + ChatColor.RED + " for " + ChatColor.GOLD + i
    16. + ChatColor.RED + " seconds!");
    17.  
    18. victim.sendMessage(ChatColor.RED + "You have been tagged by" + ChatColor.GOLD
    19. + damager + ChatColor.RED + " for " + ChatColor.GOLD + i
    20. + ChatColor.RED + " seconds!");
    21. i--;
    22.  
    23. if (pname==pdamager)
    24. {
    25. damager.setHealth(0.00);
    26. victim.sendMessage(ChatColor.BLUE + "Your oponennt combat logged!");
    27. }
    28. else if (pname==pvictim)
    29. {
    30. victim.setHealth(0.00);
    31. damager.sendMessage(ChatColor.BLUE + "Your oponennt combat logged!");
    32. }
    33.  
    34. }},0L ,20L);
    35. }
    36.  
    37. }


    Any help would be appreciated! :D
     
  2. Offline

    _LB

    Did you actually register events for your listener?
     
  3. Neymar11 Debug your code to find out which parts aren't executed. And you cast to a player and then check if it's a player. This is wrong.
     
  4. Offline

    Neymar11

    _LB Thanks. I just sometimes forget the simple things in coding -.-
    AdamQpzm It works now. Thanks though!
     
  5. Neymar11 I'm still concerned about my other point.
     
  6. Offline

    Neymar11

    AdamQpzm Could you clarify, I am not sure I understand..

    AdamQpzm _LB Also, one more question. I have created an unendless loop with the Runnable.
    Is there a way to finish it when "i" is 0? Or is there maybe a different timer that could be better in my case then this one?

    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: Jun 9, 2016
  7. Neymar11 On lines 3 and 4 you're casting the damager and the entity to a player. One line 6, you check if those two actually are players. At this point, they're either players (making your check redundant) or the method would throw an exception when it tried to cast to a Player.

    And a BukkitRunnable works exactly the same as a Runnable but has a cancel() method
     
  8. Offline

    fireblast709

    I would like to add that you should not schedule a BukkitRunnable with the scheduler, but with the built-in runTask*() methods
     
    AdamQpzm likes this.
  9. Offline

    mrgreen33gamer

    If you want to end the task, just give it a task ID:

    Code:
    int taskID = 1;
    Then tell it the taskID:

    Code:java
    1. taskID = this.getServer().getScheduler().scheduleSyncRepeatingTask(this, new Runnable(){}


    Then cancel it whenever:
    Code:java
    1.  
    2. Bukkit.getScheduler().cancelTask(taskID);
    3.  
     
  10. Offline

    Neymar11

  11. Offline

    mrgreen33gamer

Thread Status:
Not open for further replies.

Share This Page