Skip to content

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.

  1. Listen for the player-uses-entity event.
  2. Check the side, the hand and the mob type.
  3. Call player.startRiding(entity) and mark the event handled.
  4. Press Shift to get off.

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.

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.EVENT
  • Entity.startRiding
  • Entity.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.EntityInteract
  • Entity.startRiding
  • Entity.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();

Uses:

  • UseEntityCallback.EVENT
  • Entity.startRiding
  • Entity.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();

Uses:

  • PlayerInteractEvent.EntityInteract
  • Entity.startRiding
  • Entity.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();

Uses:

  • UseEntityCallback.EVENT
  • Entity.startRiding
  • Entity.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();

Uses:

  • PlayerInteractEvent.EntityInteract
  • Entity.startRiding
  • Entity.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();

Uses:

  • PlayerInteractEvent.EntityInteract
  • Entity.startRiding
  • Entity.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();

Uses:

  • PlayerInteractEvent.EntityInteract
  • Entity.startRiding
  • Entity.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();

Uses:

  • PlayerInteractEvent.EntityInteract
  • Entity.startRiding
  • Entity.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();

Uses:

  • PlayerInteractEvent.EntityInteract
  • Entity.startRiding
  • Entity.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);
}
}
}

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.