Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Remove ModLoadingContext and FMLJavaModLoadingContext #44

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
2 changes: 1 addition & 1 deletion core/src/main/java/net/neoforged/fml/Bindings.java
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ private Bindings() {
this.provider = providers.get(0);
}

public static Supplier<IEventBus> getForgeBus() {
public static IEventBus getForgeBus() {
return INSTANCE.provider.getForgeBusSupplier();
}

Expand Down
5 changes: 1 addition & 4 deletions core/src/main/java/net/neoforged/fml/DeferredWorkQueue.java
Original file line number Diff line number Diff line change
Expand Up @@ -75,12 +75,9 @@ public void runTasks() {
private static void makeRunnable(TaskInfo ti, Executor executor, RuntimeException aggregate) {
executor.execute(() -> {
Stopwatch timer = Stopwatch.createStarted();
ModLoadingContext.get().setActiveContainer(ti.owner);
try {
{
ti.future.exceptionally(t -> captureException(ti.owner.getModId(), aggregate, t));
ti.task.run();
} finally {
ModLoadingContext.get().setActiveContainer(null);
}
timer.stop();
if (timer.elapsed(TimeUnit.SECONDS) >= 1) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
import java.util.function.Supplier;

public interface IBindingsProvider {
Supplier<IEventBus> getForgeBusSupplier();
IEventBus getForgeBusSupplier();
Supplier<I18NParser> getMessageParser();
Supplier<IConfigEvent.ConfigConfig> getConfigConfiguration();
}
14 changes: 0 additions & 14 deletions core/src/main/java/net/neoforged/fml/InterModComms.java
Original file line number Diff line number Diff line change
Expand Up @@ -63,20 +63,6 @@ public final <T> Supplier<T> getMessageSupplier() {

private static ConcurrentMap<String, ConcurrentLinkedQueue<IMCMessage>> containerQueues = new ConcurrentHashMap<>();

/**
* Send IMC to remote. Sender will default to the active modcontainer, or minecraft if not.
*
* @param modId the mod id to send to
* @param method the method name to send
* @param thing the thing associated with the method name
* @return true if the message was enqueued for sending (the target modid is loaded)
*/
public static boolean sendTo(final String modId, final String method, final Supplier<?> thing) {
if (!ModList.get().isLoaded(modId)) return false;
containerQueues.computeIfAbsent(modId, k->new ConcurrentLinkedQueue<>()).add(new IMCMessage(ModLoadingContext.get().getActiveContainer().getModId(), modId, method, thing));
return true;
}

/**
* Send IMC to remote.
*
Expand Down
17 changes: 13 additions & 4 deletions core/src/main/java/net/neoforged/fml/ModContainer.java
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,6 @@ public abstract class ModContainer
protected final String namespace;
protected final IModInfo modInfo;
protected ModLoadingStage modLoadingStage;
protected Supplier<?> contextExtension;
protected final Map<ModLoadingStage, Runnable> activityMap = new HashMap<>();
protected final Map<Class<? extends IExtensionPoint<?>>, Supplier<?>> extensionPoints = new IdentityHashMap<>();
protected final EnumMap<ModConfig.Type, ModConfig> configs = new EnumMap<>(ModConfig.Type.class);
Expand Down Expand Up @@ -126,14 +125,12 @@ public static <T extends Event & IModBusEvent> CompletableFuture<Void> buildTran
final Executor executor) {
return CompletableFuture
.runAsync(() -> {
ModLoadingContext.get().setActiveContainer(target);
target.activityMap.getOrDefault(target.modLoadingStage, ()->{}).run();
target.acceptEvent(eventGenerator.apply(target));
}, executor)
.whenComplete((mc, exception) -> {
target.modLoadingStage = stateChangeHandler.apply(target.modLoadingStage, exception);
progressBar.increment();
ModLoadingContext.get().setActiveContainer(null);
});
}

Expand All @@ -147,13 +144,25 @@ public <T extends Record> Optional<T> getCustomExtension(Class<? extends IExtens
return Optional.ofNullable((T)extensionPoints.getOrDefault(point,()-> null).get());
}

/**
* Register an {@link IExtensionPoint} with the mod container.
* @param point The extension point to register
* @param extension An extension operator
* @param <T> The type signature of the extension operator
*/
public <T extends Record & IExtensionPoint<T>> void registerExtensionPoint(Class<? extends IExtensionPoint<T>> point, Supplier<T> extension)
{
extensionPoints.put(point, extension);
}

public void addConfig(final ModConfig modConfig) {
configs.put(modConfig.getType(), modConfig);
if (modConfig.getSpec().isEmpty())
{
// This handles the case where a mod tries to register a config, without any options configured inside it.
LOGGER.warn("Attempted to register an empty config for type {} on mod {}", modConfig.getType(), getModId());
}

configs.put(modConfig.getType(), modConfig);
}

/**
Expand Down
12 changes: 2 additions & 10 deletions core/src/main/java/net/neoforged/fml/ModLoader.java
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,6 @@
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.CompletionException;
import java.util.concurrent.Executor;
import java.util.function.BiConsumer;
import java.util.function.Consumer;
import java.util.function.Function;
import java.util.stream.Collectors;
Expand Down Expand Up @@ -345,20 +344,13 @@ public <T extends Event & IModBusEvent> T postEventWithReturn(T e) {
postEvent(e);
return e;
}
public <T extends Event & IModBusEvent> void postEventWrapContainerInModOrder(T event) {
postEventWithWrapInModOrder(event, (mc, e) -> ModLoadingContext.get().setActiveContainer(mc), (mc, e) -> ModLoadingContext.get().setActiveContainer(null));
}
public <T extends Event & IModBusEvent> void postEventWithWrapInModOrder(T e, BiConsumer<ModContainer, T> pre, BiConsumer<ModContainer, T> post) {
public <T extends Event & IModBusEvent> void postEventInModOrder(T e) {
if (!loadingStateValid) {
LOGGER.error("Cowardly refusing to send event {} to a broken mod state", e.getClass().getName());
return;
}
for (EventPriority phase : EventPriority.values()) {
ModList.get().forEachModInOrder(mc -> {
pre.accept(mc, e);
mc.acceptEvent(phase, e);
post.accept(mc, e);
});
ModList.get().forEachModInOrder(mc -> mc.acceptEvent(phase, e));
}
}

Expand Down
78 changes: 0 additions & 78 deletions core/src/main/java/net/neoforged/fml/ModLoadingContext.java

This file was deleted.

21 changes: 2 additions & 19 deletions languages/java/src/main/java/net/neoforged/fml/common/Mod.java
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,8 @@
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;
import java.util.function.Supplier;

import net.neoforged.api.distmarker.Dist;
import net.neoforged.bus.api.IEventBus;
import net.neoforged.fml.Bindings;
import net.neoforged.fml.javafmlmod.FMLJavaModLoadingContext;

/**
* This defines a Mod to FML.
Expand Down Expand Up @@ -70,25 +66,12 @@
enum Bus {
/**
* The main Forge Event Bus.
*
* <p>See {@code NeoForge#EVENT_BUS}</p>
*/
FORGE(Bindings.getForgeBus()),
FORGE,
/**
* The mod specific Event bus.
* @see FMLJavaModLoadingContext#getModEventBus()
*/
MOD(()-> FMLJavaModLoadingContext.get().getModEventBus());

private final Supplier<IEventBus> busSupplier;

Bus(final Supplier<IEventBus> eventBusSupplier) {
this.busSupplier = eventBusSupplier;
}

public Supplier<IEventBus> bus() {
return busSupplier;
}
MOD
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@
package net.neoforged.fml.javafmlmod;

import net.neoforged.api.distmarker.Dist;
import net.neoforged.bus.api.IEventBus;
import net.neoforged.fml.Bindings;
import net.neoforged.fml.ModContainer;
import net.neoforged.fml.common.Mod;
import net.neoforged.neoforgespi.language.ModFileScanData;
Expand Down Expand Up @@ -57,8 +59,16 @@ public static void inject(final ModContainer mod, final ModFileScanData scanData
if (Objects.equals(mod.getModId(), modId) && sides.contains(FMLEnvironment.dist)) {
try
{
LOGGER.debug(LOADING, "Auto-subscribing {} to {}", ad.clazz().getClassName(), busTarget);
busTarget.bus().get().register(Class.forName(ad.clazz().getClassName(), true, loader));
IEventBus bus = switch (busTarget) {
case FORGE -> Bindings.getForgeBus();
case MOD -> mod.getEventBus();
};

if (bus != null) {
LOGGER.debug(LOADING, "Auto-subscribing {} to {}", ad.clazz().getClassName(), busTarget);

bus.register(Class.forName(ad.clazz().getClassName(), true, loader));
}
}
catch (ClassNotFoundException e)
{
Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -50,8 +50,6 @@ public FMLModContainer(IModInfo info, String className, ModFileScanData modFileS
.markerType(IModBusEvent.class)
.allowPerPhasePost()
.build();
final FMLJavaModLoadingContext contextExtension = new FMLJavaModLoadingContext(this);
this.contextExtension = () -> contextExtension;
try
{
var layer = gameLayer.findModule(info.getOwningFile().moduleName()).orElseThrow();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@
import net.neoforged.fml.IExtensionPoint;
import net.neoforged.fml.ModContainer;
import net.neoforged.neoforgespi.language.IModInfo;
import net.neoforged.neoforgespi.language.ModFileScanData;
import org.jetbrains.annotations.Nullable;
import org.slf4j.Logger;

Expand All @@ -19,16 +18,13 @@
public class LowCodeModContainer extends ModContainer
{
private static final Logger LOGGER = LogUtils.getLogger();
private final ModFileScanData scanResults;
private Object modInstance;
private final Object modInstance;

public LowCodeModContainer(IModInfo info, ModFileScanData modFileScanResults, ModuleLayer gameLayer)
public LowCodeModContainer(IModInfo info)
{
super(info);
LOGGER.debug(LOADING, "Creating LowCodeModContainer for {}", info.getModId());
this.scanResults = modFileScanResults;
this.modInstance = new Object();
this.contextExtension = () -> null;
this.extensionPoints.remove(IExtensionPoint.DisplayTest.class);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;

import java.lang.reflect.Constructor;
import java.lang.reflect.InvocationTargetException;
import java.util.Map;
import java.util.function.Consumer;
Expand All @@ -41,8 +40,7 @@ public <T> T loadMod(final IModInfo info, final ModFileScanData modFileScanResul
try {
final Class<?> fmlContainer = Class.forName("net.neoforged.fml.lowcodemod.LowCodeModContainer", true, Thread.currentThread().getContextClassLoader());
LOGGER.debug(LOADING, "Loading LowCodeModContainer from classloader {} - got {}", Thread.currentThread().getContextClassLoader(), fmlContainer.getClassLoader());
final Constructor<?> constructor = fmlContainer.getConstructor(IModInfo.class, ModFileScanData.class, ModuleLayer.class);
return (T) constructor.newInstance(info, modFileScanResults, gameLayer);
return (T) fmlContainer.getConstructor(IModInfo.class).newInstance(info);
} catch (InvocationTargetException e) {
LOGGER.fatal(LOADING, "Failed to build mod", e);
if (e.getTargetException() instanceof ModLoadingException mle) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@ public class MinecraftModContainer extends ModContainer {

public MinecraftModContainer(final IModInfo info) {
super(info);
contextExtension = () -> null;
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,9 +41,8 @@ public static class MinecraftModTarget implements IModLanguageLoader {
@Override
public <T> T loadMod(final IModInfo info, final ModFileScanData modFileScanResults, final ModuleLayer gameLayer) {
try {
var module = gameLayer.findModule("minecraft").orElseThrow();
final Class<?> mcModClass = Class.forName(getClass().getModule(), "net.neoforged.fml.mclanguageprovider.MinecraftModContainer");
return (T)mcModClass.getConstructor(IModInfo.class).newInstance(info);
return (T) mcModClass.getConstructor(IModInfo.class).newInstance(info);
} catch (InstantiationException | IllegalAccessException | NoSuchMethodException | InvocationTargetException e) {
LOGGER.fatal(LOADING,"Unable to load MinecraftModContainer, wut?", e);
throw new RuntimeException(e);
Expand Down