How to share code between Fabric and NeoForge (Minecraft mod)
Porting is mostly sorting. Code that uses only Minecraft’s own classes (blocks, items, recipes, your game rules) runs on every loader of the same Minecraft version, so keep it in plain classes. Three things differ: how you register things, how you listen to events, and the metadata file (fabric.mod.json, neoforge.mods.toml, or mods.toml on Forge). Keep those in thin loader classes that call your shared classes. A project with several loaders keeps the shared classes in a common module.
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.
- Move every class that imports only net.minecraft classes into your shared code.
- Leave registration, events and the metadata file in each loader’s module.
- Make each loader’s entry point call one shared method.
- Build for each loader and fix the imports that do not exist there.
- Check the metadata: mod id, version, dependencies and entry points in each loader’s file.
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
Section titled “Fabric and NeoForge 1.20.4 to 1.21.1, NeoForge 1.20.1 to 1.20.2”Uses:
Component.literalServerPlayer.sendSystemMessage
src/main/java/com/example/examplemod/common/SharedLogic.java
package com.example.examplemod.common;
import net.minecraft.network.chat.Component;import net.minecraft.server.level.ServerPlayer;
// Shared Logic: shared code. It imports only net.minecraft classes, so Fabric and NeoForge can both call it.// Each loader's own class makes one call from its own event, for example SharedLogic.greet(player).public final class SharedLogic { private SharedLogic() { }
public static void greet(ServerPlayer player) { player.sendSystemMessage(Component.literal("Welcome, " + player.getName().getString() + "!")); }}Fabric and NeoForge 1.21.2 to 26.3, Fabric 1.19 to 1.20.3
Section titled “Fabric and NeoForge 1.21.2 to 26.3, Fabric 1.19 to 1.20.3”Uses:
Component.literalServerPlayer.sendSystemMessage
src/main/java/com/example/examplemod/common/SharedLogic.java
package com.example.examplemod.common;
import net.minecraft.network.chat.Component;import net.minecraft.server.level.ServerPlayer;
// Shared Logic: shared code. It imports only net.minecraft classes, so Fabric and NeoForge can both call it.// Each loader's own class makes one call from its own event, for example SharedLogic.greet(player).public final class SharedLogic { private SharedLogic() { }
public static void greet(ServerPlayer player) { player.sendSystemMessage(Component.literal("Welcome, " + player.getName().getString() + "!")); }}Forge 1.17.1 to 1.18.2
Section titled “Forge 1.17.1 to 1.18.2”Uses:
Component.literalServerPlayer.sendSystemMessage
src/main/java/com/example/examplemod/common/SharedLogic.java
package com.example.examplemod.common;
import net.minecraft.Util;import net.minecraft.network.chat.TextComponent;import net.minecraft.network.chat.Component;import net.minecraft.server.level.ServerPlayer;
// Shared Logic: shared code. It imports only net.minecraft classes, so any loader can call it.// Each loader's own class makes one call from its own event, for example SharedLogic.greet(player).public final class SharedLogic { private SharedLogic() { }
public static void greet(ServerPlayer player) { player.sendMessage(new TextComponent("Welcome, " + player.getName().getString() + "!"), Util.NIL_UUID); }}Forge 1.19 to 1.20.1
Section titled “Forge 1.19 to 1.20.1”Uses:
Component.literalServerPlayer.sendSystemMessage
src/main/java/com/example/examplemod/common/SharedLogic.java
package com.example.examplemod.common;
import net.minecraft.network.chat.Component;import net.minecraft.server.level.ServerPlayer;
// Shared Logic: shared code. It imports only net.minecraft classes, so any loader can call it.// Each loader's own class makes one call from its own event, for example SharedLogic.greet(player).public final class SharedLogic { private SharedLogic() { }
public static void greet(ServerPlayer player) { player.sendSystemMessage(Component.literal("Welcome, " + player.getName().getString() + "!")); }}Fabric 1.16.5 to 1.18.2
Section titled “Fabric 1.16.5 to 1.18.2”Uses:
Component.literalServerPlayer.sendSystemMessage
src/main/java/com/example/examplemod/common/SharedLogic.java
package com.example.examplemod.common;
import net.minecraft.Util;import net.minecraft.network.chat.TextComponent;import net.minecraft.network.chat.Component;import net.minecraft.server.level.ServerPlayer;
// Shared Logic: shared code. It imports only net.minecraft classes, so Fabric and NeoForge can both call it.// Each loader's own class makes one call from its own event, for example SharedLogic.greet(player).public final class SharedLogic { private SharedLogic() { }
public static void greet(ServerPlayer player) { player.sendMessage(new TextComponent("Welcome, " + player.getName().getString() + "!"), Util.NIL_UUID); }}Forge 1.16.5
Section titled “Forge 1.16.5”Uses:
StringTextComponentServerPlayerEntity.sendMessage
src/main/java/com/example/examplemod/common/SharedLogic.java
package com.example.examplemod.common;
import net.minecraft.util.Util;import net.minecraft.util.text.StringTextComponent;
import net.minecraft.entity.player.ServerPlayerEntity;
// Shared Logic: shared code. It imports only net.minecraft classes, so any loader can call it.// Each loader's own class makes one call from its own event, for example SharedLogic.greet(player).public final class SharedLogic { private SharedLogic() { }
public static void greet(ServerPlayerEntity player) { player.sendMessage(new StringTextComponent("Welcome, " + player.getName().getString() + "!"), Util.NIL_UUID); }}Forge 1.12.2
Section titled “Forge 1.12.2”Uses:
TextComponentStringEntityPlayerMP.sendMessage
src/main/java/com/example/examplemod/common/SharedLogic.java
package com.example.examplemod.common;
import net.minecraft.entity.player.EntityPlayerMP;import net.minecraft.util.text.TextComponentString;
// Shared Logic: shared code. It imports only net.minecraft classes, so any loader can call it.// Each loader's own class makes one call from its own event, for example SharedLogic.greet(player).public final class SharedLogic { private SharedLogic() { }
public static void greet(EntityPlayerMP player) { player.sendMessage(new TextComponentString("Welcome, " + player.getName() + "!")); }}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”- Find out why a mod is not in the mod list
- A mob that drops an item
- Add drops to Minecraft’s loot
- Publish my mod on Modrinth or CurseForge
- Do something when a player joins
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.