How to make an item that teleports you forward (Minecraft mod)
Teleporting is a change of position, on the server. For a player, ServerPlayer.teleportTo moves them and tells their client (teleportTo(x, y, z) on 1.21.2 and newer, teleportTo(level, x, y, z, yaw, pitch) before; setPositionAndUpdate on 1.12.2). The direction the player looks is player.getLookAngle(), a unit vector; multiply it by a distance and add it to the player’s position. This does not check for walls, so the player can end up inside a block: test the target with level.noCollision or raycast first when it matters.
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 use and run the teleport only on the server.
- Take getLookAngle and scale it by the distance.
- Call teleportTo on the ServerPlayer with the new position.
- Add a cooldown so it cannot be spammed.
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 |
|---|---|---|
| distance | 8 |
How far it moves you, in blocks. |
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.20 to 1.20.1, Fabric 1.20 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.20 to 1.20.1, Fabric 1.20 to 1.20.3”Uses:
ServerPlayer.teleportToEntity.getLookAngleVec3.scaleItemCooldowns.addCooldown
src/main/java/com/example/examplemod/item/BlinkStaffItem.java
package com.example.examplemod.item;
import net.minecraft.server.level.ServerPlayer;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.Vec3;
// Blink Staff: right-click to jump 8.0F blocks the way you look. use runs on both sides; the move happens on the server.public class BlinkStaffItem extends Item { public BlinkStaffItem(Item.Properties properties) { super(properties); }
@Override public InteractionResultHolder<ItemStack> use(Level level, Player player, InteractionHand hand) { if (player instanceof ServerPlayer serverPlayer) { Vec3 target = serverPlayer.position().add(serverPlayer.getLookAngle().scale(8.0F)); serverPlayer.teleportTo(serverPlayer.serverLevel(), target.x, target.y, target.z, serverPlayer.getYRot(), serverPlayer.getXRot()); serverPlayer.getCooldowns().addCooldown(this, 20); } return InteractionResultHolder.sidedSuccess(player.getItemInHand(hand), level.isClientSide()); }}It’s registered like any other item, as BlinkStaffItem: see a new item.
Fabric and NeoForge 1.21.2 to 26.3
Section titled “Fabric and NeoForge 1.21.2 to 26.3”Uses:
ServerPlayer.teleportToEntity.getLookAngleVec3.scaleItemCooldowns.addCooldown
src/main/java/com/example/examplemod/item/BlinkStaffItem.java
package com.example.examplemod.item;
import net.minecraft.server.level.ServerPlayer;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.Vec3;
// Blink Staff: right-click to jump 8.0F blocks the way you look. use runs on both sides; the move happens on the server.public class BlinkStaffItem extends Item { public BlinkStaffItem(Item.Properties properties) { super(properties); }
@Override public InteractionResult use(Level level, Player player, InteractionHand hand) { if (player instanceof ServerPlayer serverPlayer) { Vec3 target = serverPlayer.position().add(serverPlayer.getLookAngle().scale(8.0F)); serverPlayer.teleportTo(target.x, target.y, target.z); serverPlayer.getCooldowns().addCooldown(serverPlayer.getItemInHand(hand), 20); } return InteractionResult.SUCCESS; }}It’s registered like any other item, as BlinkStaffItem: see a new item.
Forge 1.17.1 to 1.19.4, Fabric 1.17 to 1.19.4
Section titled “Forge 1.17.1 to 1.19.4, Fabric 1.17 to 1.19.4”Uses:
ServerPlayer.teleportToEntity.getLookAngleVec3.scaleItemCooldowns.addCooldown
src/main/java/com/example/examplemod/item/BlinkStaffItem.java
package com.example.examplemod.item;
import net.minecraft.server.level.ServerPlayer;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.Vec3;
// Blink Staff: right-click to jump 8.0F blocks the way you look. use runs on both sides; the move happens on the server.public class BlinkStaffItem extends Item { public BlinkStaffItem(Item.Properties properties) { super(properties); }
@Override public InteractionResultHolder<ItemStack> use(Level level, Player player, InteractionHand hand) { if (player instanceof ServerPlayer serverPlayer) { Vec3 target = serverPlayer.position().add(serverPlayer.getLookAngle().scale(8.0F)); serverPlayer.teleportTo(serverPlayer.getLevel(), target.x, target.y, target.z, serverPlayer.getYRot(), serverPlayer.getXRot()); serverPlayer.getCooldowns().addCooldown(this, 20); } return InteractionResultHolder.sidedSuccess(player.getItemInHand(hand), level.isClientSide()); }}It’s registered like any other item, as BlinkStaffItem: see a new item.
Forge 1.16.5
Section titled “Forge 1.16.5”Uses:
ServerPlayerEntity.teleportToEntity.getLookAngleVector3d.scaleCooldownTracker.addCooldown
src/main/java/com/example/examplemod/item/BlinkStaffItem.java
package com.example.examplemod.item;
import net.minecraft.entity.player.ServerPlayerEntity;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.World;import net.minecraft.util.math.vector.Vector3d;
// Blink Staff: right-click to jump 8.0F blocks the way you look. use runs on both sides; the move happens on the server.public class BlinkStaffItem extends Item { public BlinkStaffItem(Item.Properties properties) { super(properties); }
@Override public ActionResult<ItemStack> use(World world, PlayerEntity player, Hand hand) { if (player instanceof ServerPlayerEntity) { ServerPlayerEntity serverPlayer = (ServerPlayerEntity) player; Vector3d target = serverPlayer.position().add(serverPlayer.getLookAngle().scale(8.0F)); serverPlayer.teleportTo(serverPlayer.getLevel(), target.x, target.y, target.z, serverPlayer.yRot, serverPlayer.xRot); serverPlayer.getCooldowns().addCooldown(this, 20); } return ActionResult.sidedSuccess(player.getItemInHand(hand), world.isClientSide()); }}It’s registered like any other item, as BlinkStaffItem: see a new item.
Fabric 1.16.5
Section titled “Fabric 1.16.5”Uses:
ServerPlayer.teleportToEntity.getLookAngleVec3.scaleItemCooldowns.addCooldown
src/main/java/com/example/examplemod/item/BlinkStaffItem.java
package com.example.examplemod.item;
import net.minecraft.server.level.ServerPlayer;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.Vec3;
// Blink Staff: right-click to jump 8.0F blocks the way you look. use runs on both sides; the move happens on the server.public class BlinkStaffItem extends Item { public BlinkStaffItem(Item.Properties properties) { super(properties); }
@Override public InteractionResultHolder<ItemStack> use(Level level, Player player, InteractionHand hand) { if (player instanceof ServerPlayer) { ServerPlayer serverPlayer = (ServerPlayer) player; Vec3 target = serverPlayer.position().add(serverPlayer.getLookAngle().scale(8.0F)); serverPlayer.teleportTo(serverPlayer.getLevel(), target.x, target.y, target.z, serverPlayer.yRot, serverPlayer.xRot); serverPlayer.getCooldowns().addCooldown(this, 20); } return InteractionResultHolder.sidedSuccess(player.getItemInHand(hand), level.isClientSide()); }}It’s registered like any other item, as BlinkStaffItem: see a new item.
Forge 1.12.2
Section titled “Forge 1.12.2”Uses:
EntityPlayerMP.teleportToEntity.getLookAngleVec3d.scaleCooldownTracker.addCooldown
In 1.12.2 the call is player.setPositionAndUpdate(x, y, z).
src/main/java/com/example/examplemod/item/BlinkStaffItem.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.Vec3d;import net.minecraft.world.World;
// Blink Staff: right-click to jump 8.0F blocks the way you look. onItemRightClick runs on both sides; the move happens on the server.public class BlinkStaffItem extends Item { @Override public ActionResult<ItemStack> onItemRightClick(World world, EntityPlayer player, EnumHand hand) { if (!world.isRemote) { Vec3d target = player.getPositionVector().add(player.getLookVec().scale(8.0F)); player.setPositionAndUpdate(target.x, target.y, target.z); player.getCooldownTracker().setCooldown(this, 20); } return new ActionResult<>(EnumActionResult.SUCCESS, player.getHeldItem(hand)); }}It’s registered like any other item, as BlinkStaffItem: 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
- https://docs.neoforged.net/docs/1.21.1/entities/index
Related how-tos
Section titled “Related how-tos”- An item that launches you into the air
- An item that explodes where you aim
- An item that fills the hunger bar
- An item that gives the player another item
- An item that heals you when you use it
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.