EntityDamageByEntityEvent

Discussion in 'Plugin Development' started by kabbage, Jan 11, 2012.

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

    kabbage

    I'm trying to figure out how to use the EntityDamageByEntityEvent class, but have been completely at a loss at even getting it to register when an entity is damaged. I know I'm probably missing something simple, but I can't figure it out. Here's the code I'm using so far...

    The main class
    Code:
    public class MaxHealth extends JavaPlugin {
        Logger log = Logger.getLogger("Minecraft");
    
        public final MaxHealthEntityListener entityListener = new MaxHealthEntityListener(this);
        public final MaxHealthPlayerListener playerListener = new MaxHealthPlayerListener(this);
    
        public String message(String message) {
            log.info(message);
            return message;
        }
    
        @Override
        public void onDisable() {
            message("MaxHealth Plugin disabled!");
    
        }
    
        @Override
        public void onEnable() {
            message("MaxHealth Plugin enabled!");
            PluginManager pm = getServer().getPluginManager();
            pm.registerEvent(Event.Type.ENTITY_DAMAGE, this.entityListener, Event.Priority.Normal, this);
            playerListener.registerEvents(pm);
            entityListener.registerEvents(pm);
        }
    
    }
    The listener class
    Code:
    public class MaxHealthEntityListener extends EntityListener {
        private MaxHealth plugin;
     
        public MaxHealthEntityListener(MaxHealth instance) {
            plugin = instance;
        }
     
        public void registerEvents(PluginManager pm) {
            pm.registerEvent(Event.Type.ENTITY_DEATH, this, Priority.High, plugin);
            pm.registerEvent(Event.Type.CREATURE_SPAWN, this, Priority.High, plugin);
            pm.registerEvent(Event.Type.ENTITY_DAMAGE, this, Priority.High, plugin);
    
        }
        public void OnEntityDamage (EntityDamageByEntityEvent event) {
            EntityDamageByEntityEvent damageEvent = (EntityDamageByEntityEvent) event;
            System.out.print("Entity: " + event.getEntity().getEntityId());
    
            if (damageEvent.getDamager() instanceof Player) {
            Player player = (Player) damageEvent.getDamager();
                    plugin.message("lol someone got hit");
                    System.out.print("Entity Damager " + damageEvent.getDamager().getEntityId());
    
            }
    
        }
    
     
  2. Offline

    wwsean08

    why are you registering events within your listener?
     
  3. Your OnEntityDamage should be onEntityDamage

    Yes, the case DOES matter :)

    And the every event fired on entity death is an EntityDeathEvent which can then be cast to other sub class events. Your method head should be:

    Code:java
    1.  
    2. public void onEntityDamage(EntityDamageEvent event){
    3. //see below for the rest.
    4. }
    5.  


    First you should check if the event is an instance of EntityDamageByEntity event as that could case problems you can do this by :
    Code:java
    1.  
    2. public void onEntityDamage(EntityDamageEvent event){
    3. if(event instanceof EntityDamageByEntityEvent){
    4. EntityDamageByEntityEvent damageEvent = (EntityDamageByEntityEvent)event;
    5. //the rest here
    6. } else {
    7. //any other checks...
    8. }
    9. }
    10.  
     
    kabbage likes this.
  4. Offline

    nisovin

    There is no magic at work here. Bukkit works because it knows to call certain methods. Your method for your entity damage listener must look like this:

    public void onEntityDamage(EntityDamageEvent)

    You can check if it's an EntityDamageByEntityEvent and cast it within the method.

    I do this all the time. I like having the registrations in each listener better than having them all together in the main class.
     
  5. Offline

    kabbage

    Thanks! I was looking in the bukkit class list, and saw EntityDamageByEntity would do what I needed, and didn't think I needed anything else.

    One question though, when I put this code:
    Code:
                 if (damageEvent.getDamager() instanceof Player) {
                plugin.message("Working now?");
    into where you had this comment
    Code:
    //the rest here
    whenever I attack something, the message is logged twice instead of only once. Any reason to this happening? And is there any simple way to make it only register once?
     
  6. Yes, just put it under where you have casted the event.

    EDIT : In the code posted initially you have two lines of code which log output to the console, just remove whichever one you don't want.
     
  7. Offline

    wwsean08

    ah ok, i've never done it that way, thats pretty smart, i'll have to remember that
     
  8. Offline

    kabbage

    Oh, duh. I was trying to figure out where those were supposed to go, and never got rid of one of the two.
    Thanks again for the help.
     
    Adamki11s likes this.
Thread Status:
Not open for further replies.

Share This Page