Path: blob/1.21.x/fmlcore/src/main/java/net/minecraftforge/fml/ModLoadingContext.java
7251 views
/*1* Copyright (c) Forge Development LLC and contributors2* SPDX-License-Identifier: LGPL-2.1-only3*/45package net.minecraftforge.fml;67import com.mojang.logging.LogUtils;8import net.minecraftforge.fml.config.IConfigSpec;9import net.minecraftforge.fml.config.ModConfig;10import org.slf4j.Logger;11import java.util.function.BiPredicate;12import java.util.function.Supplier;1314public class ModLoadingContext {15private static final Logger LOGGER = LogUtils.getLogger();16private static final ThreadLocal<ModLoadingContext> context = ThreadLocal.withInitial(ModLoadingContext::new);17private ModContainer activeContainer;18private Object languageExtension;1920/**21* @deprecated Use the context provided by your language loader in your mod's constructor22*/23@Deprecated(forRemoval = true, since="1.21.1")24public static ModLoadingContext get() {25return context.get();26}2728/**29* @deprecated Going to be moved to ForgeHooks for Internal use.30*/31@Deprecated(forRemoval = true, since="1.21.1")32public void setActiveContainer(final ModContainer container) {33this.activeContainer = container;34this.languageExtension = container == null ? null : container.contextExtension.get();35}3637/**38* Going to be moved to ForgeHooks for Internal use.39* @deprecated Override/Use {@link ModLoadingContext#getContainer()}40*/41@Deprecated(forRemoval = true, since="1.21.1")42public ModContainer getActiveContainer() {43return activeContainer == null ? ModList.get().getModContainerById("minecraft").orElseThrow(()->new RuntimeException("Where is minecraft???!")) : activeContainer;44}4546/**47* @deprecated Going to be moved to ForgeHooks for Internal use.48*/49@Deprecated(forRemoval = true, since="1.21.1")50public String getActiveNamespace() {51return activeContainer == null ? "minecraft" : activeContainer.getNamespace();52}5354/**55* @return {@link ModLoadingContext#getActiveContainer()} by default.56*/57public ModContainer getContainer() {58return getActiveContainer();59}6061/**62* Register an {@link IExtensionPoint} with the mod container.63* @param point The extension point to register64* @param extension An extension operator65* @param <T> The type signature of the extension operator66*/67public <T extends Record & IExtensionPoint<T>> void registerExtensionPoint(Class<? extends IExtensionPoint<T>> point, Supplier<T> extension) {68getContainer().registerExtensionPoint(point, extension);69}7071/**72* Register a {@link IExtensionPoint.DisplayTest} with the mod container.73* <p>A shorthand for registering a DisplayTest with {@link #registerExtensionPoint(Class, Supplier)}.</p>74* @param displayTest The {@link IExtensionPoint.DisplayTest} to register75*/76public void registerDisplayTest(IExtensionPoint.DisplayTest displayTest) {77getContainer().registerDisplayTest(() -> displayTest);78}7980/**81* Register a {@link IExtensionPoint.DisplayTest} with the mod container.82* <p>A shorthand for registering a DisplayTest supplier with {@link #registerExtensionPoint(Class, Supplier)}.</p>83* @param displayTest The {@link Supplier<IExtensionPoint.DisplayTest>} to register84*/85public void registerDisplayTest(Supplier<IExtensionPoint.DisplayTest> displayTest) {86getContainer().registerDisplayTest(displayTest);87}8889/**90* Register a {@link IExtensionPoint.DisplayTest} with the mod container.91* <p>A shorthand for registering a DisplayTest with {@link #registerExtensionPoint(Class, Supplier)} that also92* creates the DisplayTest instance for you using the provided parameters.</p>93* @see IExtensionPoint.DisplayTest#DisplayTest(String, BiPredicate)94*/95public void registerDisplayTest(String version, BiPredicate<String, Boolean> remoteVersionTest) {96getContainer().registerDisplayTest(new IExtensionPoint.DisplayTest(version, remoteVersionTest));97}9899/**100* Register a {@link IExtensionPoint.DisplayTest} with the mod container.101* <p>A shorthand for registering a DisplayTest with {@link #registerExtensionPoint(Class, Supplier)} that also102* creates the DisplayTest instance for you using the provided parameters.</p>103* @see IExtensionPoint.DisplayTest#DisplayTest(Supplier, BiPredicate)104*/105public void registerDisplayTest(Supplier<String> suppliedVersion, BiPredicate<String, Boolean> remoteVersionTest) {106getContainer().registerDisplayTest(new IExtensionPoint.DisplayTest(suppliedVersion, remoteVersionTest));107}108109public void registerConfig(ModConfig.Type type, IConfigSpec<?> spec) {110if (spec.isEmpty())111{112// This handles the case where a mod tries to register a config, without any options configured inside it.113LOGGER.debug("Attempted to register an empty config for type {} on mod {}", type, getContainer().getModId());114return;115}116117getContainer().addConfig(new ModConfig(type, spec, getContainer()));118}119120public void registerConfig(ModConfig.Type type, IConfigSpec<?> spec, String fileName) {121if (spec.isEmpty())122{123// This handles the case where a mod tries to register a config, without any options configured inside it.124LOGGER.debug("Attempted to register an empty config for type {} on mod {} using file name {}", type, getContainer().getModId(), fileName);125return;126}127128getContainer().addConfig(new ModConfig(type, spec, getContainer(), fileName));129}130131132@SuppressWarnings("unchecked")133public <T> T extension() {134return (T)languageExtension;135}136}137138139