Events

Events are an essential part of building your own launcher. They allow you to receive updates from the backend, such as the current launch status, the game exit code, and many other events during the launch process.

Where events live

Events aren't all posted by one class - different modules post to different categories, but they all follow the same EventBus/subscribe()/unsubscribe() pattern described on this page:

  • Connection: online/offline detection, posted by minified-launch before network-dependent work.
  • Download: asset, library and client JAR download progress.
  • Environment: graphics environment setup on Linux.
  • Launch: the Minecraft process starting and stopping.
  • Loader: Forge/NeoForge installer progress.
  • Java: managed Java runtime resolution, download and extraction (minified-java).
  • Auth: session encryption (minified-auth).

All events

Connection

minified-events
CheckConnectionEventGets fired when we're checking whether the user is online or not
OfflineEventGets fired when no connection could be resolved

Auth

minified-auth
CreateMasterKeyEventGets fired when AuthManager generates a brand new master key used to encrypt/decrypt the saved session file
LoadEncryptedSessionEventGets fired right after AuthManager successfully decrypts a previously saved session from disk
SaveEncryptedSessionEventGets fired right after AuthManager finishes encrypting and saving a session to disk

Java

minified-java
EnsureJavaVersionEventCalled when the Java version requestedMajorVersion must be ensured before the game can launch.
ExtractArchiveEventGets fired while a downloaded Java archive is being extracted
JavaArchiveDownloadEventGets fired while a Java runtime archive is being downloaded

Launch

minified-launch
ConfigureX11EnvironmentEventGets fired when the X11 Environment gets configured, only on Linux when using Wayland
DownloadAssetsEventGets fired every time a new asset file has been downloaded
DownloadClientJarEventGets fired while the Minecraft client JAR is being downloaded
DownloadLibrariesEventGets fired every time a new library file has been downloaded
GameOutputEventGets fired for every line the game prints, only when captureGameOutput is enabled in the launch configuration
GameProcessStartedEventGets fired right after the Minecraft process was spawned, so a launcher can keep a handle to it (to stop it, for example)
GameStartEventGets fired when Minecraft is starting
GameStoppedEventGets fired when the Minecraft Process has been stopped
InstallForgeEventFired continuously while Minecraft Forge is installing
InstallNeoforgeEventFired continuously while NeoForge is installing

Usage

Firstly, create your EventBus Object:

EventBus eventBus = new EventBus();

Then, in your LaunchConfiguration, add

.eventBus(eventBus)

Subscribing to Events

// replace SomeEvent with any event you'd like to listen to
eventBus.subscribe(SomeEvent.class, event -> {
    // Code here will get executed when the event gets triggered
});

Unsubscribing to Events

This might not seem useful, but it's a requirement for a clean and fast launcher. For example, if the user closes some GUI that displays it, listening to an event that triggers often would waste resources. And generally, forgetting to unsubscribe can eventually lead to memory leaks, which you should avoid.

Unsubscribing from an event prevents the code you registered during subscription from being triggered when the event is fired.

// replace SomeEvent with the event you want to unsubscribe from
eventBus.unsubscribe(SomeEvent.class, listener);

Example

EventBus eventBus = new EventBus();

LaunchConfiguration config = new LaunchConfiguration.Builder()
        .downloadThreads(10)
        .launcherName("MinifiedLauncher")
        .launcherVersion("1.0.0")
        .assetsDirectory(Path.of("<assets-directory>"))
        .librariesDirectory(Path.of("<libraries-directory>"))
        .jarFile(Path.of("<client.jar>"))
        .isDemoUser(false)
        .loader(new VanillaLoader("1.21.11"))
        .eventBus(eventBus) // EventBus used by the API to dispatch events
        .build();

Launcher.launchMinecraft(
        user,
        config
);

// Subscribe to the Event
eventBus.subscribe(DownloadAssetsEvent.class, event -> {
    System.out.printf(
            "\rDownloading assets | %.2f%% | %s | %d/%d bytes",
            event.progress() * 100,
            event.currentFile(),
            event.downloadedBytes(),
            event.totalBytes()
    );
    System.out.flush();
});