Skip to content

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.

  1. Move every class that imports only net.minecraft classes into your shared code.
  2. Leave registration, events and the metadata file in each loader’s module.
  3. Make each loader’s entry point call one shared method.
  4. Build for each loader and fix the imports that do not exist there.
  5. Check the metadata: mod id, version, dependencies and entry points in each loader’s file.

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.literal
  • ServerPlayer.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.literal
  • ServerPlayer.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() + "!"));
}
}

Uses:

  • Component.literal
  • ServerPlayer.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);
}
}

Uses:

  • Component.literal
  • ServerPlayer.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() + "!"));
}
}

Uses:

  • Component.literal
  • ServerPlayer.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);
}
}

Uses:

  • StringTextComponent
  • ServerPlayerEntity.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);
}
}

Uses:

  • TextComponentString
  • EntityPlayerMP.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() + "!"));
}
}

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.