Skip to content

How to make an item that explodes where you aim (Minecraft mod)

player.pick(distance, partialTick, includeFluids) fires a ray from the player’s eyes along their look direction and returns a HitResult (player.rayTrace(distance, partialTicks) on 1.12.2). Check its type: BLOCK means it hit a block and getLocation is the exact point; MISS means nothing was in range. level.explode(source, x, y, z, power, interaction) makes the explosion; Level.ExplosionInteraction decides whether it breaks blocks (BLOCK), also hurts nothing (NONE) or acts like TNT. Run it on the server.

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 use and act only on the server.
  2. Call player.pick to get the HitResult.
  3. If the type is BLOCK, take getLocation().
  4. Call level.explode there, and add a cooldown.

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

Setting In the example What it does
power 3 Explosion size (TNT is 4).
range 40 How far it reaches, in blocks.

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, Fabric 1.19.3 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.19.3 to 1.20.1, Fabric 1.19.3 to 1.20.3”

Uses:

  • Entity.pick
  • HitResult.getType
  • HitResult.getLocation
  • Level.explode
  • Level.ExplosionInteraction

src/main/java/com/example/examplemod/item/BoomWandItem.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.item.Item;
import net.minecraft.world.item.ItemStack;
import net.minecraft.world.level.Level;
import net.minecraft.world.phys.HitResult;
import net.minecraft.world.phys.Vec3;
// Boom Wand: right-click to explode the block you look at, up to 40 blocks away. The ray and the explosion run on the server.
public class BoomWandItem extends Item {
public BoomWandItem(Item.Properties properties) {
super(properties);
}
@Override
public InteractionResultHolder<ItemStack> use(Level level, Player player, InteractionHand hand) {
if (!level.isClientSide()) {
HitResult hit = player.pick(40, 1.0F, false);
if (hit.getType() == HitResult.Type.BLOCK) {
Vec3 at = hit.getLocation();
level.explode(null, at.x, at.y, at.z, 3.0F, Level.ExplosionInteraction.BLOCK);
player.getCooldowns().addCooldown(this, 20);
}
}
return InteractionResultHolder.sidedSuccess(player.getItemInHand(hand), level.isClientSide());
}
}

It’s registered like any other item, as BoomWandItem: see a new item.

Uses:

  • Entity.pick
  • HitResult.getType
  • HitResult.getLocation
  • Level.explode
  • Level.ExplosionInteraction

src/main/java/com/example/examplemod/item/BoomWandItem.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.item.Item;
import net.minecraft.world.item.ItemStack;
import net.minecraft.world.level.Level;
import net.minecraft.world.phys.HitResult;
import net.minecraft.world.phys.Vec3;
// Boom Wand: right-click to explode the block you look at, up to 40 blocks away. The ray and the explosion run on the server.
public class BoomWandItem extends Item {
public BoomWandItem(Item.Properties properties) {
super(properties);
}
@Override
public InteractionResult use(Level level, Player player, InteractionHand hand) {
if (!level.isClientSide()) {
HitResult hit = player.pick(40, 1.0F, false);
if (hit.getType() == HitResult.Type.BLOCK) {
Vec3 at = hit.getLocation();
level.explode(null, at.x, at.y, at.z, 3.0F, Level.ExplosionInteraction.BLOCK);
player.getCooldowns().addCooldown(player.getItemInHand(hand), 20);
}
}
return InteractionResult.SUCCESS;
}
}

It’s registered like any other item, as BoomWandItem: see a new item.

Forge 1.17.1 to 1.19.2, Fabric 1.16.5 to 1.19.2

Section titled “Forge 1.17.1 to 1.19.2, Fabric 1.16.5 to 1.19.2”

Uses:

  • Entity.pick
  • HitResult.getType
  • HitResult.getLocation
  • Level.explode
  • Level.ExplosionInteraction

src/main/java/com/example/examplemod/item/BoomWandItem.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.item.Item;
import net.minecraft.world.item.ItemStack;
import net.minecraft.world.level.Explosion;
import net.minecraft.world.level.Level;
import net.minecraft.world.phys.HitResult;
import net.minecraft.world.phys.Vec3;
// Boom Wand: right-click to explode the block you look at, up to 40 blocks away. The ray and the explosion run on the server.
public class BoomWandItem extends Item {
public BoomWandItem(Item.Properties properties) {
super(properties);
}
@Override
public InteractionResultHolder<ItemStack> use(Level level, Player player, InteractionHand hand) {
if (!level.isClientSide()) {
HitResult hit = player.pick(40, 1.0F, false);
if (hit.getType() == HitResult.Type.BLOCK) {
Vec3 at = hit.getLocation();
level.explode(null, at.x, at.y, at.z, 3.0F, Explosion.BlockInteraction.BREAK);
player.getCooldowns().addCooldown(this, 20);
}
}
return InteractionResultHolder.sidedSuccess(player.getItemInHand(hand), level.isClientSide());
}
}

It’s registered like any other item, as BoomWandItem: see a new item.

Uses:

  • Entity.pick
  • RayTraceResult.getType
  • RayTraceResult.getLocation
  • World.explode
  • Explosion.Mode

src/main/java/com/example/examplemod/item/BoomWandItem.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.item.Item;
import net.minecraft.item.ItemStack;
import net.minecraft.world.Explosion;
import net.minecraft.world.World;
import net.minecraft.util.math.RayTraceResult;
import net.minecraft.util.math.vector.Vector3d;
// Boom Wand: right-click to explode the block you look at, up to 40 blocks away. The ray and the explosion run on the server.
public class BoomWandItem extends Item {
public BoomWandItem(Item.Properties properties) {
super(properties);
}
@Override
public ActionResult<ItemStack> use(World world, PlayerEntity player, Hand hand) {
if (!world.isClientSide()) {
RayTraceResult hit = player.pick(40, 1.0F, false);
if (hit.getType() == RayTraceResult.Type.BLOCK) {
Vector3d at = hit.getLocation();
world.explode(null, at.x, at.y, at.z, 3.0F, Explosion.Mode.BREAK);
player.getCooldowns().addCooldown(this, 20);
}
}
return ActionResult.sidedSuccess(player.getItemInHand(hand), world.isClientSide());
}
}

It’s registered like any other item, as BoomWandItem: see a new item.

Uses:

  • Entity.pick
  • RayTraceResult.getType
  • RayTraceResult.getLocation
  • World.explode
  • Explosion.Mode

In 1.12.2 the ray is player.rayTrace(distance, 1.0F) and the explosion is world.createExplosion(player, x, y, z, power, true).

src/main/java/com/example/examplemod/item/BoomWandItem.java

package com.example.examplemod.item;
import net.minecraft.entity.player.EntityPlayer;
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.util.math.RayTraceResult;
import net.minecraft.util.math.Vec3d;
import net.minecraft.world.World;
// Boom Wand: right-click to explode the block you look at, up to 40 blocks away. The ray and the explosion run on the server.
public class BoomWandItem extends Item {
@Override
public ActionResult<ItemStack> onItemRightClick(World world, EntityPlayer player, EnumHand hand) {
if (!world.isRemote) {
RayTraceResult hit = player.rayTrace(40, 1.0F);
if (hit != null && hit.typeOfHit == RayTraceResult.Type.BLOCK) {
Vec3d at = hit.hitVec;
world.createExplosion(player, at.x, at.y, at.z, 3.0F, true);
player.getCooldownTracker().setCooldown(this, 20);
}
}
return new ActionResult<>(EnumActionResult.SUCCESS, player.getHeldItem(hand));
}
}

It’s registered like any other item, as BoomWandItem: 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.