Advertisement
Superloup10

Untitled

Jan 23rd, 2019
371
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 3.49 KB | None | 0 0
  1. package com.example.examplemod;
  2.  
  3. import net.minecraft.block.Block;
  4. import net.minecraft.init.Blocks;
  5. import net.minecraftforge.common.MinecraftForge;
  6. import net.minecraftforge.event.RegistryEvent;
  7. import net.minecraftforge.eventbus.api.SubscribeEvent;
  8. import net.minecraftforge.fml.InterModComms;
  9. import net.minecraftforge.fml.common.Mod;
  10. import net.minecraftforge.fml.event.lifecycle.FMLClientSetupEvent;
  11. import net.minecraftforge.fml.event.lifecycle.FMLCommonSetupEvent;
  12. import net.minecraftforge.fml.event.lifecycle.InterModEnqueueEvent;
  13. import net.minecraftforge.fml.event.lifecycle.InterModProcessEvent;
  14. import net.minecraftforge.fml.event.server.FMLServerStartingEvent;
  15. import net.minecraftforge.fml.javafmlmod.FMLModLoadingContext;
  16. import org.apache.logging.log4j.LogManager;
  17. import org.apache.logging.log4j.Logger;
  18.  
  19. import java.util.stream.Collectors;
  20.  
  21. // The value here should match an entry in the META-INF/mods.toml file
  22. @Mod("examplemod")
  23. public class ExampleMod
  24. {
  25.     // Directly reference a log4j logger.
  26.     private static final Logger LOGGER = LogManager.getLogger();
  27.  
  28.     public ExampleMod() {
  29.         // Register the setup method for modloading
  30.         FMLModLoadingContext.get().getModEventBus().addListener(this::setup);
  31.         // Register the enqueueIMC method for modloading
  32.         FMLModLoadingContext.get().getModEventBus().addListener(this::enqueueIMC);
  33.         // Register the processIMC method for modloading
  34.         FMLModLoadingContext.get().getModEventBus().addListener(this::processIMC);
  35.         // Register the doClientStuff method for modloading
  36.         FMLModLoadingContext.get().getModEventBus().addListener(this::doClientStuff);
  37.  
  38.         // Register ourselves for server, registry and other game events we are interested in
  39.         MinecraftForge.EVENT_BUS.register(this);
  40.     }
  41.  
  42.     private void setup(final FMLCommonSetupEvent event)
  43.     {
  44.         // some preinit code
  45.         LOGGER.info("HELLO FROM PREINIT");
  46.         LOGGER.info("DIRT BLOCK >> {}", Blocks.DIRT.getRegistryName());
  47.     }
  48.  
  49.     private void doClientStuff(final FMLClientSetupEvent event) {
  50.         // do something that can only be done on the client
  51.         LOGGER.info("Got game settings {}", event.getMinecraftSupplier().get().gameSettings);
  52.     }
  53.  
  54.     private void enqueueIMC(final InterModEnqueueEvent event)
  55.     {
  56.         // some example code to dispatch IMC to another mod
  57.         InterModComms.sendTo("forge", "helloworld", () -> { LOGGER.info("Hello world"); return "Hello world";});
  58.     }
  59.  
  60.     private void processIMC(final InterModProcessEvent event)
  61.     {
  62.         // some example code to receive and process InterModComms from other mods
  63.         LOGGER.info("Got IMC", event.getIMCStream().
  64.                 map(m->m.getMessageSupplier().get()).
  65.                 collect(Collectors.toList()));
  66.     }
  67.     // You can use SubscribeEvent and let the Event Bus discover methods to call
  68.     @SubscribeEvent
  69.     public void onBlocksRegistry(final RegistryEvent.Register<Block> blockRegistryEvent) {
  70.         // register a new block here
  71.         LOGGER.info("HELLO from Register Block");
  72.     }
  73.  
  74.     // You can use EventBusSubscriber to automatically subscribe events on the contained class
  75.     @Mod.EventBusSubscriber
  76.     public static class ServerEvents {
  77.         @SubscribeEvent
  78.         public static void onServerStarting(FMLServerStartingEvent event) {
  79.             // do something when the server starts
  80.             LOGGER.info("HELLO from server starting");
  81.         }
  82.     }
  83. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement