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-launchbefore 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-eventsCheckConnectionEventGets fired when we're checking whether the user is online or notOfflineEventGets fired when no connection could be resolvedAuth
minified-authCreateMasterKeyEventGets fired when AuthManager generates a brand new master key used to encrypt/decrypt the saved
session fileLoadEncryptedSessionEventGets fired right after AuthManager successfully decrypts a previously saved session from diskSaveEncryptedSessionEventGets fired right after AuthManager finishes encrypting and saving a session to diskJava
minified-javaEnsureJavaVersionEventCalled when the Java version requestedMajorVersion must be ensured before the game can launch.ExtractArchiveEventGets fired while a downloaded Java archive is being extractedJavaArchiveDownloadEventGets fired while a Java runtime archive is being downloadedLaunch
minified-launchConfigureX11EnvironmentEventGets fired when the X11 Environment gets configured, only on Linux when using WaylandDownloadAssetsEventGets fired every time a new asset file has been downloadedDownloadClientJarEventGets fired while the Minecraft client JAR is being downloadedDownloadLibrariesEventGets fired every time a new library file has been downloadedGameOutputEventGets fired for every line the game prints, only when captureGameOutput is enabled in the launch configurationGameProcessStartedEventGets 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 startingGameStoppedEventGets fired when the Minecraft Process has been stoppedInstallForgeEventFired continuously while Minecraft Forge is installingInstallNeoforgeEventFired continuously while NeoForge is installingUsage
Firstly, create your EventBus Object:
EventBus eventBus = new EventBus();val eventBus = 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
});// replace SomeEvent with any event you'd like to listen to
eventBus.subscribe(SomeEvent::class.java) { 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);// replace SomeEvent with the event you want to unsubscribe from
eventBus.unsubscribe(SomeEvent::class.java, 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();
});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 event
eventBus.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()
}