How to make a mob with attributes and a spawn egg (Minecraft mod)
A mob is an entity class (here a PathfinderMob with AI goals) registered as an EntityType with its size and category. Every living entity needs attributes (health, speed…) registered for its type, and a renderer in the client, or the game crashes when it shows. A spawn egg is an item that spawns it.
Works on Minecraft 1.20.1 to 26.3 on NeoForge; Minecraft 1.16.5 to 26.3 on Fabric; Minecraft 1.16.5 to 1.20.1 on Forge. Each example was compiled against its loader and version before it shipped in Linen.
- Make the entity class with its attributes and registerGoals().
- Register its EntityType, built with EntityType.Builder and sized(…).
- Register its attributes, and its renderer in the client.
- Add a spawn egg item for it.
- Name it in the lang file (en_us.json).
What you can change
Section titled “What you can change”The example uses these values. Change them in the code.
| Setting | In the example | What it does |
|---|---|---|
| health | 10 |
Its health, in half hearts. |
| speed | 0.25 |
Its movement speed (a pig 0.25, a zombie 0.23). |
| width | 0.6 |
Its hitbox width in blocks. |
| height | 1.95 |
Its hitbox height in blocks. |
| primary | 6196282 |
The spawn egg’s base colour (RGB number). |
| secondary | 3099166 |
The spawn egg’s spots colour (RGB number). |
For each loader and version
Section titled “For each loader and version”The example mod is examplemod in the package com.example.examplemod. Use your own mod id and package.
Fabric 1.19.3 to 1.20.6
Section titled “Fabric 1.19.3 to 1.20.6”Uses:
EntityType.Builder.ofBuiltInRegistries.ENTITY_TYPEFabricDefaultAttributeRegistry.registerEntityRendererRegistry.registerMobRendererSpawnEggItem
The official pages for entities are written for later versions; this code is compile-tested against 1.20.1. It looks like a zombie until you give it a model and texture of its own.
src/main/java/com/example/examplemod/entity/ForestSpiritEntity.java
package com.example.examplemod.entity;
import net.minecraft.world.entity.EntityType;import net.minecraft.world.entity.PathfinderMob;import net.minecraft.world.entity.ai.attributes.AttributeSupplier;import net.minecraft.world.entity.ai.attributes.Attributes;import net.minecraft.world.entity.ai.goal.FloatGoal;import net.minecraft.world.entity.ai.goal.LookAtPlayerGoal;import net.minecraft.world.entity.ai.goal.PanicGoal;import net.minecraft.world.entity.ai.goal.RandomLookAroundGoal;import net.minecraft.world.entity.ai.goal.WaterAvoidingRandomStrollGoal;import net.minecraft.world.entity.player.Player;import net.minecraft.world.level.Level;
// Forest Spirit: a peaceful mob that wanders, looks at players and runs when hurt. Its goals are its behaviour; its attributes its stats.public class ForestSpiritEntity extends PathfinderMob { public ForestSpiritEntity(EntityType<? extends PathfinderMob> type, Level level) { super(type, level); }
// Registered for the entity type when the mod starts; every living entity needs its attributes. public static AttributeSupplier.Builder createAttributes() { return PathfinderMob.createMobAttributes().add(Attributes.MAX_HEALTH, 10.0F).add(Attributes.MOVEMENT_SPEED, 0.25F); }
@Override protected void registerGoals() { goalSelector.addGoal(0, new FloatGoal(this)); goalSelector.addGoal(1, new PanicGoal(this, 1.25D)); goalSelector.addGoal(2, new WaterAvoidingRandomStrollGoal(this, 1.0D)); goalSelector.addGoal(3, new LookAtPlayerGoal(this, Player.class, 6.0F)); goalSelector.addGoal(4, new RandomLookAroundGoal(this)); }}src/main/java/com/example/examplemod/client/ForestSpiritRenderer.java
package com.example.examplemod.client;
import com.example.examplemod.entity.ForestSpiritEntity;import net.minecraft.client.model.HumanoidModel;import net.minecraft.client.model.geom.ModelLayers;import net.minecraft.client.renderer.entity.EntityRendererProvider;import net.minecraft.client.renderer.entity.MobRenderer;import net.minecraft.resources.ResourceLocation;
// How Forest Spirit is drawn: with the zombie's model and texture until you make its own (in Blockbench, say).public class ForestSpiritRenderer extends MobRenderer<ForestSpiritEntity, HumanoidModel<ForestSpiritEntity>> { private static final ResourceLocation TEXTURE = new ResourceLocation("textures/entity/zombie/zombie.png");
public ForestSpiritRenderer(EntityRendererProvider.Context context) { super(context, new HumanoidModel<>(context.bakeLayer(ModelLayers.ZOMBIE)), 0.5F); }
@Override public ResourceLocation getTextureLocation(ForestSpiritEntity entity) { return TEXTURE; }}src/main/resources/assets/examplemod/models/item/forest_spirit_spawn_egg.json
{ "parent": "minecraft:item/template_spawn_egg"}src/main/java/com/example/examplemod/item/ForestSpiritSpawnEggItem.java
package com.example.examplemod.item;
import com.example.examplemod.registry.ModEntities;import net.minecraft.world.item.Item;import net.minecraft.world.item.SpawnEggItem;
// The spawn egg of Forest Spirit, in its two colours.public class ForestSpiritSpawnEggItem extends SpawnEggItem { public ForestSpiritSpawnEggItem(Item.Properties properties) { super(ModEntities.FOREST_SPIRIT, 6196282, 3099166, properties); }}src/main/java/com/example/examplemod/registry/ModEntities.java
package com.example.examplemod.registry;
import com.example.examplemod.ExampleMod;import com.example.examplemod.entity.ForestSpiritEntity;import net.fabricmc.fabric.api.object.builder.v1.entity.FabricDefaultAttributeRegistry;import net.minecraft.core.Registry;import net.minecraft.core.registries.BuiltInRegistries;import net.minecraft.world.entity.EntityType;import net.minecraft.world.entity.MobCategory;
public final class ModEntities { public static final EntityType<ForestSpiritEntity> FOREST_SPIRIT = Registry.register(BuiltInRegistries.ENTITY_TYPE, ExampleMod.id("forest_spirit"), EntityType.Builder.of(ForestSpiritEntity::new, MobCategory.CREATURE).sized(0.6F, 1.95F).build("forest_spirit"));
private ModEntities() { }
// Called from ExampleMod#onInitialize: loading this class registers the entity types above, then their attributes. public static void register() { FabricDefaultAttributeRegistry.register(FOREST_SPIRIT, ForestSpiritEntity.createAttributes()); }}In your main mod class, inside onInitialize:
ModEntities.register();In your client setup class, inside onInitializeClient:
EntityRendererRegistry.register(ModEntities.FOREST_SPIRIT, ForestSpiritRenderer::new);In src/main/resources/assets/examplemod/lang/en_us.json:
{ "entity.examplemod.forest_spirit": "Forest Spirit"}It’s registered like any other item, as ForestSpiritSpawnEggItem: see a new item.
Forge 1.17.1
Section titled “Forge 1.17.1”Uses:
EntityType.Builder.ofDeferredRegister.createForgeRegistries.ENTITY_TYPESEntityAttributeCreationEvent.putEntityRenderersEvent.RegisterRenderers.registerEntityRendererForgeSpawnEggItem
The official pages for entities are written for later versions; this code is compile-tested against 1.20.1. It looks like a zombie until you give it a model and texture of its own.
src/main/java/com/example/examplemod/entity/ForestSpiritEntity.java
package com.example.examplemod.entity;
import net.minecraft.world.entity.EntityType;import net.minecraft.world.entity.PathfinderMob;import net.minecraft.world.entity.ai.attributes.AttributeSupplier;import net.minecraft.world.entity.ai.attributes.Attributes;import net.minecraft.world.entity.ai.goal.FloatGoal;import net.minecraft.world.entity.ai.goal.LookAtPlayerGoal;import net.minecraft.world.entity.ai.goal.PanicGoal;import net.minecraft.world.entity.ai.goal.RandomLookAroundGoal;import net.minecraft.world.entity.ai.goal.WaterAvoidingRandomStrollGoal;import net.minecraft.world.entity.player.Player;import net.minecraft.world.level.Level;
// Forest Spirit: a peaceful mob that wanders, looks at players and runs when hurt. Its goals are its behaviour; its attributes its stats.public class ForestSpiritEntity extends PathfinderMob { public ForestSpiritEntity(EntityType<? extends PathfinderMob> type, Level level) { super(type, level); }
// Registered for the entity type when the mod starts; every living entity needs its attributes. public static AttributeSupplier.Builder createAttributes() { return PathfinderMob.createMobAttributes().add(Attributes.MAX_HEALTH, 10.0F).add(Attributes.MOVEMENT_SPEED, 0.25F); }
@Override protected void registerGoals() { goalSelector.addGoal(0, new FloatGoal(this)); goalSelector.addGoal(1, new PanicGoal(this, 1.25D)); goalSelector.addGoal(2, new WaterAvoidingRandomStrollGoal(this, 1.0D)); goalSelector.addGoal(3, new LookAtPlayerGoal(this, Player.class, 6.0F)); goalSelector.addGoal(4, new RandomLookAroundGoal(this)); }}src/main/java/com/example/examplemod/client/ForestSpiritRenderer.java
package com.example.examplemod.client;
import com.example.examplemod.entity.ForestSpiritEntity;import net.minecraft.client.model.HumanoidModel;import net.minecraft.client.model.geom.ModelLayers;import net.minecraft.client.renderer.entity.EntityRendererProvider;import net.minecraft.client.renderer.entity.MobRenderer;import net.minecraft.resources.ResourceLocation;
// How Forest Spirit is drawn: with the zombie's model and texture until you make its own (in Blockbench, say).public class ForestSpiritRenderer extends MobRenderer<ForestSpiritEntity, HumanoidModel<ForestSpiritEntity>> { private static final ResourceLocation TEXTURE = new ResourceLocation("textures/entity/zombie/zombie.png");
public ForestSpiritRenderer(EntityRendererProvider.Context context) { super(context, new HumanoidModel<>(context.bakeLayer(ModelLayers.ZOMBIE)), 0.5F); }
@Override public ResourceLocation getTextureLocation(ForestSpiritEntity entity) { return TEXTURE; }}src/main/resources/assets/examplemod/models/item/forest_spirit_spawn_egg.json
{ "parent": "minecraft:item/template_spawn_egg"}src/main/java/com/example/examplemod/item/ForestSpiritSpawnEggItem.java
package com.example.examplemod.item;
import com.example.examplemod.registry.ModEntities;import net.minecraft.world.item.Item;import net.minecraftforge.common.ForgeSpawnEggItem;
// The spawn egg of Forest Spirit, in its two colours.public class ForestSpiritSpawnEggItem extends ForgeSpawnEggItem { public ForestSpiritSpawnEggItem(Item.Properties properties) { super(ModEntities.FOREST_SPIRIT, 6196282, 3099166, properties); }}src/main/java/com/example/examplemod/registry/ModEntities.java
package com.example.examplemod.registry;
import com.example.examplemod.ExampleMod;import com.example.examplemod.entity.ForestSpiritEntity;import net.minecraft.world.entity.EntityType;import net.minecraft.world.entity.MobCategory;import net.minecraftforge.event.entity.EntityAttributeCreationEvent;import net.minecraftforge.eventbus.api.IEventBus;import net.minecraftforge.fml.javafmlmod.FMLJavaModLoadingContext;import net.minecraftforge.registries.DeferredRegister;import net.minecraftforge.registries.ForgeRegistries;import net.minecraftforge.fmllegacy.RegistryObject;
public final class ModEntities { public static final DeferredRegister<EntityType<?>> ENTITY_TYPES = DeferredRegister.create(ForgeRegistries.ENTITIES, ExampleMod.MOD_ID);
public static final RegistryObject<EntityType<ForestSpiritEntity>> FOREST_SPIRIT = ENTITY_TYPES.register("forest_spirit", () -> EntityType.Builder.of(ForestSpiritEntity::new, MobCategory.CREATURE).sized(0.6F, 1.95F).build("forest_spirit"));
private ModEntities() { }
// Called from the ExampleMod constructor: the entity types and their attributes join the mod event bus. public static void register() { IEventBus modBus = FMLJavaModLoadingContext.get().getModEventBus(); ENTITY_TYPES.register(modBus); modBus.addListener(ModEntities::registerAttributes); }
// Every living entity type needs its attributes. private static void registerAttributes(EntityAttributeCreationEvent event) { event.put(FOREST_SPIRIT.get(), ForestSpiritEntity.createAttributes().build()); }}src/main/java/com/example/examplemod/client/ForestSpiritEntityClient.java
package com.example.examplemod.client;
import com.example.examplemod.registry.ModEntities;import net.minecraftforge.client.event.EntityRenderersEvent;import net.minecraftforge.fml.javafmlmod.FMLJavaModLoadingContext;
// The client side of Forest Spirit: its renderer.public final class ForestSpiritEntityClient { private ForestSpiritEntityClient() { }
// Called from the mod's constructor, in the client only. public static void register() { FMLJavaModLoadingContext.get().getModEventBus().addListener(ForestSpiritEntityClient::registerRenderers); }
private static void registerRenderers(EntityRenderersEvent.RegisterRenderers event) { event.registerEntityRenderer(ModEntities.FOREST_SPIRIT.get(), ForestSpiritRenderer::new); }}In your main mod class, inside <init>:
ModEntities.register();In your main mod class, inside <init>:
if (FMLEnvironment.dist == Dist.CLIENT) { ForestSpiritEntityClient.register();}In src/main/resources/assets/examplemod/lang/en_us.json:
{ "entity.examplemod.forest_spirit": "Forest Spirit"}It’s registered like any other item, as ForestSpiritSpawnEggItem: see a new item.
Forge 1.18.2
Section titled “Forge 1.18.2”Uses:
EntityType.Builder.ofDeferredRegister.createForgeRegistries.ENTITY_TYPESEntityAttributeCreationEvent.putEntityRenderersEvent.RegisterRenderers.registerEntityRendererForgeSpawnEggItem
The official pages for entities are written for later versions; this code is compile-tested against 1.20.1. It looks like a zombie until you give it a model and texture of its own.
src/main/java/com/example/examplemod/entity/ForestSpiritEntity.java
package com.example.examplemod.entity;
import net.minecraft.world.entity.EntityType;import net.minecraft.world.entity.PathfinderMob;import net.minecraft.world.entity.ai.attributes.AttributeSupplier;import net.minecraft.world.entity.ai.attributes.Attributes;import net.minecraft.world.entity.ai.goal.FloatGoal;import net.minecraft.world.entity.ai.goal.LookAtPlayerGoal;import net.minecraft.world.entity.ai.goal.PanicGoal;import net.minecraft.world.entity.ai.goal.RandomLookAroundGoal;import net.minecraft.world.entity.ai.goal.WaterAvoidingRandomStrollGoal;import net.minecraft.world.entity.player.Player;import net.minecraft.world.level.Level;
// Forest Spirit: a peaceful mob that wanders, looks at players and runs when hurt. Its goals are its behaviour; its attributes its stats.public class ForestSpiritEntity extends PathfinderMob { public ForestSpiritEntity(EntityType<? extends PathfinderMob> type, Level level) { super(type, level); }
// Registered for the entity type when the mod starts; every living entity needs its attributes. public static AttributeSupplier.Builder createAttributes() { return PathfinderMob.createMobAttributes().add(Attributes.MAX_HEALTH, 10.0F).add(Attributes.MOVEMENT_SPEED, 0.25F); }
@Override protected void registerGoals() { goalSelector.addGoal(0, new FloatGoal(this)); goalSelector.addGoal(1, new PanicGoal(this, 1.25D)); goalSelector.addGoal(2, new WaterAvoidingRandomStrollGoal(this, 1.0D)); goalSelector.addGoal(3, new LookAtPlayerGoal(this, Player.class, 6.0F)); goalSelector.addGoal(4, new RandomLookAroundGoal(this)); }}src/main/java/com/example/examplemod/client/ForestSpiritRenderer.java
package com.example.examplemod.client;
import com.example.examplemod.entity.ForestSpiritEntity;import net.minecraft.client.model.HumanoidModel;import net.minecraft.client.model.geom.ModelLayers;import net.minecraft.client.renderer.entity.EntityRendererProvider;import net.minecraft.client.renderer.entity.MobRenderer;import net.minecraft.resources.ResourceLocation;
// How Forest Spirit is drawn: with the zombie's model and texture until you make its own (in Blockbench, say).public class ForestSpiritRenderer extends MobRenderer<ForestSpiritEntity, HumanoidModel<ForestSpiritEntity>> { private static final ResourceLocation TEXTURE = new ResourceLocation("textures/entity/zombie/zombie.png");
public ForestSpiritRenderer(EntityRendererProvider.Context context) { super(context, new HumanoidModel<>(context.bakeLayer(ModelLayers.ZOMBIE)), 0.5F); }
@Override public ResourceLocation getTextureLocation(ForestSpiritEntity entity) { return TEXTURE; }}src/main/resources/assets/examplemod/models/item/forest_spirit_spawn_egg.json
{ "parent": "minecraft:item/template_spawn_egg"}src/main/java/com/example/examplemod/item/ForestSpiritSpawnEggItem.java
package com.example.examplemod.item;
import com.example.examplemod.registry.ModEntities;import net.minecraft.world.item.Item;import net.minecraftforge.common.ForgeSpawnEggItem;
// The spawn egg of Forest Spirit, in its two colours.public class ForestSpiritSpawnEggItem extends ForgeSpawnEggItem { public ForestSpiritSpawnEggItem(Item.Properties properties) { super(ModEntities.FOREST_SPIRIT, 6196282, 3099166, properties); }}src/main/java/com/example/examplemod/registry/ModEntities.java
package com.example.examplemod.registry;
import com.example.examplemod.ExampleMod;import com.example.examplemod.entity.ForestSpiritEntity;import net.minecraft.world.entity.EntityType;import net.minecraft.world.entity.MobCategory;import net.minecraftforge.event.entity.EntityAttributeCreationEvent;import net.minecraftforge.eventbus.api.IEventBus;import net.minecraftforge.fml.javafmlmod.FMLJavaModLoadingContext;import net.minecraftforge.registries.DeferredRegister;import net.minecraftforge.registries.ForgeRegistries;import net.minecraftforge.registries.RegistryObject;
public final class ModEntities { public static final DeferredRegister<EntityType<?>> ENTITY_TYPES = DeferredRegister.create(ForgeRegistries.ENTITIES, ExampleMod.MOD_ID);
public static final RegistryObject<EntityType<ForestSpiritEntity>> FOREST_SPIRIT = ENTITY_TYPES.register("forest_spirit", () -> EntityType.Builder.of(ForestSpiritEntity::new, MobCategory.CREATURE).sized(0.6F, 1.95F).build("forest_spirit"));
private ModEntities() { }
// Called from the ExampleMod constructor: the entity types and their attributes join the mod event bus. public static void register() { IEventBus modBus = FMLJavaModLoadingContext.get().getModEventBus(); ENTITY_TYPES.register(modBus); modBus.addListener(ModEntities::registerAttributes); }
// Every living entity type needs its attributes. private static void registerAttributes(EntityAttributeCreationEvent event) { event.put(FOREST_SPIRIT.get(), ForestSpiritEntity.createAttributes().build()); }}src/main/java/com/example/examplemod/client/ForestSpiritEntityClient.java
package com.example.examplemod.client;
import com.example.examplemod.registry.ModEntities;import net.minecraftforge.client.event.EntityRenderersEvent;import net.minecraftforge.fml.javafmlmod.FMLJavaModLoadingContext;
// The client side of Forest Spirit: its renderer.public final class ForestSpiritEntityClient { private ForestSpiritEntityClient() { }
// Called from the mod's constructor, in the client only. public static void register() { FMLJavaModLoadingContext.get().getModEventBus().addListener(ForestSpiritEntityClient::registerRenderers); }
private static void registerRenderers(EntityRenderersEvent.RegisterRenderers event) { event.registerEntityRenderer(ModEntities.FOREST_SPIRIT.get(), ForestSpiritRenderer::new); }}In your main mod class, inside <init>:
ModEntities.register();In your main mod class, inside <init>:
if (FMLEnvironment.dist == Dist.CLIENT) { ForestSpiritEntityClient.register();}In src/main/resources/assets/examplemod/lang/en_us.json:
{ "entity.examplemod.forest_spirit": "Forest Spirit"}It’s registered like any other item, as ForestSpiritSpawnEggItem: see a new item.
Forge 1.19 to 1.20.1, NeoForge 1.20.1
Section titled “Forge 1.19 to 1.20.1, NeoForge 1.20.1”Uses:
EntityType.Builder.ofDeferredRegister.createForgeRegistries.ENTITY_TYPESEntityAttributeCreationEvent.putEntityRenderersEvent.RegisterRenderers.registerEntityRendererForgeSpawnEggItem
The official pages for entities are written for later versions; this code is compile-tested against 1.20.1. It looks like a zombie until you give it a model and texture of its own.
src/main/java/com/example/examplemod/entity/ForestSpiritEntity.java
package com.example.examplemod.entity;
import net.minecraft.world.entity.EntityType;import net.minecraft.world.entity.PathfinderMob;import net.minecraft.world.entity.ai.attributes.AttributeSupplier;import net.minecraft.world.entity.ai.attributes.Attributes;import net.minecraft.world.entity.ai.goal.FloatGoal;import net.minecraft.world.entity.ai.goal.LookAtPlayerGoal;import net.minecraft.world.entity.ai.goal.PanicGoal;import net.minecraft.world.entity.ai.goal.RandomLookAroundGoal;import net.minecraft.world.entity.ai.goal.WaterAvoidingRandomStrollGoal;import net.minecraft.world.entity.player.Player;import net.minecraft.world.level.Level;
// Forest Spirit: a peaceful mob that wanders, looks at players and runs when hurt. Its goals are its behaviour; its attributes its stats.public class ForestSpiritEntity extends PathfinderMob { public ForestSpiritEntity(EntityType<? extends PathfinderMob> type, Level level) { super(type, level); }
// Registered for the entity type when the mod starts; every living entity needs its attributes. public static AttributeSupplier.Builder createAttributes() { return PathfinderMob.createMobAttributes().add(Attributes.MAX_HEALTH, 10.0F).add(Attributes.MOVEMENT_SPEED, 0.25F); }
@Override protected void registerGoals() { goalSelector.addGoal(0, new FloatGoal(this)); goalSelector.addGoal(1, new PanicGoal(this, 1.25D)); goalSelector.addGoal(2, new WaterAvoidingRandomStrollGoal(this, 1.0D)); goalSelector.addGoal(3, new LookAtPlayerGoal(this, Player.class, 6.0F)); goalSelector.addGoal(4, new RandomLookAroundGoal(this)); }}src/main/java/com/example/examplemod/client/ForestSpiritRenderer.java
package com.example.examplemod.client;
import com.example.examplemod.entity.ForestSpiritEntity;import net.minecraft.client.model.HumanoidModel;import net.minecraft.client.model.geom.ModelLayers;import net.minecraft.client.renderer.entity.EntityRendererProvider;import net.minecraft.client.renderer.entity.MobRenderer;import net.minecraft.resources.ResourceLocation;
// How Forest Spirit is drawn: with the zombie's model and texture until you make its own (in Blockbench, say).public class ForestSpiritRenderer extends MobRenderer<ForestSpiritEntity, HumanoidModel<ForestSpiritEntity>> { private static final ResourceLocation TEXTURE = new ResourceLocation("textures/entity/zombie/zombie.png");
public ForestSpiritRenderer(EntityRendererProvider.Context context) { super(context, new HumanoidModel<>(context.bakeLayer(ModelLayers.ZOMBIE)), 0.5F); }
@Override public ResourceLocation getTextureLocation(ForestSpiritEntity entity) { return TEXTURE; }}src/main/resources/assets/examplemod/models/item/forest_spirit_spawn_egg.json
{ "parent": "minecraft:item/template_spawn_egg"}src/main/java/com/example/examplemod/item/ForestSpiritSpawnEggItem.java
package com.example.examplemod.item;
import com.example.examplemod.registry.ModEntities;import net.minecraft.world.item.Item;import net.minecraftforge.common.ForgeSpawnEggItem;
// The spawn egg of Forest Spirit, in its two colours.public class ForestSpiritSpawnEggItem extends ForgeSpawnEggItem { public ForestSpiritSpawnEggItem(Item.Properties properties) { super(ModEntities.FOREST_SPIRIT, 6196282, 3099166, properties); }}src/main/java/com/example/examplemod/registry/ModEntities.java
package com.example.examplemod.registry;
import com.example.examplemod.ExampleMod;import com.example.examplemod.entity.ForestSpiritEntity;import net.minecraft.world.entity.EntityType;import net.minecraft.world.entity.MobCategory;import net.minecraftforge.event.entity.EntityAttributeCreationEvent;import net.minecraftforge.eventbus.api.IEventBus;import net.minecraftforge.fml.javafmlmod.FMLJavaModLoadingContext;import net.minecraftforge.registries.DeferredRegister;import net.minecraftforge.registries.ForgeRegistries;import net.minecraftforge.registries.RegistryObject;
public final class ModEntities { public static final DeferredRegister<EntityType<?>> ENTITY_TYPES = DeferredRegister.create(ForgeRegistries.ENTITY_TYPES, ExampleMod.MOD_ID);
public static final RegistryObject<EntityType<ForestSpiritEntity>> FOREST_SPIRIT = ENTITY_TYPES.register("forest_spirit", () -> EntityType.Builder.of(ForestSpiritEntity::new, MobCategory.CREATURE).sized(0.6F, 1.95F).build("forest_spirit"));
private ModEntities() { }
// Called from the ExampleMod constructor: the entity types and their attributes join the mod event bus. public static void register() { IEventBus modBus = FMLJavaModLoadingContext.get().getModEventBus(); ENTITY_TYPES.register(modBus); modBus.addListener(ModEntities::registerAttributes); }
// Every living entity type needs its attributes. private static void registerAttributes(EntityAttributeCreationEvent event) { event.put(FOREST_SPIRIT.get(), ForestSpiritEntity.createAttributes().build()); }}src/main/java/com/example/examplemod/client/ForestSpiritEntityClient.java
package com.example.examplemod.client;
import com.example.examplemod.registry.ModEntities;import net.minecraftforge.client.event.EntityRenderersEvent;import net.minecraftforge.fml.javafmlmod.FMLJavaModLoadingContext;
// The client side of Forest Spirit: its renderer.public final class ForestSpiritEntityClient { private ForestSpiritEntityClient() { }
// Called from the mod's constructor, in the client only. public static void register() { FMLJavaModLoadingContext.get().getModEventBus().addListener(ForestSpiritEntityClient::registerRenderers); }
private static void registerRenderers(EntityRenderersEvent.RegisterRenderers event) { event.registerEntityRenderer(ModEntities.FOREST_SPIRIT.get(), ForestSpiritRenderer::new); }}In your main mod class, inside <init>:
ModEntities.register();In your main mod class, inside <init>:
if (FMLEnvironment.dist == Dist.CLIENT) { ForestSpiritEntityClient.register();}In src/main/resources/assets/examplemod/lang/en_us.json:
{ "entity.examplemod.forest_spirit": "Forest Spirit"}It’s registered like any other item, as ForestSpiritSpawnEggItem: see a new item.
Fabric 1.21 to 1.21.1
Section titled “Fabric 1.21 to 1.21.1”Uses:
EntityType.Builder.ofBuiltInRegistries.ENTITY_TYPEFabricDefaultAttributeRegistry.registerEntityRendererRegistry.registerMobRendererSpawnEggItem
The official pages for entities are written for later versions; this code is compile-tested against 1.21.1. It looks like a zombie until you give it a model and texture of its own.
src/main/java/com/example/examplemod/entity/ForestSpiritEntity.java
package com.example.examplemod.entity;
import net.minecraft.world.entity.EntityType;import net.minecraft.world.entity.PathfinderMob;import net.minecraft.world.entity.ai.attributes.AttributeSupplier;import net.minecraft.world.entity.ai.attributes.Attributes;import net.minecraft.world.entity.ai.goal.FloatGoal;import net.minecraft.world.entity.ai.goal.LookAtPlayerGoal;import net.minecraft.world.entity.ai.goal.PanicGoal;import net.minecraft.world.entity.ai.goal.RandomLookAroundGoal;import net.minecraft.world.entity.ai.goal.WaterAvoidingRandomStrollGoal;import net.minecraft.world.entity.player.Player;import net.minecraft.world.level.Level;
// Forest Spirit: a peaceful mob that wanders, looks at players and runs when hurt. Its goals are its behaviour; its attributes its stats.public class ForestSpiritEntity extends PathfinderMob { public ForestSpiritEntity(EntityType<? extends PathfinderMob> type, Level level) { super(type, level); }
// Registered for the entity type when the mod starts; every living entity needs its attributes. public static AttributeSupplier.Builder createAttributes() { return PathfinderMob.createMobAttributes().add(Attributes.MAX_HEALTH, 10.0F).add(Attributes.MOVEMENT_SPEED, 0.25F); }
@Override protected void registerGoals() { goalSelector.addGoal(0, new FloatGoal(this)); goalSelector.addGoal(1, new PanicGoal(this, 1.25D)); goalSelector.addGoal(2, new WaterAvoidingRandomStrollGoal(this, 1.0D)); goalSelector.addGoal(3, new LookAtPlayerGoal(this, Player.class, 6.0F)); goalSelector.addGoal(4, new RandomLookAroundGoal(this)); }}src/main/java/com/example/examplemod/client/ForestSpiritRenderer.java
package com.example.examplemod.client;
import com.example.examplemod.entity.ForestSpiritEntity;import net.minecraft.client.model.HumanoidModel;import net.minecraft.client.model.geom.ModelLayers;import net.minecraft.client.renderer.entity.EntityRendererProvider;import net.minecraft.client.renderer.entity.MobRenderer;import net.minecraft.resources.ResourceLocation;
// How Forest Spirit is drawn: with the zombie's model and texture until you make its own (in Blockbench, say).public class ForestSpiritRenderer extends MobRenderer<ForestSpiritEntity, HumanoidModel<ForestSpiritEntity>> { private static final ResourceLocation TEXTURE = ResourceLocation.withDefaultNamespace("textures/entity/zombie/zombie.png");
public ForestSpiritRenderer(EntityRendererProvider.Context context) { super(context, new HumanoidModel<>(context.bakeLayer(ModelLayers.ZOMBIE)), 0.5F); }
@Override public ResourceLocation getTextureLocation(ForestSpiritEntity entity) { return TEXTURE; }}src/main/resources/assets/examplemod/models/item/forest_spirit_spawn_egg.json
{ "parent": "minecraft:item/template_spawn_egg"}src/main/java/com/example/examplemod/item/ForestSpiritSpawnEggItem.java
package com.example.examplemod.item;
import com.example.examplemod.registry.ModEntities;import net.minecraft.world.item.Item;import net.minecraft.world.item.SpawnEggItem;
// The spawn egg of Forest Spirit, in its two colours.public class ForestSpiritSpawnEggItem extends SpawnEggItem { public ForestSpiritSpawnEggItem(Item.Properties properties) { super(ModEntities.FOREST_SPIRIT, 6196282, 3099166, properties); }}src/main/java/com/example/examplemod/registry/ModEntities.java
package com.example.examplemod.registry;
import com.example.examplemod.ExampleMod;import com.example.examplemod.entity.ForestSpiritEntity;import net.fabricmc.fabric.api.object.builder.v1.entity.FabricDefaultAttributeRegistry;import net.minecraft.core.Registry;import net.minecraft.core.registries.BuiltInRegistries;import net.minecraft.world.entity.EntityType;import net.minecraft.world.entity.MobCategory;
public final class ModEntities { public static final EntityType<ForestSpiritEntity> FOREST_SPIRIT = Registry.register(BuiltInRegistries.ENTITY_TYPE, ExampleMod.id("forest_spirit"), EntityType.Builder.of(ForestSpiritEntity::new, MobCategory.CREATURE).sized(0.6F, 1.95F).build("forest_spirit"));
private ModEntities() { }
// Called from ExampleMod#onInitialize: loading this class registers the entity types above, then their attributes. public static void register() { FabricDefaultAttributeRegistry.register(FOREST_SPIRIT, ForestSpiritEntity.createAttributes()); }}In your main mod class, inside onInitialize:
ModEntities.register();In your client setup class, inside onInitializeClient:
EntityRendererRegistry.register(ModEntities.FOREST_SPIRIT, ForestSpiritRenderer::new);In src/main/resources/assets/examplemod/lang/en_us.json:
{ "entity.examplemod.forest_spirit": "Forest Spirit"}It’s registered like any other item, as ForestSpiritSpawnEggItem: see a new item.
NeoForge 1.21 to 1.21.1
Section titled “NeoForge 1.21 to 1.21.1”Uses:
EntityType.Builder.ofDeferredRegister.createRegistries.ENTITY_TYPEEntityAttributeCreationEvent.putEntityRenderersEvent.RegisterRenderers.registerEntityRendererDeferredSpawnEggItem
The official pages for entities are written for later versions; this code is compile-tested against 1.21.1. It looks like a zombie until you give it a model and texture of its own.
src/main/java/com/example/examplemod/entity/ForestSpiritEntity.java
package com.example.examplemod.entity;
import net.minecraft.world.entity.EntityType;import net.minecraft.world.entity.PathfinderMob;import net.minecraft.world.entity.ai.attributes.AttributeSupplier;import net.minecraft.world.entity.ai.attributes.Attributes;import net.minecraft.world.entity.ai.goal.FloatGoal;import net.minecraft.world.entity.ai.goal.LookAtPlayerGoal;import net.minecraft.world.entity.ai.goal.PanicGoal;import net.minecraft.world.entity.ai.goal.RandomLookAroundGoal;import net.minecraft.world.entity.ai.goal.WaterAvoidingRandomStrollGoal;import net.minecraft.world.entity.player.Player;import net.minecraft.world.level.Level;
// Forest Spirit: a peaceful mob that wanders, looks at players and runs when hurt. Its goals are its behaviour; its attributes its stats.public class ForestSpiritEntity extends PathfinderMob { public ForestSpiritEntity(EntityType<? extends PathfinderMob> type, Level level) { super(type, level); }
// Registered for the entity type when the mod starts; every living entity needs its attributes. public static AttributeSupplier.Builder createAttributes() { return PathfinderMob.createMobAttributes().add(Attributes.MAX_HEALTH, 10.0F).add(Attributes.MOVEMENT_SPEED, 0.25F); }
@Override protected void registerGoals() { goalSelector.addGoal(0, new FloatGoal(this)); goalSelector.addGoal(1, new PanicGoal(this, 1.25D)); goalSelector.addGoal(2, new WaterAvoidingRandomStrollGoal(this, 1.0D)); goalSelector.addGoal(3, new LookAtPlayerGoal(this, Player.class, 6.0F)); goalSelector.addGoal(4, new RandomLookAroundGoal(this)); }}src/main/java/com/example/examplemod/client/ForestSpiritRenderer.java
package com.example.examplemod.client;
import com.example.examplemod.entity.ForestSpiritEntity;import net.minecraft.client.model.HumanoidModel;import net.minecraft.client.model.geom.ModelLayers;import net.minecraft.client.renderer.entity.EntityRendererProvider;import net.minecraft.client.renderer.entity.MobRenderer;import net.minecraft.resources.ResourceLocation;
// How Forest Spirit is drawn: with the zombie's model and texture until you make its own (in Blockbench, say).public class ForestSpiritRenderer extends MobRenderer<ForestSpiritEntity, HumanoidModel<ForestSpiritEntity>> { private static final ResourceLocation TEXTURE = ResourceLocation.withDefaultNamespace("textures/entity/zombie/zombie.png");
public ForestSpiritRenderer(EntityRendererProvider.Context context) { super(context, new HumanoidModel<>(context.bakeLayer(ModelLayers.ZOMBIE)), 0.5F); }
@Override public ResourceLocation getTextureLocation(ForestSpiritEntity entity) { return TEXTURE; }}src/main/resources/assets/examplemod/models/item/forest_spirit_spawn_egg.json
{ "parent": "minecraft:item/template_spawn_egg"}src/main/java/com/example/examplemod/item/ForestSpiritSpawnEggItem.java
package com.example.examplemod.item;
import com.example.examplemod.registry.ModEntities;import net.minecraft.world.item.Item;import net.neoforged.neoforge.common.DeferredSpawnEggItem;
// The spawn egg of Forest Spirit, in its two colours.public class ForestSpiritSpawnEggItem extends DeferredSpawnEggItem { public ForestSpiritSpawnEggItem(Item.Properties properties) { super(ModEntities.FOREST_SPIRIT, 6196282, 3099166, properties); }}src/main/java/com/example/examplemod/registry/ModEntities.java
package com.example.examplemod.registry;
import com.example.examplemod.ExampleMod;import com.example.examplemod.entity.ForestSpiritEntity;import net.minecraft.core.registries.Registries;import net.minecraft.world.entity.EntityType;import net.minecraft.world.entity.MobCategory;import net.neoforged.neoforge.event.entity.EntityAttributeCreationEvent;import net.neoforged.neoforge.registries.DeferredHolder;import net.neoforged.neoforge.registries.DeferredRegister;
public final class ModEntities { // Registered on the mod event bus in ExampleMod's constructor. public static final DeferredRegister<EntityType<?>> ENTITY_TYPES = DeferredRegister.create(Registries.ENTITY_TYPE, ExampleMod.MOD_ID);
public static final DeferredHolder<EntityType<?>, EntityType<ForestSpiritEntity>> FOREST_SPIRIT = ENTITY_TYPES.register("forest_spirit", () -> EntityType.Builder.of(ForestSpiritEntity::new, MobCategory.CREATURE).sized(0.6F, 1.95F).build("forest_spirit"));
private ModEntities() { }
// Listens on the mod event bus: every living entity type needs its attributes. public static void registerAttributes(EntityAttributeCreationEvent event) { event.put(FOREST_SPIRIT.get(), ForestSpiritEntity.createAttributes().build()); }}src/main/java/com/example/examplemod/client/ForestSpiritEntityClient.java
package com.example.examplemod.client;
import com.example.examplemod.registry.ModEntities;import net.neoforged.bus.api.IEventBus;import net.neoforged.neoforge.client.event.EntityRenderersEvent;
// The client side of Forest Spirit: its renderer.public final class ForestSpiritEntityClient { private ForestSpiritEntityClient() { }
// Called from the mod's constructor, in the client only. public static void register(IEventBus modBus) { modBus.addListener(ForestSpiritEntityClient::registerRenderers); }
private static void registerRenderers(EntityRenderersEvent.RegisterRenderers event) { event.registerEntityRenderer(ModEntities.FOREST_SPIRIT.get(), ForestSpiritRenderer::new); }}In your main mod class, inside <init>:
ModEntities.ENTITY_TYPES.register(modEventBus);In your main mod class, inside <init>:
modEventBus.addListener(ModEntities::registerAttributes);In your main mod class, inside <init>:
if (FMLEnvironment.dist == Dist.CLIENT) { ForestSpiritEntityClient.register(modEventBus);}In src/main/resources/assets/examplemod/lang/en_us.json:
{ "entity.examplemod.forest_spirit": "Forest Spirit"}It’s registered like any other item, as ForestSpiritSpawnEggItem: see a new item.
NeoForge 1.20.2 to 1.20.6
Section titled “NeoForge 1.20.2 to 1.20.6”Uses:
EntityType.Builder.ofDeferredRegister.createRegistries.ENTITY_TYPEEntityAttributeCreationEvent.putEntityRenderersEvent.RegisterRenderers.registerEntityRendererDeferredSpawnEggItem
The official pages for entities are written for later versions; this code is compile-tested against 1.21.1. It looks like a zombie until you give it a model and texture of its own.
src/main/java/com/example/examplemod/entity/ForestSpiritEntity.java
package com.example.examplemod.entity;
import net.minecraft.world.entity.EntityType;import net.minecraft.world.entity.PathfinderMob;import net.minecraft.world.entity.ai.attributes.AttributeSupplier;import net.minecraft.world.entity.ai.attributes.Attributes;import net.minecraft.world.entity.ai.goal.FloatGoal;import net.minecraft.world.entity.ai.goal.LookAtPlayerGoal;import net.minecraft.world.entity.ai.goal.PanicGoal;import net.minecraft.world.entity.ai.goal.RandomLookAroundGoal;import net.minecraft.world.entity.ai.goal.WaterAvoidingRandomStrollGoal;import net.minecraft.world.entity.player.Player;import net.minecraft.world.level.Level;
// Forest Spirit: a peaceful mob that wanders, looks at players and runs when hurt. Its goals are its behaviour; its attributes its stats.public class ForestSpiritEntity extends PathfinderMob { public ForestSpiritEntity(EntityType<? extends PathfinderMob> type, Level level) { super(type, level); }
// Registered for the entity type when the mod starts; every living entity needs its attributes. public static AttributeSupplier.Builder createAttributes() { return PathfinderMob.createMobAttributes().add(Attributes.MAX_HEALTH, 10.0F).add(Attributes.MOVEMENT_SPEED, 0.25F); }
@Override protected void registerGoals() { goalSelector.addGoal(0, new FloatGoal(this)); goalSelector.addGoal(1, new PanicGoal(this, 1.25D)); goalSelector.addGoal(2, new WaterAvoidingRandomStrollGoal(this, 1.0D)); goalSelector.addGoal(3, new LookAtPlayerGoal(this, Player.class, 6.0F)); goalSelector.addGoal(4, new RandomLookAroundGoal(this)); }}src/main/java/com/example/examplemod/client/ForestSpiritRenderer.java
package com.example.examplemod.client;
import com.example.examplemod.entity.ForestSpiritEntity;import net.minecraft.client.model.HumanoidModel;import net.minecraft.client.model.geom.ModelLayers;import net.minecraft.client.renderer.entity.EntityRendererProvider;import net.minecraft.client.renderer.entity.MobRenderer;import net.minecraft.resources.ResourceLocation;
// How Forest Spirit is drawn: with the zombie's model and texture until you make its own (in Blockbench, say).public class ForestSpiritRenderer extends MobRenderer<ForestSpiritEntity, HumanoidModel<ForestSpiritEntity>> { private static final ResourceLocation TEXTURE = new ResourceLocation("textures/entity/zombie/zombie.png");
public ForestSpiritRenderer(EntityRendererProvider.Context context) { super(context, new HumanoidModel<>(context.bakeLayer(ModelLayers.ZOMBIE)), 0.5F); }
@Override public ResourceLocation getTextureLocation(ForestSpiritEntity entity) { return TEXTURE; }}src/main/resources/assets/examplemod/models/item/forest_spirit_spawn_egg.json
{ "parent": "minecraft:item/template_spawn_egg"}src/main/java/com/example/examplemod/item/ForestSpiritSpawnEggItem.java
package com.example.examplemod.item;
import com.example.examplemod.registry.ModEntities;import net.minecraft.world.item.Item;import net.neoforged.neoforge.common.DeferredSpawnEggItem;
// The spawn egg of Forest Spirit, in its two colours.public class ForestSpiritSpawnEggItem extends DeferredSpawnEggItem { public ForestSpiritSpawnEggItem(Item.Properties properties) { super(ModEntities.FOREST_SPIRIT, 6196282, 3099166, properties); }}src/main/java/com/example/examplemod/registry/ModEntities.java
package com.example.examplemod.registry;
import com.example.examplemod.ExampleMod;import com.example.examplemod.entity.ForestSpiritEntity;import net.minecraft.core.registries.Registries;import net.minecraft.world.entity.EntityType;import net.minecraft.world.entity.MobCategory;import net.neoforged.neoforge.event.entity.EntityAttributeCreationEvent;import net.neoforged.neoforge.registries.DeferredHolder;import net.neoforged.neoforge.registries.DeferredRegister;
public final class ModEntities { // Registered on the mod event bus in ExampleMod's constructor. public static final DeferredRegister<EntityType<?>> ENTITY_TYPES = DeferredRegister.create(Registries.ENTITY_TYPE, ExampleMod.MOD_ID);
public static final DeferredHolder<EntityType<?>, EntityType<ForestSpiritEntity>> FOREST_SPIRIT = ENTITY_TYPES.register("forest_spirit", () -> EntityType.Builder.of(ForestSpiritEntity::new, MobCategory.CREATURE).sized(0.6F, 1.95F).build("forest_spirit"));
private ModEntities() { }
// Listens on the mod event bus: every living entity type needs its attributes. public static void registerAttributes(EntityAttributeCreationEvent event) { event.put(FOREST_SPIRIT.get(), ForestSpiritEntity.createAttributes().build()); }}src/main/java/com/example/examplemod/client/ForestSpiritEntityClient.java
package com.example.examplemod.client;
import com.example.examplemod.registry.ModEntities;import net.neoforged.bus.api.IEventBus;import net.neoforged.neoforge.client.event.EntityRenderersEvent;
// The client side of Forest Spirit: its renderer.public final class ForestSpiritEntityClient { private ForestSpiritEntityClient() { }
// Called from the mod's constructor, in the client only. public static void register(IEventBus modBus) { modBus.addListener(ForestSpiritEntityClient::registerRenderers); }
private static void registerRenderers(EntityRenderersEvent.RegisterRenderers event) { event.registerEntityRenderer(ModEntities.FOREST_SPIRIT.get(), ForestSpiritRenderer::new); }}In your main mod class, inside <init>:
ModEntities.ENTITY_TYPES.register(modEventBus);In your main mod class, inside <init>:
modEventBus.addListener(ModEntities::registerAttributes);In your main mod class, inside <init>:
if (FMLEnvironment.dist == Dist.CLIENT) { ForestSpiritEntityClient.register(modEventBus);}In src/main/resources/assets/examplemod/lang/en_us.json:
{ "entity.examplemod.forest_spirit": "Forest Spirit"}It’s registered like any other item, as ForestSpiritSpawnEggItem: see a new item.
Fabric 1.21.2 to 1.21.3
Section titled “Fabric 1.21.2 to 1.21.3”Uses:
EntityType.Builder.buildBuiltInRegistries.ENTITY_TYPEFabricDefaultAttributeRegistry.registerEntityRendererRegistry.registerMobRenderer.createRenderStateSpawnEggItem
Since 1.21.2 renderers draw a render state and an entity type is built with its registry key; the spawn egg’s two colours are still constructor arguments until 1.21.4 moved them into the item definition. It looks like a zombie until you give it a model and texture of its own.
src/main/java/com/example/examplemod/entity/ForestSpiritEntity.java
package com.example.examplemod.entity;
import net.minecraft.world.entity.EntityType;import net.minecraft.world.entity.PathfinderMob;import net.minecraft.world.entity.ai.attributes.AttributeSupplier;import net.minecraft.world.entity.ai.attributes.Attributes;import net.minecraft.world.entity.ai.goal.FloatGoal;import net.minecraft.world.entity.ai.goal.LookAtPlayerGoal;import net.minecraft.world.entity.ai.goal.PanicGoal;import net.minecraft.world.entity.ai.goal.RandomLookAroundGoal;import net.minecraft.world.entity.ai.goal.WaterAvoidingRandomStrollGoal;import net.minecraft.world.entity.player.Player;import net.minecraft.world.level.Level;
// Forest Spirit: a peaceful mob that wanders, looks at players and runs when hurt. Its goals are its behaviour; its attributes its stats.public class ForestSpiritEntity extends PathfinderMob { public ForestSpiritEntity(EntityType<? extends PathfinderMob> type, Level level) { super(type, level); }
// Registered for the entity type when the mod starts; every living entity needs its attributes. public static AttributeSupplier.Builder createAttributes() { return PathfinderMob.createMobAttributes().add(Attributes.MAX_HEALTH, 10.0F).add(Attributes.MOVEMENT_SPEED, 0.25F); }
@Override protected void registerGoals() { goalSelector.addGoal(0, new FloatGoal(this)); goalSelector.addGoal(1, new PanicGoal(this, 1.25D)); goalSelector.addGoal(2, new WaterAvoidingRandomStrollGoal(this, 1.0D)); goalSelector.addGoal(3, new LookAtPlayerGoal(this, Player.class, 6.0F)); goalSelector.addGoal(4, new RandomLookAroundGoal(this)); }}src/main/java/com/example/examplemod/client/ForestSpiritRenderer.java
package com.example.examplemod.client;
import com.example.examplemod.entity.ForestSpiritEntity;import net.minecraft.client.model.HumanoidModel;import net.minecraft.client.model.geom.ModelLayers;import net.minecraft.client.renderer.entity.EntityRendererProvider;import net.minecraft.client.renderer.entity.MobRenderer;import net.minecraft.client.renderer.entity.state.HumanoidRenderState;import net.minecraft.resources.ResourceLocation;
// How Forest Spirit is drawn: with the zombie's model and texture until you make its own (in Blockbench, say).// Since 1.21.2 a renderer draws a render state (a snapshot of the entity), not the entity itself.public class ForestSpiritRenderer extends MobRenderer<ForestSpiritEntity, HumanoidRenderState, HumanoidModel<HumanoidRenderState>> { private static final ResourceLocation TEXTURE = ResourceLocation.withDefaultNamespace("textures/entity/zombie/zombie.png");
public ForestSpiritRenderer(EntityRendererProvider.Context context) { super(context, new HumanoidModel<>(context.bakeLayer(ModelLayers.ZOMBIE)), 0.5F); }
@Override public HumanoidRenderState createRenderState() { return new HumanoidRenderState(); }
@Override public ResourceLocation getTextureLocation(HumanoidRenderState state) { return TEXTURE; }}src/main/resources/assets/examplemod/models/item/forest_spirit_spawn_egg.json
{ "parent": "minecraft:item/template_spawn_egg"}src/main/java/com/example/examplemod/item/ForestSpiritSpawnEggItem.java
package com.example.examplemod.item;
import com.example.examplemod.registry.ModEntities;import net.minecraft.world.item.SpawnEggItem;
// The spawn egg of Forest Spirit, in its two colours.public class ForestSpiritSpawnEggItem extends SpawnEggItem { public ForestSpiritSpawnEggItem(Properties properties) { super(ModEntities.FOREST_SPIRIT, 6196282, 3099166, properties); }}src/main/java/com/example/examplemod/registry/ModEntities.java
package com.example.examplemod.registry;
import com.example.examplemod.ExampleMod;import com.example.examplemod.entity.ForestSpiritEntity;import net.fabricmc.fabric.api.object.builder.v1.entity.FabricDefaultAttributeRegistry;import net.minecraft.core.Registry;import net.minecraft.core.registries.BuiltInRegistries;import net.minecraft.core.registries.Registries;import net.minecraft.resources.ResourceKey;import net.minecraft.world.entity.EntityType;import net.minecraft.world.entity.MobCategory;
public final class ModEntities { public static final EntityType<ForestSpiritEntity> FOREST_SPIRIT = Registry.register(BuiltInRegistries.ENTITY_TYPE, ExampleMod.id("forest_spirit"), EntityType.Builder.of(ForestSpiritEntity::new, MobCategory.CREATURE).sized(0.6F, 1.95F).build(ResourceKey.create(Registries.ENTITY_TYPE, ExampleMod.id("forest_spirit"))));
private ModEntities() { }
// Called from ExampleMod#onInitialize: loading this class registers the entity types above, then their attributes. public static void register() { FabricDefaultAttributeRegistry.register(FOREST_SPIRIT, ForestSpiritEntity.createAttributes()); }}In your main mod class, inside onInitialize:
ModEntities.register();In your client setup class, inside onInitializeClient:
EntityRendererRegistry.register(ModEntities.FOREST_SPIRIT, ForestSpiritRenderer::new);In src/main/resources/assets/examplemod/lang/en_us.json:
{ "entity.examplemod.forest_spirit": "Forest Spirit"}It’s registered like any other item, as ForestSpiritSpawnEggItem: see a new item.
Fabric 1.21.4 to 1.21.8
Section titled “Fabric 1.21.4 to 1.21.8”Uses:
EntityType.Builder.buildBuiltInRegistries.ENTITY_TYPEFabricDefaultAttributeRegistry.registerEntityRendererRegistry.registerMobRenderer.createRenderStateSpawnEggItem
Since 1.21.2 renderers draw a render state and since 1.21.4 an item’s model and tints live in assets/<namespace>/items/<id>.json; this code is compile-tested against 1.21.4. It looks like a zombie until you give it a model and texture of its own.
src/main/java/com/example/examplemod/entity/ForestSpiritEntity.java
package com.example.examplemod.entity;
import net.minecraft.world.entity.EntityType;import net.minecraft.world.entity.PathfinderMob;import net.minecraft.world.entity.ai.attributes.AttributeSupplier;import net.minecraft.world.entity.ai.attributes.Attributes;import net.minecraft.world.entity.ai.goal.FloatGoal;import net.minecraft.world.entity.ai.goal.LookAtPlayerGoal;import net.minecraft.world.entity.ai.goal.PanicGoal;import net.minecraft.world.entity.ai.goal.RandomLookAroundGoal;import net.minecraft.world.entity.ai.goal.WaterAvoidingRandomStrollGoal;import net.minecraft.world.entity.player.Player;import net.minecraft.world.level.Level;
// Forest Spirit: a peaceful mob that wanders, looks at players and runs when hurt. Its goals are its behaviour; its attributes its stats.public class ForestSpiritEntity extends PathfinderMob { public ForestSpiritEntity(EntityType<? extends PathfinderMob> type, Level level) { super(type, level); }
// Registered for the entity type when the mod starts; every living entity needs its attributes. public static AttributeSupplier.Builder createAttributes() { return PathfinderMob.createMobAttributes().add(Attributes.MAX_HEALTH, 10.0F).add(Attributes.MOVEMENT_SPEED, 0.25F); }
@Override protected void registerGoals() { goalSelector.addGoal(0, new FloatGoal(this)); goalSelector.addGoal(1, new PanicGoal(this, 1.25D)); goalSelector.addGoal(2, new WaterAvoidingRandomStrollGoal(this, 1.0D)); goalSelector.addGoal(3, new LookAtPlayerGoal(this, Player.class, 6.0F)); goalSelector.addGoal(4, new RandomLookAroundGoal(this)); }}src/main/java/com/example/examplemod/client/ForestSpiritRenderer.java
package com.example.examplemod.client;
import com.example.examplemod.entity.ForestSpiritEntity;import net.minecraft.client.model.HumanoidModel;import net.minecraft.client.model.geom.ModelLayers;import net.minecraft.client.renderer.entity.EntityRendererProvider;import net.minecraft.client.renderer.entity.MobRenderer;import net.minecraft.client.renderer.entity.state.HumanoidRenderState;import net.minecraft.resources.ResourceLocation;
// How Forest Spirit is drawn: with the zombie's model and texture until you make its own (in Blockbench, say).// Since 1.21.2 a renderer draws a render state (a snapshot of the entity), not the entity itself.public class ForestSpiritRenderer extends MobRenderer<ForestSpiritEntity, HumanoidRenderState, HumanoidModel<HumanoidRenderState>> { private static final ResourceLocation TEXTURE = ResourceLocation.withDefaultNamespace("textures/entity/zombie/zombie.png");
public ForestSpiritRenderer(EntityRendererProvider.Context context) { super(context, new HumanoidModel<>(context.bakeLayer(ModelLayers.ZOMBIE)), 0.5F); }
@Override public HumanoidRenderState createRenderState() { return new HumanoidRenderState(); }
@Override public ResourceLocation getTextureLocation(HumanoidRenderState state) { return TEXTURE; }}src/main/resources/assets/examplemod/items/forest_spirit_spawn_egg.json
{ "model": { "type": "minecraft:model", "model": "minecraft:item/template_spawn_egg", "tints": [ { "type": "minecraft:constant", "value": 6196282 }, { "type": "minecraft:constant", "value": 3099166 } ] }}src/main/java/com/example/examplemod/item/ForestSpiritSpawnEggItem.java
package com.example.examplemod.item;
import com.example.examplemod.registry.ModEntities;import net.minecraft.world.item.Item;import net.minecraft.world.item.SpawnEggItem;
// The spawn egg of Forest Spirit. Its two colours are in its item definition, assets/examplemod/items/forest_spirit_spawn_egg.json.public class ForestSpiritSpawnEggItem extends SpawnEggItem { public ForestSpiritSpawnEggItem(Item.Properties properties) { super(ModEntities.FOREST_SPIRIT, properties); }}src/main/java/com/example/examplemod/registry/ModEntities.java
package com.example.examplemod.registry;
import com.example.examplemod.ExampleMod;import com.example.examplemod.entity.ForestSpiritEntity;import net.fabricmc.fabric.api.object.builder.v1.entity.FabricDefaultAttributeRegistry;import net.minecraft.core.Registry;import net.minecraft.core.registries.BuiltInRegistries;import net.minecraft.core.registries.Registries;import net.minecraft.resources.ResourceKey;import net.minecraft.world.entity.EntityType;import net.minecraft.world.entity.MobCategory;
public final class ModEntities { public static final EntityType<ForestSpiritEntity> FOREST_SPIRIT = Registry.register(BuiltInRegistries.ENTITY_TYPE, ExampleMod.id("forest_spirit"), EntityType.Builder.of(ForestSpiritEntity::new, MobCategory.CREATURE).sized(0.6F, 1.95F).build(ResourceKey.create(Registries.ENTITY_TYPE, ExampleMod.id("forest_spirit"))));
private ModEntities() { }
// Called from ExampleMod#onInitialize: loading this class registers the entity types above, then their attributes. public static void register() { FabricDefaultAttributeRegistry.register(FOREST_SPIRIT, ForestSpiritEntity.createAttributes()); }}In your main mod class, inside onInitialize:
ModEntities.register();In your client setup class, inside onInitializeClient:
EntityRendererRegistry.register(ModEntities.FOREST_SPIRIT, ForestSpiritRenderer::new);In src/main/resources/assets/examplemod/lang/en_us.json:
{ "entity.examplemod.forest_spirit": "Forest Spirit"}It’s registered like any other item, as ForestSpiritSpawnEggItem: see a new item.
NeoForge 1.21.2 to 1.21.3
Section titled “NeoForge 1.21.2 to 1.21.3”Uses:
EntityType.Builder.ofDeferredRegister.createRegistries.ENTITY_TYPEEntityAttributeCreationEvent.putEntityRenderersEvent.RegisterRenderers.registerEntityRendererSpawnEggItemHumanoidRenderState
Since 1.21.2 renderers draw a render state, not the entity, and an entity type is built with its registry key; DeferredSpawnEggItem is gone in 1.21.3, so the egg extends SpawnEggItem. It looks like a zombie until you give it a model and texture of its own.
src/main/java/com/example/examplemod/entity/ForestSpiritEntity.java
package com.example.examplemod.entity;
import net.minecraft.world.entity.EntityType;import net.minecraft.world.entity.PathfinderMob;import net.minecraft.world.entity.ai.attributes.AttributeSupplier;import net.minecraft.world.entity.ai.attributes.Attributes;import net.minecraft.world.entity.ai.goal.FloatGoal;import net.minecraft.world.entity.ai.goal.LookAtPlayerGoal;import net.minecraft.world.entity.ai.goal.PanicGoal;import net.minecraft.world.entity.ai.goal.RandomLookAroundGoal;import net.minecraft.world.entity.ai.goal.WaterAvoidingRandomStrollGoal;import net.minecraft.world.entity.player.Player;import net.minecraft.world.level.Level;
// Forest Spirit: a peaceful mob that wanders, looks at players and runs when hurt. Its goals are its behaviour; its attributes its stats.public class ForestSpiritEntity extends PathfinderMob { public ForestSpiritEntity(EntityType<? extends PathfinderMob> type, Level level) { super(type, level); }
// Registered for the entity type when the mod starts; every living entity needs its attributes. public static AttributeSupplier.Builder createAttributes() { return PathfinderMob.createMobAttributes().add(Attributes.MAX_HEALTH, 10.0F).add(Attributes.MOVEMENT_SPEED, 0.25F); }
@Override protected void registerGoals() { goalSelector.addGoal(0, new FloatGoal(this)); goalSelector.addGoal(1, new PanicGoal(this, 1.25D)); goalSelector.addGoal(2, new WaterAvoidingRandomStrollGoal(this, 1.0D)); goalSelector.addGoal(3, new LookAtPlayerGoal(this, Player.class, 6.0F)); goalSelector.addGoal(4, new RandomLookAroundGoal(this)); }}src/main/java/com/example/examplemod/client/ForestSpiritRenderer.java
package com.example.examplemod.client;
import com.example.examplemod.entity.ForestSpiritEntity;import net.minecraft.client.model.HumanoidModel;import net.minecraft.client.model.geom.ModelLayers;import net.minecraft.client.renderer.entity.EntityRendererProvider;import net.minecraft.client.renderer.entity.MobRenderer;import net.minecraft.client.renderer.entity.state.HumanoidRenderState;import net.minecraft.resources.ResourceLocation;
// How Forest Spirit is drawn: with the zombie's model and texture until you make its own (in Blockbench, say).// Since 1.21.2 a renderer draws a render state (a snapshot of the entity), not the entity itself.public class ForestSpiritRenderer extends MobRenderer<ForestSpiritEntity, HumanoidRenderState, HumanoidModel<HumanoidRenderState>> { private static final ResourceLocation TEXTURE = ResourceLocation.withDefaultNamespace("textures/entity/zombie/zombie.png");
public ForestSpiritRenderer(EntityRendererProvider.Context context) { super(context, new HumanoidModel<>(context.bakeLayer(ModelLayers.ZOMBIE)), 0.5F); }
@Override public HumanoidRenderState createRenderState() { return new HumanoidRenderState(); }
@Override public ResourceLocation getTextureLocation(HumanoidRenderState state) { return TEXTURE; }}src/main/resources/assets/examplemod/models/item/forest_spirit_spawn_egg.json
{ "parent": "minecraft:item/template_spawn_egg"}src/main/java/com/example/examplemod/item/ForestSpiritSpawnEggItem.java
package com.example.examplemod.item;
import com.example.examplemod.registry.ModEntities;import net.minecraft.world.item.SpawnEggItem;
// The spawn egg of Forest Spirit, in its two colours.public class ForestSpiritSpawnEggItem extends SpawnEggItem { public ForestSpiritSpawnEggItem(Properties properties) { super(ModEntities.FOREST_SPIRIT.get(), 6196282, 3099166, properties); }}src/main/java/com/example/examplemod/registry/ModEntities.java
package com.example.examplemod.registry;
import com.example.examplemod.ExampleMod;import com.example.examplemod.entity.ForestSpiritEntity;import net.minecraft.core.registries.Registries;import net.minecraft.resources.ResourceKey;import net.minecraft.resources.ResourceLocation;import net.minecraft.world.entity.EntityType;import net.minecraft.world.entity.MobCategory;import net.neoforged.neoforge.event.entity.EntityAttributeCreationEvent;import net.neoforged.neoforge.registries.DeferredHolder;import net.neoforged.neoforge.registries.DeferredRegister;
public final class ModEntities { // Registered on the mod event bus in ExampleMod's constructor. public static final DeferredRegister<EntityType<?>> ENTITY_TYPES = DeferredRegister.create(Registries.ENTITY_TYPE, ExampleMod.MOD_ID);
public static final DeferredHolder<EntityType<?>, EntityType<ForestSpiritEntity>> FOREST_SPIRIT = ENTITY_TYPES.register("forest_spirit", () -> EntityType.Builder.of(ForestSpiritEntity::new, MobCategory.CREATURE).sized(0.6F, 1.95F).build(ResourceKey.create(Registries.ENTITY_TYPE, ResourceLocation.fromNamespaceAndPath(ExampleMod.MOD_ID, "forest_spirit"))));
private ModEntities() { }
// Listens on the mod event bus: every living entity type needs its attributes. public static void registerAttributes(EntityAttributeCreationEvent event) { event.put(FOREST_SPIRIT.get(), ForestSpiritEntity.createAttributes().build()); }}src/main/java/com/example/examplemod/client/ForestSpiritEntityClient.java
package com.example.examplemod.client;
import com.example.examplemod.registry.ModEntities;import net.neoforged.bus.api.IEventBus;import net.neoforged.neoforge.client.event.EntityRenderersEvent;
// The client side of Forest Spirit: its renderer.public final class ForestSpiritEntityClient { private ForestSpiritEntityClient() { }
// Called from the mod's constructor, in the client only. public static void register(IEventBus modBus) { modBus.addListener(ForestSpiritEntityClient::registerRenderers); }
private static void registerRenderers(EntityRenderersEvent.RegisterRenderers event) { event.registerEntityRenderer(ModEntities.FOREST_SPIRIT.get(), ForestSpiritRenderer::new); }}In your main mod class, inside <init>:
ModEntities.ENTITY_TYPES.register(modEventBus);In your main mod class, inside <init>:
modEventBus.addListener(ModEntities::registerAttributes);In your main mod class, inside <init>:
if (FMLEnvironment.dist == Dist.CLIENT) { ForestSpiritEntityClient.register(modEventBus);}In src/main/resources/assets/examplemod/lang/en_us.json:
{ "entity.examplemod.forest_spirit": "Forest Spirit"}It’s registered like any other item, as ForestSpiritSpawnEggItem: see a new item.
NeoForge 1.21.4 to 1.21.8
Section titled “NeoForge 1.21.4 to 1.21.8”Uses:
EntityType.Builder.ofDeferredRegister.createRegistries.ENTITY_TYPEEntityAttributeCreationEvent.putEntityRenderersEvent.RegisterRenderers.registerEntityRendererSpawnEggItemHumanoidRenderState
Since 1.21.2 renderers draw a render state, and since 1.21.4 an item’s model and its spawn egg colours live in assets/<namespace>/items/<id>.json; DeferredSpawnEggItem is gone, use SpawnEggItem. It looks like a zombie until you give it a model and texture of its own.
src/main/java/com/example/examplemod/entity/ForestSpiritEntity.java
package com.example.examplemod.entity;
import net.minecraft.world.entity.EntityType;import net.minecraft.world.entity.PathfinderMob;import net.minecraft.world.entity.ai.attributes.AttributeSupplier;import net.minecraft.world.entity.ai.attributes.Attributes;import net.minecraft.world.entity.ai.goal.FloatGoal;import net.minecraft.world.entity.ai.goal.LookAtPlayerGoal;import net.minecraft.world.entity.ai.goal.PanicGoal;import net.minecraft.world.entity.ai.goal.RandomLookAroundGoal;import net.minecraft.world.entity.ai.goal.WaterAvoidingRandomStrollGoal;import net.minecraft.world.entity.player.Player;import net.minecraft.world.level.Level;
// Forest Spirit: a peaceful mob that wanders, looks at players and runs when hurt. Its goals are its behaviour; its attributes its stats.public class ForestSpiritEntity extends PathfinderMob { public ForestSpiritEntity(EntityType<? extends PathfinderMob> type, Level level) { super(type, level); }
// Registered for the entity type when the mod starts; every living entity needs its attributes. public static AttributeSupplier.Builder createAttributes() { return PathfinderMob.createMobAttributes().add(Attributes.MAX_HEALTH, 10.0F).add(Attributes.MOVEMENT_SPEED, 0.25F); }
@Override protected void registerGoals() { goalSelector.addGoal(0, new FloatGoal(this)); goalSelector.addGoal(1, new PanicGoal(this, 1.25D)); goalSelector.addGoal(2, new WaterAvoidingRandomStrollGoal(this, 1.0D)); goalSelector.addGoal(3, new LookAtPlayerGoal(this, Player.class, 6.0F)); goalSelector.addGoal(4, new RandomLookAroundGoal(this)); }}src/main/java/com/example/examplemod/client/ForestSpiritRenderer.java
package com.example.examplemod.client;
import com.example.examplemod.entity.ForestSpiritEntity;import net.minecraft.client.model.HumanoidModel;import net.minecraft.client.model.geom.ModelLayers;import net.minecraft.client.renderer.entity.EntityRendererProvider;import net.minecraft.client.renderer.entity.MobRenderer;import net.minecraft.client.renderer.entity.state.HumanoidRenderState;import net.minecraft.resources.ResourceLocation;
// How Forest Spirit is drawn: with the zombie's model and texture until you make its own (in Blockbench, say).// Since 1.21.2 a renderer draws a render state (a snapshot of the entity), not the entity itself.public class ForestSpiritRenderer extends MobRenderer<ForestSpiritEntity, HumanoidRenderState, HumanoidModel<HumanoidRenderState>> { private static final ResourceLocation TEXTURE = ResourceLocation.withDefaultNamespace("textures/entity/zombie/zombie.png");
public ForestSpiritRenderer(EntityRendererProvider.Context context) { super(context, new HumanoidModel<>(context.bakeLayer(ModelLayers.ZOMBIE)), 0.5F); }
@Override public HumanoidRenderState createRenderState() { return new HumanoidRenderState(); }
@Override public ResourceLocation getTextureLocation(HumanoidRenderState state) { return TEXTURE; }}src/main/resources/assets/examplemod/items/forest_spirit_spawn_egg.json
{ "model": { "type": "minecraft:model", "model": "minecraft:item/template_spawn_egg", "tints": [ { "type": "minecraft:constant", "value": 6196282 }, { "type": "minecraft:constant", "value": 3099166 } ] }}src/main/java/com/example/examplemod/item/ForestSpiritSpawnEggItem.java
package com.example.examplemod.item;
import com.example.examplemod.registry.ModEntities;import net.minecraft.world.item.Item;import net.minecraft.world.item.SpawnEggItem;
// The spawn egg of Forest Spirit. Its two colours are in its item definition, assets/examplemod/items/forest_spirit_spawn_egg.json.public class ForestSpiritSpawnEggItem extends SpawnEggItem { public ForestSpiritSpawnEggItem(Item.Properties properties) { super(ModEntities.FOREST_SPIRIT.get(), properties); }}src/main/java/com/example/examplemod/registry/ModEntities.java
package com.example.examplemod.registry;
import com.example.examplemod.ExampleMod;import com.example.examplemod.entity.ForestSpiritEntity;import net.minecraft.core.registries.Registries;import net.minecraft.resources.ResourceKey;import net.minecraft.resources.ResourceLocation;import net.minecraft.world.entity.EntityType;import net.minecraft.world.entity.MobCategory;import net.neoforged.neoforge.event.entity.EntityAttributeCreationEvent;import net.neoforged.neoforge.registries.DeferredHolder;import net.neoforged.neoforge.registries.DeferredRegister;
public final class ModEntities { // Registered on the mod event bus in ExampleMod's constructor. public static final DeferredRegister<EntityType<?>> ENTITY_TYPES = DeferredRegister.create(Registries.ENTITY_TYPE, ExampleMod.MOD_ID);
public static final DeferredHolder<EntityType<?>, EntityType<ForestSpiritEntity>> FOREST_SPIRIT = ENTITY_TYPES.register("forest_spirit", () -> EntityType.Builder.of(ForestSpiritEntity::new, MobCategory.CREATURE).sized(0.6F, 1.95F).build(ResourceKey.create(Registries.ENTITY_TYPE, ResourceLocation.fromNamespaceAndPath(ExampleMod.MOD_ID, "forest_spirit"))));
private ModEntities() { }
// Listens on the mod event bus: every living entity type needs its attributes. public static void registerAttributes(EntityAttributeCreationEvent event) { event.put(FOREST_SPIRIT.get(), ForestSpiritEntity.createAttributes().build()); }}src/main/java/com/example/examplemod/client/ForestSpiritEntityClient.java
package com.example.examplemod.client;
import com.example.examplemod.registry.ModEntities;import net.neoforged.bus.api.IEventBus;import net.neoforged.neoforge.client.event.EntityRenderersEvent;
// The client side of Forest Spirit: its renderer.public final class ForestSpiritEntityClient { private ForestSpiritEntityClient() { }
// Called from the mod's constructor, in the client only. public static void register(IEventBus modBus) { modBus.addListener(ForestSpiritEntityClient::registerRenderers); }
private static void registerRenderers(EntityRenderersEvent.RegisterRenderers event) { event.registerEntityRenderer(ModEntities.FOREST_SPIRIT.get(), ForestSpiritRenderer::new); }}In your main mod class, inside <init>:
ModEntities.ENTITY_TYPES.register(modEventBus);In your main mod class, inside <init>:
modEventBus.addListener(ModEntities::registerAttributes);In your main mod class, inside <init>:
if (FMLEnvironment.dist == Dist.CLIENT) { ForestSpiritEntityClient.register(modEventBus);}In src/main/resources/assets/examplemod/lang/en_us.json:
{ "entity.examplemod.forest_spirit": "Forest Spirit"}It’s registered like any other item, as ForestSpiritSpawnEggItem: see a new item.
NeoForge 1.21.9 to 1.21.10
Section titled “NeoForge 1.21.9 to 1.21.10”Uses:
EntityType.Builder.ofDeferredRegister.createRegistries.ENTITY_TYPEEntityAttributeCreationEvent.putEntityRenderersEvent.RegisterRenderers.registerEntityRendererSpawnEggItemHumanoidRenderState
Since 1.21.2 renderers draw a render state, and since 1.21.4 an item’s model and its spawn egg colours live in assets/<namespace>/items/<id>.json; DeferredSpawnEggItem is gone, use SpawnEggItem. It looks like a zombie until you give it a model and texture of its own.
src/main/java/com/example/examplemod/entity/ForestSpiritEntity.java
package com.example.examplemod.entity;
import net.minecraft.world.entity.EntityType;import net.minecraft.world.entity.PathfinderMob;import net.minecraft.world.entity.ai.attributes.AttributeSupplier;import net.minecraft.world.entity.ai.attributes.Attributes;import net.minecraft.world.entity.ai.goal.FloatGoal;import net.minecraft.world.entity.ai.goal.LookAtPlayerGoal;import net.minecraft.world.entity.ai.goal.PanicGoal;import net.minecraft.world.entity.ai.goal.RandomLookAroundGoal;import net.minecraft.world.entity.ai.goal.WaterAvoidingRandomStrollGoal;import net.minecraft.world.entity.player.Player;import net.minecraft.world.level.Level;
// Forest Spirit: a peaceful mob that wanders, looks at players and runs when hurt. Its goals are its behaviour; its attributes its stats.public class ForestSpiritEntity extends PathfinderMob { public ForestSpiritEntity(EntityType<? extends PathfinderMob> type, Level level) { super(type, level); }
// Registered for the entity type when the mod starts; every living entity needs its attributes. public static AttributeSupplier.Builder createAttributes() { return PathfinderMob.createMobAttributes().add(Attributes.MAX_HEALTH, 10.0F).add(Attributes.MOVEMENT_SPEED, 0.25F); }
@Override protected void registerGoals() { goalSelector.addGoal(0, new FloatGoal(this)); goalSelector.addGoal(1, new PanicGoal(this, 1.25D)); goalSelector.addGoal(2, new WaterAvoidingRandomStrollGoal(this, 1.0D)); goalSelector.addGoal(3, new LookAtPlayerGoal(this, Player.class, 6.0F)); goalSelector.addGoal(4, new RandomLookAroundGoal(this)); }}src/main/java/com/example/examplemod/client/ForestSpiritRenderer.java
package com.example.examplemod.client;
import com.example.examplemod.entity.ForestSpiritEntity;import net.minecraft.client.model.HumanoidModel;import net.minecraft.client.model.geom.ModelLayers;import net.minecraft.client.renderer.entity.EntityRendererProvider;import net.minecraft.client.renderer.entity.MobRenderer;import net.minecraft.client.renderer.entity.state.HumanoidRenderState;import net.minecraft.resources.ResourceLocation;
// How Forest Spirit is drawn: with the zombie's model and texture until you make its own (in Blockbench, say).// Since 1.21.2 a renderer draws a render state (a snapshot of the entity), not the entity itself.public class ForestSpiritRenderer extends MobRenderer<ForestSpiritEntity, HumanoidRenderState, HumanoidModel<HumanoidRenderState>> { private static final ResourceLocation TEXTURE = ResourceLocation.withDefaultNamespace("textures/entity/zombie/zombie.png");
public ForestSpiritRenderer(EntityRendererProvider.Context context) { super(context, new HumanoidModel<>(context.bakeLayer(ModelLayers.ZOMBIE)), 0.5F); }
@Override public HumanoidRenderState createRenderState() { return new HumanoidRenderState(); }
@Override public ResourceLocation getTextureLocation(HumanoidRenderState state) { return TEXTURE; }}src/main/resources/assets/examplemod/items/forest_spirit_spawn_egg.json
{ "model": { "type": "minecraft:model", "model": "minecraft:item/template_spawn_egg", "tints": [ { "type": "minecraft:constant", "value": 6196282 }, { "type": "minecraft:constant", "value": 3099166 } ] }}src/main/java/com/example/examplemod/item/ForestSpiritSpawnEggItem.java
package com.example.examplemod.item;
import com.example.examplemod.registry.ModEntities;import net.minecraft.world.item.Item;import net.minecraft.world.item.SpawnEggItem;
// The spawn egg of Forest Spirit: Item.Properties#spawnEgg gives it the mob since 1.21.6. Its two colours are in its item definition, assets/examplemod/items/forest_spirit_spawn_egg.json.public class ForestSpiritSpawnEggItem extends SpawnEggItem { public ForestSpiritSpawnEggItem(Item.Properties properties) { super(properties.spawnEgg(ModEntities.FOREST_SPIRIT.get())); }}src/main/java/com/example/examplemod/registry/ModEntities.java
package com.example.examplemod.registry;
import com.example.examplemod.ExampleMod;import com.example.examplemod.entity.ForestSpiritEntity;import net.minecraft.core.registries.Registries;import net.minecraft.resources.ResourceKey;import net.minecraft.resources.ResourceLocation;import net.minecraft.world.entity.EntityType;import net.minecraft.world.entity.MobCategory;import net.neoforged.neoforge.event.entity.EntityAttributeCreationEvent;import net.neoforged.neoforge.registries.DeferredHolder;import net.neoforged.neoforge.registries.DeferredRegister;
public final class ModEntities { // Registered on the mod event bus in ExampleMod's constructor. public static final DeferredRegister<EntityType<?>> ENTITY_TYPES = DeferredRegister.create(Registries.ENTITY_TYPE, ExampleMod.MOD_ID);
public static final DeferredHolder<EntityType<?>, EntityType<ForestSpiritEntity>> FOREST_SPIRIT = ENTITY_TYPES.register("forest_spirit", () -> EntityType.Builder.of(ForestSpiritEntity::new, MobCategory.CREATURE).sized(0.6F, 1.95F).build(ResourceKey.create(Registries.ENTITY_TYPE, ResourceLocation.fromNamespaceAndPath(ExampleMod.MOD_ID, "forest_spirit"))));
private ModEntities() { }
// Listens on the mod event bus: every living entity type needs its attributes. public static void registerAttributes(EntityAttributeCreationEvent event) { event.put(FOREST_SPIRIT.get(), ForestSpiritEntity.createAttributes().build()); }}src/main/java/com/example/examplemod/client/ForestSpiritEntityClient.java
package com.example.examplemod.client;
import com.example.examplemod.registry.ModEntities;import net.neoforged.bus.api.IEventBus;import net.neoforged.neoforge.client.event.EntityRenderersEvent;
// The client side of Forest Spirit: its renderer.public final class ForestSpiritEntityClient { private ForestSpiritEntityClient() { }
// Called from the mod's constructor, in the client only. public static void register(IEventBus modBus) { modBus.addListener(ForestSpiritEntityClient::registerRenderers); }
private static void registerRenderers(EntityRenderersEvent.RegisterRenderers event) { event.registerEntityRenderer(ModEntities.FOREST_SPIRIT.get(), ForestSpiritRenderer::new); }}In your main mod class, inside <init>:
ModEntities.ENTITY_TYPES.register(modEventBus);In your main mod class, inside <init>:
modEventBus.addListener(ModEntities::registerAttributes);In your main mod class, inside <init>:
if (FMLEnvironment.getDist() == Dist.CLIENT) { ForestSpiritEntityClient.register(modEventBus);}In src/main/resources/assets/examplemod/lang/en_us.json:
{ "entity.examplemod.forest_spirit": "Forest Spirit"}It’s registered like any other item, as ForestSpiritSpawnEggItem: see a new item.
Fabric 1.21.9 to 1.21.10
Section titled “Fabric 1.21.9 to 1.21.10”Uses:
EntityType.Builder.buildBuiltInRegistries.ENTITY_TYPEFabricDefaultAttributeRegistry.registerEntityRendererRegistry.registerMobRenderer.createRenderStateSpawnEggItem
Since 1.21.2 renderers draw a render state and since 1.21.4 an item’s model and tints live in assets/<namespace>/items/<id>.json; this code is compile-tested against 1.21.4. It looks like a zombie until you give it a model and texture of its own.
src/main/java/com/example/examplemod/entity/ForestSpiritEntity.java
package com.example.examplemod.entity;
import net.minecraft.world.entity.EntityType;import net.minecraft.world.entity.PathfinderMob;import net.minecraft.world.entity.ai.attributes.AttributeSupplier;import net.minecraft.world.entity.ai.attributes.Attributes;import net.minecraft.world.entity.ai.goal.FloatGoal;import net.minecraft.world.entity.ai.goal.LookAtPlayerGoal;import net.minecraft.world.entity.ai.goal.PanicGoal;import net.minecraft.world.entity.ai.goal.RandomLookAroundGoal;import net.minecraft.world.entity.ai.goal.WaterAvoidingRandomStrollGoal;import net.minecraft.world.entity.player.Player;import net.minecraft.world.level.Level;
// Forest Spirit: a peaceful mob that wanders, looks at players and runs when hurt. Its goals are its behaviour; its attributes its stats.public class ForestSpiritEntity extends PathfinderMob { public ForestSpiritEntity(EntityType<? extends PathfinderMob> type, Level level) { super(type, level); }
// Registered for the entity type when the mod starts; every living entity needs its attributes. public static AttributeSupplier.Builder createAttributes() { return PathfinderMob.createMobAttributes().add(Attributes.MAX_HEALTH, 10.0F).add(Attributes.MOVEMENT_SPEED, 0.25F); }
@Override protected void registerGoals() { goalSelector.addGoal(0, new FloatGoal(this)); goalSelector.addGoal(1, new PanicGoal(this, 1.25D)); goalSelector.addGoal(2, new WaterAvoidingRandomStrollGoal(this, 1.0D)); goalSelector.addGoal(3, new LookAtPlayerGoal(this, Player.class, 6.0F)); goalSelector.addGoal(4, new RandomLookAroundGoal(this)); }}src/main/java/com/example/examplemod/client/ForestSpiritRenderer.java
package com.example.examplemod.client;
import com.example.examplemod.entity.ForestSpiritEntity;import net.minecraft.client.model.HumanoidModel;import net.minecraft.client.model.geom.ModelLayers;import net.minecraft.client.renderer.entity.EntityRendererProvider;import net.minecraft.client.renderer.entity.MobRenderer;import net.minecraft.client.renderer.entity.state.HumanoidRenderState;import net.minecraft.resources.ResourceLocation;
// How Forest Spirit is drawn: with the zombie's model and texture until you make its own (in Blockbench, say).// Since 1.21.2 a renderer draws a render state (a snapshot of the entity), not the entity itself.public class ForestSpiritRenderer extends MobRenderer<ForestSpiritEntity, HumanoidRenderState, HumanoidModel<HumanoidRenderState>> { private static final ResourceLocation TEXTURE = ResourceLocation.withDefaultNamespace("textures/entity/zombie/zombie.png");
public ForestSpiritRenderer(EntityRendererProvider.Context context) { super(context, new HumanoidModel<>(context.bakeLayer(ModelLayers.ZOMBIE)), 0.5F); }
@Override public HumanoidRenderState createRenderState() { return new HumanoidRenderState(); }
@Override public ResourceLocation getTextureLocation(HumanoidRenderState state) { return TEXTURE; }}src/main/resources/assets/examplemod/items/forest_spirit_spawn_egg.json
{ "model": { "type": "minecraft:model", "model": "minecraft:item/template_spawn_egg", "tints": [ { "type": "minecraft:constant", "value": 6196282 }, { "type": "minecraft:constant", "value": 3099166 } ] }}src/main/java/com/example/examplemod/item/ForestSpiritSpawnEggItem.java
package com.example.examplemod.item;
import com.example.examplemod.registry.ModEntities;import net.minecraft.world.item.Item;import net.minecraft.world.item.SpawnEggItem;
// The spawn egg of Forest Spirit. Its two colours are in its item definition, assets/examplemod/items/forest_spirit_spawn_egg.json.public class ForestSpiritSpawnEggItem extends SpawnEggItem { public ForestSpiritSpawnEggItem(Item.Properties properties) { super(properties.spawnEgg(ModEntities.FOREST_SPIRIT)); }}src/main/java/com/example/examplemod/registry/ModEntities.java
package com.example.examplemod.registry;
import com.example.examplemod.ExampleMod;import com.example.examplemod.entity.ForestSpiritEntity;import net.fabricmc.fabric.api.object.builder.v1.entity.FabricDefaultAttributeRegistry;import net.minecraft.core.Registry;import net.minecraft.core.registries.BuiltInRegistries;import net.minecraft.core.registries.Registries;import net.minecraft.resources.ResourceKey;import net.minecraft.world.entity.EntityType;import net.minecraft.world.entity.MobCategory;
public final class ModEntities { public static final EntityType<ForestSpiritEntity> FOREST_SPIRIT = Registry.register(BuiltInRegistries.ENTITY_TYPE, ExampleMod.id("forest_spirit"), EntityType.Builder.of(ForestSpiritEntity::new, MobCategory.CREATURE).sized(0.6F, 1.95F).build(ResourceKey.create(Registries.ENTITY_TYPE, ExampleMod.id("forest_spirit"))));
private ModEntities() { }
// Called from ExampleMod#onInitialize: loading this class registers the entity types above, then their attributes. public static void register() { FabricDefaultAttributeRegistry.register(FOREST_SPIRIT, ForestSpiritEntity.createAttributes()); }}In your main mod class, inside onInitialize:
ModEntities.register();In your client setup class, inside onInitializeClient:
EntityRendererRegistry.register(ModEntities.FOREST_SPIRIT, ForestSpiritRenderer::new);In src/main/resources/assets/examplemod/lang/en_us.json:
{ "entity.examplemod.forest_spirit": "Forest Spirit"}It’s registered like any other item, as ForestSpiritSpawnEggItem: see a new item.
NeoForge 1.21.11 to 26.3
Section titled “NeoForge 1.21.11 to 26.3”Uses:
EntityType.Builder.ofDeferredRegister.createRegistries.ENTITY_TYPEEntityAttributeCreationEvent.putEntityRenderersEvent.RegisterRenderers.registerEntityRendererSpawnEggItemHumanoidRenderState
Since 1.21.2 renderers draw a render state, and since 1.21.4 an item’s model and its spawn egg colours live in assets/<namespace>/items/<id>.json; DeferredSpawnEggItem is gone, use SpawnEggItem. It looks like a zombie until you give it a model and texture of its own.
src/main/java/com/example/examplemod/entity/ForestSpiritEntity.java
package com.example.examplemod.entity;
import net.minecraft.world.entity.EntityType;import net.minecraft.world.entity.PathfinderMob;import net.minecraft.world.entity.ai.attributes.AttributeSupplier;import net.minecraft.world.entity.ai.attributes.Attributes;import net.minecraft.world.entity.ai.goal.FloatGoal;import net.minecraft.world.entity.ai.goal.LookAtPlayerGoal;import net.minecraft.world.entity.ai.goal.PanicGoal;import net.minecraft.world.entity.ai.goal.RandomLookAroundGoal;import net.minecraft.world.entity.ai.goal.WaterAvoidingRandomStrollGoal;import net.minecraft.world.entity.player.Player;import net.minecraft.world.level.Level;
// Forest Spirit: a peaceful mob that wanders, looks at players and runs when hurt. Its goals are its behaviour; its attributes its stats.public class ForestSpiritEntity extends PathfinderMob { public ForestSpiritEntity(EntityType<? extends PathfinderMob> type, Level level) { super(type, level); }
// Registered for the entity type when the mod starts; every living entity needs its attributes. public static AttributeSupplier.Builder createAttributes() { return PathfinderMob.createMobAttributes().add(Attributes.MAX_HEALTH, 10.0F).add(Attributes.MOVEMENT_SPEED, 0.25F); }
@Override protected void registerGoals() { goalSelector.addGoal(0, new FloatGoal(this)); goalSelector.addGoal(1, new PanicGoal(this, 1.25D)); goalSelector.addGoal(2, new WaterAvoidingRandomStrollGoal(this, 1.0D)); goalSelector.addGoal(3, new LookAtPlayerGoal(this, Player.class, 6.0F)); goalSelector.addGoal(4, new RandomLookAroundGoal(this)); }}src/main/java/com/example/examplemod/client/ForestSpiritRenderer.java
package com.example.examplemod.client;
import com.example.examplemod.entity.ForestSpiritEntity;import net.minecraft.client.model.HumanoidModel;import net.minecraft.client.model.geom.ModelLayers;import net.minecraft.client.renderer.entity.EntityRendererProvider;import net.minecraft.client.renderer.entity.MobRenderer;import net.minecraft.client.renderer.entity.state.HumanoidRenderState;import net.minecraft.resources.Identifier;
// How Forest Spirit is drawn: with the zombie's model and texture until you make its own (in Blockbench, say).// Since 1.21.2 a renderer draws a render state (a snapshot of the entity), not the entity itself.public class ForestSpiritRenderer extends MobRenderer<ForestSpiritEntity, HumanoidRenderState, HumanoidModel<HumanoidRenderState>> { private static final Identifier TEXTURE = Identifier.withDefaultNamespace("textures/entity/zombie/zombie.png");
public ForestSpiritRenderer(EntityRendererProvider.Context context) { super(context, new HumanoidModel<>(context.bakeLayer(ModelLayers.ZOMBIE)), 0.5F); }
@Override public HumanoidRenderState createRenderState() { return new HumanoidRenderState(); }
@Override public Identifier getTextureLocation(HumanoidRenderState state) { return TEXTURE; }}src/main/resources/assets/examplemod/items/forest_spirit_spawn_egg.json
{ "model": { "type": "minecraft:model", "model": "minecraft:item/template_spawn_egg", "tints": [ { "type": "minecraft:constant", "value": 6196282 }, { "type": "minecraft:constant", "value": 3099166 } ] }}src/main/java/com/example/examplemod/item/ForestSpiritSpawnEggItem.java
package com.example.examplemod.item;
import com.example.examplemod.registry.ModEntities;import net.minecraft.world.item.Item;import net.minecraft.world.item.SpawnEggItem;
// The spawn egg of Forest Spirit: Item.Properties#spawnEgg gives it the mob since 1.21.6. Its two colours are in its item definition, assets/examplemod/items/forest_spirit_spawn_egg.json.public class ForestSpiritSpawnEggItem extends SpawnEggItem { public ForestSpiritSpawnEggItem(Item.Properties properties) { super(properties.spawnEgg(ModEntities.FOREST_SPIRIT.get())); }}src/main/java/com/example/examplemod/registry/ModEntities.java
package com.example.examplemod.registry;
import com.example.examplemod.ExampleMod;import com.example.examplemod.entity.ForestSpiritEntity;import net.minecraft.core.registries.Registries;import net.minecraft.resources.ResourceKey;import net.minecraft.resources.Identifier;import net.minecraft.world.entity.EntityType;import net.minecraft.world.entity.MobCategory;import net.neoforged.neoforge.event.entity.EntityAttributeCreationEvent;import net.neoforged.neoforge.registries.DeferredHolder;import net.neoforged.neoforge.registries.DeferredRegister;
public final class ModEntities { // Registered on the mod event bus in ExampleMod's constructor. public static final DeferredRegister<EntityType<?>> ENTITY_TYPES = DeferredRegister.create(Registries.ENTITY_TYPE, ExampleMod.MOD_ID);
public static final DeferredHolder<EntityType<?>, EntityType<ForestSpiritEntity>> FOREST_SPIRIT = ENTITY_TYPES.register("forest_spirit", () -> EntityType.Builder.of(ForestSpiritEntity::new, MobCategory.CREATURE).sized(0.6F, 1.95F).build(ResourceKey.create(Registries.ENTITY_TYPE, Identifier.fromNamespaceAndPath(ExampleMod.MOD_ID, "forest_spirit"))));
private ModEntities() { }
// Listens on the mod event bus: every living entity type needs its attributes. public static void registerAttributes(EntityAttributeCreationEvent event) { event.put(FOREST_SPIRIT.get(), ForestSpiritEntity.createAttributes().build()); }}src/main/java/com/example/examplemod/client/ForestSpiritEntityClient.java
package com.example.examplemod.client;
import com.example.examplemod.registry.ModEntities;import net.neoforged.bus.api.IEventBus;import net.neoforged.neoforge.client.event.EntityRenderersEvent;
// The client side of Forest Spirit: its renderer.public final class ForestSpiritEntityClient { private ForestSpiritEntityClient() { }
// Called from the mod's constructor, in the client only. public static void register(IEventBus modBus) { modBus.addListener(ForestSpiritEntityClient::registerRenderers); }
private static void registerRenderers(EntityRenderersEvent.RegisterRenderers event) { event.registerEntityRenderer(ModEntities.FOREST_SPIRIT.get(), ForestSpiritRenderer::new); }}In your main mod class, inside <init>:
ModEntities.ENTITY_TYPES.register(modEventBus);In your main mod class, inside <init>:
modEventBus.addListener(ModEntities::registerAttributes);In your main mod class, inside <init>:
if (FMLEnvironment.getDist() == Dist.CLIENT) { ForestSpiritEntityClient.register(modEventBus);}In src/main/resources/assets/examplemod/lang/en_us.json:
{ "entity.examplemod.forest_spirit": "Forest Spirit"}It’s registered like any other item, as ForestSpiritSpawnEggItem: see a new item.
Fabric 1.21.11 to 26.3
Section titled “Fabric 1.21.11 to 26.3”Uses:
EntityType.Builder.buildBuiltInRegistries.ENTITY_TYPEFabricDefaultAttributeRegistry.registerEntityRendererRegistry.registerMobRenderer.createRenderStateSpawnEggItem
Since 1.21.2 renderers draw a render state and since 1.21.4 an item’s model and tints live in assets/<namespace>/items/<id>.json; this code is compile-tested against 1.21.4. It looks like a zombie until you give it a model and texture of its own.
src/main/java/com/example/examplemod/entity/ForestSpiritEntity.java
package com.example.examplemod.entity;
import net.minecraft.world.entity.EntityType;import net.minecraft.world.entity.PathfinderMob;import net.minecraft.world.entity.ai.attributes.AttributeSupplier;import net.minecraft.world.entity.ai.attributes.Attributes;import net.minecraft.world.entity.ai.goal.FloatGoal;import net.minecraft.world.entity.ai.goal.LookAtPlayerGoal;import net.minecraft.world.entity.ai.goal.PanicGoal;import net.minecraft.world.entity.ai.goal.RandomLookAroundGoal;import net.minecraft.world.entity.ai.goal.WaterAvoidingRandomStrollGoal;import net.minecraft.world.entity.player.Player;import net.minecraft.world.level.Level;
// Forest Spirit: a peaceful mob that wanders, looks at players and runs when hurt. Its goals are its behaviour; its attributes its stats.public class ForestSpiritEntity extends PathfinderMob { public ForestSpiritEntity(EntityType<? extends PathfinderMob> type, Level level) { super(type, level); }
// Registered for the entity type when the mod starts; every living entity needs its attributes. public static AttributeSupplier.Builder createAttributes() { return PathfinderMob.createMobAttributes().add(Attributes.MAX_HEALTH, 10.0F).add(Attributes.MOVEMENT_SPEED, 0.25F); }
@Override protected void registerGoals() { goalSelector.addGoal(0, new FloatGoal(this)); goalSelector.addGoal(1, new PanicGoal(this, 1.25D)); goalSelector.addGoal(2, new WaterAvoidingRandomStrollGoal(this, 1.0D)); goalSelector.addGoal(3, new LookAtPlayerGoal(this, Player.class, 6.0F)); goalSelector.addGoal(4, new RandomLookAroundGoal(this)); }}src/main/java/com/example/examplemod/client/ForestSpiritRenderer.java
package com.example.examplemod.client;
import com.example.examplemod.entity.ForestSpiritEntity;import net.minecraft.client.model.HumanoidModel;import net.minecraft.client.model.geom.ModelLayers;import net.minecraft.client.renderer.entity.EntityRendererProvider;import net.minecraft.client.renderer.entity.MobRenderer;import net.minecraft.client.renderer.entity.state.HumanoidRenderState;import net.minecraft.resources.Identifier;
// How Forest Spirit is drawn: with the zombie's model and texture until you make its own (in Blockbench, say).// Since 1.21.2 a renderer draws a render state (a snapshot of the entity), not the entity itself.public class ForestSpiritRenderer extends MobRenderer<ForestSpiritEntity, HumanoidRenderState, HumanoidModel<HumanoidRenderState>> { private static final Identifier TEXTURE = Identifier.withDefaultNamespace("textures/entity/zombie/zombie.png");
public ForestSpiritRenderer(EntityRendererProvider.Context context) { super(context, new HumanoidModel<>(context.bakeLayer(ModelLayers.ZOMBIE)), 0.5F); }
@Override public HumanoidRenderState createRenderState() { return new HumanoidRenderState(); }
@Override public Identifier getTextureLocation(HumanoidRenderState state) { return TEXTURE; }}src/main/resources/assets/examplemod/items/forest_spirit_spawn_egg.json
{ "model": { "type": "minecraft:model", "model": "minecraft:item/template_spawn_egg", "tints": [ { "type": "minecraft:constant", "value": 6196282 }, { "type": "minecraft:constant", "value": 3099166 } ] }}src/main/java/com/example/examplemod/item/ForestSpiritSpawnEggItem.java
package com.example.examplemod.item;
import com.example.examplemod.registry.ModEntities;import net.minecraft.world.item.Item;import net.minecraft.world.item.SpawnEggItem;
// The spawn egg of Forest Spirit. Its two colours are in its item definition, assets/examplemod/items/forest_spirit_spawn_egg.json.public class ForestSpiritSpawnEggItem extends SpawnEggItem { public ForestSpiritSpawnEggItem(Item.Properties properties) { super(properties.spawnEgg(ModEntities.FOREST_SPIRIT)); }}src/main/java/com/example/examplemod/registry/ModEntities.java
package com.example.examplemod.registry;
import com.example.examplemod.ExampleMod;import com.example.examplemod.entity.ForestSpiritEntity;import net.fabricmc.fabric.api.object.builder.v1.entity.FabricDefaultAttributeRegistry;import net.minecraft.core.Registry;import net.minecraft.core.registries.BuiltInRegistries;import net.minecraft.core.registries.Registries;import net.minecraft.resources.ResourceKey;import net.minecraft.world.entity.EntityType;import net.minecraft.world.entity.MobCategory;
public final class ModEntities { public static final EntityType<ForestSpiritEntity> FOREST_SPIRIT = Registry.register(BuiltInRegistries.ENTITY_TYPE, ExampleMod.id("forest_spirit"), EntityType.Builder.of(ForestSpiritEntity::new, MobCategory.CREATURE).sized(0.6F, 1.95F).build(ResourceKey.create(Registries.ENTITY_TYPE, ExampleMod.id("forest_spirit"))));
private ModEntities() { }
// Called from ExampleMod#onInitialize: loading this class registers the entity types above, then their attributes. public static void register() { FabricDefaultAttributeRegistry.register(FOREST_SPIRIT, ForestSpiritEntity.createAttributes()); }}In your main mod class, inside onInitialize:
ModEntities.register();In your client setup class, inside onInitializeClient:
EntityRendererRegistry.register(ModEntities.FOREST_SPIRIT, ForestSpiritRenderer::new);In src/main/resources/assets/examplemod/lang/en_us.json:
{ "entity.examplemod.forest_spirit": "Forest Spirit"}It’s registered like any other item, as ForestSpiritSpawnEggItem: see a new item.
Fabric 1.17 to 1.19.2
Section titled “Fabric 1.17 to 1.19.2”Uses:
EntityType.Builder.ofRegistry.ENTITY_TYPEFabricDefaultAttributeRegistry.registerEntityRendererRegistry.registerMobRendererSpawnEggItem
src/main/java/com/example/examplemod/entity/ForestSpiritEntity.java
package com.example.examplemod.entity;
import net.minecraft.world.entity.EntityType;import net.minecraft.world.entity.PathfinderMob;import net.minecraft.world.entity.ai.attributes.AttributeSupplier;import net.minecraft.world.entity.ai.attributes.Attributes;import net.minecraft.world.entity.ai.goal.FloatGoal;import net.minecraft.world.entity.ai.goal.LookAtPlayerGoal;import net.minecraft.world.entity.ai.goal.PanicGoal;import net.minecraft.world.entity.ai.goal.RandomLookAroundGoal;import net.minecraft.world.entity.ai.goal.WaterAvoidingRandomStrollGoal;import net.minecraft.world.entity.player.Player;import net.minecraft.world.level.Level;
// Forest Spirit: a peaceful mob that wanders, looks at players and runs when hurt. Its goals are its behaviour; its attributes its stats.public class ForestSpiritEntity extends PathfinderMob { public ForestSpiritEntity(EntityType<? extends PathfinderMob> type, Level level) { super(type, level); }
// Registered for the entity type when the mod starts; every living entity needs its attributes. public static AttributeSupplier.Builder createAttributes() { return PathfinderMob.createMobAttributes().add(Attributes.MAX_HEALTH, 10.0F).add(Attributes.MOVEMENT_SPEED, 0.25F); }
@Override protected void registerGoals() { goalSelector.addGoal(0, new FloatGoal(this)); goalSelector.addGoal(1, new PanicGoal(this, 1.25D)); goalSelector.addGoal(2, new WaterAvoidingRandomStrollGoal(this, 1.0D)); goalSelector.addGoal(3, new LookAtPlayerGoal(this, Player.class, 6.0F)); goalSelector.addGoal(4, new RandomLookAroundGoal(this)); }}src/main/java/com/example/examplemod/client/ForestSpiritRenderer.java
package com.example.examplemod.client;
import com.example.examplemod.entity.ForestSpiritEntity;import net.minecraft.client.model.HumanoidModel;import net.minecraft.client.model.geom.ModelLayers;import net.minecraft.client.renderer.entity.EntityRendererProvider;import net.minecraft.client.renderer.entity.MobRenderer;import net.minecraft.resources.ResourceLocation;
// How Forest Spirit is drawn: with the zombie's model and texture until you make its own (in Blockbench, say).public class ForestSpiritRenderer extends MobRenderer<ForestSpiritEntity, HumanoidModel<ForestSpiritEntity>> { private static final ResourceLocation TEXTURE = new ResourceLocation("textures/entity/zombie/zombie.png");
public ForestSpiritRenderer(EntityRendererProvider.Context context) { super(context, new HumanoidModel<>(context.bakeLayer(ModelLayers.ZOMBIE)), 0.5F); }
@Override public ResourceLocation getTextureLocation(ForestSpiritEntity entity) { return TEXTURE; }}src/main/resources/assets/examplemod/models/item/forest_spirit_spawn_egg.json
{ "parent": "minecraft:item/template_spawn_egg"}src/main/java/com/example/examplemod/item/ForestSpiritSpawnEggItem.java
package com.example.examplemod.item;
import com.example.examplemod.registry.ModEntities;import net.minecraft.world.item.Item;import net.minecraft.world.item.SpawnEggItem;
// The spawn egg of Forest Spirit, in its two colours.public class ForestSpiritSpawnEggItem extends SpawnEggItem { public ForestSpiritSpawnEggItem(Item.Properties properties) { super(ModEntities.FOREST_SPIRIT, 6196282, 3099166, properties); }}src/main/java/com/example/examplemod/registry/ModEntities.java
package com.example.examplemod.registry;
import com.example.examplemod.ExampleMod;import com.example.examplemod.entity.ForestSpiritEntity;import net.fabricmc.fabric.api.object.builder.v1.entity.FabricDefaultAttributeRegistry;import net.minecraft.core.Registry;import net.minecraft.world.entity.EntityType;import net.minecraft.world.entity.MobCategory;
public final class ModEntities { public static final EntityType<ForestSpiritEntity> FOREST_SPIRIT = Registry.register(Registry.ENTITY_TYPE, ExampleMod.id("forest_spirit"), EntityType.Builder.of(ForestSpiritEntity::new, MobCategory.CREATURE).sized(0.6F, 1.95F).build("forest_spirit"));
private ModEntities() { }
// Called from ExampleMod#onInitialize: loading this class registers the entity types above, then their attributes. public static void register() { FabricDefaultAttributeRegistry.register(FOREST_SPIRIT, ForestSpiritEntity.createAttributes()); }}In your main mod class, inside onInitialize:
ModEntities.register();In your client setup class, inside onInitializeClient:
EntityRendererRegistry.register(ModEntities.FOREST_SPIRIT, ForestSpiritRenderer::new);In src/main/resources/assets/examplemod/lang/en_us.json:
{ "entity.examplemod.forest_spirit": "Forest Spirit"}It’s registered like any other item, as ForestSpiritSpawnEggItem: see a new item.
Forge 1.16.5
Section titled “Forge 1.16.5”Uses:
EntityType.Builder.ofDeferredRegister.createForgeRegistries.ENTITIESEntityAttributeCreationEvent.putRenderingRegistry.registerEntityRenderingHandlerForgeSpawnEggItem
It looks like a zombie until you give it a model and texture of its own.
src/main/java/com/example/examplemod/entity/ForestSpiritEntity.java
package com.example.examplemod.entity;
import net.minecraft.entity.CreatureEntity;import net.minecraft.entity.EntityType;import net.minecraft.entity.ai.attributes.AttributeModifierMap;import net.minecraft.entity.ai.attributes.Attributes;import net.minecraft.entity.ai.goal.LookAtGoal;import net.minecraft.entity.ai.goal.LookRandomlyGoal;import net.minecraft.entity.ai.goal.PanicGoal;import net.minecraft.entity.ai.goal.SwimGoal;import net.minecraft.entity.ai.goal.WaterAvoidingRandomWalkingGoal;import net.minecraft.entity.player.PlayerEntity;import net.minecraft.world.World;
// Forest Spirit: a peaceful mob that wanders, looks at players and runs when hurt. Its goals are its behaviour; its attributes its stats.public class ForestSpiritEntity extends CreatureEntity { public ForestSpiritEntity(EntityType<? extends CreatureEntity> type, World world) { super(type, world); }
// Registered for the entity type when the mod starts; every living entity needs its attributes. public static AttributeModifierMap.MutableAttribute createAttributes() { return CreatureEntity.createMobAttributes().add(Attributes.MAX_HEALTH, 10.0F).add(Attributes.MOVEMENT_SPEED, 0.25F); }
@Override protected void registerGoals() { goalSelector.addGoal(0, new SwimGoal(this)); goalSelector.addGoal(1, new PanicGoal(this, 1.25D)); goalSelector.addGoal(2, new WaterAvoidingRandomWalkingGoal(this, 1.0D)); goalSelector.addGoal(3, new LookAtGoal(this, PlayerEntity.class, 6.0F)); goalSelector.addGoal(4, new LookRandomlyGoal(this)); }}src/main/java/com/example/examplemod/client/ForestSpiritRenderer.java
package com.example.examplemod.client;
import com.example.examplemod.entity.ForestSpiritEntity;import net.minecraft.client.renderer.entity.EntityRendererManager;import net.minecraft.client.renderer.entity.MobRenderer;import net.minecraft.client.renderer.entity.model.BipedModel;import net.minecraft.util.ResourceLocation;
// How Forest Spirit is drawn: with the zombie's texture on a biped model until you make its own (in Blockbench, say).public class ForestSpiritRenderer extends MobRenderer<ForestSpiritEntity, BipedModel<ForestSpiritEntity>> { private static final ResourceLocation TEXTURE = new ResourceLocation("textures/entity/zombie/zombie.png");
public ForestSpiritRenderer(EntityRendererManager manager) { super(manager, new BipedModel<>(0.0F), 0.5F); }
@Override public ResourceLocation getTextureLocation(ForestSpiritEntity entity) { return TEXTURE; }}src/main/resources/assets/examplemod/models/item/forest_spirit_spawn_egg.json
{ "parent": "minecraft:item/template_spawn_egg"}src/main/java/com/example/examplemod/item/ForestSpiritSpawnEggItem.java
package com.example.examplemod.item;
import com.example.examplemod.registry.ModEntities;import net.minecraft.item.Item;import net.minecraftforge.common.ForgeSpawnEggItem;
// The spawn egg of Forest Spirit, in its two colours.public class ForestSpiritSpawnEggItem extends ForgeSpawnEggItem { public ForestSpiritSpawnEggItem(Item.Properties properties) { super(ModEntities.FOREST_SPIRIT, 6196282, 3099166, properties); }}src/main/java/com/example/examplemod/registry/ModEntities.java
package com.example.examplemod.registry;
import com.example.examplemod.ExampleMod;import com.example.examplemod.entity.ForestSpiritEntity;import net.minecraft.entity.EntityClassification;import net.minecraft.entity.EntityType;import net.minecraftforge.event.entity.EntityAttributeCreationEvent;import net.minecraftforge.eventbus.api.IEventBus;import net.minecraftforge.fml.RegistryObject;import net.minecraftforge.fml.javafmlmod.FMLJavaModLoadingContext;import net.minecraftforge.registries.DeferredRegister;import net.minecraftforge.registries.ForgeRegistries;
public final class ModEntities { public static final DeferredRegister<EntityType<?>> ENTITY_TYPES = DeferredRegister.create(ForgeRegistries.ENTITIES, ExampleMod.MOD_ID);
public static final RegistryObject<EntityType<ForestSpiritEntity>> FOREST_SPIRIT = ENTITY_TYPES.register("forest_spirit", () -> EntityType.Builder.of(ForestSpiritEntity::new, EntityClassification.CREATURE).sized(0.6F, 1.95F).build("forest_spirit"));
private ModEntities() { }
// Called from the ExampleMod constructor: the entity types and their attributes join the mod event bus. public static void register() { IEventBus modBus = FMLJavaModLoadingContext.get().getModEventBus(); ENTITY_TYPES.register(modBus); modBus.addListener(ModEntities::registerAttributes); }
// Every living entity type needs its attributes. private static void registerAttributes(EntityAttributeCreationEvent event) { event.put(FOREST_SPIRIT.get(), ForestSpiritEntity.createAttributes().build()); }}src/main/java/com/example/examplemod/client/ForestSpiritEntityClient.java
package com.example.examplemod.client;
import com.example.examplemod.registry.ModEntities;import net.minecraftforge.eventbus.api.IEventBus;import net.minecraftforge.fml.client.registry.RenderingRegistry;import net.minecraftforge.fml.event.lifecycle.FMLClientSetupEvent;import net.minecraftforge.fml.javafmlmod.FMLJavaModLoadingContext;
// The client side of Forest Spirit: its renderer (Forge 1.16.5: RenderingRegistry, in FMLClientSetupEvent).public final class ForestSpiritEntityClient { private ForestSpiritEntityClient() { }
// Called from the mod's constructor, in the client only. public static void register() { IEventBus modBus = FMLJavaModLoadingContext.get().getModEventBus(); modBus.addListener(ForestSpiritEntityClient::onClientSetup); }
private static void onClientSetup(FMLClientSetupEvent event) { RenderingRegistry.registerEntityRenderingHandler(ModEntities.FOREST_SPIRIT.get(), ForestSpiritRenderer::new); }}In your main mod class, inside <init>:
ModEntities.register();In your main mod class, inside <init>:
if (FMLEnvironment.dist == Dist.CLIENT) { ForestSpiritEntityClient.register();}In src/main/resources/assets/examplemod/lang/en_us.json:
{ "entity.examplemod.forest_spirit": "Forest Spirit"}It’s registered like any other item, as ForestSpiritSpawnEggItem: see a new item.
Fabric 1.16.5
Section titled “Fabric 1.16.5”Uses:
EntityType.Builder.ofRegistry.ENTITY_TYPEFabricDefaultAttributeRegistry.registerEntityRendererRegistry.registerMobRendererSpawnEggItem
src/main/java/com/example/examplemod/entity/ForestSpiritEntity.java
package com.example.examplemod.entity;
import net.minecraft.world.entity.EntityType;import net.minecraft.world.entity.PathfinderMob;import net.minecraft.world.entity.ai.attributes.AttributeSupplier;import net.minecraft.world.entity.ai.attributes.Attributes;import net.minecraft.world.entity.ai.goal.FloatGoal;import net.minecraft.world.entity.ai.goal.LookAtPlayerGoal;import net.minecraft.world.entity.ai.goal.PanicGoal;import net.minecraft.world.entity.ai.goal.RandomLookAroundGoal;import net.minecraft.world.entity.ai.goal.WaterAvoidingRandomStrollGoal;import net.minecraft.world.entity.player.Player;import net.minecraft.world.level.Level;
// Forest Spirit: a peaceful mob that wanders, looks at players and runs when hurt. Its goals are its behaviour; its attributes its stats.public class ForestSpiritEntity extends PathfinderMob { public ForestSpiritEntity(EntityType<? extends PathfinderMob> type, Level level) { super(type, level); }
// Registered for the entity type when the mod starts; every living entity needs its attributes. public static AttributeSupplier.Builder createAttributes() { return PathfinderMob.createMobAttributes().add(Attributes.MAX_HEALTH, 10.0F).add(Attributes.MOVEMENT_SPEED, 0.25F); }
@Override protected void registerGoals() { goalSelector.addGoal(0, new FloatGoal(this)); goalSelector.addGoal(1, new PanicGoal(this, 1.25D)); goalSelector.addGoal(2, new WaterAvoidingRandomStrollGoal(this, 1.0D)); goalSelector.addGoal(3, new LookAtPlayerGoal(this, Player.class, 6.0F)); goalSelector.addGoal(4, new RandomLookAroundGoal(this)); }}src/main/java/com/example/examplemod/client/ForestSpiritRenderer.java
package com.example.examplemod.client;
import com.example.examplemod.entity.ForestSpiritEntity;import net.minecraft.client.model.HumanoidModel;import net.minecraft.client.renderer.entity.EntityRenderDispatcher;import net.minecraft.client.renderer.entity.MobRenderer;import net.minecraft.resources.ResourceLocation;
// How Forest Spirit is drawn: with the zombie's model and texture until you make its own (in Blockbench, say).public class ForestSpiritRenderer extends MobRenderer<ForestSpiritEntity, HumanoidModel<ForestSpiritEntity>> { private static final ResourceLocation TEXTURE = new ResourceLocation("textures/entity/zombie/zombie.png");
public ForestSpiritRenderer(EntityRenderDispatcher dispatcher) { super(dispatcher, new HumanoidModel<>(0.0F), 0.5F); }
@Override public ResourceLocation getTextureLocation(ForestSpiritEntity entity) { return TEXTURE; }}src/main/resources/assets/examplemod/models/item/forest_spirit_spawn_egg.json
{ "parent": "minecraft:item/template_spawn_egg"}src/main/java/com/example/examplemod/item/ForestSpiritSpawnEggItem.java
package com.example.examplemod.item;
import com.example.examplemod.registry.ModEntities;import net.minecraft.world.item.Item;import net.minecraft.world.item.SpawnEggItem;
// The spawn egg of Forest Spirit, in its two colours.public class ForestSpiritSpawnEggItem extends SpawnEggItem { public ForestSpiritSpawnEggItem(Item.Properties properties) { super(ModEntities.FOREST_SPIRIT, 6196282, 3099166, properties); }}src/main/java/com/example/examplemod/registry/ModEntities.java
package com.example.examplemod.registry;
import com.example.examplemod.ExampleMod;import com.example.examplemod.entity.ForestSpiritEntity;import net.fabricmc.fabric.api.object.builder.v1.entity.FabricDefaultAttributeRegistry;import net.minecraft.core.Registry;import net.minecraft.world.entity.EntityType;import net.minecraft.world.entity.MobCategory;
public final class ModEntities { public static final EntityType<ForestSpiritEntity> FOREST_SPIRIT = Registry.register(Registry.ENTITY_TYPE, ExampleMod.id("forest_spirit"), EntityType.Builder.of(ForestSpiritEntity::new, MobCategory.CREATURE).sized(0.6F, 1.95F).build("forest_spirit"));
private ModEntities() { }
// Called from ExampleMod#onInitialize: loading this class registers the entity types above, then their attributes. public static void register() { FabricDefaultAttributeRegistry.register(FOREST_SPIRIT, ForestSpiritEntity.createAttributes()); }}In your main mod class, inside onInitialize:
ModEntities.register();In your client setup class, inside onInitializeClient:
EntityRendererRegistry.INSTANCE.register(ModEntities.FOREST_SPIRIT, (dispatcher, context) -> new ForestSpiritRenderer(dispatcher));In src/main/resources/assets/examplemod/lang/en_us.json:
{ "entity.examplemod.forest_spirit": "Forest Spirit"}It’s registered like any other item, as ForestSpiritSpawnEggItem: see a new item.
Sources
Section titled “Sources”- https://docs.fabricmc.net/1.21.11/develop/entities/first-entity
- https://maven.fabricmc.net/docs/fabric-api-0.116.17+1.21.1/net/fabricmc/fabric/api/object/builder/v1/entity/FabricDefaultAttributeRegistry.html
- https://maven.fabricmc.net/docs/fabric-api-0.116.17+1.21.1/net/fabricmc/fabric/api/client/rendering/v1/EntityRendererRegistry.html
- https://docs.neoforged.net/docs/1.21.4/entities/
- https://docs.neoforged.net/docs/1.21.4/entities/attributes
- https://docs.neoforged.net/docs/1.21.4/entities/renderer
Related how-tos
Section titled “Related how-tos”- A block that does something when clicked
- A block that gives off particles
- An item that does something when used
- An item that gives a potion effect
- Give a mob a new AI goal
In Linen
Section titled “In Linen”Linen’s How-tos library has this page too, with the example for your own mod’s loader and Minecraft version. Hover a name in your code and Linen points to the how-tos that explain it. Both are free. Download Linen.