OneConfig V1
  • Welcome
  • Documentation license
  • Introduction
    • Getting started
  • Configuration
    • Creating your config
      • Option dependencies & hiding options
      • Listening for option changes
    • Available options
      • Boolean options
        • Switch option
        • Checkbox option
      • Number options
        • Slider option
        • Number option
      • Selector options
        • Dropdown option
        • Radio button option
      • Text option
      • Color option
      • Keybind option
      • Buttons
      • Accordions
      • Decorations
    • Creating your own options
  • Commands
    • Annotation-based commands
    • Tree-based commands
  • Events
    • Using the events system
      • Callback events
      • Subscriber events
  • Utilities Module
    • Hypixel utilities
    • IO utilities
    • Networking utilities
    • JSON utilities
    • Multithreading
    • Platform-specific utilities
  • Migration
    • Temporary V0 -> V1 migration guide
    • Migrating your configs from Vigilance
Powered by GitBook
On this page
  • Registering a listener
  • Listening for events
Export as PDF
  1. Events
  2. Using the events system

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:

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

Listening for events

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

public class ExampleListener {
    // This should be called somewhere - Like when your mod is first initialized
    public void run() {
        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!");   
    }
}
object ExampleListener {
    // This should be called somewhere - Like when your mod is first initialized
    fun run() {
        EventManager.INSTANCE.register(this)
    }
    
    // This method is called every tick, as the TickEvent is dispatched every game tick
    @Subscribe
    fun onTick(event: TickEvent) {
        println("Tick tick tick!")
    }
}

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

PreviousCallback eventsNextHypixel utilities

Last updated 6 months ago