Clean up the mod loading context objects. There is only one ThreadLocal now.

**BREAKING CHANGE** FMLModLoadingContext is renamed to FMLJavaModLoadingContext.
LanguageProviders can setup additional contextual data.

Signed-off-by: cpw <cpw+github@weeksfamily.ca>
This commit is contained in:
cpw 2019-02-13 22:13:59 -05:00
parent 586c24f9d6
commit 28c0ffe333
No known key found for this signature in database
GPG key ID: 8EB3DF749553B1B7
8 changed files with 100 additions and 95 deletions

View file

@ -12,7 +12,7 @@ import net.minecraftforge.fml.event.lifecycle.FMLCommonSetupEvent;
import net.minecraftforge.fml.event.lifecycle.InterModEnqueueEvent;
import net.minecraftforge.fml.event.lifecycle.InterModProcessEvent;
import net.minecraftforge.fml.event.server.FMLServerStartingEvent;
import net.minecraftforge.fml.javafmlmod.FMLModLoadingContext;
import net.minecraftforge.fml.javafmlmod.FMLJavaModLoadingContext;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
@ -27,13 +27,13 @@ public class ExampleMod
public ExampleMod() {
// Register the setup method for modloading
FMLModLoadingContext.get().getModEventBus().addListener(this::setup);
FMLJavaModLoadingContext.get().getModEventBus().addListener(this::setup);
// Register the enqueueIMC method for modloading
FMLModLoadingContext.get().getModEventBus().addListener(this::enqueueIMC);
FMLJavaModLoadingContext.get().getModEventBus().addListener(this::enqueueIMC);
// Register the processIMC method for modloading
FMLModLoadingContext.get().getModEventBus().addListener(this::processIMC);
FMLJavaModLoadingContext.get().getModEventBus().addListener(this::processIMC);
// Register the doClientStuff method for modloading
FMLModLoadingContext.get().getModEventBus().addListener(this::doClientStuff);
FMLJavaModLoadingContext.get().getModEventBus().addListener(this::doClientStuff);
// Register ourselves for server, registry and other game events we are interested in
MinecraftForge.EVENT_BUS.register(this);
@ -66,18 +66,18 @@ public class ExampleMod
}
// You can use SubscribeEvent and let the Event Bus discover methods to call
@SubscribeEvent
public void onBlocksRegistry(final RegistryEvent.Register<Block> blockRegistryEvent) {
// register a new block here
LOGGER.info("HELLO from Register Block");
public static void onServerStarting(FMLServerStartingEvent event) {
// do something when the server starts
LOGGER.info("HELLO from server starting");
}
// You can use EventBusSubscriber to automatically subscribe events on the contained class
@Mod.EventBusSubscriber
public static class ServerEvents {
// You can use EventBusSubscriber to automatically subscribe events on the contained class (this is subscribing to the MOD event bus
@Mod.EventBusSubscriber(bus=Mod.EventBusSubscriber.Bus.MOD)
public static class RegistryEvents {
@SubscribeEvent
public static void onServerStarting(FMLServerStartingEvent event) {
// do something when the server starts
LOGGER.info("HELLO from server starting");
public void onBlocksRegistry(final RegistryEvent.Register<Block> blockRegistryEvent) {
// register a new block here
LOGGER.info("HELLO from Register Block");
}
}
}

View file

@ -19,7 +19,9 @@
package net.minecraftforge.common;
import net.minecraftforge.eventbus.api.IEventBus;
import net.minecraftforge.fml.FMLWorldPersistenceHook;
import net.minecraftforge.fml.ModLoadingContext;
import net.minecraftforge.fml.VersionChecker;
import net.minecraftforge.fml.WorldPersistenceHooks;
import net.minecraftforge.fml.config.ModConfig;
@ -29,7 +31,7 @@ import net.minecraftforge.fml.event.lifecycle.FMLModIdMappingEvent;
import net.minecraftforge.fml.event.lifecycle.InterModProcessEvent;
import net.minecraftforge.fml.event.server.FMLServerStartingEvent;
import net.minecraftforge.fml.event.server.FMLServerStoppingEvent;
import net.minecraftforge.fml.javafmlmod.FMLModLoadingContext;
import net.minecraftforge.fml.javafmlmod.FMLJavaModLoadingContext;
import net.minecraftforge.fml.loading.FMLPaths;
import net.minecraftforge.server.command.ForgeCommand;
import net.minecraftforge.versions.forge.ForgeVersion;
@ -86,15 +88,16 @@ public class ForgeMod implements WorldPersistenceHooks.WorldPersistenceHook
INSTANCE = this;
WorldPersistenceHooks.addHook(this);
WorldPersistenceHooks.addHook(new FMLWorldPersistenceHook());
FMLModLoadingContext.get().getModEventBus().addListener(this::preInit);
FMLModLoadingContext.get().getModEventBus().addListener(this::postInit);
FMLModLoadingContext.get().getModEventBus().addListener(this::onAvailable);
final IEventBus modEventBus = FMLJavaModLoadingContext.get().getModEventBus();
modEventBus.addListener(this::preInit);
modEventBus.addListener(this::postInit);
modEventBus.addListener(this::onAvailable);
MinecraftForge.EVENT_BUS.addListener(this::serverStarting);
MinecraftForge.EVENT_BUS.addListener(this::playerLogin);
MinecraftForge.EVENT_BUS.addListener(this::serverStopping);
FMLModLoadingContext.get().registerConfig(ModConfig.Type.CLIENT, ForgeConfig.clientSpec);
FMLModLoadingContext.get().registerConfig(ModConfig.Type.SERVER, ForgeConfig.serverSpec);
FMLModLoadingContext.get().getModEventBus().register(ForgeConfig.class);
ModLoadingContext.get().registerConfig(ModConfig.Type.CLIENT, ForgeConfig.clientSpec);
ModLoadingContext.get().registerConfig(ModConfig.Type.SERVER, ForgeConfig.serverSpec);
modEventBus.register(ForgeConfig.class);
loadConfig(ForgeConfig.chunk_spec, FMLPaths.CONFIGDIR.get().resolve("forge_chunks.toml"));
}

View file

@ -45,6 +45,7 @@ public abstract class ModContainer
protected final String namespace;
protected final IModInfo modInfo;
protected ModLoadingStage modLoadingStage;
protected Supplier<?> contextExtension;
protected final Map<ModLoadingStage, Consumer<LifecycleEventProvider.LifecycleEvent>> triggerMap;
protected final Map<ExtensionPoint, Supplier<?>> extensionPoints = new IdentityHashMap<>();
protected final EnumMap<ModConfig.Type, ModConfig> configs = new EnumMap<>(ModConfig.Type.class);
@ -95,8 +96,10 @@ public abstract class ModContainer
{
try
{
ModLoadingContext.get().setActiveContainer(this, contextExtension.get());
triggerMap.getOrDefault(modLoadingStage, e->{}).accept(event);
modLoadingStage = event.toStage();
ModLoadingContext.get().setActiveContainer(null, null);
}
catch (ModLoadingException e)
{

View file

@ -19,11 +19,15 @@
package net.minecraftforge.fml;
import net.minecraftforge.common.ForgeConfigSpec;
import net.minecraftforge.fml.config.ModConfig;
import java.util.function.Supplier;
public class ModLoadingContext
{
private static ThreadLocal<ModLoadingContext> context = ThreadLocal.withInitial(ModLoadingContext::new);
private Object languageExtension;
public static ModLoadingContext get() {
return context.get();
@ -31,8 +35,9 @@ public class ModLoadingContext
private ModContainer activeContainer;
public void setActiveContainer(final ModContainer container) {
public void setActiveContainer(final ModContainer container, final Object languageExtension) {
this.activeContainer = container;
this.languageExtension = languageExtension;
}
public ModContainer getActiveContainer() {
@ -49,4 +54,17 @@ public class ModLoadingContext
getActiveContainer().registerExtensionPoint(point, extension);
}
public void registerConfig(ModConfig.Type type, ForgeConfigSpec spec) {
getActiveContainer().addConfig(new ModConfig(type, spec, getActiveContainer()));
}
public void registerConfig(ModConfig.Type type, ForgeConfigSpec spec, String fileName) {
getActiveContainer().addConfig(new ModConfig(type, spec, getActiveContainer(), fileName));
}
@SuppressWarnings("unchecked")
public <T> T extension() {
return (T)languageExtension;
}
}

View file

@ -29,7 +29,7 @@ import net.minecraftforge.api.distmarker.Dist;
import net.minecraftforge.common.MinecraftForge;
import net.minecraftforge.eventbus.api.IEventBus;
import net.minecraftforge.fml.event.lifecycle.ModLifecycleEvent;
import net.minecraftforge.fml.javafmlmod.FMLModLoadingContext;
import net.minecraftforge.fml.javafmlmod.FMLJavaModLoadingContext;
/**
* This defines a Mod to FML.
@ -90,9 +90,9 @@ public @interface Mod
FORGE(()-> MinecraftForge.EVENT_BUS),
/**
* The mod specific Event bus.
* @see FMLModLoadingContext#getModEventBus()
* @see FMLJavaModLoadingContext#getModEventBus()
*/
MOD(()-> FMLModLoadingContext.get().getModEventBus());
MOD(()-> FMLJavaModLoadingContext.get().getModEventBus());
private final Supplier<IEventBus> busSupplier;

View file

@ -0,0 +1,49 @@
/*
* Minecraft Forge
* Copyright (c) 2016-2019.
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation version 2.1
* of the License.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
*/
package net.minecraftforge.fml.javafmlmod;
import net.minecraftforge.eventbus.api.IEventBus;
import net.minecraftforge.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();
}
}

View file

@ -63,6 +63,8 @@ public class FMLModContainer extends ModContainer
triggerMap.put(ModLoadingStage.COMPLETE, dummy().andThen(this::beforeEvent).andThen(this::completeLoading).andThen(this::fireEvent).andThen(this::afterEvent));
this.eventBus = IEventBus.create(this::onEventFailed);
this.configHandler = Optional.of(event -> this.eventBus.post(event));
final FMLJavaModLoadingContext contextExtension = new FMLJavaModLoadingContext(this);
this.contextExtension = () -> contextExtension;
try
{
modClass = Class.forName(className, true, modClassLoader);
@ -93,8 +95,6 @@ public class FMLModContainer extends ModContainer
}
private void beforeEvent(LifecycleEventProvider.LifecycleEvent lifecycleEvent) {
FMLModLoadingContext.get().setActiveContainer(this);
ModLoadingContext.get().setActiveContainer(this);
}
private void fireEvent(LifecycleEventProvider.LifecycleEvent lifecycleEvent) {
@ -113,8 +113,6 @@ public class FMLModContainer extends ModContainer
}
private void afterEvent(LifecycleEventProvider.LifecycleEvent lifecycleEvent) {
ModLoadingContext.get().setActiveContainer(null);
FMLModLoadingContext.get().setActiveContainer(null);
if (getCurrentState() == ModLoadingStage.ERROR) {
LOGGER.error(LOADING,"An error occurred while dispatching event {} to {}", lifecycleEvent.fromStage(), getModId());
}

View file

@ -1,66 +0,0 @@
/*
* Minecraft Forge
* Copyright (c) 2016-2019.
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation version 2.1
* of the License.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
*/
package net.minecraftforge.fml.javafmlmod;
import net.minecraftforge.common.ForgeConfigSpec;
import net.minecraftforge.eventbus.api.IEventBus;
import net.minecraftforge.fml.ExtensionPoint;
import net.minecraftforge.fml.config.ModConfig;
import java.util.function.Supplier;
public class FMLModLoadingContext
{
private static ThreadLocal<FMLModLoadingContext> context = ThreadLocal.withInitial(FMLModLoadingContext::new);
private FMLModContainer activeContainer;
public static FMLModLoadingContext get() {
return context.get();
}
public void registerConfig(ModConfig.Type type, ForgeConfigSpec spec) {
getActiveContainer().addConfig(new ModConfig(type, spec, getActiveContainer()));
}
public void registerConfig(ModConfig.Type type, ForgeConfigSpec spec, String fileName) {
getActiveContainer().addConfig(new ModConfig(type, spec, getActiveContainer(), fileName));
}
/**
* @return The mod's event bus, to allow subscription to Mod specific events
*/
public IEventBus getModEventBus()
{
return getActiveContainer().getEventBus();
}
/**
* Only valid during {@link net.minecraftforge.fml.event.lifecycle.ModLifecycleEvent} dispatch and Mod construction
* @return the active FML container
*/
public FMLModContainer getActiveContainer()
{
return activeContainer;
}
public void setActiveContainer(FMLModContainer activeContainer)
{
this.activeContainer = activeContainer;
}
}