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.
- Find the method to hook with Linen’s decompiled sources.
- Write the mixin class in your mixin package with @Mixin and @Inject.
- Add its name to the mixins list of your mixins.json.
- Run the game and check the log for mixin errors.
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 |
|---|---|---|
| message | Hop! |
What the player sees when they jump. |
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, 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")CallbackInfoLivingEntity.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"Fabric and NeoForge 26.1 to 26.3
Section titled “Fabric and NeoForge 26.1 to 26.3”Uses:
@Mixin@Inject@At("TAIL")CallbackInfoLivingEntity.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")CallbackInfoLivingEntity.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")CallbackInfoLivingEntity.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"Fabric 1.16.5 to 1.17.1
Section titled “Fabric 1.16.5 to 1.17.1”Uses:
@Mixin@Inject@At("TAIL")CallbackInfoLivingEntity.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"Forge 1.16.5
Section titled “Forge 1.16.5”Uses:
@Mixin@Inject@At("TAIL")CallbackInfoLivingEntity.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"Forge 1.12.2
Section titled “Forge 1.12.2”Uses:
@Mixin@Inject@At("TAIL")CallbackInfoEntityLivingBase.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"Sources
Section titled “Sources”- https://docs.fabricmc.net/1.21.1/develop/events
- https://docs.neoforged.net/docs/1.21.1/gettingstarted/modfiles
Related how-tos
Section titled “Related how-tos”- A block that does something when clicked
- A mob with attributes and a spawn egg
- An item that does something when used
- An item that gives a potion effect
- Give a mob a new AI goal
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.