Advertisement
Superloup10

WolflinModContainer

Jan 28th, 2019
147
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Kotlin 5.41 KB | None | 0 0
  1. package fr.wolfdev.wolflin.lang
  2.  
  3. import net.minecraftforge.eventbus.EventBusErrorMessage
  4. import net.minecraftforge.eventbus.api.Event
  5. import net.minecraftforge.eventbus.api.IEventBus
  6. import net.minecraftforge.eventbus.api.IEventListener
  7. import net.minecraftforge.fml.*
  8. import net.minecraftforge.fml.Logging.LOADING
  9. import net.minecraftforge.fml.loading.moddiscovery.ModInfo
  10. import net.minecraftforge.forgespi.language.ModFileScanData
  11. import org.apache.logging.log4j.LogManager
  12. import java.util.function.Consumer
  13.  
  14. class WolflinModContainer(info: ModInfo, className: String, val modClassLoader: ClassLoader, val modFileScanData: ModFileScanData): ModContainer(info) {
  15.     private val LOGGER = LogManager.getLogger()
  16.     private var eventBus: IEventBus
  17.     private lateinit var modInstance: Any
  18.     private var modClass: Class<*>
  19.  
  20.     init {
  21.         LOGGER.debug(LOADING, "Creating WolflinModContainer instance for $className with classLoader $modClassLoader & ${javaClass.classLoader}")
  22.         this.triggerMap[ModLoadingStage.CONSTRUCT] = this.dummy().andThen(this::beforeEvent).andThen(this::constructMod).andThen(this::afterEvent)
  23.         this.triggerMap[ModLoadingStage.CREATE_REGISTRIES] = this.dummy().andThen(this::beforeEvent).andThen(this::fireEvent).andThen(this::afterEvent)
  24.         this.triggerMap[ModLoadingStage.LOAD_REGISTRIES] = this.dummy().andThen(this::beforeEvent).andThen(this::fireEvent).andThen(this::afterEvent)
  25.         this.triggerMap[ModLoadingStage.COMMON_SETUP] = this.dummy().andThen(this::beforeEvent).andThen(this::preinitMod).andThen(this::fireEvent).andThen(this::afterEvent)
  26.         this.triggerMap[ModLoadingStage.SIDED_SETUP] = this.dummy().andThen(this::beforeEvent).andThen(this::fireEvent).andThen(this::afterEvent)
  27.         this.triggerMap[ModLoadingStage.ENQUEUE_IMC] = this.dummy().andThen(this::beforeEvent).andThen(this::initMod).andThen(this::fireEvent).andThen(this::afterEvent)
  28.         this.triggerMap[ModLoadingStage.PROCESS_IMC] = this.dummy().andThen(this::beforeEvent).andThen(this::fireEvent).andThen(this::afterEvent)
  29.         this.triggerMap[ModLoadingStage.COMPLETE] = this.dummy().andThen(this::beforeEvent).andThen(this::completeLoading).andThen(this::fireEvent).andThen(this::afterEvent)
  30.  
  31.         eventBus = IEventBus.create(this::onEventFailed)
  32.         try {
  33.             modClass = Class.forName(className, true, modClassLoader)
  34.             LOGGER.debug(LOADING, "Loaded modclass ${modClass.name} with ${modClass.classLoader}")
  35.         } catch(e: Throwable) {
  36.             LOGGER.error(LOADING, "Failed to load class $className", e)
  37.             throw ModLoadingException(modInfo, ModLoadingStage.CONSTRUCT, "Wut!?!", e)
  38.         }
  39.     }
  40.  
  41.     private fun completeLoading(lifecycleEvent: LifecycleEventProvider.LifecycleEvent) {
  42.     }
  43.  
  44.     private fun initMod(lifecycleEvent: LifecycleEventProvider.LifecycleEvent) {
  45.     }
  46.  
  47.     private fun dummy(): Consumer<LifecycleEventProvider.LifecycleEvent> = Consumer {}
  48.     private fun onEventFailed(eventBus: IEventBus, event: Event, eventListener: Array<IEventListener>, i: Int, throwable: Throwable) {
  49.         LOGGER.error(EventBusErrorMessage(event, i, eventListener, throwable))
  50.     }
  51.  
  52.     private fun beforeEvent(lifecycleEvent: LifecycleEventProvider.LifecycleEvent) {
  53.         WolflinModLoadingContext.get().activeContainer = this
  54.         ModThreadContext.get().activeContainer = this
  55.     }
  56.  
  57.     private fun fireEvent(lifecycleEvent: LifecycleEventProvider.LifecycleEvent) {
  58.         val event = lifecycleEvent.fromStage().getModEvent(this)
  59.         LOGGER.debug(LOADING, "Firing event to modid ${getModId()} : ${event.javaClass.name}")
  60.         try {
  61.             eventBus.post(event)
  62.             LOGGER.debug(LOADING, "Fired event to modid ${getModId()} : ${event.javaClass.name}")
  63.         } catch(e: Throwable) {
  64.             throw ModLoadingException(modInfo, lifecycleEvent.fromStage(), "Error firing event", e)
  65.         }
  66.     }
  67.  
  68.     private fun afterEvent(lifecycleEvent: LifecycleEventProvider.LifecycleEvent) {
  69.         ModThreadContext.get().activeContainer = null
  70.         WolflinModLoadingContext.get().activeContainer = null
  71.         if(currentState == ModLoadingStage.ERROR) {
  72.             LOGGER.error(LOADING, "An error occurred while dispatching event ${lifecycleEvent.fromStage()} to ${getModId()}")
  73.         }
  74.     }
  75.  
  76.     private fun preinitMod(lifecycleEvent: LifecycleEventProvider.LifecycleEvent) {
  77.     }
  78.  
  79.     private fun constructMod(lifecycleEvent: LifecycleEventProvider.LifecycleEvent) {
  80.         try {
  81.             LOGGER.debug(LOADING, "Loading mod instance ${getModId()} of type ${modClass.name}")
  82.             this.modInstance = modClass.newInstance()
  83.             LOGGER.debug(LOADING, "Loaded mod instance ${getModId()} of type ${modClass.name}")
  84.         } catch(e: Throwable) {
  85.             LOGGER.error(LOADING, "Failed to create mod instance. ModId ${getModId()} for class ${modClass.name}", e)
  86.             throw ModLoadingException(modInfo, lifecycleEvent.fromStage(), "Failed to load mod", e, modClass)
  87.         }
  88.         LOGGER.debug(LOADING, "Injecting Automatic event subscribers for ${getModId()}")
  89.         AutomaticEventSubscriber.inject(this, modFileScanData, modClassLoader)
  90.         LOGGER.debug(LOADING, "Completed Automatic event subscribers for ${getModId()}")
  91.     }
  92.  
  93.     override fun matches(mod: Any?) = mod == modInstance
  94.     override fun getMod() = this.modInstance
  95.     fun getEventBus(): IEventBus = this.eventBus
  96. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement