A Butterfly Spawn Category

One problem that’s been around since the mod’s inception is that butterfly spawns take away from other animal spawns. That means fewer cows, sheep, rabbits, and so on in any Minecraft game with Bok’s Banging Butterflies installed. And as more butterfly species were added, the problem only got worse. It’s about time this bug got fixed.

Spawn Categories


In Minecraft, mobs are divided into Spawn Categories, each with a separate spawn cap. You don’t need to know the exact limits to understand the problem. Just know that Minecraft only allows so many mobs of each type at a time.

Here’s a breakdown of Minecraft’s spawn categories1:

Mob CategoryDescription
MonsterHostile mobs
CreatureAnimals
AmbientBats only
AxolotlsTake a guess…
Underground water creaturesGlow squid only
Water creaturesDolphins and squid
Water ambientFish
MiscellaneousBoats, minecarts, etc.

When I first implemented butterfly spawns, I placed them in the Creature category. It made sense: butterflies are passive, animal-like mobs. This allowed them to spawn naturally alongside pigs, cows, and wolves.

The problem? I now have over 50 butterfly species, and they all share the same pool as other animals. As butterflies filled up the creature cap, vanilla animal spawns began to vanish. Players reported that it became almost impossible to find leather for their books or feathers for arrows. That’s a serious gameplay issue.

I had tried to rebalance spawn weights before, but clearly that wasn’t enough. I needed a better fix.

So I asked: what if butterflies had their own spawn category?

The Butterfly Spawn Category


Thankfully, creating a new spawn category is straightforward. Forge allows you to define a new category like this:

public class ButterflyMobCategory {
    public static final MobCategory BUTTERFLY =
            MobCategory.create("BUTTERFLY_SPAWNS", "butterfly_spawns", 70, true, true, 128);
}

This defines:

  • A unique ID (BUTTERFLY_SPAWNS)
  • A maximum number of butterfly entities (70)
  • Flags indicating that these are peaceful and animal-type mobs
  • A despawn distance of 128 blocks

Once this was set up, I updated my mob registration code. For example, caterpillars now look like this:

    private RegistryObject<EntityType<Caterpillar>> registerCaterpillar(int butterflyIndex) {
        return this.deferredRegister.register(Caterpillar.getRegistryId(butterflyIndex),
                () -> EntityType.Builder.of(Caterpillar::new, ButterflyMobCategory.BUTTERFLY)
                .sized(0.1f, 0.1f)
                .build(Caterpillar.getRegistryId(butterflyIndex)));
    }

I loaded into the game, wandered around, and confirmed: butterflies still spawn. Mission accomplished! Or so I thought…

Extensible Enums


Starting with NeoForge 1.21.1, the .create() method for custom enum values is no longer accessible. Instead, you now need to use Extensible Enums, a new system that lets you extend core enums like MobCategory.

To do this, I had to:

1. Enable enum extensions in neoforge.mods.toml:

## The file is relative to the output directory of the resources, or the root path inside the jar when compiled
## The 'resources' directory represents the root output directory of the resources
enumExtensions="META-INF/enumextensions.json"

2. Create enumextensions.json in the root of my resources directory:

{
  "entries": [
    {
      "enum": "net/minecraft/world/entity/MobCategory",
      "name": "BUTTERFLY_SPAWNS",
      "constructor": "(Ljava/lang/String;IZZI)V",
      "parameters": [ "butterflies:butterfly_spawns", 70, true, true, 128 ]
    }
  ]
}

3. Update the code to reference the extended enum value:

public class ButterflyMobCategory {
    public static final MobCategory BUTTERFLY = MobCategory.valueOf("BUTTERFLY_SPAWNS");
}

This setup works in versions 1.21.1 and up, letting me keep support across multiple versions of the mod.

Result


This is the solution I should have used from the start.

When you’re adding a large number of new mobs, putting them in their own spawn category makes a huge difference. It prevents them from crowding out vanilla animals and preserves the balance of the game. That said, it comes with a minor cost: more total mobs in the world, which means more memory use. But that’s par for the course in modded Minecraft.

A new version of the mod is out now with this change! Players should now be able to collect butterflies and still find a sheep or two to make a bed before nightfall.

  1. This table is borrowed from bithole.dev, which is a great resource and goes into much more detail about spawns than I do here. ↩︎