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();val eventBus = EventBus()Then, in your LaunchConfiguration, add
.eventBus(eventBus)Subscribing to Events
Section titled “Subscribing to Events”// replace SomeEvent with any event you'd like to listen toeventBus.subscribe(SomeEvent.class, event -> { // Code here will get executed when the event gets triggered});// replace SomeEvent with any event you'd like to listen toeventBus.subscribe(SomeEvent::class.java) { event -> // Code here will get executed when the event gets triggered}Unsubscribing to Events
Section titled “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 fromeventBus.unsubscribe(SomeEvent.class, listener);// replace SomeEvent with any event you'd like to listen toeventBus.subscribe(SomeEvent::class.java) { event -> // Code here will get executed when the event gets triggered}Example
Section titled “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 EventeventBus.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();});val eventBus = EventBus()
val config = 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(VanillaLoader("1.21.11")) .eventBus(eventBus) // EventBus used by the API to dispatch events .build()
Launcher.launchMinecraft( user, config)
// Subscribe to the eventeventBus.subscribe(DownloadAssetsEvent::class.java) { event -> print( "\rDownloading assets | %.2f%% | %s | %d/%d bytes".format( event.progress() * 100, event.currentFile(), event.downloadedBytes(), event.totalBytes() ) ) System.out.flush()}