How to make an item that spawns a mob where you click (Minecraft mod)
To spawn a mob, call entityType.spawn(serverLevel, pos, spawnReason) with MobSpawnType.MOB_SUMMONED before 1.21.2 and EntitySpawnReason.MOB_SUMMONED from 1.21.2. It builds the entity, places it and adds it to the world in one call, and it only works on the server. For the position, use useOn and take the block that was clicked plus one block up.
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; Minecraft 1.12.2 on Forge. Each example was compiled against its loader and version before it shipped in Linen.
- Override useOn and work only on the server.
- Take context.getClickedPos().above() as the spot.
- Call EntityType.X.spawn(serverLevel, pos, spawnReason) with the MOB_SUMMONED reason.
- Shrink the stack if the item is one-use.
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 |
|---|---|---|
| mob | ZOMBIE |
The mob it spawns. |
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 and NeoForge 1.20.4 to 1.21.1, NeoForge 1.20.1 to 1.20.2, Forge 1.19.3 to 1.20.1
Section titled “Fabric and NeoForge 1.20.4 to 1.21.1, NeoForge 1.20.1 to 1.20.2, Forge 1.19.3 to 1.20.1”Uses:
EntityType.spawnMobSpawnType.MOB_SUMMONEDUseOnContext.getClickedPos
src/main/java/com/example/examplemod/item/SummonStoneItem.java
package com.example.examplemod.item;
import net.minecraft.server.level.ServerLevel;import net.minecraft.core.BlockPos;import net.minecraft.network.chat.Component;import net.minecraft.world.InteractionHand;import net.minecraft.world.InteractionResult;import net.minecraft.world.InteractionResultHolder;import net.minecraft.world.entity.EntityType;import net.minecraft.world.entity.MobSpawnType;import net.minecraft.world.entity.player.Player;import net.minecraft.world.item.Item;import net.minecraft.world.item.ItemStack;import net.minecraft.world.item.context.UseOnContext;import net.minecraft.world.level.Level;import net.minecraft.world.level.block.Blocks;import net.minecraft.world.phys.BlockHitResult;import net.minecraft.world.phys.HitResult;import net.minecraft.world.phys.Vec3;
// Summon Stone: right-click a block to spawn a ZOMBIE on top of it.public class SummonStoneItem extends Item { public SummonStoneItem(Item.Properties properties) { super(properties); }
@Override public InteractionResult useOn(UseOnContext context) { if (context.getLevel() instanceof ServerLevel serverLevel) { EntityType.ZOMBIE.spawn(serverLevel, context.getClickedPos().above(), MobSpawnType.MOB_SUMMONED); context.getItemInHand().shrink(1); } return InteractionResult.sidedSuccess(context.getLevel().isClientSide()); }}It’s registered like any other item, as SummonStoneItem: see a new item.
Fabric and NeoForge 1.21.2 to 26.1.2
Section titled “Fabric and NeoForge 1.21.2 to 26.1.2”Uses:
EntityType.spawnEntitySpawnReason.MOB_SUMMONEDUseOnContext.getClickedPos
src/main/java/com/example/examplemod/item/SummonStoneItem.java
package com.example.examplemod.item;
import net.minecraft.server.level.ServerLevel;import net.minecraft.core.BlockPos;import net.minecraft.network.chat.Component;import net.minecraft.world.InteractionHand;import net.minecraft.world.InteractionResult;import net.minecraft.world.InteractionResult;import net.minecraft.world.entity.EntityType;import net.minecraft.world.entity.EntitySpawnReason;import net.minecraft.world.entity.player.Player;import net.minecraft.world.item.Item;import net.minecraft.world.item.ItemStack;import net.minecraft.world.item.context.UseOnContext;import net.minecraft.world.level.Level;import net.minecraft.world.level.block.Blocks;import net.minecraft.world.phys.BlockHitResult;import net.minecraft.world.phys.HitResult;import net.minecraft.world.phys.Vec3;
// Summon Stone: right-click a block to spawn a ZOMBIE on top of it.public class SummonStoneItem extends Item { public SummonStoneItem(Item.Properties properties) { super(properties); }
@Override public InteractionResult useOn(UseOnContext context) { if (context.getLevel() instanceof ServerLevel serverLevel) { EntityType.ZOMBIE.spawn(serverLevel, context.getClickedPos().above(), EntitySpawnReason.MOB_SUMMONED); context.getItemInHand().shrink(1); } return InteractionResult.SUCCESS; }}It’s registered like any other item, as SummonStoneItem: see a new item.
Fabric and NeoForge 26.2 to 26.3
Section titled “Fabric and NeoForge 26.2 to 26.3”Uses:
EntityTypes.ZOMBIEEntityType.spawnEntitySpawnReason.MOB_SUMMONEDUseOnContext.getClickedPos
Since 26.2 the entity type constants (ZOMBIE, COW…) are in EntityTypes, not EntityType.
src/main/java/com/example/examplemod/item/SummonStoneItem.java
package com.example.examplemod.item;
import net.minecraft.server.level.ServerLevel;import net.minecraft.world.InteractionResult;import net.minecraft.world.entity.EntitySpawnReason;import net.minecraft.world.entity.EntityTypes;import net.minecraft.world.item.Item;import net.minecraft.world.item.context.UseOnContext;
// Summon Stone: right-click a block to spawn a ZOMBIE on top of it.public class SummonStoneItem extends Item { public SummonStoneItem(Item.Properties properties) { super(properties); }
@Override public InteractionResult useOn(UseOnContext context) { if (context.getLevel() instanceof ServerLevel serverLevel) { EntityTypes.ZOMBIE.spawn(serverLevel, context.getClickedPos().above(), EntitySpawnReason.MOB_SUMMONED); context.getItemInHand().shrink(1); } return InteractionResult.SUCCESS; }}It’s registered like any other item, as SummonStoneItem: see a new item.
Forge 1.17.1 to 1.19.2
Section titled “Forge 1.17.1 to 1.19.2”Uses:
EntityType.spawnMobSpawnType.MOB_SUMMONEDUseOnContext.getClickedPos
src/main/java/com/example/examplemod/item/SummonStoneItem.java
package com.example.examplemod.item;
import net.minecraft.server.level.ServerLevel;import net.minecraft.core.BlockPos;import net.minecraft.network.chat.Component;import net.minecraft.world.InteractionHand;import net.minecraft.world.InteractionResult;import net.minecraft.world.InteractionResultHolder;import net.minecraft.world.entity.EntityType;import net.minecraft.world.entity.Mob;import net.minecraft.world.entity.player.Player;import net.minecraft.world.item.Item;import net.minecraft.world.item.ItemStack;import net.minecraft.world.item.context.UseOnContext;import net.minecraft.world.level.Level;import net.minecraft.world.level.block.Blocks;import net.minecraft.world.phys.BlockHitResult;import net.minecraft.world.phys.HitResult;import net.minecraft.world.phys.Vec3;
// Summon Stone: right-click a block to spawn a ZOMBIE on top of it.public class SummonStoneItem extends Item { public SummonStoneItem(Item.Properties properties) { super(properties); }
@Override public InteractionResult useOn(UseOnContext context) { if (context.getLevel() instanceof ServerLevel serverLevel) { Mob mob = EntityType.ZOMBIE.create(serverLevel); mob.moveTo(Vec3.atBottomCenterOf(context.getClickedPos().above())); serverLevel.addFreshEntity(mob); context.getItemInHand().shrink(1); } return InteractionResult.sidedSuccess(context.getLevel().isClientSide()); }}It’s registered like any other item, as SummonStoneItem: see a new item.
Forge 1.16.5
Section titled “Forge 1.16.5”Uses:
EntityType.spawnSpawnReason.MOB_SUMMONEDItemUseContext.getClickedPos
src/main/java/com/example/examplemod/item/SummonStoneItem.java
package com.example.examplemod.item;
import net.minecraft.item.ItemStack;import net.minecraft.world.server.ServerWorld;import net.minecraft.util.ActionResultType;import net.minecraft.entity.EntityType;import net.minecraft.entity.SpawnReason;import net.minecraft.item.Item;import net.minecraft.item.ItemUseContext;
// Summon Stone: right-click a block to spawn a ZOMBIE on top of it.public class SummonStoneItem extends Item { public SummonStoneItem(Item.Properties properties) { super(properties); }
@Override public ActionResultType useOn(ItemUseContext context) { if (context.getLevel() instanceof ServerWorld) { ServerWorld serverWorld = (ServerWorld) context.getLevel(); EntityType.ZOMBIE.spawn(serverWorld, ItemStack.EMPTY, context.getPlayer(), context.getClickedPos().above(), SpawnReason.MOB_SUMMONED, false, false); context.getItemInHand().shrink(1); } return ActionResultType.sidedSuccess(context.getLevel().isClientSide()); }}It’s registered like any other item, as SummonStoneItem: see a new item.
Fabric 1.18 to 1.20.3
Section titled “Fabric 1.18 to 1.20.3”Uses:
EntityType.spawnMobSpawnType.MOB_SUMMONEDUseOnContext.getClickedPos
src/main/java/com/example/examplemod/item/SummonStoneItem.java
package com.example.examplemod.item;
import net.minecraft.server.level.ServerLevel;import net.minecraft.core.BlockPos;import net.minecraft.network.chat.Component;import net.minecraft.world.InteractionHand;import net.minecraft.world.InteractionResult;import net.minecraft.world.InteractionResultHolder;import net.minecraft.world.entity.EntityType;import net.minecraft.world.entity.Mob;import net.minecraft.world.phys.Vec3;import net.minecraft.world.entity.player.Player;import net.minecraft.world.item.Item;import net.minecraft.world.item.ItemStack;import net.minecraft.world.item.context.UseOnContext;import net.minecraft.world.level.Level;import net.minecraft.world.level.block.Blocks;import net.minecraft.world.phys.BlockHitResult;import net.minecraft.world.phys.HitResult;import net.minecraft.world.phys.Vec3;
// Summon Stone: right-click a block to spawn a ZOMBIE on top of it.public class SummonStoneItem extends Item { public SummonStoneItem(Item.Properties properties) { super(properties); }
@Override public InteractionResult useOn(UseOnContext context) { if (context.getLevel() instanceof ServerLevel serverLevel) { Mob mob = EntityType.ZOMBIE.create(serverLevel); mob.moveTo(Vec3.atBottomCenterOf(context.getClickedPos().above())); serverLevel.addFreshEntity(mob); context.getItemInHand().shrink(1); } return InteractionResult.sidedSuccess(context.getLevel().isClientSide()); }}It’s registered like any other item, as SummonStoneItem: see a new item.
Fabric 1.16.5 to 1.17.1
Section titled “Fabric 1.16.5 to 1.17.1”Uses:
EntityType.spawnMobSpawnType.MOB_SUMMONEDUseOnContext.getClickedPos
src/main/java/com/example/examplemod/item/SummonStoneItem.java
package com.example.examplemod.item;
import net.minecraft.server.level.ServerLevel;import net.minecraft.core.BlockPos;import net.minecraft.network.chat.Component;import net.minecraft.world.InteractionHand;import net.minecraft.world.InteractionResult;import net.minecraft.world.InteractionResultHolder;import net.minecraft.world.entity.EntityType;import net.minecraft.world.entity.Mob;import net.minecraft.world.phys.Vec3;import net.minecraft.world.entity.player.Player;import net.minecraft.world.item.Item;import net.minecraft.world.item.ItemStack;import net.minecraft.world.item.context.UseOnContext;import net.minecraft.world.level.Level;import net.minecraft.world.level.block.Blocks;import net.minecraft.world.phys.BlockHitResult;import net.minecraft.world.phys.HitResult;import net.minecraft.world.phys.Vec3;
// Summon Stone: right-click a block to spawn a ZOMBIE on top of it.public class SummonStoneItem extends Item { public SummonStoneItem(Item.Properties properties) { super(properties); }
@Override public InteractionResult useOn(UseOnContext context) { if (context.getLevel() instanceof ServerLevel) { ServerLevel serverLevel = (ServerLevel) context.getLevel(); Mob mob = EntityType.ZOMBIE.create(serverLevel); mob.moveTo(Vec3.atBottomCenterOf(context.getClickedPos().above())); serverLevel.addFreshEntity(mob); context.getItemInHand().shrink(1); } return InteractionResult.sidedSuccess(context.getLevel().isClientSide()); }}It’s registered like any other item, as SummonStoneItem: see a new item.
Forge 1.12.2
Section titled “Forge 1.12.2”Uses:
EntityType.spawnSpawnReason.MOB_SUMMONEDItem.getClickedPos
In 1.12.2 the item overrides onItemUse, you build the mob from its registry name, place it with setPosition and add it with world.spawnEntity.
src/main/java/com/example/examplemod/item/SummonStoneItem.java
package com.example.examplemod.item;
import net.minecraft.entity.Entity;import net.minecraft.entity.EntityList;import net.minecraft.entity.player.EntityPlayer;import net.minecraft.item.Item;import net.minecraft.util.EnumActionResult;import net.minecraft.util.EnumFacing;import net.minecraft.util.EnumHand;import net.minecraft.util.ResourceLocation;import net.minecraft.util.math.BlockPos;import net.minecraft.world.World;
// Summon Stone: right-click a block to spawn a ZOMBIE on top of it. A 1.12.2 entity is made from its registry name.public class SummonStoneItem extends Item { @Override public EnumActionResult onItemUse(EntityPlayer player, World world, BlockPos pos, EnumHand hand, EnumFacing facing, float hitX, float hitY, float hitZ) { if (!world.isRemote) { Entity mob = EntityList.createEntityByIDFromName(new ResourceLocation("minecraft", "ZOMBIE".toLowerCase()), world); if (mob != null) { mob.setPosition(pos.getX() + 0.5, pos.getY() + 1.0, pos.getZ() + 0.5); world.spawnEntity(mob); player.getHeldItem(hand).shrink(1); } } return EnumActionResult.SUCCESS; }}It’s registered like any other item, as SummonStoneItem: see a new item.
Sources
Section titled “Sources”- https://docs.neoforged.net/docs/1.21.1/items/interactions
- https://docs.fabricmc.net/1.21.1/develop/items/first-item
Related how-tos
Section titled “Related how-tos”- A wand that strikes lightning where you look
- An item that shoots a projectile
- Spawn a mob every few seconds
- A tool that breaks a 3 by 3 area
- An item that changes the block you click
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.