Solved Detecting damage events that has beed caused by plugin

Discussion in 'Plugin Development' started by WinuX, May 4, 2014.

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

    WinuX

    Hello. I'm developing a plugin. A part of it should return some percentage of dealt to player damage to damager. Here's the code:
    Code:java
    1. @EventHandler
    2. public void entityDamaged(EntityDamageEvent e){
    3. if(e.getEntity().getType() != EntityType.PLAYER) return;
    4. if(e.getCause().equals(DamageCause.DROWNING)) return;
    5. Player p = (Player) e.getEntity();
    6. PlayerStatus ps = plugin.getStatus(p);
    7. if(ps.getBranch() == 0){
    8. //some another code goes here
    9. if(e instanceof EntityDamageByEntityEvent && ps.getAbility(6) > 0){
    10. EntityDamageByEntityEvent ee = (EntityDamageByEntityEvent) e;
    11. if(!(ee.getDamager() instanceof LivingEntity)) return;
    12. NCPManager.disableChecks(p);
    13. LivingEntity le = (LivingEntity) ee.getDamager();
    14. le.damage(e.getDamage() * ability_0_6[ps.getAbility(6)], p);
    15. NCPManager.enableChecks(p);
    16. }
    17. }
    18. }

    The problem is when two players with "return damage" ability hitting each other, it hangs the server up and ending with stack overflow error. I know why it's that: it's because plugin creates another EntityDamageEvent that passes checks and trying to return damage from returned damage. How can I prevent this from happening (deny plugin to process damage that was result of return)?
     
  2. Offline

    raGan.

    You can either set damage source to something you can track or you can use setHealth() instead. I think the latter is not calling damage event.
     
  3. Offline

    RawCode

  4. Offline

    raGan.

  5. Offline

    NathanWolf

  6. Offline

    Garris0n

    Look closely at what it's doing. It's a perfectly valid answer.
     
  7. Offline

    NathanWolf


    I stared at it for quite a while. I honestly have no idea what it's trying to do, and I very much doubt the OP does either. No offense to the OP, I am more saying that coming in and dumping a link to a random piece of source code with no documentation, crazy-jibberish in-game messages (??), and providing absolutely no explanation is not super helpful.

    OP asked how to detect damage events that were caused by a plugin- looking for the "CUSTOM" cause does exactly that. Sure you can start mapping your own damages with cooldowns or whatever, but isn't that a lot more complex and prone to getting out of sync?

    I don't see how damaging players on quit if they haven't ... done ... something... within a certain threshold... or whatever, relates to any of this at all- but yeah you are probably right that I haven't looked closely and may be missing something... but I'm not sure I should have to look this closely to see what RawCode was trying to say. Maybe I'm daft ;)
     
  8. Offline

    Garris0n


    There is a boolean. It is set to true before (possibly) killing the player and then false after. In the death event, it will return before doing anything if the boolean is true. What this means is that if he kills the player inside the PlayerQuitEvent the death event will be ignored. You could apply a similar system to prevent recursion in situations like the OP's.
     
    xTigerRebornx and RawCode like this.
  9. Offline

    RawCode

    NathanWolf
    learn 2 read code without spoonfeeding comments at every line, this helps.
    messages in russian.

    Garris0n
    you had explained properly.
     
    Garris0n likes this.
  10. Offline

    NathanWolf

    It would have been 100% more useful to say "set a flag to prevent recursion" than just post a random link to vaguely-relevant code. I don't want to spoonfeed people, either, but if you're trying to help don't you think a little explanation would be nice?

    I still contend that checking the damage source is tremendously more straightforward and I haven't heard a good argument against it.

    EDIT: Made it less flamey.
     
  11. Offline

    RawCode

    i provided valid code about setting flag and working with it.
    if you can't read or require major explanation about simple code - you will have bad time if attempt to perform original research or read obfustaced code (like NMS) NathanWolf
     
  12. Offline

    WinuX

Thread Status:
Not open for further replies.

Share This Page