Skip to content

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.

  1. Override useOn and work only on the server.
  2. Take context.getClickedPos().above() as the spot.
  3. Call EntityType.X.spawn(serverLevel, pos, spawnReason) with the MOB_SUMMONED reason.
  4. Shrink the stack if the item is one-use.

The example uses these values. Change them in the code.

Setting In the example What it does
mob ZOMBIE The mob it spawns.

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.spawn
  • MobSpawnType.MOB_SUMMONED
  • UseOnContext.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.

Uses:

  • EntityType.spawn
  • EntitySpawnReason.MOB_SUMMONED
  • UseOnContext.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.

Uses:

  • EntityTypes.ZOMBIE
  • EntityType.spawn
  • EntitySpawnReason.MOB_SUMMONED
  • UseOnContext.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.

Uses:

  • EntityType.spawn
  • MobSpawnType.MOB_SUMMONED
  • UseOnContext.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.

Uses:

  • EntityType.spawn
  • SpawnReason.MOB_SUMMONED
  • ItemUseContext.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.

Uses:

  • EntityType.spawn
  • MobSpawnType.MOB_SUMMONED
  • UseOnContext.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.

Uses:

  • EntityType.spawn
  • MobSpawnType.MOB_SUMMONED
  • UseOnContext.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.

Uses:

  • EntityType.spawn
  • SpawnReason.MOB_SUMMONED
  • Item.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.

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.