# BaseEvent

`com.jsmacrosce.jsmacros.core.event.BaseEvent`

Base class for event payloads dispatched to registered listeners. By default, the runtime
class's [Event](Event.md) annotation supplies its name and cancellable/joinable flags; without an
annotation, the simple class name is used and both flags are false. Subclasses may override
these methods, as [EventCustom](../../../../../../events/com/jsmacrosce/jsmacros/core/event/impl/EventCustom.md) does.  

Dispatch waits for a listener only when that listener requests joined handling and the event
permits joining. A joined listener may cancel or change the event while its event lock is held.  
A listener that did not request joined handling receives a read-only delivery: dispatch does not
wait for it, so the game may already have read the event by the time its callback runs. It cannot
cancel or change the event. Call [canModify()](#com.jsmacrosce.jsmacros.core.event.BaseEvent_canModify-) to check the current delivery before
attempting a change.  
```js
const listener = JsMacros.on("SendMessage", true, JavaWrapper.methodToJava(event => {
  if (event.getMessage() === "/hello") {
    // Cancel before the message is sent or added to chat history.  
    event.cancel();  
  }
  // Remove a one-shot listener using the handle returned by on().  
  JsMacros.off(listener);  
}));  
```

## Constructors

### new BaseEvent {#com.jsmacrosce.jsmacros.core.event.BaseEvent_constructor-Core-}

```js
new BaseEvent(Core<?, ?> runner)
```

Sets the core through which this event is dispatched.

**Parameters:**
- `runner`: the core this event is dispatched through

## Fields

### runner {#com.jsmacrosce.jsmacros.core.event.BaseEvent_runner}

```js
runner: Core<?, ?>
```

The core through which this event is dispatched.

## Methods

### cancel {#com.jsmacrosce.jsmacros.core.event.BaseEvent_cancel-}

```js
cancel(): void
```

Marks this event cancelled. This is only valid from a joined listener while its event lock is
still held; see [canModify()](#com.jsmacrosce.jsmacros.core.event.BaseEvent_canModify-). Cancel before releasing the lock so the raising code
observes the flag when dispatch returns. Throws [UnsupportedOperationException](https://docs.oracle.com/en/java/javase/25/docs/api/java.base/java/lang/UnsupportedOperationException.html) if this
event is not cancellable, and [IllegalStateException](https://docs.oracle.com/en/java/javase/25/docs/api/java.base/java/lang/IllegalStateException.html) if the current delivery cannot
change the event.

### cancellable {#com.jsmacrosce.jsmacros.core.event.BaseEvent_cancellable-}

```js
cancellable(): boolean
```

whether this event can be cancelled, read from [Event.cancellable()](Event.md#com.jsmacrosce.jsmacros.core.event.Event_cancellable-) on the subclass.  
It is the flag [cancel()](#com.jsmacrosce.jsmacros.core.event.BaseEvent_cancel-) checks, so it is also what decides whether a cancel throws
rather than sticking. A subclass with no `@Event` defaults to `false`.

**Returns:** `true` if the event declares itself cancellable.

### canModify {#com.jsmacrosce.jsmacros.core.event.BaseEvent_canModify-}

```js
canModify(): boolean
```

Whether the current callback may cancel or change this event. This is `true` for a
joined listener while its event lock is still held, for the raising thread during dispatch,  
and for a custom event's creator between dispatches. It is `false` for a non-joined
listener, after the event lock is released, and for built-in events after dispatch returns.

**Returns:** `true` if the current delivery may cancel or change the event

**Since:** 2.2.0

### getEventName {#com.jsmacrosce.jsmacros.core.event.BaseEvent_getEventName-}

```js
getEventName(): String
```

The event name from [Event.value()](Event.md#com.jsmacrosce.jsmacros.core.event.Event_value-), or the runtime class's simple name if it has no
annotation.

**Returns:** the name from the annotation, for example `SendMessage`, or the runtime
class's simple name when no annotation is present.

### isCanceled {#com.jsmacrosce.jsmacros.core.event.BaseEvent_isCanceled-}

```js
isCanceled(): boolean
```

Whether [cancel()](#com.jsmacrosce.jsmacros.core.event.BaseEvent_cancel-) has been called on this event.

**Returns:** `true` once something has called [cancel()](#com.jsmacrosce.jsmacros.core.event.BaseEvent_cancel-) on this instance.

### joinable {#com.jsmacrosce.jsmacros.core.event.BaseEvent_joinable-}

```js
joinable(): boolean
```

Whether this event permits joining based on its own cancellable or joinable flag. Named-event
dispatch may also permit joining based on [BaseEventRegistry.joinableEvents](BaseEventRegistry.md#com.jsmacrosce.jsmacros.core.event.BaseEventRegistry_joinableEvents).

**Returns:** `true` if the event is cancellable or declares itself joinable.

### trigger {#com.jsmacrosce.jsmacros.core.event.BaseEvent_trigger-}

```js
trigger(): void
```

Dispatches this event through its core's profile. Listener work may outlive this call unless
dispatch joins it; custom events dispatch under their custom name.
