package at.hannibal2.skyhanni import at.hannibal2.skyhanni.api.event.SkyHanniEvents import at.hannibal2.skyhanni.config.ConfigFileType import at.hannibal2.skyhanni.config.ConfigManager import at.hannibal2.skyhanni.config.Features import at.hannibal2.skyhanni.config.SackData import at.hannibal2.skyhanni.config.commands.Commands import at.hannibal2.skyhanni.data.OtherInventoryData import at.hannibal2.skyhanni.data.jsonobjects.local.FriendsJson import at.hannibal2.skyhanni.data.jsonobjects.local.JacobContestsJson import at.hannibal2.skyhanni.data.jsonobjects.local.KnownFeaturesJson import at.hannibal2.skyhanni.data.jsonobjects.local.VisualWordsJson import at.hannibal2.skyhanni.data.repo.RepoManager import at.hannibal2.skyhanni.events.LorenzTickEvent import at.hannibal2.skyhanni.events.utils.PreInitFinishedEvent import at.hannibal2.skyhanni.features.nether.reputationhelper.CrimsonIsleReputationHelper import at.hannibal2.skyhanni.skyhannimodule.LoadedModules import at.hannibal2.skyhanni.test.command.ErrorManager import at.hannibal2.skyhanni.test.hotswap.HotswapSupport import at.hannibal2.skyhanni.utils.MinecraftConsoleFilter.Companion.initLogging import at.hannibal2.skyhanni.utils.NEUVersionCheck.checkIfNeuIsLoaded import kotlinx.coroutines.CoroutineName import kotlinx.coroutines.CoroutineScope import kotlinx.coroutines.Job import kotlinx.coroutines.SupervisorJob import kotlinx.coroutines.launch import net.minecraft.client.Minecraft import net.minecraft.client.gui.GuiScreen import net.minecraftforge.common.MinecraftForge import net.minecraftforge.fml.common.Loader import net.minecraftforge.fml.common.Mod import net.minecraftforge.fml.common.event.FMLInitializationEvent import net.minecraftforge.fml.common.event.FMLPreInitializationEvent import net.minecraftforge.fml.common.eventhandler.SubscribeEvent import org.apache.logging.log4j.Level import org.apache.logging.log4j.LogManager import org.apache.logging.log4j.Logger @Mod( modid = SkyHanniMod.MODID, clientSideOnly = true, useMetadata = true, guiFactory = "at.hannibal2.skyhanni.config.ConfigGuiForgeInterop", version = "0.27.Beta.8", ) class SkyHanniMod { @Mod.EventHandler fun preInit(event: FMLPreInitializationEvent?) { checkIfNeuIsLoaded() HotswapSupport.load() loadModule(this) LoadedModules.modules.forEach { loadModule(it) } loadModule(CrimsonIsleReputationHelper(this)) SkyHanniEvents.init(modules) Commands.init() PreInitFinishedEvent().post() } @Mod.EventHandler fun init(event: FMLInitializationEvent?) { configManager = ConfigManager() configManager.firstLoad() initLogging() Runtime.getRuntime().addShutdownHook( Thread { configManager.saveConfig(ConfigFileType.FEATURES, "shutdown-hook") }, ) repo = RepoManager(ConfigManager.configDirectory) loadModule(repo) try { repo.loadRepoInformation() } catch (e: Exception) { Exception("Error reading repo data", e).printStackTrace() } loadedClasses.clear() } private val loadedClasses = mutableSetOf() fun loadModule(obj: Any) { if (!loadedClasses.add(obj.javaClass.name)) throw IllegalStateException("Module ${obj.javaClass.name} is already loaded") modules.add(obj) MinecraftForge.EVENT_BUS.register(obj) } @SubscribeEvent fun onTick(event: LorenzTickEvent) { if (screenToOpen != null) { screenTicks++ if (screenTicks == 5) { Minecraft.getMinecraft().thePlayer.closeScreen() OtherInventoryData.close() Minecraft.getMinecraft().displayGuiScreen(screenToOpen) screenTicks = 0 screenToOpen = null } } } companion object { const val MODID = "skyhanni" @JvmStatic val version: String get() = Loader.instance().indexedModList[MODID]!!.version @JvmField var feature: Features = Features() lateinit var sackData: SackData lateinit var friendsData: FriendsJson lateinit var knownFeaturesData: KnownFeaturesJson lateinit var jacobContestsData: JacobContestsJson lateinit var visualWordsData: VisualWordsJson lateinit var repo: RepoManager lateinit var configManager: ConfigManager val logger: Logger = LogManager.getLogger("SkyHanni") fun getLogger(name: String): Logger { return LogManager.getLogger("SkyHanni.$name") } val modules: MutableList = ArrayList() private val globalJob: Job = Job(null) val coroutineScope = CoroutineScope( CoroutineName("SkyHanni") + SupervisorJob(globalJob), ) var screenToOpen: GuiScreen? = null private var screenTicks = 0 fun consoleLog(message: String) { logger.log(Level.INFO, message) } fun launchCoroutine(function: suspend () -> Unit) { coroutineScope.launch { try { function() } catch (ex: Exception) { ErrorManager.logErrorWithData(ex, "Asynchronous exception caught") } } } } }