Skip to content

How to change Minecraft's code with a mixin

A mixin changes one of Minecraft’s classes as it loads: @Mixin(TheClass.class) on an abstract class in your mixin package, listed in your mixins.json, and @Inject(method = “…”, at = @At(“TAIL”)) on a method that runs at that point. Use an event instead when the loader has one; a mixin is for what no event covers. Prefix your methods with your mod id so they don’t clash.

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. Find the method to hook with Linen’s decompiled sources.
  2. Write the mixin class in your mixin package with @Mixin and @Inject.
  3. Add its name to the mixins list of your mixins.json.
  4. Run the game and check the log for mixin errors.

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

Setting In the example What it does
message Hop! What the player sees when they jump.

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, Fabric and NeoForge 1.21.2 to 1.21.11, 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, Fabric and NeoForge 1.21.2 to 1.21.11, Forge 1.20 to 1.20.1, Fabric 1.20 to 1.20.3”

Uses:

  • @Mixin
  • @Inject
  • @At("TAIL")
  • CallbackInfo
  • LivingEntity.jumpFromGround

src/main/java/com/example/examplemod/mixin/JumpNoticeMixin.java

package com.example.examplemod.mixin;
import net.minecraft.network.chat.Component;
import net.minecraft.world.entity.LivingEntity;
import net.minecraft.world.entity.player.Player;
import org.spongepowered.asm.mixin.Mixin;
import org.spongepowered.asm.mixin.injection.At;
import org.spongepowered.asm.mixin.injection.Inject;
import org.spongepowered.asm.mixin.injection.callback.CallbackInfo;
// Runs at the end of LivingEntity#jumpFromGround, for every living entity that jumps; listed in examplemod.mixins.json.
@Mixin(LivingEntity.class)
public abstract class JumpNoticeMixin {
@Inject(method = "jumpFromGround", at = @At("TAIL"))
private void examplemod$afterJump(CallbackInfo info) {
if ((Object) this instanceof Player player && !player.level().isClientSide()) {
player.displayClientMessage(Component.literal("Hop!"), true);
}
}
}

In src/main/resources/examplemod.mixins.json, add this to the list at /mixins:

"JumpNoticeMixin"

Uses:

  • @Mixin
  • @Inject
  • @At("TAIL")
  • CallbackInfo
  • LivingEntity.jumpFromGround

Since 26.1 displayClientMessage(text, true) is sendOverlayMessage(text) (the action bar).

src/main/java/com/example/examplemod/mixin/JumpNoticeMixin.java

package com.example.examplemod.mixin;
import net.minecraft.network.chat.Component;
import net.minecraft.world.entity.LivingEntity;
import net.minecraft.world.entity.player.Player;
import org.spongepowered.asm.mixin.Mixin;
import org.spongepowered.asm.mixin.injection.At;
import org.spongepowered.asm.mixin.injection.Inject;
import org.spongepowered.asm.mixin.injection.callback.CallbackInfo;
// Runs at the end of LivingEntity#jumpFromGround, for every living entity that jumps; listed in examplemod.mixins.json.
@Mixin(LivingEntity.class)
public abstract class JumpNoticeMixin {
@Inject(method = "jumpFromGround", at = @At("TAIL"))
private void examplemod$afterJump(CallbackInfo info) {
if ((Object) this instanceof Player player && !player.level().isClientSide()) {
player.sendOverlayMessage(Component.literal("Hop!"));
}
}
}

In src/main/resources/examplemod.mixins.json, add this to the list at /mixins:

"JumpNoticeMixin"

Forge 1.17.1 to 1.18.2, Fabric 1.18 to 1.18.2

Section titled “Forge 1.17.1 to 1.18.2, Fabric 1.18 to 1.18.2”

Uses:

  • @Mixin
  • @Inject
  • @At("TAIL")
  • CallbackInfo
  • LivingEntity.jumpFromGround

src/main/java/com/example/examplemod/mixin/JumpNoticeMixin.java

package com.example.examplemod.mixin;
import net.minecraft.network.chat.TextComponent;
import net.minecraft.network.chat.Component;
import net.minecraft.world.entity.LivingEntity;
import net.minecraft.world.entity.player.Player;
import org.spongepowered.asm.mixin.Mixin;
import org.spongepowered.asm.mixin.injection.At;
import org.spongepowered.asm.mixin.injection.Inject;
import org.spongepowered.asm.mixin.injection.callback.CallbackInfo;
// Runs at the end of LivingEntity#jumpFromGround, for every living entity that jumps; listed in examplemod.mixins.json.
@Mixin(LivingEntity.class)
public abstract class JumpNoticeMixin {
@Inject(method = "jumpFromGround", at = @At("TAIL"))
private void examplemod$afterJump(CallbackInfo info) {
if ((Object) this instanceof Player player && !player.level.isClientSide()) {
player.displayClientMessage(new TextComponent("Hop!"), true);
}
}
}

In src/main/resources/examplemod.mixins.json, add this to the list at /mixins:

"JumpNoticeMixin"

Forge 1.19 to 1.19.4, Fabric 1.19 to 1.19.4

Section titled “Forge 1.19 to 1.19.4, Fabric 1.19 to 1.19.4”

