onDamageEvent and it's subclasses

Discussion in 'Plugin Development' started by Dutchy, Feb 22, 2011.

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

    Dutchy

    Hello there,

    I've been working on detecting damage, but the code is not doing what I expect...

    Code:
    public void onEntityDamage(EntityDamageEvent event)
    {
        if (event instanceof EntityDamageByProjectileEvent)
        {
            // my code here, never gets here
        }
        else if (event instanceof EntityDamageByBlockEvent)
        {
            // my code here, never gets here
        }
        else if (event instanceof EntityDamageByEntityEvent)
        {
            // my code here, never gets here
        }
    }
    I've registered for the Type.DAMAGE_EVENT too.
    Even though I believe I have my catches in the right order, none of them are ever used. I can only detect fire/fall/drowning, the stuff that has no real cause. Which makes me think I should be registering for extra events, but I have no idea which...

    Also, I'd like to have a look at where these events are actually generated, but I am not sure where I should be looking, is this in the CraftBukkit code or the Bukkit code? (and then, where exactly...)

    I hope anybody can shed some light on this issue.
     
  2. Offline

    Plague

    I'm gonna switch to this implementation soon, I used the old (4 different events) interface so far.

    If this truly doesn't work, it will be hell for me after updating my plugin to Minecraft 1.3.

    Does it get into the function itself though? I mean if you step in lava, is onEntityDamage() called and only those instanceof conditions do not work or doesn't it work as a whole?
     
  3. Offline

    Dutchy

    The function is called, only the conditions don't work.

    I think it should be possible in some way though, because the HeroicDeath plugin can detect all causes. Not sure how they do it, but I'll post a request for the source.
     
  4. Offline

    Edward Hand

  5. Offline

    Dutchy

    I do believe I tried that and only got the causes I mentioned earlier, as if I were not even getting the other events...

    I can't double-check atm though because of the hassle with the 1.3 update
     
  6. Offline

    Plague

  7. Offline

    Abrupt

    since you no longer are using onEntityDamageByEntity, how would you get the damager?
    edit: can you cast the event to EntityDamageByEntityEvent and from there get the damager?
     
  8. Offline

    Dutchy

    Yes. Didn't work for me but I'll have to do some more debugging, I'm probably making a mistake somewhere.

    I'd still like to see the (craft)bukkit code for the throwing of the event, but I can't find it, can anybody help me find it?
     
  9. Offline

    Nohup

    This does work, I have code that hits it. I know this is PROBABLY a silly question, but did you annotate your method as an override? Here is my code:

    Code:
    
        @Override
        public void onEntityDamage(EntityDamageEvent event)
        {
            super.onEntityDamage(event);
    
            if (event instanceof EntityDamageByEntityEvent)
            {
                EntityDamageByEntityEvent edee = (EntityDamageByEntityEvent) event;
                if (edee.getDamager() instanceof Creeper &&
                    edee.getEntity() instanceof Player)
                {
                    if (!(_plugin.getConfiguration().getBoolean(
                        CreeperDamage.PLAYER_OPT, true)))
                    {
                        event.setDamage(0);
                    }
                }
            }
        }
    
    This basically disables the damage from the Creeper that is targeted at a player based on a setting in the configuration file. Let me know if you still need help, the source code is available if you need it but I can help you narrow it down.
     
  10. Offline

    Dutchy

    Turns out I was devving against the latest Bukkit git, but running the old one on my server! No wonder it doesn't work then. Thanks for the help everyone.
     
  11. Offline

    nossr50

    I recently switched and its works a-ok for me, you can look at the source code for my mcMMO plugin and peek at the entitiy listener java file to see how I did it.
     
  12. Offline

    Nohup

    Good catch Dutchy, one of those things that will drive you slightly nuts until you have that "oh crap" moment :)
     
Thread Status:
Not open for further replies.

Share This Page