How to make an item that shoots a projectile in a Minecraft mod
A projectile is an entity. In the item’s use method, on the server only, create it with the player as owner, aim it with shootFromRotation(player, xRot, yRot, 0, speed, inaccuracy) and add it with level.addFreshEntity. Set an item cooldown so it cannot be spammed. Snowball, ThrownEgg and ThrownEnderpearl share the same constructor, so they swap freely. For your own projectile, extend ThrowableItemProjectile and register an EntityType for 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; Minecraft 1.12.2 on Forge. Each example was compiled against its loader and version before it shipped in Linen.
- Write an Item subclass and override use.
- On the server, make the projectile and aim it from the player’s rotation.
- Add it to the level with addFreshEntity.
- Add a cooldown and return a success result.
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 |
|---|---|---|
| projectile | Snowball |
What it throws. |
| speed | 1.5 |
How fast it flies (a snowball is 1.5). |
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.17.1 to 1.20.1, Fabric 1.17 to 1.20.3
Section titled “Fabric and NeoForge 1.20.4 to 1.21.1, NeoForge 1.20.1 to 1.20.2, Forge 1.17.1 to 1.20.1, Fabric 1.17 to 1.20.3”Uses:
Item.useProjectile.shootFromRotationLevel.addFreshEntityItemCooldowns.addCooldown
src/main/java/com/example/examplemod/item/SnowBlasterItem.java
package com.example.examplemod.item;
import net.minecraft.world.InteractionHand;import net.minecraft.world.InteractionResultHolder;import net.minecraft.world.entity.player.Player;import net.minecraft.world.entity.projectile.Snowball;import net.minecraft.world.item.Item;import net.minecraft.world.item.ItemStack;import net.minecraft.world.level.Level;
// Snow Blaster: right-click to throw a Snowball at speed 1.5F. use runs in the client and on the server; the projectile exists only on the server.public class SnowBlasterItem extends Item { public SnowBlasterItem(Item.Properties properties) { super(properties); }
@Override public InteractionResultHolder<ItemStack> use(Level level, Player player, InteractionHand hand) { ItemStack stack = player.getItemInHand(hand); if (!level.isClientSide()) { Snowball projectile = new Snowball(level, player); projectile.shootFromRotation(player, player.getXRot(), player.getYRot(), 0.0F, 1.5F, 1.0F); level.addFreshEntity(projectile); } player.getCooldowns().addCooldown(this, 10); return InteractionResultHolder.sidedSuccess(stack, level.isClientSide()); }}It’s registered like any other item, as SnowBlasterItem: see a new item.
Fabric and NeoForge 1.21.2 to 1.21.10
Section titled “Fabric and NeoForge 1.21.2 to 1.21.10”Uses:
Item.useProjectile.shootFromRotationLevel.addFreshEntityItemCooldowns.addCooldown
src/main/java/com/example/examplemod/item/SnowBlasterItem.java
package com.example.examplemod.item;
import net.minecraft.world.InteractionHand;import net.minecraft.world.InteractionResult;import net.minecraft.world.entity.player.Player;import net.minecraft.world.entity.projectile.Snowball;import net.minecraft.world.item.Item;import net.minecraft.world.item.ItemStack;import net.minecraft.world.level.Level;
// Snow Blaster: right-click to throw a Snowball at speed 1.5F. use runs in the client and on the server; the projectile exists only on the server.public class SnowBlasterItem extends Item { public SnowBlasterItem(Item.Properties properties) { super(properties); }
@Override public InteractionResult use(Level level, Player player, InteractionHand hand) { ItemStack stack = player.getItemInHand(hand); if (!level.isClientSide()) { Snowball projectile = new Snowball(level, player, stack); projectile.shootFromRotation(player, player.getXRot(), player.getYRot(), 0.0F, 1.5F, 1.0F); level.addFreshEntity(projectile); } player.getCooldowns().addCooldown(stack, 10); return InteractionResult.SUCCESS; }}It’s registered like any other item, as SnowBlasterItem: see a new item.
Fabric and NeoForge 1.21.11 to 26.3
Section titled “Fabric and NeoForge 1.21.11 to 26.3”Uses:
Item.useProjectile.shootFromRotationLevel.addFreshEntityItemCooldowns.addCooldown
Since 1.21.11 the thrown items (Snowball, ThrownEgg, ThrownEnderpearl) live in net.minecraft.world.entity.projectile.throwableitemprojectile.
src/main/java/com/example/examplemod/item/SnowBlasterItem.java
package com.example.examplemod.item;
import net.minecraft.world.InteractionHand;import net.minecraft.world.InteractionResult;import net.minecraft.world.entity.player.Player;import net.minecraft.world.entity.projectile.throwableitemprojectile.Snowball;import net.minecraft.world.item.Item;import net.minecraft.world.item.ItemStack;import net.minecraft.world.level.Level;
// Snow Blaster: right-click to throw a Snowball at speed 1.5F. use runs in the client and on the server; the projectile exists only on the server.public class SnowBlasterItem extends Item { public SnowBlasterItem(Item.Properties properties) { super(properties); }
@Override public InteractionResult use(Level level, Player player, InteractionHand hand) { ItemStack stack = player.getItemInHand(hand); if (!level.isClientSide()) { Snowball projectile = new Snowball(level, player, stack); projectile.shootFromRotation(player, player.getXRot(), player.getYRot(), 0.0F, 1.5F, 1.0F); level.addFreshEntity(projectile); } player.getCooldowns().addCooldown(stack, 10); return InteractionResult.SUCCESS; }}It’s registered like any other item, as SnowBlasterItem: see a new item.
Forge 1.16.5
Section titled “Forge 1.16.5”Uses:
Item.useProjectileItemEntity.shootFromRotationWorld.addFreshEntityCooldownTracker.addCooldown
src/main/java/com/example/examplemod/item/SnowBlasterItem.java
package com.example.examplemod.item;
import net.minecraft.util.Hand;import net.minecraft.util.ActionResult;import net.minecraft.entity.player.PlayerEntity;import net.minecraft.entity.projectile.SnowballEntity;import net.minecraft.item.Item;import net.minecraft.item.ItemStack;import net.minecraft.world.World;
// Snow Blaster: right-click to throw a snowball at speed 1.5F. use runs in the client and on the server; the projectile exists only on the server.public class SnowBlasterItem extends Item { public SnowBlasterItem(Item.Properties properties) { super(properties); }
@Override public ActionResult<ItemStack> use(World world, PlayerEntity player, Hand hand) { ItemStack stack = player.getItemInHand(hand); if (!world.isClientSide()) { SnowballEntity projectile = new SnowballEntity(world, player); projectile.shootFromRotation(player, player.xRot, player.yRot, 0.0F, 1.5F, 1.0F); world.addFreshEntity(projectile); } player.getCooldowns().addCooldown(this, 10); return ActionResult.sidedSuccess(stack, world.isClientSide()); }}It’s registered like any other item, as SnowBlasterItem: see a new item.
Fabric 1.16.5
Section titled “Fabric 1.16.5”Uses:
Item.useProjectile.shootFromRotationLevel.addFreshEntityItemCooldowns.addCooldown
src/main/java/com/example/examplemod/item/SnowBlasterItem.java
package com.example.examplemod.item;
import net.minecraft.world.InteractionHand;import net.minecraft.world.InteractionResultHolder;import net.minecraft.world.entity.player.Player;import net.minecraft.world.entity.projectile.Snowball;import net.minecraft.world.item.Item;import net.minecraft.world.item.ItemStack;import net.minecraft.world.level.Level;
// Snow Blaster: right-click to throw a Snowball at speed 1.5F. use runs in the client and on the server; the projectile exists only on the server.public class SnowBlasterItem extends Item { public SnowBlasterItem(Item.Properties properties) { super(properties); }
@Override public InteractionResultHolder<ItemStack> use(Level level, Player player, InteractionHand hand) { ItemStack stack = player.getItemInHand(hand); if (!level.isClientSide()) { Snowball projectile = new Snowball(level, player); projectile.shootFromRotation(player, player.xRot, player.yRot, 0.0F, 1.5F, 1.0F); level.addFreshEntity(projectile); } player.getCooldowns().addCooldown(this, 10); return InteractionResultHolder.sidedSuccess(stack, level.isClientSide()); }}It’s registered like any other item, as SnowBlasterItem: see a new item.
Forge 1.12.2
Section titled “Forge 1.12.2”Uses:
Item.useEntityThrowable.shootFromRotationWorld.addFreshEntityCooldownTracker.addCooldown
In 1.12.2 the item overrides onItemRightClick, the projectile is an EntitySnowball built with (world, player), aimed with shoot(player, pitch, yaw, 0, speed, inaccuracy), and added with world.spawnEntity.
src/main/java/com/example/examplemod/item/SnowBlasterItem.java
package com.example.examplemod.item;
import net.minecraft.entity.player.EntityPlayer;import net.minecraft.entity.projectile.EntitySnowball;import net.minecraft.item.Item;import net.minecraft.item.ItemStack;import net.minecraft.util.ActionResult;import net.minecraft.util.EnumActionResult;import net.minecraft.util.EnumHand;import net.minecraft.world.World;
// Snow Blaster: right-click to throw a snowball at speed 1.5F. onItemRightClick runs in the client and on the server; the projectile exists only on the server.public class SnowBlasterItem extends Item { @Override public ActionResult<ItemStack> onItemRightClick(World world, EntityPlayer player, EnumHand hand) { ItemStack stack = player.getHeldItem(hand); if (!world.isRemote) { EntitySnowball projectile = new EntitySnowball(world, player); projectile.shoot(player, player.rotationPitch, player.rotationYaw, 0.0F, 1.5F, 1.0F); world.spawnEntity(projectile); } player.getCooldownTracker().setCooldown(this, 10); return new ActionResult<>(EnumActionResult.SUCCESS, stack); }}It’s registered like any other item, as SnowBlasterItem: see a new item.
Sources
Section titled “Sources”- https://docs.fabricmc.net/1.21.1/develop/items/first-item
- https://docs.neoforged.net/docs/1.21.1/items/interactions
- https://docs.neoforged.net/docs/1.21.1/entities/index
Related how-tos
Section titled “Related how-tos”- An item that spawns a mob where you click
- A tool that breaks a 3 by 3 area
- A wand that strikes lightning where you look
- An item that changes the block you click
- An item that explodes where you aim
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.