[QUESTION] Slime EntityDamageEvent. How to catch all slime damage?

Discussion in 'Plugin Development' started by Flash619, May 22, 2012.

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

    Flash619

    So I'm basically, looking for damage by entities, and seeing if they are in a list by their ID's and stopping them.

    So far, I know that slime is in the list, but it only stops some damage. Sometimes he hits me and nothing happens, other times I loose a heart. :/ I'm not really sure why, if you feel like helping I look forward to it, this is not a major killer for my plugin, but it would be nice to at least know why. :)

    Here is my listener:
    Code:java
    1.  
    2. @EventHandler(priority = EventPriority.MONITOR)
    3. public void onInjurePlayer(EntityDamageEvent event){
    4. if(event.getEntity() instanceof Player){
    5. EntityDamageByEntityEvent dEvent = (EntityDamageByEntityEvent) event;
    6. if(dEvent.getDamager() instanceof Entity){
    7. Entity Damager = dEvent.getDamager();
    8. int ID = Damager.getEntityId();
    9. if(MountsIndexReference.isMountID(ID, null)){
    10. event.setCancelled(true);
    11. }
    12. }
    13. }event.setCancelled(false);
    14. }
    15.  


    Big thanks in advance. :)

    Anyone else ever have to do this before??

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

    SirTyler

    This won't fix your problem but it is going to fix errors in the future, you are casting the EntityDamageEvent as a EntityDamageByEntity event without checking if it really is. So you will get errors if you take non-entity damage, such as falling.
     
  3. Offline

    BobbyD441

  4. Offline

    Flash619

    Thanks for the link ^_^

    I changed the priority but the slime still injures me about 1 4th of the time :/ Any ideas? Could it be a different kind of damage that it is not looking for?

    How would you go about checking this? I was unable to find anything under the "EntityDamageEvent" that would give me a reference to the source of the damage...

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

    SirTyler

    well what you should do is either listen for just the EntityDamageByEntityEvent or use an instanceof check to make sure its what your trying to cast.
     
  6. Offline

    Flash619

    But thats what I mean, how do I actually write a instanceof line for this? I mean if it was as simple as "if(event.getCause() instanceof Entity" I would have done that, but it seems rather difficult to find the proper class/methods to check for this. :/
     
  7. Offline

    SirTyler

    if(event instanceof EntityDamageByEntityEvent) .....
     
  8. Offline

    Flash619


    Sigh...... I was over thinking it apparently.... Thank you for your help. :)


    Any ideas as to why the Slime doesn't seem to fall into the EntityDamageEvent ?
     
  9. Offline

    BobbyD441

    I had a look at it and came up with this eventhandler:

    Code:java
    1.  
    2. @EventHandler
    3. public void onPlayerDamage(EntityDamageEvent event)
    4. {
    5. if(event.getEntity() instanceof Player)
    6. {
    7. EntityDamageByEntityEvent dEvent = (EntityDamageByEntityEvent) event;
    8. if(dEvent.getDamager() instanceof Entity){
    9. event.setCancelled(true);
    10. }
    11. }
    12. }
    13.  


    as you can see i canceled all damage (god mode like) but this works for, even with slimes.
    Also i used the onPlayerDamage event, just seemed more logical to me =)
     
  10. Offline

    SirTyler

    It does, just might be a bug somewhere.
     
  11. Offline

    Flash619

    On my end then?

    Perhaps it could be:
    Code:java
    1.  
    2. Entity Damager = dEvent.getDamager();
    3. int ID = Damager.getEntityId();
    4.  

    ? Maybe instead of getting the slime it gets a entity related to the slimes actions?
     
  12. Offline

    SirTyler

    I have no clue xD seem very unlikely as the ID is unique per entity.
     
  13. Offline

    Flash619

    Well I tried to do this to see how far it got:
    Code:java
    1.  
    2. @EventHandler(priority = EventPriority.HIGHEST)
    3. public void onInjurePlayer(EntityDamageEvent event){
    4. if(event instanceof EntityDamageByEntityEvent){
    5. EntityDamageByEntityEvent dEvent = (EntityDamageByEntityEvent) event;
    6. System.out.print("TEST1");
    7. if(dEvent.getDamager() instanceof Entity){
    8. Entity Damager = dEvent.getDamager();
    9. int ID = Damager.getEntityId();
    10. System.out.println("TEST2");
    11. if(MountsIndexReference.isMountID(ID, null)){
    12. event.setDamage(0);
    13. event.setCancelled(true);
    14. }
    15. }
    16. }
    17. }
    18.  


    It never even made it to "TEST1" o.o

    So one of these two is blocking it:
    Code:java
    1.  
    2. public void onInjurePlayer(EntityDamageEvent event){
    3. if(event instanceof EntityDamageByEntityEvent){
    4.  


    :confused: *confused*

    xD I know you don't know much either, but I figured it may spark something I hope because I have no idea why that isn't working. >_>
     
  14. Offline

    BobbyD441

    Ok I did some testing:
    1. getEntityId() returns a unique ID, so this is not the same for same type mobs (hope you understand what i mean)
    2. A big slime has an id, the smaller ones don't (this could be the issue)
    Looking for a workaround =)

    running this code:
    Code:java
    1.  
    2. @EventHandler
    3. public void onPlayerDamage(EntityDamageEvent event)
    4. {
    5. if(event.getEntity() instanceof Player)
    6. {
    7. Player player = (Player) event.getEntity();
    8. EntityDamageByEntityEvent dEvent = (EntityDamageByEntityEvent) event;
    9. if(dEvent.getDamager() instanceof Entity){
    10. Entity Damager = dEvent.getDamager();
    11. int ID = Damager.getEntityId();
    12. player.sendMessage("id = " + ID);
    13. event.setCancelled(true);
    14.  
    15. }
    16. }
    17. }
    18.  


    I spawned in 2 zombies, both gave me a different id ?.?

    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: May 25, 2016
  15. I think it was Entity.getEntityType()
     
  16. Offline

    Flash619

    OK first. The problem is NOT getting the Entity ID. I can get multiple unique entity ID's just fine :)

    The problem is that when the slime hits me, It does not get to the line that prints "TEST1" in this quote of a post I made:

    Which makes me thing that the slime is either 1, not falling under EntityDamageEvent or 2 not falling under EntityDamageByEntityEvent. Both of which seem to be a rather large problem. Thats what I meant when I needed ideas as to why its not working xD

    Hope that helps sort out any confusion as to what my problem is. :)
     
  17. Why don't you just listen for the EntityDamageByEntityEvent, that would be easier don't you think, instead of listening for the EntityDamageEvent, and then casting it back to EntityDamageByEntityEvent.
    greetz blackwolf12333
     
  18. Offline

    Flash619

    That is true... not sure what I was thinking there......

    I'll see if that helps then report back.

    My current code is
    Code:java
    1.  
    2. @EventHandler(priority = EventPriority.HIGHEST)
    3. public void onInjurePlayer(EntityDamageByEntityEvent event){
    4. System.out.print("TEST");
    5. if(event.getDamager() instanceof Entity){
    6. Entity Damager = event.getDamager();
    7. int ID = Damager.getEntityId();
    8. System.out.println("TEST2");
    9. if(MountsIndexReference.isMountID(ID, null)){
    10. event.setDamage(0);
    11. event.setCancelled(true);
    12. }
    13. }
    14. }
    15. }
    16.  


    But I TEST was never printed into console when the slime hurt me. Is it really that impossible to find a slimes damage? I mean, every other entity works fine, but slime never seems to work >_<

    EDIT

    Removed a unnecessary if statement, but still no luck.

    Code:java
    1.  
    2. @EventHandler(priority = EventPriority.HIGHEST)
    3. public void onInjurePlayer(EntityDamageByEntityEvent event){
    4. System.out.print("TEST");
    5. Entity Damager = event.getDamager();
    6. int ID = Damager.getEntityId();
    7. System.out.println("TEST2");
    8. if(MountsIndexReference.isMountID(ID, null)){
    9. event.setDamage(0);
    10. event.setCancelled(true);
    11. }
    12. }
    13.  


    Could it have to do with the way the slime is spawned?

    Code:java
    1.  
    2. LivingEntity Mount = location.getWorld().spawnCreature(location, Mounts.getMountDurRef(ID));
    3.  


    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: May 25, 2016
  19. Are you sure that you are hit by the Slime, if not the whole event wont be called:p
    greetz blackwolf12333
     
  20. Offline

    Flash619

    *slime jumps into me* -> *I get hurt*

    I think that's me getting hit by a slime? Is that right?
     
  21. Hmm yeah, i think so:p
     
  22. Offline

    Flash619

    But then why doesn't it call the event?
     
  23. That's what i am wondering myself too at the moment, i just tried it out myself, it indeed doesn't even call the event:p i really have no idea why, sorry :(
    greetz blackwolf12333
     
  24. Offline

    Flash619

    Possibly a problem with craftbukkit!??!
     
  25. I was thinking the same, so i started looking at the source of craftbukkit, but i don't really get how that works:p
    so i didn't find anything there yet
    greetz blackwolf12333
     
  26. Offline

    Flash619

    I'm going to continue looking over the craftbukkit source as well, possibly get the git for it, and change some stuff, compile it myself see if it works, and if it continues to mess up, open a ticket on their git page.

    If I could find where the bug was I would surely try to do a pull request but I never really coded bukkit, so I'm not sure how they ran everything. >_<

    Yea not a clue, I can't make heads or tails of it. Really wish someone would come across this who actually knew bukkits programming..... >______> I mean, two people have tried it, two people with the same problem.

    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: May 25, 2016
Thread Status:
Not open for further replies.

Share This Page