VariantLoadedEvent - MeAlam1/BlueLib GitHub Wiki


VariantLoadedEvent

The VariantLoadedEvent is fired for each individual variant during the variant registration process. This provides a precise hook for developers to accept, reject, or react to specific variants for an entity.

There are two phases to this event:

  • Pre – lets you cancel the loading of a specific variant.
  • Post – runs after the variant is fully loaded and registered.

Use this when you need granular control over which variants are accepted or when you want to perform actions per variant (rather than once for all variants).


How to Use

Pre Phase (Before Each Variant Is Registered)

  • Allows you to cancel the registration of a specific variant.
  • Useful for conditional filtering based on user settings, mod compatibility, or world data.

Post Phase (After a Variant Has Been Registered)

  • Lets you respond to the loading of a specific variant.
  • Ideal for logging, transformation, or injecting additional logic after registration.

Example Use Cases

  • Skip loading a variant based on player config.
  • Log each variant as it's registered.
  • Modify entity behavior or model based on the variant name.
  • Prevent a specific variant from loading in certain dimensions or biomes.

NeoForge Usage

NeoForge

Listening to the Event

Use the mod event bus to listen to both Pre and Post phases:

@SubscribeEvent
public void onPreVariantLoad(VariantLoadedEvent.Pre event) {
    if (event.getEntity().equals("dragon") && event.getVariant().equals("lava")) {
        event.setCanceled(true); // Skip the "lava" variant
    }
}

@SubscribeEvent
public void onPostVariantLoad(VariantLoadedEvent.Post event) {
    System.out.println("Loaded variant: " + event.getVariant() + " for entity: " + event.getEntity());
}

Event Bus Registration

Register your listener:

ModLoadingContext.get().getModEventBus().register(new YourModEventHandler());

Fabric Usage

Fabric

Pre Listener (Cancelable)

Use VariantLoadedEvent.ALLOW_VARIANT_TO_LOAD to conditionally allow or deny a specific variant:

VariantLoadedEvent.ALLOW_VARIANT_TO_LOAD.register((entityName, variantName) -> {
    if (entityName.equals("dragon") && variantName.equals("ice")) {
        return false; // Skip "ice" variant
    }
    return true;
});

Post Listener

Use VariantLoadedEvent.POST to react after a variant has been registered:

VariantLoadedEvent.POST.register((entityName, variantName) -> {
    System.out.println("Loaded variant: " + variantName + " for entity: " + entityName);
});

Notes

  • The entityName identifies which entity is being processed.
  • The variant string refers to the specific variant ID or name being registered.
  • You can use this event in tandem with datapack-driven systems that define variants in JSON.
  • VariantLoadedEvent offers more granular control than AllVariantsLoadedEvent, which processes all variants at once.

⚠️ **GitHub.com Fallback** ⚠️