> For the complete documentation index, see [llms.txt](https://docsv1.polyfrost.org/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://docsv1.polyfrost.org/events/using-the-events-system/subscriber-events.md).

# Subscriber events

Getting started with OneConfig's subscriber-based events system is easy and straightforward! If you've worked with MinecraftForge or NeoForge before, you'll be more than familiar with how these function.

## Registering a listener

In order for your subscriber methods to execute when an event is dispatched, you'll need to register their containing instance as an event listener. This can be done via an instance of `EventManager`, typically the statically provided `INSTANCE`:

{% tabs %}
{% tab title="Java" %}

```java
public class ExampleListener {
    // This should be called somewhere - Like when your mod is first initialized
    public void register() {
        EventManager.INSTANCE.register(this);
    }
}
```

{% endtab %}

{% tab title="Kotlin" %}

```kotlin
object ExampleListener {
    // This should be called somewhere - Like when your mod is first initialized
    fun register() {
        EventManager.INSTANCE.register(this)
    }
}
```

{% endtab %}
{% endtabs %}

## Listening for events

Now that your instance is registered as an event listener, you can define your subscriber methods for the events you want to listen for. This can be done by defining those events as parameters for a method/function then annotating that same method/function with `@Subscribe`.

{% tabs %}
{% tab title="Java" %}

```java
public class ExampleListener {
    // This should be called somewhere - Like when your mod is first initialized
    public void register() {
        EventManager.INSTANCE.register(this);
    }
    
    // This method is called every tick, as the TickEvent is dispatched every game tick
    @Subscribe
    public void onTick(TickEvent.Start event) { // TickEvent.End also exists.
        System.out.println("Tick tick tick!");   
    }
}
```

{% endtab %}

{% tab title="Kotlin" %}

```kotlin
object ExampleListener {
    // This should be called somewhere - Like when your mod is first initialized
    fun register() {
        EventManager.INSTANCE.register(this)
    }
    
    // This method is called every tick, as the TickEvent is dispatched every game tick
    @Subscribe
    fun onTick(event: TickEvent.Start) {
        println("Tick tick tick!")
    }
}
```

{% endtab %}
{% endtabs %}

And there you have it! This instance of our `ExampleListener` now receives all dispatched instances of `TickEvent.Start` as long as our `register` method/function is executed at least once in the games' lifetime.


---

# Agent Instructions
This documentation is published with GitBook. GitBook is the documentation platform designed so that both humans and AI agents can read, navigate, and reason over technical content effectively. Learn more at gitbook.com.

## Querying This Documentation
If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter, and the optional `goal` query parameter:

```
GET https://docsv1.polyfrost.org/events/using-the-events-system/subscriber-events.md?ask=<question>&goal=<endgoal>
```

`ask` is the immediate question: it should be specific, self-contained, and written in natural language.
`goal` is optional and describes the broader end goal you are ultimately trying to accomplish on behalf of the user. GitBook uses it to tailor the answer towards what is most useful for that goal.

The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
