Solved EntityDamageByEntityEvent not called

Discussion in 'Plugin Development' started by Hex_27, Nov 21, 2014.

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

    Hex_27

    This is the code:
    Code:java
    1. public void onAttack(EntityDamageByEntityEvent event){
    2. Entity victim = event.getEntity();
    3. Entity e = event.getDamager();
    4. if(e instanceof Player){
    5. Player p = (Player) e;
    6. if(plugin.skill.getInt(p.getName()) == 3){
    7. p.sendMessage("You're a wizard harry.");
    8. if(hasStaff(p)){
    9. double truedamage = Math.floor(1 + plugin.config.getInt(p.getName()) / 3);
    10. double heal = Math.floor(plugin.config.getInt(p.getName()) / 5);
    11. double currhealth = p.getHealth();
    12.  
    13. double victimhp = ((LivingEntity) victim).getHealth();
    14. ((LivingEntity) victim).setHealth(victimhp - truedamage);
    15. if(currhealth + heal < 21){
    16. p.setHealth(currhealth + heal);
    17. p.sendMessage(ChatColor.BLUE + "Healed for " + heal + " health");
    18. }else{
    19. p.setHealth(20.0);
    20. p.sendMessage(ChatColor.BLUE + "Healed for " + heal + " health");
    21. }
    22.  
    23. }else {
    24. p.sendMessage("You should get a staff");
    25. }
    26. }
    27. }
    28. }


    The messages, "you're a wizard harry" and "You should get a staff" do not display. I'm assuming the code isn't even called at all. But I can't find out why.
     
  2. Offline

    JordyPwner

    Did you put @EventHandler above the event? Did you registered in the main?
     
  3. Offline

    FabeGabeMC

    Hex_27 as JordyPwner said, make sure to register it in the main class (which extends JavaPlugin) and add the @EventHandler annotation.
    Also, make sure you cast safely, as victim is not recognized as a LivingEntity as it may be an Item, EnderCrystal, a vehicle (minecart/boat), or a HangingEntity (item frame/painting).
     
  4. Offline

    tommyhoogstra

    Make sure you are also implementing listener
     
  5. Offline

    Hex_27

    @FabeGabeMC
    was just removing the casts temporarily so see if there was even an error...
    This is my new (whole) code.
    Code:java
    1. package me.leothepro555.random;
    2.  
    3. import org.bukkit.ChatColor;
    4. import org.bukkit.Material;
    5. import org.bukkit.entity.Entity;
    6. import org.bukkit.entity.LivingEntity;
    7. import org.bukkit.entity.Player;
    8. import org.bukkit.event.EventHandler;
    9. import org.bukkit.event.Listener;
    10. import org.bukkit.event.entity.EntityDamageByEntityEvent;
    11.  
    12. public class Mage implements Listener {
    13.  
    14. public Main plugin;
    15.  
    16. public Mage(Main plugin) {
    17. this.plugin = plugin;
    18. }
    19.  
    20. public boolean hasStaff(Player p){
    21. Material item = p.getItemInHand().getType();
    22. if(item != null){
    23. if(item == Material.WOOD_HOE){
    24. return true;
    25. }else if(item == Material.STONE_HOE){
    26. return true;
    27. }else if(item == Material.IRON_HOE){
    28. return true;
    29. }else if(item == Material.GOLD_HOE){
    30. return true;
    31. }else if(item == Material.DIAMOND_HOE){
    32. return true;
    33. }else {
    34. return false;
    35. }
    36. }
    37. return false;
    38.  
    39. }
    40. @EventHandler
    41. public void onAttack(EntityDamageByEntityEvent event){
    42. Entity victim = event.getEntity();
    43. Entity e = event.getDamager();
    44. if(e instanceof Player){
    45. Player p = (Player) e;
    46. if(plugin.skill.getInt(p.getName()) == 3){
    47. if(hasStaff(p)){
    48. double truedamage = Math.floor(1 + plugin.config.getInt(p.getName()) / 3);
    49. double heal = Math.floor(plugin.config.getInt(p.getName()) / 5);
    50. double currhealth = p.getHealth();
    51.  
    52. double victimhp = ((LivingEntity) victim).getHealth();
    53. if(victim instanceof LivingEntity){
    54. ((LivingEntity) victim).setHealth(victimhp - truedamage);
    55. if(currhealth + heal < 21){
    56. p.setHealth(currhealth + heal);
    57. p.sendMessage(ChatColor.BLUE + "Healed for " + heal + " health");
    58. }else{
    59. p.setHealth(20.0);
    60. p.sendMessage(ChatColor.BLUE + "Healed for " + heal + " health");
    61. }
    62. }
    63. }else {
    64. }
    65. }
    66. }
    67. }
    68.  
    69. }
    70.  

    My console says theres an error at line 54.... but I did check if victim was a living entity. What error is it?
     
  6. Offline

    tommyhoogstra

    Instead of doing (LivingEntity) victim, you should do
    if(event.getEntity() instanceof LivingEntity){
    LivingEntity victim = event.getEntity();
    victim.setHealth(victim.getHealth() - truedamage);
     
  7. Offline

    CoolGuy2001

    You do not register the events.. Please add the following code.

    Code:
    getServer().getPluginManager().registerEvents(this, this);
    Add that to your onEnable() method.
     
  8. Offline

    Hex_27

    This isn't the main class. I already added that to the main
     
  9. Offline

    tommyhoogstra

    You have to register events in all used classes
     
  10. Offline

    mythbusterma

  11. Offline

    tommyhoogstra

    In any instance in which you intend on using events.

    Hex_27
    In your Mage classes constructor, add
    Bukkit.getPluginManager().registerEvents(this, plugin (Your main class with the onEnable in it));
     
  12. Offline

    mythbusterma

    tommyhoogstra

    He already said he registered this class in the class that extends JavaPlugin, what in the world are you on about?
     
  13. Offline

    tommyhoogstra

    Nobody has seen his class that extends javaplugin, so for all I know, he hasnt registered the listener for the mage class there. So he should do so in the constructor

    Take this class for example
    Code:java
    1. public class Main extends JavaPlugin{
    2. private static Main instance;
    3. @Override
    4. public void onEnable(){
    5. instance = this;
    6.  
    7. ConfigManager.save();
    8. ConfigManager.check("MineResetInterval", 1);
    9. new TreeRegen();
    10. new MineManager();
    11. MineCreation mc = new MineCreation();
    12. getCommand("setup").setExecutor(mc);
    13. getCommand("CreateMine").setExecutor(mc);
    14. initializeMines(classes);
    15. }}

    This is the main class for a plugin I am currently working on, but it has no listeners.
    So if we goto a class such as TreeRegen,

    Code:java
    1. public class TreeRegen implements Listener{
    2. public TreeRegen(){
    3. Bukkit.getPluginManager().registerEvents(this, Main.getInstance());
    4. }


    I don't know about you, but this is the way I have always done it.
     
  14. Offline

    acer5999

    tommyhoogstra Thats wrong. You should be registering all Listener classes via the onEnable in the main class.
    Code:java
    1. public void onEnable()
    2. {
    3. getServer().getPluginManager().registerEvents(new YourRandomListener(), this);
    4. }
     
  15. Offline

    mythbusterma

    acer5999 tommyhoogstra

    Honestly guys, it's completely and utterly irrelevant where you do it, as long as you do it exactly once when the plugin is loaded.
     
    teej107 likes this.
  16. Offline

    acer5999

    mythbusterma
    On enable is the best place to do it just saying...
     
  17. Offline

    teej107

    Not for one plugin I was making. I registered the listeners if they weren't already by using a command and when I was done listening for the certain events, I just unregistered them. Thought I would try that route instead of relying on booleans to either skip the event or not. Either way, most plugins want to listen for events ASAP so registering them when the plugin enables is the way to go of coarse.
     
  18. Offline

    tommyhoogstra

    It honestly doesn't make a difference.
    Thats just the way I do it
     
  19. Offline

    Hex_27

    The plugin works already... I just forgot to set it to solved..... turns out im just too dumb to realise I forgot @EventHandler
     
    acer5999 likes this.
Thread Status:
Not open for further replies.

Share This Page