Skip to content

How to make a wand that strikes lightning where you look (Minecraft mod)

A lightning bolt is an entity. Create it with EntityType.LIGHTNING_BOLT.create(level) (EntityTypes.LIGHTNING_BOLT from 26.2), move it to the spot (moveTo, snapTo from 1.21.5), and add it to the world with addFreshEntity. Do this only on the server. To find the spot, trace a line from the player’s eyes along the look angle with level.clip; the hit result holds the block position.

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 work only on the server.
  2. Trace a line from the eyes along getLookAngle with level.clip.
  3. Create the bolt and move it to the hit position.
  4. Add it with addFreshEntity and set a cooldown.

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

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”

Uses:

  • EntityType.create
  • Level.clip
  • Level.addFreshEntity
  • Entity.moveTo

src/main/java/com/example/examplemod/item/StormWandItem.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;
// Storm Wand: right-click to strike lightning at the block you look at, up to 50 blocks away.
public class StormWandItem extends Item {
public StormWandItem(Item.Properties properties) {
super(properties);
}
@Override
public InteractionResultHolder<ItemStack> use(Level level, Player player, InteractionHand hand) {
if (level instanceof ServerLevel serverLevel) {
BlockHitResult hit = level.clip(new net.minecraft.world.level.ClipContext(player.getEyePosition(), player.getEyePosition().add(player.getLookAngle().scale(50)), net.minecraft.world.level.ClipContext.Block.OUTLINE, net.minecraft.world.level.ClipContext.Fluid.NONE, player));
if (hit.getType() == HitResult.Type.BLOCK) {
net.minecraft.world.entity.LightningBolt bolt = EntityType.LIGHTNING_BOLT.create(serverLevel);
if (bolt != null) {
bolt.moveTo(Vec3.atBottomCenterOf(hit.getBlockPos().above()));
serverLevel.addFreshEntity(bolt);
player.getCooldowns().addCooldown(this, 40);
}
}
}
return InteractionResultHolder.sidedSuccess(player.getItemInHand(hand), level.isClientSide());
}
}

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

Uses:

  • EntityType.create
  • Level.clip
  • Level.addFreshEntity
  • Entity.moveTo

src/main/java/com/example/examplemod/item/StormWandItem.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;
// Storm Wand: right-click to strike lightning at the block you look at, up to 50 blocks away.
public class StormWandItem extends Item {
public StormWandItem(Item.Properties properties) {
super(properties);
}
@Override
public InteractionResult use(Level level, Player player, InteractionHand hand) {
if (level instanceof ServerLevel serverLevel) {
BlockHitResult hit = level.clip(new net.minecraft.world.level.ClipContext(player.getEyePosition(), player.getEyePosition().add(player.getLookAngle().scale(50)), net.minecraft.world.level.ClipContext.Block.OUTLINE, net.minecraft.world.level.ClipContext.Fluid.NONE, player));
if (hit.getType() == HitResult.Type.BLOCK) {
net.minecraft.world.entity.LightningBolt bolt = EntityType.LIGHTNING_BOLT.create(serverLevel, net.minecraft.world.entity.EntitySpawnReason.TRIGGERED);
if (bolt != null) {
bolt.moveTo(Vec3.atBottomCenterOf(hit.getBlockPos().above()));
serverLevel.addFreshEntity(bolt);
player.getCooldowns().addCooldown(player.getItemInHand(hand), 40);
}
}
}
return InteractionResult.SUCCESS;
}
}

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

Uses:

  • EntityType.create
  • Level.clip
  • Level.addFreshEntity
  • Entity.snapTo

Since 1.21.5 Entity#moveTo is snapTo.

src/main/java/com/example/examplemod/item/StormWandItem.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;
// Storm Wand: right-click to strike lightning at the block you look at, up to 50 blocks away.
public class StormWandItem extends Item {
public StormWandItem(Item.Properties properties) {
super(properties);
}
@Override
public InteractionResult use(Level level, Player player, InteractionHand hand) {
if (level instanceof ServerLevel serverLevel) {
BlockHitResult hit = level.clip(new net.minecraft.world.level.ClipContext(player.getEyePosition(), player.getEyePosition().add(player.getLookAngle().scale(50)), net.minecraft.world.level.ClipContext.Block.OUTLINE, net.minecraft.world.level.ClipContext.Fluid.NONE, player));
if (hit.getType() == HitResult.Type.BLOCK) {
net.minecraft.world.entity.LightningBolt bolt = EntityType.LIGHTNING_BOLT.create(serverLevel, net.minecraft.world.entity.EntitySpawnReason.TRIGGERED);
if (bolt != null) {
bolt.snapTo(Vec3.atBottomCenterOf(hit.getBlockPos().above()));
serverLevel.addFreshEntity(bolt);
player.getCooldowns().addCooldown(player.getItemInHand(hand), 40);
}
}
}
return InteractionResult.SUCCESS;
}
}

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

Uses:

  • EntityTypes.LIGHTNING_BOLT
  • EntityType.create
  • Level.clip
  • Level.addFreshEntity
  • Entity.snapTo

Since 26.2 the entity type constants (LIGHTNING_BOLT, ZOMBIE…) are in EntityTypes, not EntityType.

