diff --git a/mdk/src/main/java/com/example/examplemod/ExampleMod.java b/mdk/src/main/java/com/example/examplemod/ExampleMod.java index 30056fe01..88204a0cc 100644 --- a/mdk/src/main/java/com/example/examplemod/ExampleMod.java +++ b/mdk/src/main/java/com/example/examplemod/ExampleMod.java @@ -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 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 blockRegistryEvent) { + // register a new block here + LOGGER.info("HELLO from Register Block"); } } } diff --git a/src/main/java/net/minecraftforge/common/ForgeMod.java b/src/main/java/net/minecraftforge/common/ForgeMod.java index 48cbc8c55..92891416e 100644 --- a/src/main/java/net/minecraftforge/common/ForgeMod.java +++ b/src/main/java/net/minecraftforge/common/ForgeMod.java @@ -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")); } diff --git a/src/main/java/net/minecraftforge/fml/ModContainer.java b/src/main/java/net/minecraftforge/fml/ModContainer.java index 92a7c7ada..684fea104 100644 --- a/src/main/java/net/minecraftforge/fml/ModContainer.java +++ b/src/main/java/net/minecraftforge/fml/ModContainer.java @@ -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> triggerMap; protected final Map> extensionPoints = new IdentityHashMap<>(); protected final EnumMap 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) { diff --git a/src/main/java/net/minecraftforge/fml/ModLoadingContext.java b/src/main/java/net/minecraftforge/fml/ModLoadingContext.java index d654666a9..72b4cd0bf 100644 --- a/src/main/java/net/minecraftforge/fml/ModLoadingContext.java +++ b/src/main/java/net/minecraftforge/fml/ModLoadingContext.java @@ -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 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 extension() { + return (T)languageExtension; + } } diff --git a/src/main/java/net/minecraftforge/fml/common/Mod.java b/src/main/java/net/minecraftforge/fml/common/Mod.java index 1137ea9ca..d0d8e96e9 100644 --- a/src/main/java/net/minecraftforge/fml/common/Mod.java +++ b/src/main/java/net/minecraftforge/fml/common/Mod.java @@ -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 busSupplier; diff --git a/src/main/java/net/minecraftforge/fml/javafmlmod/FMLJavaModLoadingContext.java b/src/main/java/net/minecraftforge/fml/javafmlmod/FMLJavaModLoadingContext.java new file mode 100644 index 000000000..b3dd20c98 --- /dev/null +++ b/src/main/java/net/minecraftforge/fml/javafmlmod/FMLJavaModLoadingContext.java @@ -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(); + } +} diff --git a/src/main/java/net/minecraftforge/fml/javafmlmod/FMLModContainer.java b/src/main/java/net/minecraftforge/fml/javafmlmod/FMLModContainer.java index e22622926..8bf6be4e3 100644 --- a/src/main/java/net/minecraftforge/fml/javafmlmod/FMLModContainer.java +++ b/src/main/java/net/minecraftforge/fml/javafmlmod/FMLModContainer.java @@ -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()); } diff --git a/src/main/java/net/minecraftforge/fml/javafmlmod/FMLModLoadingContext.java b/src/main/java/net/minecraftforge/fml/javafmlmod/FMLModLoadingContext.java deleted file mode 100644 index 3a6cfd760..000000000 --- a/src/main/java/net/minecraftforge/fml/javafmlmod/FMLModLoadingContext.java +++ /dev/null @@ -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 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; - } -}