Skip to content

Commit

Permalink
Add Java-based core mods
Browse files Browse the repository at this point in the history
Mods must ship a separate Jar-file (for example via jar-in-jar) and mark it as FMLModType: LIBRARY to make it load above the GAME layer. They then must provide an implementation of ICoreMod via the Java ServiceLoader to contribute their transformers.
  • Loading branch information
shartte committed Jan 21, 2024
1 parent 697a95e commit a239796
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,13 @@
package net.neoforged.fml.loading;

import com.mojang.logging.LogUtils;
import cpw.mods.modlauncher.Launcher;
import cpw.mods.modlauncher.api.*;
import joptsimple.ArgumentAcceptingOptionSpec;
import joptsimple.OptionSpecBuilder;
import net.neoforged.fml.loading.moddiscovery.ModFile;
import net.neoforged.neoforgespi.Environment;
import net.neoforged.neoforgespi.coremod.ICoreMod;
import org.jetbrains.annotations.NotNull;
import org.slf4j.Logger;

Expand All @@ -20,6 +22,7 @@
import java.util.List;
import java.util.Locale;
import java.util.Map;
import java.util.ServiceLoader;
import java.util.Set;
import java.util.function.BiFunction;
import static net.neoforged.fml.loading.LogMarkers.CORE;
Expand Down Expand Up @@ -134,7 +137,23 @@ public void argumentValues(OptionResult option)
public @NotNull List<ITransformer> transformers()
{
LOGGER.debug(CORE, "Loading coremod transformers");
return new ArrayList<>(FMLLoader.getCoreModProvider().getCoreModTransformers());
var result = new ArrayList<ITransformer>(FMLLoader.getCoreModProvider().getCoreModTransformers());

// Find all Java core mods
var pluginLayer = Launcher.INSTANCE.findLayerManager()
.flatMap(m -> m.getLayer(IModuleLayerManager.Layer.PLUGIN))
.orElseThrow();
for (var coreMod : ServiceLoader.load(pluginLayer, ICoreMod.class)) {
for (var transformer : coreMod.getTransformers()) {
if (transformer == null) {
throw new IllegalStateException("Core mod " + coreMod + " is trying to add null transformer");
}
LOGGER.debug("Adding {} transformer from core-mod {}", transformer.targets(), coreMod);
result.add(transformer);
}
}

return result;
}

}
10 changes: 10 additions & 0 deletions spi/src/main/java/net/neoforged/neoforgespi/coremod/ICoreMod.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
package net.neoforged.neoforgespi.coremod;

import cpw.mods.modlauncher.api.ITransformer;

/**
* Provide using the Java {@link java.util.ServiceLoader} mechanism.
*/
public interface ICoreMod {
Iterable<? extends ITransformer<?>> getTransformers();
}

0 comments on commit a239796

Please sign in to comment.