Use Fabric Lifecycle Events (V1)
26.2Fabric Lifecycle Events (V1) provide hooks to respond to changes in the lifecycle of the Minecraft client, the Minecraft server, and various objects (worlds, chunks, entities, block entities) within them.
Events are organized into category-specific classes. To use an event, you must implement the specific callback interface nested within that category class. For example, to listen to ServerLifecycleEvents.SERVER_STARTING, you implement the ServerLifecycleEvents.ServerStarting interface.