Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
MinecraftForge
GitHub Repository: MinecraftForge/MinecraftForge
Path: blob/1.21.x/fmlcore/src/main/java/net/minecraftforge/fml/ModLoadingContext.java
7251 views
1
/*
2
* Copyright (c) Forge Development LLC and contributors
3
* SPDX-License-Identifier: LGPL-2.1-only
4
*/
5
6
package net.minecraftforge.fml;
7
8
import com.mojang.logging.LogUtils;
9
import net.minecraftforge.fml.config.IConfigSpec;
10
import net.minecraftforge.fml.config.ModConfig;
11
import org.slf4j.Logger;
12
import java.util.function.BiPredicate;
13
import java.util.function.Supplier;
14
15
public class ModLoadingContext {
16
private static final Logger LOGGER = LogUtils.getLogger();
17
private static final ThreadLocal<ModLoadingContext> context = ThreadLocal.withInitial(ModLoadingContext::new);
18
private ModContainer activeContainer;
19
private Object languageExtension;
20
21
/**
22
* @deprecated Use the context provided by your language loader in your mod's constructor
23
*/
24
@Deprecated(forRemoval = true, since="1.21.1")
25
public static ModLoadingContext get() {
26
return context.get();
27
}
28
29
/**
30
* @deprecated Going to be moved to ForgeHooks for Internal use.
31
*/
32
@Deprecated(forRemoval = true, since="1.21.1")
33
public void setActiveContainer(final ModContainer container) {
34
this.activeContainer = container;
35
this.languageExtension = container == null ? null : container.contextExtension.get();
36
}
37
38
/**
39
* Going to be moved to ForgeHooks for Internal use.
40
* @deprecated Override/Use {@link ModLoadingContext#getContainer()}
41
*/
42
@Deprecated(forRemoval = true, since="1.21.1")
43
public ModContainer getActiveContainer() {
44
return activeContainer == null ? ModList.get().getModContainerById("minecraft").orElseThrow(()->new RuntimeException("Where is minecraft???!")) : activeContainer;
45
}
46
47
/**
48
* @deprecated Going to be moved to ForgeHooks for Internal use.
49
*/
50
@Deprecated(forRemoval = true, since="1.21.1")
51
public String getActiveNamespace() {
52
return activeContainer == null ? "minecraft" : activeContainer.getNamespace();
53
}
54
55
/**
56
* @return {@link ModLoadingContext#getActiveContainer()} by default.
57
*/
58
public ModContainer getContainer() {
59
return getActiveContainer();
60
}
61
62
/**
63
* Register an {@link IExtensionPoint} with the mod container.
64
* @param point The extension point to register
65
* @param extension An extension operator
66
* @param <T> The type signature of the extension operator
67
*/
68
public <T extends Record & IExtensionPoint<T>> void registerExtensionPoint(Class<? extends IExtensionPoint<T>> point, Supplier<T> extension) {
69
getContainer().registerExtensionPoint(point, extension);
70
}
71
72
/**
73
* Register a {@link IExtensionPoint.DisplayTest} with the mod container.
74
* <p>A shorthand for registering a DisplayTest with {@link #registerExtensionPoint(Class, Supplier)}.</p>
75
* @param displayTest The {@link IExtensionPoint.DisplayTest} to register
76
*/
77
public void registerDisplayTest(IExtensionPoint.DisplayTest displayTest) {
78
getContainer().registerDisplayTest(() -> displayTest);
79
}
80
81
/**
82
* Register a {@link IExtensionPoint.DisplayTest} with the mod container.
83
* <p>A shorthand for registering a DisplayTest supplier with {@link #registerExtensionPoint(Class, Supplier)}.</p>
84
* @param displayTest The {@link Supplier<IExtensionPoint.DisplayTest>} to register
85
*/
86
public void registerDisplayTest(Supplier<IExtensionPoint.DisplayTest> displayTest) {
87
getContainer().registerDisplayTest(displayTest);
88
}
89
90
/**
91
* Register a {@link IExtensionPoint.DisplayTest} with the mod container.
92
* <p>A shorthand for registering a DisplayTest with {@link #registerExtensionPoint(Class, Supplier)} that also
93
* creates the DisplayTest instance for you using the provided parameters.</p>
94
* @see IExtensionPoint.DisplayTest#DisplayTest(String, BiPredicate)
95
*/
96
public void registerDisplayTest(String version, BiPredicate<String, Boolean> remoteVersionTest) {
97
getContainer().registerDisplayTest(new IExtensionPoint.DisplayTest(version, remoteVersionTest));
98
}
99
100
/**
101
* Register a {@link IExtensionPoint.DisplayTest} with the mod container.
102
* <p>A shorthand for registering a DisplayTest with {@link #registerExtensionPoint(Class, Supplier)} that also
103
* creates the DisplayTest instance for you using the provided parameters.</p>
104
* @see IExtensionPoint.DisplayTest#DisplayTest(Supplier, BiPredicate)
105
*/
106
public void registerDisplayTest(Supplier<String> suppliedVersion, BiPredicate<String, Boolean> remoteVersionTest) {
107
getContainer().registerDisplayTest(new IExtensionPoint.DisplayTest(suppliedVersion, remoteVersionTest));
108
}
109
110
public void registerConfig(ModConfig.Type type, IConfigSpec<?> spec) {
111
if (spec.isEmpty())
112
{
113
// This handles the case where a mod tries to register a config, without any options configured inside it.
114
LOGGER.debug("Attempted to register an empty config for type {} on mod {}", type, getContainer().getModId());
115
return;
116
}
117
118
getContainer().addConfig(new ModConfig(type, spec, getContainer()));
119
}
120
121
public void registerConfig(ModConfig.Type type, IConfigSpec<?> spec, String fileName) {
122
if (spec.isEmpty())
123
{
124
// This handles the case where a mod tries to register a config, without any options configured inside it.
125
LOGGER.debug("Attempted to register an empty config for type {} on mod {} using file name {}", type, getContainer().getModId(), fileName);
126
return;
127
}
128
129
getContainer().addConfig(new ModConfig(type, spec, getContainer(), fileName));
130
}
131
132
133
@SuppressWarnings("unchecked")
134
public <T> T extension() {
135
return (T)languageExtension;
136
}
137
}
138
139