Skip to content

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.

Firstly, create your EventBus Object:

EventBus eventBus = new EventBus();

Then, in your LaunchConfiguration, add

.eventBus(eventBus)
// 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
});

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);
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();
});