API Reference › Events › cancellable
keyword

cancellable

event cancellable Name { … }

cancellable marks an event as a decision, not just a notification. A plain event tells listeners something happened; a cancellable event is fired before an action so a listener can veto it by calling cancel. The caller reads the verdict back by using emit as an expression: it yields 0 when no listener vetoed and 1 when one did, so the action is applied only on 0. This is the Bukkit / DOM preventDefault shape — observation becomes control — and a foreign mod vetoes the same way by setting the payload's trailing cancelled flag over the ABI.

Example

program CancellableExample {
  event cancellable BeforeHurt { amount: int = 0 }

  var current_health: int = 100

  @On(BeforeHurt) handler AbsorbSmallHits { if amount <= 5 { cancel } }

  handler TakeDamage phase Update {
    if Input.key() == 'h' {
      if emit BeforeHurt(amount: 3) == 0 { current_health = current_health - 3 }
    }
  }

  handler DrawWorld phase Render {
    Screen.clear(Color.MidnightBlue)
    Screen.draw_number(x: 4, y: 4, value: current_health, color: Color.Crimson, scale: 1)
    Screen.show()
  }
}
← All symbols