From a8d4c779bb345f046c3e9c9281960b1a1ab29cea Mon Sep 17 00:00:00 2001 From: dima_dencep Date: Fri, 1 Dec 2023 22:36:30 +0700 Subject: [PATCH] Remove `*ModLoadingContext` --- .../main/java/net/neoforged/fml/Bindings.java | 2 +- .../net/neoforged/fml/DeferredWorkQueue.java | 5 +- .../net/neoforged/fml/IBindingsProvider.java | 2 +- .../java/net/neoforged/fml/InterModComms.java | 14 ---- .../java/net/neoforged/fml/ModContainer.java | 17 +++- .../java/net/neoforged/fml/ModLoader.java | 12 +-- .../net/neoforged/fml/ModLoadingContext.java | 78 ------------------- .../java/net/neoforged/fml/common/Mod.java | 21 +---- .../javafmlmod/AutomaticEventSubscriber.java | 14 +++- .../javafmlmod/FMLJavaModLoadingContext.java | 35 --------- .../fml/javafmlmod/FMLModContainer.java | 2 - .../fml/lowcodemod/LowCodeModContainer.java | 8 +- .../LowCodeModLanguageProvider.java | 4 +- .../MinecraftModContainer.java | 1 - .../MinecraftModLanguageProvider.java | 3 +- 15 files changed, 36 insertions(+), 182 deletions(-) delete mode 100644 core/src/main/java/net/neoforged/fml/ModLoadingContext.java delete mode 100644 languages/java/src/main/java/net/neoforged/fml/javafmlmod/FMLJavaModLoadingContext.java diff --git a/core/src/main/java/net/neoforged/fml/Bindings.java b/core/src/main/java/net/neoforged/fml/Bindings.java index f64523e9..3e9f1e74 100644 --- a/core/src/main/java/net/neoforged/fml/Bindings.java +++ b/core/src/main/java/net/neoforged/fml/Bindings.java @@ -26,7 +26,7 @@ private Bindings() { this.provider = providers.get(0); } - public static Supplier getForgeBus() { + public static IEventBus getForgeBus() { return INSTANCE.provider.getForgeBusSupplier(); } diff --git a/core/src/main/java/net/neoforged/fml/DeferredWorkQueue.java b/core/src/main/java/net/neoforged/fml/DeferredWorkQueue.java index d7334c88..e15b9023 100644 --- a/core/src/main/java/net/neoforged/fml/DeferredWorkQueue.java +++ b/core/src/main/java/net/neoforged/fml/DeferredWorkQueue.java @@ -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) { diff --git a/core/src/main/java/net/neoforged/fml/IBindingsProvider.java b/core/src/main/java/net/neoforged/fml/IBindingsProvider.java index 31cfa677..7f9842c9 100644 --- a/core/src/main/java/net/neoforged/fml/IBindingsProvider.java +++ b/core/src/main/java/net/neoforged/fml/IBindingsProvider.java @@ -11,7 +11,7 @@ import java.util.function.Supplier; public interface IBindingsProvider { - Supplier getForgeBusSupplier(); + IEventBus getForgeBusSupplier(); Supplier getMessageParser(); Supplier getConfigConfiguration(); } diff --git a/core/src/main/java/net/neoforged/fml/InterModComms.java b/core/src/main/java/net/neoforged/fml/InterModComms.java index 3f51a54a..cc687be0 100644 --- a/core/src/main/java/net/neoforged/fml/InterModComms.java +++ b/core/src/main/java/net/neoforged/fml/InterModComms.java @@ -63,20 +63,6 @@ public final Supplier getMessageSupplier() { private static ConcurrentMap> 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. * diff --git a/core/src/main/java/net/neoforged/fml/ModContainer.java b/core/src/main/java/net/neoforged/fml/ModContainer.java index d2d497c2..a9afb488 100644 --- a/core/src/main/java/net/neoforged/fml/ModContainer.java +++ b/core/src/main/java/net/neoforged/fml/ModContainer.java @@ -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 activityMap = new HashMap<>(); protected final Map>, Supplier> extensionPoints = new IdentityHashMap<>(); protected final EnumMap configs = new EnumMap<>(ModConfig.Type.class); @@ -126,14 +125,12 @@ public static CompletableFuture 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); }); } @@ -147,13 +144,25 @@ public Optional getCustomExtension(Class null).get()); } + /** + * Register an {@link IExtensionPoint} with the mod container. + * @param point The extension point to register + * @param extension An extension operator + * @param The type signature of the extension operator + */ public > void registerExtensionPoint(Class> point, Supplier 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); } /** diff --git a/core/src/main/java/net/neoforged/fml/ModLoader.java b/core/src/main/java/net/neoforged/fml/ModLoader.java index 07e20ef1..4832f97f 100644 --- a/core/src/main/java/net/neoforged/fml/ModLoader.java +++ b/core/src/main/java/net/neoforged/fml/ModLoader.java @@ -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; @@ -345,20 +344,13 @@ public T postEventWithReturn(T e) { postEvent(e); return e; } - public void postEventWrapContainerInModOrder(T event) { - postEventWithWrapInModOrder(event, (mc, e) -> ModLoadingContext.get().setActiveContainer(mc), (mc, e) -> ModLoadingContext.get().setActiveContainer(null)); - } - public void postEventWithWrapInModOrder(T e, BiConsumer pre, BiConsumer post) { + public 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)); } } diff --git a/core/src/main/java/net/neoforged/fml/ModLoadingContext.java b/core/src/main/java/net/neoforged/fml/ModLoadingContext.java deleted file mode 100644 index ab653ba2..00000000 --- a/core/src/main/java/net/neoforged/fml/ModLoadingContext.java +++ /dev/null @@ -1,78 +0,0 @@ -/* - * Copyright (c) Forge Development LLC and contributors - * SPDX-License-Identifier: LGPL-2.1-only - */ - -package net.neoforged.fml; - -import com.mojang.logging.LogUtils; -import net.neoforged.fml.config.IConfigSpec; -import net.neoforged.fml.config.ModConfig; -import org.slf4j.Logger; - -import java.util.function.Supplier; - -public class ModLoadingContext -{ - private static final Logger LOGGER = LogUtils.getLogger(); - private static final ThreadLocal context = ThreadLocal.withInitial(ModLoadingContext::new); - private Object languageExtension; - private ModLoadingStage stage; - - public static ModLoadingContext get() { - return context.get(); - } - - private ModContainer activeContainer; - - public void setActiveContainer(final ModContainer container) { - this.activeContainer = container; - this.languageExtension = container == null ? null : container.contextExtension.get(); - } - - public ModContainer getActiveContainer() { - return activeContainer == null ? ModList.get().getModContainerById("minecraft").orElseThrow(()->new RuntimeException("Where is minecraft???!")) : activeContainer; - } - - public String getActiveNamespace() { - return activeContainer == null ? "minecraft" : activeContainer.getNamespace(); - } - - /** - * Register an {@link IExtensionPoint} with the mod container. - * @param point The extension point to register - * @param extension An extension operator - * @param The type signature of the extension operator - */ - public > void registerExtensionPoint(Class> point, Supplier extension) { - getActiveContainer().registerExtensionPoint(point, extension); - } - - public void registerConfig(ModConfig.Type type, IConfigSpec spec) { - if (spec.isEmpty()) - { - // This handles the case where a mod tries to register a config, without any options configured inside it. - LOGGER.debug("Attempted to register an empty config for type {} on mod {}", type, getActiveContainer().getModId()); - return; - } - - getActiveContainer().addConfig(new ModConfig(type, spec, getActiveContainer())); - } - - public void registerConfig(ModConfig.Type type, IConfigSpec spec, String fileName) { - if (spec.isEmpty()) - { - // This handles the case where a mod tries to register a config, without any options configured inside it. - LOGGER.debug("Attempted to register an empty config for type {} on mod {} using file name {}", type, getActiveContainer().getModId(), fileName); - return; - } - - getActiveContainer().addConfig(new ModConfig(type, spec, getActiveContainer(), fileName)); - } - - - @SuppressWarnings("unchecked") - public T extension() { - return (T)languageExtension; - } -} diff --git a/languages/java/src/main/java/net/neoforged/fml/common/Mod.java b/languages/java/src/main/java/net/neoforged/fml/common/Mod.java index f0f42ae6..8c7b044c 100644 --- a/languages/java/src/main/java/net/neoforged/fml/common/Mod.java +++ b/languages/java/src/main/java/net/neoforged/fml/common/Mod.java @@ -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. @@ -70,25 +66,12 @@ enum Bus { /** * The main Forge Event Bus. - * - *

See {@code NeoForge#EVENT_BUS}

*/ - FORGE(Bindings.getForgeBus()), + FORGE, /** * The mod specific Event bus. - * @see FMLJavaModLoadingContext#getModEventBus() */ - MOD(()-> FMLJavaModLoadingContext.get().getModEventBus()); - - private final Supplier busSupplier; - - Bus(final Supplier eventBusSupplier) { - this.busSupplier = eventBusSupplier; - } - - public Supplier bus() { - return busSupplier; - } + MOD } } } diff --git a/languages/java/src/main/java/net/neoforged/fml/javafmlmod/AutomaticEventSubscriber.java b/languages/java/src/main/java/net/neoforged/fml/javafmlmod/AutomaticEventSubscriber.java index d22f00fe..b8ab3bac 100644 --- a/languages/java/src/main/java/net/neoforged/fml/javafmlmod/AutomaticEventSubscriber.java +++ b/languages/java/src/main/java/net/neoforged/fml/javafmlmod/AutomaticEventSubscriber.java @@ -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; @@ -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) { diff --git a/languages/java/src/main/java/net/neoforged/fml/javafmlmod/FMLJavaModLoadingContext.java b/languages/java/src/main/java/net/neoforged/fml/javafmlmod/FMLJavaModLoadingContext.java deleted file mode 100644 index 68a5129b..00000000 --- a/languages/java/src/main/java/net/neoforged/fml/javafmlmod/FMLJavaModLoadingContext.java +++ /dev/null @@ -1,35 +0,0 @@ -/* - * Copyright (c) Forge Development LLC and contributors - * SPDX-License-Identifier: LGPL-2.1-only - */ - -package net.neoforged.fml.javafmlmod; - -import net.neoforged.bus.api.IEventBus; -import net.neoforged.fml.ModLoadingContext; - -public class FMLJavaModLoadingContext -{ - private final FMLModContainer container; - - FMLJavaModLoadingContext(FMLModContainer container) { - this.container = container; - } - - /** - * @return The mod's event bus, to allow subscription to Mod specific events - */ - public IEventBus getModEventBus() - { - return container.getEventBus(); - } - - - /** - * Helper to get the right instance from the {@link ModLoadingContext} correctly. - * @return The FMLJavaMod language specific extension from the ModLoadingContext - */ - public static FMLJavaModLoadingContext get() { - return ModLoadingContext.get().extension(); - } -} diff --git a/languages/java/src/main/java/net/neoforged/fml/javafmlmod/FMLModContainer.java b/languages/java/src/main/java/net/neoforged/fml/javafmlmod/FMLModContainer.java index e0f40c7c..925c7b1a 100644 --- a/languages/java/src/main/java/net/neoforged/fml/javafmlmod/FMLModContainer.java +++ b/languages/java/src/main/java/net/neoforged/fml/javafmlmod/FMLModContainer.java @@ -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(); diff --git a/languages/lowcode/src/main/java/net/neoforged/fml/lowcodemod/LowCodeModContainer.java b/languages/lowcode/src/main/java/net/neoforged/fml/lowcodemod/LowCodeModContainer.java index 437851d7..cfdb829c 100644 --- a/languages/lowcode/src/main/java/net/neoforged/fml/lowcodemod/LowCodeModContainer.java +++ b/languages/lowcode/src/main/java/net/neoforged/fml/lowcodemod/LowCodeModContainer.java @@ -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; @@ -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); } diff --git a/languages/lowcode/src/main/java/net/neoforged/fml/lowcodemod/LowCodeModLanguageProvider.java b/languages/lowcode/src/main/java/net/neoforged/fml/lowcodemod/LowCodeModLanguageProvider.java index b9e70b6b..0e135a6a 100644 --- a/languages/lowcode/src/main/java/net/neoforged/fml/lowcodemod/LowCodeModLanguageProvider.java +++ b/languages/lowcode/src/main/java/net/neoforged/fml/lowcodemod/LowCodeModLanguageProvider.java @@ -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; @@ -41,8 +40,7 @@ public 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) { diff --git a/languages/minecraft/src/main/java/net/neoforged/fml/mclanguageprovider/MinecraftModContainer.java b/languages/minecraft/src/main/java/net/neoforged/fml/mclanguageprovider/MinecraftModContainer.java index 9f0cc0a5..dd01b0f7 100644 --- a/languages/minecraft/src/main/java/net/neoforged/fml/mclanguageprovider/MinecraftModContainer.java +++ b/languages/minecraft/src/main/java/net/neoforged/fml/mclanguageprovider/MinecraftModContainer.java @@ -17,7 +17,6 @@ public class MinecraftModContainer extends ModContainer { public MinecraftModContainer(final IModInfo info) { super(info); - contextExtension = () -> null; } @Override diff --git a/languages/minecraft/src/main/java/net/neoforged/fml/mclanguageprovider/MinecraftModLanguageProvider.java b/languages/minecraft/src/main/java/net/neoforged/fml/mclanguageprovider/MinecraftModLanguageProvider.java index 82470039..a8fd72b7 100644 --- a/languages/minecraft/src/main/java/net/neoforged/fml/mclanguageprovider/MinecraftModLanguageProvider.java +++ b/languages/minecraft/src/main/java/net/neoforged/fml/mclanguageprovider/MinecraftModLanguageProvider.java @@ -41,9 +41,8 @@ public static class MinecraftModTarget implements IModLanguageLoader { @Override public 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);