Crystal Frog!
Custom Mob
Bring your mod to life by adding a brand new animal the Crystal Frog! It wanders around, follows players holding Crystal Gems, and drops loot when defeated.
Bring your mod to life by adding a brand new animal the Crystal Frog! It wanders around, follows players holding Crystal Gems, and drops loot when defeated.
A custom mob needs: an Entity class (the brain), an EntityType registration, a Renderer (how it looks), a Renderer registration, a model (the shape), a texture (the skin), a spawn egg item, and a loot table. That's a lot but we'll build each piece together!
Create CrystalFrogEntity.java in your crystalmod package. This class holds health, sounds, and AI goals that make the frog wander and follow players holding Crystal Gems.
Right-click the com.example.crystalmod package โ New โ Java Class โ name it CrystalFrogEntity
ModItems.CRYSTAL_GEM so hold a Crystal Gem and the frog will come running!
Just like items and blocks, entities need to be registered. Create ModEntities.java to give the Crystal Frog an EntityType which includes its bounding box size and spawn behaviour.
ModEntities.registerModEntities(); inside onInitialize(). Don't forget this or the entity won't be registered!
The model defines how the frog looks which parts it has, their sizes and positions. We'll keep it simple: one body cube and four leg stubs. You can always make it fancier later!
Right-click the crystalmod package โ New โ Java Class โ CrystalFrogModel
Create CrystalFrogRenderer.java to pair the model with its texture file. Then create ModEntityRenderers.java to register the renderer on the client side. These run only when the game is rendering (client), not during gameplay logic (server).
registerEntityRenderers(). Never call it from CrystalMod.java or the game will crash on dedicated servers!
Fabric has separate entry points for server-side code and client-side code. Rendering only runs on the client. We need to create CrystalModClient.java and register it in fabric.mod.json.
Open src/main/resources/fabric.mod.json and add a client entry point:
fabric.mod.json has a "client" key pointing to com.example.crystalmod.CrystalModClient.ClientModInitializer, not ModInitializer. Import: net.fabricmc.api.ClientModInitializer.net.fabricmc.fabric.api.client.rendering.v1.EntityRendererRegistry.Every mob needs a texture (its skin). We'll create a 32ร32 PNG file. We defined the texture size as 32ร32 in CrystalFrogModel the UV coordinates map parts of the model to areas of this image.
src/main/resources/assets/crystalmod/textures/Fabric provides a SpawnEggItem class that automatically handles right-click-to-spawn. We just need to register it in ModItems.java and give it two colours (the egg body and the spots).
Open ModItems.java and add these two new lines:
This must happen in CrystalMod.java's onInitialize it tells Minecraft what stats the Crystal Frog has:
Still in ModItems.java, add the spawn egg to the creative inventory inside registerModItems():
Create a loot table JSON that makes Crystal Frogs drop 1-3 Crystal Gems when killed. Entity loot tables go in a different folder from block loot tables.
loot_table/entities/ not loot_table/blocks/ make sure you're in the right place!
Use Fabric's BiomeModifications API to add the Crystal Frog as a natural spawn in swamp biomes. This means players can find them exploring the world not just with a spawn egg!
BiomeKeys.JUNGLE, BiomeKeys.LUSH_CAVES, or even BiomeSelectors.all() to spawn them everywhere!
Build the mod and launch Minecraft. Use the spawn egg in creative mode to summon a frog, then hold a Crystal Gem and watch it follow you!
textures/entity/crystal_frog.png.
"crystalmod:magic_cookie".