Uses:

  • @Mixin
  • @Inject
  • @At("TAIL")
  • CallbackInfo
  • LivingEntity.jumpFromGround

src/main/java/com/example/examplemod/mixin/JumpNoticeMixin.java

package com.example.examplemod.mixin;
import net.minecraft.network.chat.Component;
import net.minecraft.world.entity.LivingEntity;
import net.minecraft.world.entity.player.Player;
import org.spongepowered.asm.mixin.Mixin;
import org.spongepowered.asm.mixin.injection.At;
import org.spongepowered.asm.mixin.injection.Inject;
import org.spongepowered.asm.mixin.injection.callback.CallbackInfo;
// Runs at the end of LivingEntity#jumpFromGround, for every living entity that jumps; listed in examplemod.mixins.json.
@Mixin(LivingEntity.class)
public abstract class JumpNoticeMixin {
@Inject(method = "jumpFromGround", at = @At("TAIL"))
private void examplemod$afterJump(CallbackInfo info) {
if ((Object) this instanceof Player player && !player.level.isClientSide()) {
player.displayClientMessage(Component.literal("Hop!"), true);
}
}
}

In src/main/resources/examplemod.mixins.json, add this to the list at /mixins:

"JumpNoticeMixin"

Uses:

  • @Mixin
  • @Inject
  • @At("TAIL")
  • CallbackInfo
  • LivingEntity.jumpFromGround

src/main/java/com/example/examplemod/mixin/JumpNoticeMixin.java

package com.example.examplemod.mixin;
import net.minecraft.network.chat.TextComponent;
import net.minecraft.network.chat.Component;
import net.minecraft.world.entity.LivingEntity;
import net.minecraft.world.entity.player.Player;
import org.spongepowered.asm.mixin.Mixin;
import org.spongepowered.asm.mixin.injection.At;
import org.spongepowered.asm.mixin.injection.Inject;
import org.spongepowered.asm.mixin.injection.callback.CallbackInfo;
// Runs at the end of LivingEntity#jumpFromGround, for every living entity that jumps; listed in examplemod.mixins.json.
@Mixin(LivingEntity.class)
public abstract class JumpNoticeMixin {
@Inject(method = "jumpFromGround", at = @At("TAIL"))
private void examplemod$afterJump(CallbackInfo info) {
if ((Object) this instanceof Player) {
Player player = (Player) (Object) this;
if (!player.level.isClientSide()) {
player.displayClientMessage(new TextComponent("Hop!"), true);
}
}
}
}

In src/main/resources/examplemod.mixins.json, add this to the list at /mixins:

"JumpNoticeMixin"

Uses:

  • @Mixin
  • @Inject
  • @At("TAIL")
  • CallbackInfo
  • LivingEntity.jumpFromGround

src/main/java/com/example/examplemod/mixin/JumpNoticeMixin.java

package com.example.examplemod.mixin;
import net.minecraft.util.text.StringTextComponent;
import net.minecraft.entity.LivingEntity;
import net.minecraft.entity.player.PlayerEntity;
import org.spongepowered.asm.mixin.Mixin;
import org.spongepowered.asm.mixin.injection.At;
import org.spongepowered.asm.mixin.injection.Inject;
import org.spongepowered.asm.mixin.injection.callback.CallbackInfo;
// Runs at the end of LivingEntity#jumpFromGround, for every living entity that jumps; listed in examplemod.mixins.json.
@Mixin(LivingEntity.class)
public abstract class JumpNoticeMixin {
@Inject(method = "jumpFromGround", at = @At("TAIL"))
private void examplemod$afterJump(CallbackInfo info) {
if ((Object) this instanceof PlayerEntity && !((PlayerEntity) (Object) this).level.isClientSide()) {
PlayerEntity player = (PlayerEntity) (Object) this;
player.displayClientMessage(new StringTextComponent("Hop!"), true);
}
}
}

In src/main/resources/examplemod.mixins.json, add this to the list at /mixins:

"JumpNoticeMixin"

Uses:

  • @Mixin
  • @Inject
  • @At("TAIL")
  • CallbackInfo
  • EntityLivingBase.jumpFromGround

src/main/java/com/example/examplemod/mixin/JumpNoticeMixin.java

package com.example.examplemod.mixin;
import net.minecraft.entity.EntityLivingBase;
import net.minecraft.entity.player.EntityPlayer;
import net.minecraft.util.text.TextComponentString;
import org.spongepowered.asm.mixin.Mixin;
import org.spongepowered.asm.mixin.injection.At;
import org.spongepowered.asm.mixin.injection.Inject;
import org.spongepowered.asm.mixin.injection.callback.CallbackInfo;
// Jump Notice: runs after a living entity jumps. A mixin is injected into the game's own class; it needs MixinBooter on 1.12.2.
@Mixin(EntityLivingBase.class)
public abstract class JumpNoticeMixin {
@Inject(method = "jump", at = @At("TAIL"))
private void examplemod$afterJump(CallbackInfo info) {
if ((Object) this instanceof EntityPlayer && !((EntityPlayer) (Object) this).world.isRemote) {
((EntityPlayer) (Object) this).sendStatusMessage(new TextComponentString("Hop!"), true);
}
}
}

In src/main/resources/examplemod.mixins.json, add this to the list at /mixins:

"JumpNoticeMixin"

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.