Solved EntityDamageByEntity event Code not Working

Discussion in 'Plugin Development' started by Lanuk, Nov 21, 2012.

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

    Lanuk

    Please pardon my poor Bukkit/Java skills, but I am having trouble set up this event. Basically, I want to make it so that when a player hits another player/mob/etc., and they are holding a sword with an unsafe enchantment of power, something specific will happen (in this case, their mcMMO skills will increase, but I know this part works already). Here is my code:

    Code:java
    1.  
    2. public void onEntityDamageByEntity(EntityDamageByEntityEvent event, Entity damager){
    3.  
    4. if (event.getDamager() instanceof Player) {
    5. damager = (Player) event.getDamager();
    6. }
    7.  
    8. //Testing (Does not work)
    9. ((Player) damager).sendMessage("Test");
    10.  
    11. //If the item is a sword
    12. ItemStack i = ((Player) damager).getItemInHand();
    13. if(i.getTypeId() == 267 || i.getTypeId() == 268 || i.getTypeId() == 276 ||
    14. i.getTypeId() == 272 || i.getTypeId() == 283){
    15.  
    16. Map<Enchantment, Integer> Enchants = i.getEnchantments();
    17. Integer level = Enchants.get(Enchantment.ARROW_DAMAGE);
    18.  
    19. //If the enchantment is power
    20. if (Enchants.containsKey(Enchantment.ARROW_DAMAGE)) {
    21.  
    22. //Give the player sword levels
    23. Users.getProfile((Player) damager).modifySkill(SkillType.SWORDS, level * 20);
    24. ((Player) damager).sendMessage("Success!");
    25.  
    26. }
    27. }
    28. }
    29.  


    Thanks in advance for any help!
     
  2. Offline

    Tirelessly

    @EventHandler before the method. And make sure it's registered in onEnable.
     
  3. Offline

    Lanuk

    It is registered (I have other working events), and I am not exactly sure how it works, but I found that this specific event doesn't need and event hander, and when I do add @EventHandler it gives an error.
     
  4. Offline

    Tirelessly

    Of course it needs an event handler... have you tried reading the error it gives you? You probably just need to import it.

    Oh. Why are you getting "Entity damager" passed to you? removed that and use e.getDamager()

    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: May 30, 2016
  5. Offline

    Lanuk

    Ohh duh, thanks XD

    And the error I was getting was something like this:

    Code:
    attempted to register an invalid EventHandler method signature "public void
    ... and then it told me that the event with issues was the EntityDamageByEntity event
     
  6. Offline

    Tirelessly

    You need to completely finish the method declaration or else the event handler annotation will have an error. It probably had that because you were getting Entity Damager passed to you.
     
  7. Offline

    Lanuk

    You were right.. I have no idea what I read online. Thanks :) It works fine now
     
Thread Status:
Not open for further replies.

Share This Page