Перейти к содержанию
SoulFire LogoSoulFire

Event system

Listen for SoulFire events and change behavior through event data.

Это руководство доступно только на английском.

Use events before you use Mixins. Events cover server lifecycle, bots, chat, packets, and settings. They are easier to maintain than Mixins.

How listener registration works

SoulFireAPI.loadPlugin(...) registers both the static and the instance @EventHandler methods of your plugin class. Both patterns work:

@EventHandler
public static void onBotInit(BotConnectionInitEvent event) {
  // stateless handler
}
@EventHandler
public void onSettingsRegistryInit(InstanceSettingsRegistryInitEvent event) {
  // instance handler
}

The annotation comes from LambdaEvents:

import net.lenni0451.lambdaevents.EventHandler;

Event hierarchy

Each event has one of three scopes:

  • SoulFireGlobalEvent: the whole SoulFire server
  • SoulFireInstanceEvent: one project (called an instance in the API)
  • SoulFireBotEvent: one bot, which also gives access to its project

A bot event provides a BotConnection. From it, you can access the InstanceManager and SoulFireServer.

Server and project events

EventScopeWhen it fires
CommandManagerInitEventglobalafter built-in commands are registered and before you add your own
ServerSettingsRegistryInitEventglobalwhile the server settings registry is being assembled
UserPermissionsChangedEventglobalafter a user change that can affect permissions is saved
UserSessionsInvalidatedEventglobalafter a user's sessions are invalidated or the user is deleted
InstanceSettingsRegistryInitEventinstancewhile an instance settings registry is being assembled
InstanceInitEventinstanceimmediately after an InstanceManager is created

Bot events

These events live in com.soulfiremc.server.api.event.bot:

EventWhat it is for
PreBotConnectEventasync pre-connect hook before the bot joins
BotConnectionInitEventright after the BotConnection object exists
BotConnectedEventafter the Minecraft login handshake completes
BotConnectionRemovedEventafter a disconnected bot is removed from its instance
BotPreTickEventbot tick hook before the tick body finishes
BotPostTickEventbot tick hook after the tick body finishes
BotPreEntityTickEventbefore entity tracking logic ticks
BotPostEntityTickEventafter entity tracking logic ticks
ChatMessageReceiveEventincoming chat messages
BotPacketPreReceiveEventinspect or replace inbound packets before handling
BotPacketPreSendEventinspect or replace outbound packets before send
BotDamageEventhealth went down
BotBlockUpdateEventone block state changed in the bot's client world
BotOpenContainerEventa container or screen opened
BotClientBrandEventoutgoing client brand can be changed
BotClientSettingsEventoutgoing client settings can be changed
BotShouldRespawnEventcontrol automatic respawn behavior
BotDisconnectedEventthe bot disconnected

Mutable hooks

Most events are immutable records. These events have setters, so you can change behavior without a Mixin:

  • BotPacketPreReceiveEvent: replace the packet, or set it to null to suppress handling
  • BotPacketPreSendEvent: replace the packet, or set it to null to suppress sending
  • BotClientBrandEvent: change the brand string
  • BotClientSettingsEvent: change the ClientInformation
  • BotShouldRespawnEvent: set shouldRespawn

Timing and thread context

Check when and on which thread an event runs:

  • PreBotConnectEvent runs before the player is ready and can do blocking work
  • BotConnectionInitEvent has a fully constructed BotConnection, but the bot is not connected yet
  • BotConnectedEvent runs after login, but the player can still be missing until the world loads
  • BotPreTickEvent and BotPostTickEvent run in the live bot tick loop
  • BotPreEntityTickEvent and BotPostEntityTickEvent are even closer to the entity update path

In early lifecycle hooks, minecraft().player can still be null.

Custom commands

If your plugin needs terminal commands, listen for CommandManagerInitEvent and add Brigadier commands there. SoulFire posts this event after its built-in commands are already registered.

When to stop and use a Mixin instead

Use a Mixin only when:

  • there is no event at the right timing point
  • you need data that the event API does not provide
  • you need to alter control flow inside Minecraft or SoulFire internals

Before you write a Mixin, make sure that an existing packet or client-setting event cannot do the work.

Reference code to study

The built-in plugins and SoulFire's own event-emitting mixins are the best reference:

Next steps

Последнее обновление:

Полезна ли эта страница?