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.HypixelData import at.hannibal2.skyhanni.data.OtherInventoryData import at.hannibal2.skyhanni.data.SkillExperience 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.chat.Translator import at.hannibal2.skyhanni.features.garden.farming.FarmingWeightDisplay import at.hannibal2.skyhanni.features.misc.CollectionTracker import at.hannibal2.skyhanni.features.nether.reputationhelper.CrimsonIsleReputationHelper import at.hannibal2.skyhanni.skyhannimodule.LoadedModules import at.hannibal2.skyhanni.test.SkyHanniDebugsAndTests 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.26.Beta.8", ) class SkyHanniMod { @Mod.EventHandler fun preInit(event: FMLPreInitializationEvent?) { checkIfNeuIsLoaded() HotswapSupport.load() loadModule(this) LoadedModules.modules.forEach { loadModule(it) } // data loadModule(HypixelData()) loadModule(SkillExperience()) // features loadModule(CollectionTracker()) loadModule(CrimsonIsleReputationHelper(this)) loadModule(FarmingWeightDisplay()) loadModule(Translator()) // test stuff loadModule(SkyHanniDebugsAndTests()) 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 @JvmStatic val feature: Features get() = configManager.features val sackData: SackData get() = configManager.sackData val friendsData: FriendsJson get() = configManager.friendsData val knownFeaturesData: KnownFeaturesJson get() = configManager.knownFeaturesData val jacobContestsData: JacobContestsJson get() = configManager.jacobContestData val visualWordsData: VisualWordsJson get() = configManager.visualWordsData 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") } } } } }