• Forums

Navigation

  • Home
  • Style Guide
  • Getting Started
    • Home
    • Structuring Your Mod
    • Forge Update Checker
    • Debug Profiler
  • Concepts
    • Sides
    • Resources
    • Data
    • Registries
    • Mod Lifecycle
      • Registry Events
      • Data Generation
      • Common Setup
      • Sided Setup
      • InterModComms
    • Internationalization and Localization
  • Blocks
    • Home
    • BlockStates
    • Interaction
  • Block Entities
    • Home
    • Renderer
  • Items
    • Home
    • Loot Modification
  • Models
    • Intro to Models
    • Model Files
    • BlockStates
      • Intro to BlockState JSON
    • Coloring Textures
    • Item Properties
    • Advanced Models
      • BakedModel
      • Perspective
      • Item Overrides
  • Rendering
    • BlockEntityWithoutLevelRenderer
  • Data Generation
    • Introduction
    • Model Providers
  • Events
    • Basic Usage
  • Networking
    • Home
    • Overview
    • SimpleImpl
    • Entities
  • Data Storage
    • Capabilities
    • Saved Data
  • Utilities
    • Recipes
    • Tags
  • Effects
    • Particles
    • Sounds
  • Conventions
    • Versioning
    • Locations
  • Advanced Topics
    • Access Transformers
  • Contributing to Forge
    • Getting Started
    • PR Guidelines
  • Legacy Versions
    • Home
    • Porting to 1.17

Mod Lifecycle

During the mod loading process, the various lifecycle events are fired on the mod-specific event bus. Many actions are performed during these events, such as registering objects, preparing for data generation, or communicating with other mods.

Event listeners should be registered either using @EventBusSubscriber(bus = Bus.MOD) or in the mod constructor:

@Mod.EventBusSubscriber(modid = "mymod", bus = Mod.EventBusSubscriber.Bus.MOD)
public class MyModEventSubscriber {
    @SubscribeEvent
    static void onCommonSetup(FMLCommonSetupEvent event) { ... }
}

@Mod("mymod")
public class MyMod {
    public MyMod() {
        FMLModLoadingContext.get().getModEventBus().addListener(this::onCommonSetup);
    } 

    private void onCommonSetup(FMLCommonSetupEvent event) { ... }
}

Warning

Most of the lifecycle events are fired in parallel: all mods will concurrently receive the same event.

Mods must take care to be thread-safe, like when calling other mods’ APIs or accessing vanilla systems. Defer code for later execution via ParallelDispatchEvent#enqueueWork.

Registry Events

The RegistryEvents are fired after the mod instance construction. There are two: the NewRegistry event and the Register event. These events are fired synchronously during mod loading.

The RegistryEvent$NewRegistry event allows modders to register their own custom registries, using the RegistryBuilder class.

The RegistryEvent$Register<?> event is for registering objects into the registries. A Register event is fired for each registry.

Data Generation

If the game is setup to run data generators, then the GatherDataEvent will be the last event to fire. This event is for registering mods’ data providers to their associated data generator. This event is also fired synchronously.

Common Setup

FMLCommonSetupEvent is for actions that are common to both physical client and server, such as registering capabilities.

Sided Setup

The sided-setup events are fired on their respective physical sides: FMLClientSetupEvent on the physical client, and FMLDedicatedServerSetupEvent for the dedicated server. This is where physical side-specific initialization should occur, such as registering client-side key bindings.

InterModComms

This is where messages can be sent to mods for cross-mod compatibility. There are two events: InterModEnqueueEvent and InterModProcessEvent.

InterModComms is the class responsible for holding messages for mods. The methods are safe to call during the lifecycle events, as it is backed by a ConcurrentMap.

During the InterModEnqueueEvent, use InterModComms#sendTo to send messages to different mods, then during the InterModProcessEvent, call InterModComms#getMessages to get a stream of all received messages.

Note

There are two other lifecycle events: FMLConstructModEvent, fired directly after mod instance construction but before the RegistryEvents, and FMLLoadCompleteEvent, fired after the InterModComms events, for when the mod loading process is complete.

Built with MkDocs using a custom theme. Hosted by Read the Docs.
Enable Dark Theme