How to ride a mob in a Minecraft mod
Riding is one entity being a passenger of another: player.startRiding(vehicle). Vanilla does it for boats and saddled animals. To make any mob rideable, listen for a right-click on an entity and call startRiding on the server. Steering comes from the vehicle: a mob that is not a Saddleable or a controllable type does not follow the player, it just carries them. For your own mob, override getControllingPassenger and travel.
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.
- Listen for the player-uses-entity event.
- Check the side, the hand and the mob type.
- Call player.startRiding(entity) and mark the event handled.
- Press Shift to get off.
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 |
|---|---|---|
| mob | PIG |
The mob that can be ridden. |
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 1.16.5 to 1.21.1, Fabric 1.21.2 to 1.21.8
Section titled “Fabric 1.16.5 to 1.21.1, Fabric 1.21.2 to 1.21.8”Uses:
UseEntityCallback.EVENTEntity.startRidingEntity.isPassenger
src/main/java/com/example/examplemod/event/MobRiderRiding.java
package com.example.examplemod.event;
import net.fabricmc.fabric.api.event.player.UseEntityCallback;import net.minecraft.world.InteractionHand;import net.minecraft.world.InteractionResult;import net.minecraft.world.entity.EntityType;
// Right-click a PIG with an empty hand to ride it (Fabric API: UseEntityCallback).public final class MobRiderRiding { private MobRiderRiding() { }
// Called from the mod's onInitialize. public static void register() { UseEntityCallback.EVENT.register((player, world, hand, entity, hit) -> { if (!world.isClientSide && hand == InteractionHand.MAIN_HAND && player.getMainHandItem().isEmpty() && entity.getType() == EntityType.PIG && !player.isPassenger()) { player.startRiding(entity); return InteractionResult.SUCCESS; } return InteractionResult.PASS; }); }}In your main mod class, inside onInitialize:
MobRiderRiding.register();NeoForge 1.20.2 to 1.21.1, NeoForge 1.21.2 to 26.1.2
Section titled “NeoForge 1.20.2 to 1.21.1, NeoForge 1.21.2 to 26.1.2”Uses:
PlayerInteractEvent.EntityInteractEntity.startRidingEntity.isPassenger
src/main/java/com/example/examplemod/event/MobRiderRiding.java
package com.example.examplemod.event;
import net.minecraft.world.InteractionHand;import net.minecraft.world.InteractionResult;import net.minecraft.world.entity.EntityType;import net.neoforged.neoforge.common.NeoForge;import net.neoforged.neoforge.event.entity.player.PlayerInteractEvent;
// Right-click a PIG with an empty hand to ride it (NeoForge: PlayerInteractEvent.EntityInteract).public final class MobRiderRiding { private MobRiderRiding() { }
// Called from the mod's constructor. public static void register() { NeoForge.EVENT_BUS.addListener(MobRiderRiding::onInteract); }
private static void onInteract(PlayerInteractEvent.EntityInteract event) { if (!event.getLevel().isClientSide() && event.getHand() == InteractionHand.MAIN_HAND && event.getEntity().getMainHandItem().isEmpty() && event.getTarget().getType() == EntityType.PIG && !event.getEntity().isPassenger()) { event.getEntity().startRiding(event.getTarget()); event.setCancellationResult(InteractionResult.SUCCESS); event.setCanceled(true); } }}In your main mod class, inside <init>:
MobRiderRiding.register();Fabric 1.21.9 to 26.1.2
Section titled “Fabric 1.21.9 to 26.1.2”Uses:
UseEntityCallback.EVENTEntity.startRidingEntity.isPassenger
src/main/java/com/example/examplemod/event/MobRiderRiding.java
package com.example.examplemod.event;
import net.fabricmc.fabric.api.event.player.UseEntityCallback;import net.minecraft.world.InteractionHand;import net.minecraft.world.InteractionResult;import net.minecraft.world.entity.EntityType;
// Right-click a PIG with an empty hand to ride it (Fabric API: UseEntityCallback).public final class MobRiderRiding { private MobRiderRiding() { }
// Called from the mod's onInitialize. public static void register() { UseEntityCallback.EVENT.register((player, world, hand, entity, hit) -> { if (!world.isClientSide() && hand == InteractionHand.MAIN_HAND && player.getMainHandItem().isEmpty() && entity.getType() == EntityType.PIG && !player.isPassenger()) { player.startRiding(entity); return InteractionResult.SUCCESS; } return InteractionResult.PASS; }); }}In your main mod class, inside onInitialize:
MobRiderRiding.register();NeoForge 26.2 to 26.3
Section titled “NeoForge 26.2 to 26.3”Uses:
PlayerInteractEvent.EntityInteractEntity.startRidingEntity.isPassenger
src/main/java/com/example/examplemod/event/MobRiderRiding.java
package com.example.examplemod.event;
import net.minecraft.world.InteractionHand;import net.minecraft.world.InteractionResult;import net.minecraft.world.entity.EntityTypes;import net.neoforged.neoforge.common.NeoForge;import net.neoforged.neoforge.event.entity.player.PlayerInteractEvent;
// Right-click a PIG with an empty hand to ride it (NeoForge: PlayerInteractEvent.EntityInteract).public final class MobRiderRiding { private MobRiderRiding() { }
// Called from the mod's constructor. public static void register() { NeoForge.EVENT_BUS.addListener(MobRiderRiding::onInteract); }
private static void onInteract(PlayerInteractEvent.EntityInteract event) { if (!event.getLevel().isClientSide() && event.getHand() == InteractionHand.MAIN_HAND && event.getEntity().getMainHandItem().isEmpty() && event.getTarget().getType() == EntityTypes.PIG && !event.getEntity().isPassenger()) { event.getEntity().startRiding(event.getTarget()); event.setCancellationResult(InteractionResult.SUCCESS); event.setCanceled(true); } }}In your main mod class, inside <init>:
MobRiderRiding.register();Fabric 26.2 to 26.3
Section titled “Fabric 26.2 to 26.3”Uses:
UseEntityCallback.EVENTEntity.startRidingEntity.isPassenger
src/main/java/com/example/examplemod/event/MobRiderRiding.java
package com.example.examplemod.event;
import net.fabricmc.fabric.api.event.player.UseEntityCallback;import net.minecraft.world.InteractionHand;import net.minecraft.world.InteractionResult;import net.minecraft.world.entity.EntityTypes;
// Right-click a PIG with an empty hand to ride it (Fabric API: UseEntityCallback).public final class MobRiderRiding { private MobRiderRiding() { }
// Called from the mod's onInitialize. public static void register() { UseEntityCallback.EVENT.register((player, world, hand, entity, hit) -> { if (!world.isClientSide() && hand == InteractionHand.MAIN_HAND && player.getMainHandItem().isEmpty() && entity.getType() == EntityTypes.PIG && !player.isPassenger()) { player.startRiding(entity); return InteractionResult.SUCCESS; } return InteractionResult.PASS; }); }}In your main mod class, inside onInitialize:
MobRiderRiding.register();Forge 1.17.1 to 1.18.2
Section titled “Forge 1.17.1 to 1.18.2”Uses:
PlayerInteractEvent.EntityInteractEntity.startRidingEntity.isPassenger
src/main/java/com/example/examplemod/event/MobRiderRiding.java
package com.example.examplemod.event;
import net.minecraft.world.InteractionHand;import net.minecraft.world.InteractionResult;import net.minecraft.world.entity.EntityType;import net.minecraftforge.common.MinecraftForge;import net.minecraftforge.event.entity.player.PlayerInteractEvent;
// Right-click a PIG with an empty hand to ride it (Forge: PlayerInteractEvent.EntityInteract).public final class MobRiderRiding { private MobRiderRiding() { }
// Called from the mod's constructor. public static void register() { MinecraftForge.EVENT_BUS.addListener(MobRiderRiding::onInteract); }
private static void onInteract(PlayerInteractEvent.EntityInteract event) { if (!event.getWorld().isClientSide() && event.getHand() == InteractionHand.MAIN_HAND && event.getPlayer().getMainHandItem().isEmpty() && event.getTarget().getType() == EntityType.PIG && !event.getPlayer().isPassenger()) { event.getPlayer().startRiding(event.getTarget()); event.setCancellationResult(InteractionResult.SUCCESS); event.setCanceled(true); } }}In your main mod class, inside <init>:
MobRiderRiding.register();Forge 1.19 to 1.20.1
Section titled “Forge 1.19 to 1.20.1”Uses:
PlayerInteractEvent.EntityInteractEntity.startRidingEntity.isPassenger
src/main/java/com/example/examplemod/event/MobRiderRiding.java
package com.example.examplemod.event;
import net.minecraft.world.InteractionHand;import net.minecraft.world.InteractionResult;import net.minecraft.world.entity.EntityType;import net.minecraftforge.common.MinecraftForge;import net.minecraftforge.event.entity.player.PlayerInteractEvent;
// Right-click a PIG with an empty hand to ride it (Forge: PlayerInteractEvent.EntityInteract).public final class MobRiderRiding { private MobRiderRiding() { }
// Called from the mod's constructor. public static void register() { MinecraftForge.EVENT_BUS.addListener(MobRiderRiding::onInteract); }
private static void onInteract(PlayerInteractEvent.EntityInteract event) { if (!event.getLevel().isClientSide() && event.getHand() == InteractionHand.MAIN_HAND && event.getEntity().getMainHandItem().isEmpty() && event.getTarget().getType() == EntityType.PIG && !event.getEntity().isPassenger()) { event.getEntity().startRiding(event.getTarget()); event.setCancellationResult(InteractionResult.SUCCESS); event.setCanceled(true); } }}In your main mod class, inside <init>:
MobRiderRiding.register();NeoForge 1.20.1
Section titled “NeoForge 1.20.1”Uses:
PlayerInteractEvent.EntityInteractEntity.startRidingEntity.isPassenger
src/main/java/com/example/examplemod/event/MobRiderRiding.java
package com.example.examplemod.event;
import net.minecraft.world.InteractionHand;import net.minecraft.world.InteractionResult;import net.minecraft.world.entity.EntityType;import net.minecraftforge.common.MinecraftForge;import net.minecraftforge.event.entity.player.PlayerInteractEvent;
// Right-click a PIG with an empty hand to ride it (NeoForge: PlayerInteractEvent.EntityInteract).public final class MobRiderRiding { private MobRiderRiding() { }
// Called from the mod's constructor. public static void register() { MinecraftForge.EVENT_BUS.addListener(MobRiderRiding::onInteract); }
private static void onInteract(PlayerInteractEvent.EntityInteract event) { if (!event.getLevel().isClientSide() && event.getHand() == InteractionHand.MAIN_HAND && event.getEntity().getMainHandItem().isEmpty() && event.getTarget().getType() == EntityType.PIG && !event.getEntity().isPassenger()) { event.getEntity().startRiding(event.getTarget()); event.setCancellationResult(InteractionResult.SUCCESS); event.setCanceled(true); } }}In your main mod class, inside <init>:
MobRiderRiding.register();Forge 1.16.5
Section titled “Forge 1.16.5”Uses:
PlayerInteractEvent.EntityInteractEntity.startRidingEntity.isPassenger
src/main/java/com/example/examplemod/event/MobRiderRiding.java
package com.example.examplemod.event;
import net.minecraft.util.Hand;import net.minecraft.util.ActionResultType;import net.minecraft.entity.EntityType;import net.minecraftforge.common.MinecraftForge;import net.minecraftforge.event.entity.player.PlayerInteractEvent;
// Right-click a PIG with an empty hand to ride it (Forge: PlayerInteractEvent.EntityInteract).public final class MobRiderRiding { private MobRiderRiding() { }
// Called from the mod's constructor. public static void register() { MinecraftForge.EVENT_BUS.addListener(MobRiderRiding::onInteract); }
private static void onInteract(PlayerInteractEvent.EntityInteract event) { if (!event.getWorld().isClientSide() && event.getHand() == Hand.MAIN_HAND && event.getPlayer().getMainHandItem().isEmpty() && event.getTarget().getType() == EntityType.PIG && !event.getPlayer().isPassenger()) { event.getPlayer().startRiding(event.getTarget()); event.setCancellationResult(ActionResultType.SUCCESS); event.setCanceled(true); } }}In your main mod class, inside <init>:
MobRiderRiding.register();Forge 1.12.2
Section titled “Forge 1.12.2”Uses:
PlayerInteractEvent.EntityInteractEntity.startRidingEntity.isPassenger
src/main/java/com/example/examplemod/event/MobRiderRiding.java
package com.example.examplemod.event;
import com.example.examplemod.ExampleMod;import net.minecraft.entity.EntityList;import net.minecraft.util.EnumHand;import net.minecraft.util.ResourceLocation;import net.minecraftforge.event.entity.player.PlayerInteractEvent;import net.minecraftforge.fml.common.Mod;import net.minecraftforge.fml.common.eventhandler.SubscribeEvent;
// Right-click a PIG with an empty hand to ride it (Forge: PlayerInteractEvent.EntityInteract).@Mod.EventBusSubscriber(modid = ExampleMod.MOD_ID)public final class MobRiderRiding { private MobRiderRiding() { }
@SubscribeEvent public static void onInteract(PlayerInteractEvent.EntityInteract event) { ResourceLocation target = EntityList.getKey(event.getTarget()); if (!event.getWorld().isRemote && event.getHand() == EnumHand.MAIN_HAND && event.getEntityPlayer().getHeldItemMainhand().isEmpty() && new ResourceLocation("minecraft", "PIG".toLowerCase()).equals(target) && !event.getEntityPlayer().isRiding()) { event.getEntityPlayer().startRiding(event.getTarget()); event.setCanceled(true); } }}Sources
Section titled “Sources”- https://docs.fabricmc.net/1.21.1/develop/events
- https://docs.neoforged.net/docs/1.21.1/concepts/events
- https://maven.fabricmc.net/docs/fabric-api-0.116.17+1.21.1/net/fabricmc/fabric/api/event/player/UseEntityCallback.html
Related how-tos
Section titled “Related how-tos”- Give a mob a new AI goal
- An item you use on a mob or player
- Give players items when they first join
- Add a villager trade
- A command that runs on the client
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.