[SOLVED] eventCall, why you no work?

Discussion in 'Plugin Development' started by tprk77, Aug 11, 2011.

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

    tprk77

    Hopefully this hasn't been asked before.

    I want to use the PluginManager's eventCall method to fire off EntityRegainHealthEvent and EntityDamageEvent events to heal and damage entities respectively. This doesn't seem to work. I can listen to the events, but they don't affect entities' health. Am I missing something?

    Here's what I'm doing:

    Code:
    EntityRegainHealthEvent regen = new EntityRegainHealthEvent(
        entity, power, EntityRegainHealthEvent.RegainReason.CUSTOM);
    this.eventcaller.callEvent(regen);
    
    P.S. Before I was just using the entities' setHealth method, but I thought using events would have the advantage of letting other plugins to listen to or cancel the effect.
     
  2. Offline

    zhuowei

    I think the event doesn't actually perform the action; however, you might be able to get the best of both worlds: You can call the event, check if regen is cancelled, and if it is not, then run setHealth.
     
  3. Offline

    tprk77

    This doesn't really make sense to me. You can stop regen/damage by cancelling the events (like in this plugin), but apparently you can't create the events to cause regen/damage?

    Should this be a feature request for craftbukkit?
     
  4. Offline

    desht

    No, not really.

    The purpose of events is to inform plugins that something has happened, or is about to happen (and give plugins a chance to act on and/or stop that something). Events are a result of something happening, not a cause.

    @zhuowei's suggestion sounds sensible to me (not that I've tested it). Your plugin needs to behave like any other plugin - listen for the event you've just thrown, and act on it (assuming it hasn't been cancelled by another plugin).
     
  5. Offline

    tprk77

    But there is a problem with listening to damage events and apply the damage. For damage events I didn't create, the damage would get applied twice. Same thing for the regen events. I could get around this by making custom events... but that kind of defeats the purpose. It seems I might as well go back to using setHealth.
     
  6. Offline

    nisovin

    You do something like this:
    Code:
    EntityRegainHealthEvent regen = new EntityRegainHealthEvent(     entity, power, EntityRegainHealthEvent.RegainReason.CUSTOM); this.eventcaller.callEvent(regen);
    if (!regen.isCancelled()) {
        entity.setHealth(x);
    }
    
    That way, you call the event, which allows other plugins to react and cancel if necessary. Then you check the cancelled state, and only act if it hasn't been cancelled. You don't actually listen for your own event. The damage won't get applied twice here, only once.
     
  7. Offline

    tprk77

    I didn't think of that. I don't think it's particularly pretty, but it works. Thanks!

    Code:
    EntityRegainHealthEvent regen = new EntityRegainHealthEvent(
        entity, power, EntityRegainHealthEvent.RegainReason.CUSTOM);
    this.eventcaller.callEvent(regen);
    if(!regen.isCancelled()){
      int newhealth = entity.getHealth() + regen.getAmount();
      if(newhealth < 0){
        newhealth = 0;
      }else if(newhealth > 20){
        newhealth = 20;
      }
      entity.setHealth(newhealth);
    }
    
     
Thread Status:
Not open for further replies.

Share This Page