src/main/java/com/example/examplemod/item/StormWandItem.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.EntityTypes;
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;
// Storm Wand: right-click to strike lightning at the block you look at, up to 50 blocks away.
public class StormWandItem extends Item {
public StormWandItem(Item.Properties properties) {
super(properties);
}
@Override
public InteractionResult use(Level level, Player player, InteractionHand hand) {
if (level instanceof ServerLevel serverLevel) {
BlockHitResult hit = level.clip(new net.minecraft.world.level.ClipContext(player.getEyePosition(), player.getEyePosition().add(player.getLookAngle().scale(50)), net.minecraft.world.level.ClipContext.Block.OUTLINE, net.minecraft.world.level.ClipContext.Fluid.NONE, player));
if (hit.getType() == HitResult.Type.BLOCK) {
net.minecraft.world.entity.LightningBolt bolt = EntityTypes.LIGHTNING_BOLT.create(serverLevel, net.minecraft.world.entity.EntitySpawnReason.TRIGGERED);
if (bolt != null) {
bolt.snapTo(Vec3.atBottomCenterOf(hit.getBlockPos().above()));
serverLevel.addFreshEntity(bolt);
player.getCooldowns().addCooldown(player.getItemInHand(hand), 40);
}
}
}
return InteractionResult.SUCCESS;
}
}

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

Uses:

  • EntityType.create
  • World.clip
  • World.addFreshEntity
  • Entity.moveTo

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

package com.example.examplemod.item;
import net.minecraft.world.server.ServerWorld;
import net.minecraft.util.Hand;
import net.minecraft.util.ActionResult;
import net.minecraft.entity.EntityType;
import net.minecraft.entity.effect.LightningBoltEntity;
import net.minecraft.entity.player.PlayerEntity;
import net.minecraft.item.Item;
import net.minecraft.item.ItemStack;
import net.minecraft.world.World;
import net.minecraft.util.math.BlockRayTraceResult;
import net.minecraft.util.math.RayTraceContext;
import net.minecraft.util.math.RayTraceResult;
import net.minecraft.util.math.vector.Vector3d;
// Storm Wand: right-click to strike lightning at the block you look at, up to 50 blocks away.
public class StormWandItem extends Item {
public StormWandItem(Item.Properties properties) {
super(properties);
}
@Override
public ActionResult<ItemStack> use(World world, PlayerEntity player, Hand hand) {
if (!world.isClientSide()) {
ServerWorld serverWorld = (ServerWorld) world;
BlockRayTraceResult hit = world.clip(new RayTraceContext(player.getEyePosition(1.0F), player.getEyePosition(1.0F).add(player.getLookAngle().scale(50)), RayTraceContext.BlockMode.OUTLINE, RayTraceContext.FluidMode.NONE, player));
if (hit.getType() == RayTraceResult.Type.BLOCK) {
LightningBoltEntity bolt = EntityType.LIGHTNING_BOLT.create(serverWorld);
if (bolt != null) {
bolt.moveTo(Vector3d.atBottomCenterOf(hit.getBlockPos().above()));
serverWorld.addFreshEntity(bolt);
player.getCooldowns().addCooldown(this, 40);
}
}
}
return ActionResult.sidedSuccess(player.getItemInHand(hand), world.isClientSide());
}
}

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

Uses:

  • EntityType.create
  • Level.clip
  • Level.addFreshEntity
  • Entity.moveTo

src/main/java/com/example/examplemod/item/StormWandItem.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;
// Storm Wand: right-click to strike lightning at the block you look at, up to 50 blocks away.
public class StormWandItem extends Item {
public StormWandItem(Item.Properties properties) {
super(properties);
}
@Override
public InteractionResultHolder<ItemStack> use(Level level, Player player, InteractionHand hand) {
if (level instanceof ServerLevel) {
ServerLevel serverLevel = (ServerLevel) level;
BlockHitResult hit = level.clip(new net.minecraft.world.level.ClipContext(player.getEyePosition(1.0F), player.getEyePosition(1.0F).add(player.getLookAngle().scale(50)), net.minecraft.world.level.ClipContext.Block.OUTLINE, net.minecraft.world.level.ClipContext.Fluid.NONE, player));
if (hit.getType() == HitResult.Type.BLOCK) {
net.minecraft.world.entity.LightningBolt bolt = EntityType.LIGHTNING_BOLT.create(serverLevel);
if (bolt != null) {
bolt.moveTo(Vec3.atBottomCenterOf(hit.getBlockPos().above()));
serverLevel.addFreshEntity(bolt);
player.getCooldowns().addCooldown(this, 40);
}
}
}
return InteractionResultHolder.sidedSuccess(player.getItemInHand(hand), level.isClientSide());
}
}

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

Uses:

  • EntityType.create
  • World.clip
  • World.addFreshEntity
  • Entity.moveTo

In 1.12.2 the bolt is new EntityLightningBolt(world, x, y, z, false) added with world.addWeatherEffect, and the spot comes from player.rayTrace(distance, 1.0F).

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

package com.example.examplemod.item;
import net.minecraft.entity.effect.EntityLightningBolt;
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.world.World;
// Storm Wand: right-click to strike lightning at the block you look at, up to 50 blocks away.
public class StormWandItem extends Item {
@Override
public ActionResult<ItemStack> onItemRightClick(World world, EntityPlayer player, EnumHand hand) {
if (!world.isRemote) {
RayTraceResult hit = player.rayTrace(50.0D, 1.0F);
if (hit != null && hit.typeOfHit == RayTraceResult.Type.BLOCK) {
world.addWeatherEffect(new EntityLightningBolt(world, hit.hitVec.x, hit.hitVec.y, hit.hitVec.z, false));
player.getCooldownTracker().setCooldown(this, 40);
}
}
return new ActionResult<>(EnumActionResult.SUCCESS, player.getHeldItem(hand));
}
}

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