Ugdhar Posted January 19, 2020 Share Posted January 19, 2020 (edited) The ExampleMod that comes with the MDK (1.15.1-30.0.41) adds a listener for the FMLClientSetupEvent in the constructor of the main mod class, for a method also a member of the main mod class. My question is, is this safe? Or should the FMLClientSetupEvent method be part of a client-only class? Just trying to figure out current best practices to avoid the whole reaching across logical sides thing. Thanks for your input! Edited January 19, 2020 by Ugdhar added MC/Forge versions Quote Link to comment Share on other sites More sharing options...
Ugdhar Posted January 20, 2020 Author Share Posted January 20, 2020 Just in case anyone doesn't feel like opening the MDK to check out the example, this is the one packaged with 1.15.1-30.0.41): package com.example.examplemod; import net.minecraft.block.Block; import net.minecraft.block.Blocks; import net.minecraftforge.common.MinecraftForge; import net.minecraftforge.event.RegistryEvent; import net.minecraftforge.eventbus.api.SubscribeEvent; import net.minecraftforge.fml.InterModComms; import net.minecraftforge.fml.common.Mod; import net.minecraftforge.fml.event.lifecycle.FMLClientSetupEvent; 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.FMLJavaModLoadingContext; import org.apache.logging.log4j.LogManager; import org.apache.logging.log4j.Logger; import java.util.stream.Collectors; // The value here should match an entry in the META-INF/mods.toml file @Mod("examplemod") public class ExampleMod { // Directly reference a log4j logger. private static final Logger LOGGER = LogManager.getLogger(); public ExampleMod() { // Register the setup method for modloading FMLJavaModLoadingContext.get().getModEventBus().addListener(this::setup); // Register the enqueueIMC method for modloading FMLJavaModLoadingContext.get().getModEventBus().addListener(this::enqueueIMC); // Register the processIMC method for modloading FMLJavaModLoadingContext.get().getModEventBus().addListener(this::processIMC); // Register the doClientStuff method for modloading FMLJavaModLoadingContext.get().getModEventBus().addListener(this::doClientStuff); // Register ourselves for server and other game events we are interested in MinecraftForge.EVENT_BUS.register(this); } private void setup(final FMLCommonSetupEvent event) { // some preinit code LOGGER.info("HELLO FROM PREINIT"); LOGGER.info("DIRT BLOCK >> {}", Blocks.DIRT.getRegistryName()); } private void doClientStuff(final FMLClientSetupEvent event) { // do something that can only be done on the client LOGGER.info("Got game settings {}", event.getMinecraftSupplier().get().gameSettings); } private void enqueueIMC(final InterModEnqueueEvent event) { // some example code to dispatch IMC to another mod InterModComms.sendTo("examplemod", "helloworld", () -> { LOGGER.info("Hello world from the MDK"); return "Hello world";}); } private void processIMC(final InterModProcessEvent event) { // some example code to receive and process InterModComms from other mods LOGGER.info("Got IMC {}", event.getIMCStream(). map(m->m.getMessageSupplier().get()). collect(Collectors.toList())); } // You can use SubscribeEvent and let the Event Bus discover methods to call @SubscribeEvent public 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 (this is subscribing to the MOD // Event bus for receiving Registry Events) @Mod.EventBusSubscriber(bus=Mod.EventBusSubscriber.Bus.MOD) public static class RegistryEvents { @SubscribeEvent public static void onBlocksRegistry(final RegistryEvent.Register<Block> blockRegistryEvent) { // register a new block here LOGGER.info("HELLO from Register Block"); } } } Quote Link to comment Share on other sites More sharing options...
Ugdhar Posted January 22, 2020 Author Share Posted January 22, 2020 I hate bumping topics, but I'm genuinely curious about this. No input from anyone huh? Quote Link to comment Share on other sites More sharing options...
sciwhiz12 Posted January 22, 2020 Share Posted January 22, 2020 From what I understand, classes in Java are only loaded by the JVM on the first called reference to that class. Referencing FMLClientSetupEvent is safe, because that class exists in the net.minecraftforge.fml.event.lifecycle package, which is common to both client and server distributions. Futhermore, calling event.getMinecraftSupplier() is safe, even though it calls net.minecraft.client.Minecraft (of which the whole package only exists in the physical client), because if that line is called and run by the JVM, it is because FMLClientSetupEvent was called, which is only called on the physical client. And referencing Minecraft is safe, because the Minecraft class will only be loaded if and only when something about that Minecraft class is queried (such as Minecraft.getInstance()). And, as said above, the only time the Minecraft class is called in the mod class is when FMLClientSetupEvent is called, which is only done on the physical client. Same principles apply to DedicatedServer: as long as your physical client code never reaches any statement referring to anything in net.minecraft.server.dedicated, your code should be safe. FYI: Checking FMLEnvironment.dist or using DistExecutor are two other ways to check what physical side you are running and to run code on a specific physical side, respectively. Source: https://stackoverflow.com/questions/16330355/when-are-imported-classes-loaded-in-java https://stackoverflow.com/questions/7560721/when-does-the-jvm-load-classes 1 Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.