diff options
Diffstat (limited to 'src/main/java/at/hannibal2/skyhanni/features/event')
8 files changed, 522 insertions, 27 deletions
diff --git a/src/main/java/at/hannibal2/skyhanni/features/event/hoppity/HoppityAPI.kt b/src/main/java/at/hannibal2/skyhanni/features/event/hoppity/HoppityAPI.kt new file mode 100644 index 000000000..5c63fe3a8 --- /dev/null +++ b/src/main/java/at/hannibal2/skyhanni/features/event/hoppity/HoppityAPI.kt @@ -0,0 +1,175 @@ +package at.hannibal2.skyhanni.features.event.hoppity + +import at.hannibal2.skyhanni.SkyHanniMod +import at.hannibal2.skyhanni.events.GuiContainerEvent +import at.hannibal2.skyhanni.events.LorenzChatEvent +import at.hannibal2.skyhanni.events.hoppity.RabbitFoundEvent +import at.hannibal2.skyhanni.features.event.hoppity.HoppityEggsManager.getEggType +import at.hannibal2.skyhanni.features.inventory.chocolatefactory.ChocolateFactoryAPI +import at.hannibal2.skyhanni.skyhannimodule.SkyHanniModule +import at.hannibal2.skyhanni.utils.InventoryUtils +import at.hannibal2.skyhanni.utils.ItemUtils.getLore +import at.hannibal2.skyhanni.utils.ItemUtils.itemName +import at.hannibal2.skyhanni.utils.LorenzRarity +import at.hannibal2.skyhanni.utils.LorenzRarity.DIVINE +import at.hannibal2.skyhanni.utils.RegexUtils.firstMatcher +import at.hannibal2.skyhanni.utils.RegexUtils.groupOrNull +import at.hannibal2.skyhanni.utils.RegexUtils.matchMatcher +import at.hannibal2.skyhanni.utils.SkyblockSeason +import net.minecraft.util.ChatComponentText +import net.minecraftforge.fml.common.eventhandler.EventPriority +import net.minecraftforge.fml.common.eventhandler.SubscribeEvent + +@SkyHanniModule +object HoppityAPI { + + private var hoppityEggChat = mutableListOf<String>() + private var duplicate = false + private var lastRarity = "" + private var lastName = "" + private var newRabbit = false + private var lastChatMeal: HoppityEggType? = null + private var lastDuplicateAmount: Long? = null + private var rabbitBought = false + + val hoppityRarities by lazy { LorenzRarity.entries.filter { it <= DIVINE } } + + private fun resetChatData() { + this.hoppityEggChat = mutableListOf() + this.duplicate = false + this.newRabbit = false + this.lastRarity = "" + this.lastName = "" + this.lastChatMeal = null + this.lastDuplicateAmount = null + this.rabbitBought = false + } + + fun isHoppityEvent() = (SkyblockSeason.currentSeason == SkyblockSeason.SPRING || SkyHanniMod.feature.dev.debug.alwaysHoppitys) + + fun rarityByRabbit(rabbit: String): LorenzRarity? = hoppityRarities.firstOrNull { it.chatColorCode == rabbit.substring(0, 2) } + + /** + * REGEX-TEST: §f1st Chocolate Milestone + * REGEX-TEST: §915th Chocolate Milestone + * REGEX-TEST: §622nd Chocolate Milestone + */ + private val milestoneNamePattern by ChocolateFactoryAPI.patternGroup.pattern( + "rabbit.milestone", + "(?:§.)*?(?<milestone>\\d{1,2})[a-z]{2} Chocolate Milestone", + ) + + /** + * REGEX-TEST: §7Reach §6300B Chocolate §7all-time to + * REGEX-TEST: §7Reach §61k Chocolate §7all-time to unlock + */ + private val allTimeLorePattern by ChocolateFactoryAPI.patternGroup.pattern( + "milestone.alltime", + "§7Reach §6(?<amount>[\\d.MBk]*) Chocolate §7all-time.*", + ) + + /** + * REGEX-TEST: §7Spend §6150B Chocolate §7in the + * REGEX-TEST: §7Spend §62M Chocolate §7in the §6Chocolate + */ + private val shopLorePattern by ChocolateFactoryAPI.patternGroup.pattern( + "milestone.shop", + "§7Spend §6(?<amount>[\\d.MBk]*) Chocolate §7in.*", + ) + + fun fireSideDishMessage() { + LorenzChatEvent( + "§d§lHOPPITY'S HUNT §r§dYou found a §r§6§lSide Dish §r§6Egg §r§din the Chocolate Factory§r§d!", + ChatComponentText(""), + ).postAndCatch() + } + + @SubscribeEvent(priority = EventPriority.HIGH) + fun onSlotClick(event: GuiContainerEvent.SlotClickEvent) { + val index = event.slot?.slotIndex ?: return + if (index == -999) return + + val clickedStack = InventoryUtils.getItemsInOpenChest() + .find { it.slotNumber == event.slot.slotNumber && it.hasStack } + ?.stack ?: return + val nameText = (if (clickedStack.hasDisplayName()) clickedStack.displayName else clickedStack.itemName) + + milestoneNamePattern.matchMatcher(nameText) { + val itemLore = clickedStack.getLore() + if (!itemLore.any { it == "§eClick to claim!" }) return + + // Will never match both all time and shop patterns together + allTimeLorePattern.firstMatcher(clickedStack.getLore()) { + LorenzChatEvent( + "§d§lHOPPITY'S HUNT §r§dYou claimed a §r§6§lChocolate Milestone Rabbit §r§din the Chocolate Factory§r§d!", + ChatComponentText(""), + ).postAndCatch() + } + + shopLorePattern.firstMatcher(clickedStack.getLore()) { + LorenzChatEvent( + "§d§lHOPPITY'S HUNT §r§dYou claimed a §r§6§lShop Milestone Rabbit §r§din the Chocolate Factory§r§d!", + ChatComponentText(""), + ).postAndCatch() + } + } + } + + // Dumbed down version of the Compact Chat for Hoppity's, + // with the additional native context of side dishes + fun handleChat(event: LorenzChatEvent) { + HoppityEggsManager.eggFoundPatterns.forEach { + it.matchMatcher(event.message) { + resetChatData() + lastChatMeal = getEggType(event) + attemptFire(event) + } + } + + HoppityEggsManager.eggBoughtPattern.matchMatcher(event.message) { + if (group("rabbitname").equals(lastName)) { + rabbitBought = true + lastChatMeal = HoppityEggType.BOUGHT + attemptFire(event) + } + } + + HoppityEggsManager.rabbitFoundPattern.matchMatcher(event.message) { + // The only cases where "You found ..." will come in with more than 1 message, + // or empty for hoppityEggChat, is where the rabbit was purchased from hoppity, + // In the case of buying, we want to reset variables to a clean state during this capture, + // as the important capture for the purchased message is the final message in + // the chain; "You found [rabbit]" -> "Dupe/New Rabbit" -> "You bought [rabbit]" + if ((hoppityEggChat.isEmpty() || hoppityEggChat.size > 1)) { + resetChatData() + } + + lastName = group("name") + lastRarity = group("rarity") + attemptFire(event) + } + + HoppityEggsManager.newRabbitFound.matchMatcher(event.message) { + newRabbit = true + groupOrNull("other")?.let { + attemptFire(event) + return + } + attemptFire(event) + } + } + + fun attemptFire(event: LorenzChatEvent, lastDuplicateAmount: Long? = null) { + lastDuplicateAmount?.let { + this.lastDuplicateAmount = it + } + hoppityEggChat.add(event.message) + if (lastDuplicateAmount != null) { + this.duplicate = true + } + val lastChatMeal = lastChatMeal ?: return + if (hoppityEggChat.size == 3) { + RabbitFoundEvent(lastChatMeal, duplicate, lastName, lastDuplicateAmount ?: 0).post() + } + } +} diff --git a/src/main/java/at/hannibal2/skyhanni/features/event/hoppity/HoppityEggDisplayManager.kt b/src/main/java/at/hannibal2/skyhanni/features/event/hoppity/HoppityEggDisplayManager.kt index f09aa4eac..fe908e58f 100644 --- a/src/main/java/at/hannibal2/skyhanni/features/event/hoppity/HoppityEggDisplayManager.kt +++ b/src/main/java/at/hannibal2/skyhanni/features/event/hoppity/HoppityEggDisplayManager.kt @@ -76,7 +76,7 @@ object HoppityEggDisplayManager { if (ReminderUtils.isBusy() && !config.showWhileBusy) return emptyList() val displayList = - HoppityEggType.entries.map { "§7 - ${it.formattedName} ${it.timeUntil().format()}" }.toMutableList() + HoppityEggType.resettingEntries.map { "§7 - ${it.formattedName} ${it.timeUntil().format()}" }.toMutableList() displayList.add(0, "§bUnclaimed Eggs:") if (config.showCollectedLocationCount && LorenzUtils.inSkyBlock) { diff --git a/src/main/java/at/hannibal2/skyhanni/features/event/hoppity/HoppityEggLocator.kt b/src/main/java/at/hannibal2/skyhanni/features/event/hoppity/HoppityEggLocator.kt index dcf56aac6..d15d43d81 100644 --- a/src/main/java/at/hannibal2/skyhanni/features/event/hoppity/HoppityEggLocator.kt +++ b/src/main/java/at/hannibal2/skyhanni/features/event/hoppity/HoppityEggLocator.kt @@ -9,7 +9,6 @@ import at.hannibal2.skyhanni.events.LorenzWorldChangeEvent import at.hannibal2.skyhanni.events.ReceiveParticleEvent import at.hannibal2.skyhanni.features.fame.ReminderUtils import at.hannibal2.skyhanni.features.garden.GardenAPI -import at.hannibal2.skyhanni.features.inventory.chocolatefactory.ChocolateFactoryAPI import at.hannibal2.skyhanni.skyhannimodule.SkyHanniModule import at.hannibal2.skyhanni.utils.ColorUtils.toChromaColor import at.hannibal2.skyhanni.utils.InventoryUtils @@ -278,7 +277,7 @@ object HoppityEggLocator { type == EnumParticleTypes.ENCHANTMENT_TABLE && speed == -2.0f && count == 10 fun isEnabled() = LorenzUtils.inSkyBlock && config.waypoints && !GardenAPI.inGarden() && - !ReminderUtils.isBusy(true) && ChocolateFactoryAPI.isHoppityEvent() + !ReminderUtils.isBusy(true) && HoppityAPI.isHoppityEvent() private val ItemStack.isLocatorItem get() = getInternalName() == locatorItem diff --git a/src/main/java/at/hannibal2/skyhanni/features/event/hoppity/HoppityEggType.kt b/src/main/java/at/hannibal2/skyhanni/features/event/hoppity/HoppityEggType.kt index 2846262e6..23e00cf88 100644 --- a/src/main/java/at/hannibal2/skyhanni/features/event/hoppity/HoppityEggType.kt +++ b/src/main/java/at/hannibal2/skyhanni/features/event/hoppity/HoppityEggType.kt @@ -14,9 +14,14 @@ enum class HoppityEggType( BREAKFAST("Breakfast", "§6", 7), LUNCH("Lunch", "§9", 14), DINNER("Dinner", "§a", 21), + SIDE_DISH("Side Dish", "§6§l", -1), + BOUGHT("Bought", "§a", -1), + CHOCOLATE_SHOP_MILESTONE("Shop Milestone", "§6", -1), + CHOCOLATE_FACTORY_MILESTONE("Chocolate Milestone", "§6", -1) ; fun timeUntil(): Duration { + if (resetsAt == -1) return Duration.INFINITE val now = SkyBlockTime.now() if (now.hour >= resetsAt) { return now.copy(day = now.day + 1, hour = resetsAt, minute = 0, second = 0) @@ -38,7 +43,9 @@ enum class HoppityEggType( val coloredName get() = "$mealColor$mealName" companion object { - fun allFound() = entries.forEach { it.markClaimed() } + val resettingEntries = entries.filter { it.resetsAt != -1 } + + fun allFound() = resettingEntries.forEach { it.markClaimed() } fun getMealByName(mealName: String) = entries.find { it.mealName == mealName } @@ -47,7 +54,7 @@ enum class HoppityEggType( val currentSbDay = currentSbTime.day val currentSbHour = currentSbTime.hour - for (eggType in entries) { + for (eggType in resettingEntries) { if (currentSbHour < eggType.resetsAt || eggType.lastResetDay == currentSbDay) continue eggType.markSpawned() eggType.lastResetDay = currentSbDay @@ -60,11 +67,11 @@ enum class HoppityEggType( } fun eggsRemaining(): Boolean { - return entries.any { !it.claimed } + return resettingEntries.any { !it.claimed } } fun allEggsRemaining(): Boolean { - return entries.all { !it.claimed } + return resettingEntries.all { !it.claimed } } } } diff --git a/src/main/java/at/hannibal2/skyhanni/features/event/hoppity/HoppityEggsCompactChat.kt b/src/main/java/at/hannibal2/skyhanni/features/event/hoppity/HoppityEggsCompactChat.kt index 35f87d7da..6bc1c8dfc 100644 --- a/src/main/java/at/hannibal2/skyhanni/features/event/hoppity/HoppityEggsCompactChat.kt +++ b/src/main/java/at/hannibal2/skyhanni/features/event/hoppity/HoppityEggsCompactChat.kt @@ -1,7 +1,11 @@ package at.hannibal2.skyhanni.features.event.hoppity -import at.hannibal2.skyhanni.config.features.event.HoppityEggsConfig +import at.hannibal2.skyhanni.config.features.event.hoppity.HoppityEggsConfig import at.hannibal2.skyhanni.events.LorenzChatEvent +import at.hannibal2.skyhanni.features.event.hoppity.HoppityEggType.CHOCOLATE_FACTORY_MILESTONE +import at.hannibal2.skyhanni.features.event.hoppity.HoppityEggType.CHOCOLATE_SHOP_MILESTONE +import at.hannibal2.skyhanni.features.event.hoppity.HoppityEggType.SIDE_DISH +import at.hannibal2.skyhanni.features.event.hoppity.HoppityEggsManager.eggFoundPatterns import at.hannibal2.skyhanni.features.event.hoppity.HoppityEggsManager.getEggType import at.hannibal2.skyhanni.features.inventory.chocolatefactory.ChocolateFactoryAPI import at.hannibal2.skyhanni.utils.ChatUtils @@ -67,7 +71,10 @@ object HoppityEggsCompactChat { private fun createCompactMessage(): String { val mealName = lastChatMeal?.coloredName ?: "" - val mealNameFormatted = if (rabbitBought) "§aBought Rabbit" else "$mealName Egg" + val mealNameFormatted = if (rabbitBought) "§aBought Rabbit" + else if (lastChatMeal == SIDE_DISH) "§6§lSide Dish §r§6Egg" + else if (lastChatMeal == CHOCOLATE_SHOP_MILESTONE || lastChatMeal == CHOCOLATE_FACTORY_MILESTONE) "§6§lMilestone Rabbit" + else "$mealName Egg" val rarityConfig = HoppityEggsManager.config.rarityInCompact return if (duplicate) { @@ -86,10 +93,12 @@ object HoppityEggsCompactChat { } fun handleChat(event: LorenzChatEvent) { - HoppityEggsManager.eggFoundPattern.matchMatcher(event.message) { - resetCompactData() - lastChatMeal = getEggType(event) - compactChat(event) + eggFoundPatterns.forEach { + it.matchMatcher(event.message) { + resetCompactData() + lastChatMeal = getEggType(event) + compactChat(event) + } } HoppityEggsManager.eggBoughtPattern.matchMatcher(event.message) { @@ -100,12 +109,12 @@ object HoppityEggsCompactChat { } HoppityEggsManager.rabbitFoundPattern.matchMatcher(event.message) { - // The only case where "You found ..." will come in with more than 1 message, - // or empty for hoppityEggChat, is where the rabbit was purchased from hoppity - // in this case, we want to reset variables to a clean state during this capture, + // The only cases where "You found ..." will come in with more than 1 message, + // or empty for hoppityEggChat, is where the rabbit was purchased from hoppity, + // In the case of buying, we want to reset variables to a clean state during this capture, // as the important capture for the purchased message is the final message in // the chain; "You found [rabbit]" -> "Dupe/New Rabbit" -> "You bought [rabbit]" - if (hoppityEggChat.isEmpty() || hoppityEggChat.size > 1) { + if ((hoppityEggChat.isEmpty() || hoppityEggChat.size > 1)) { resetCompactData() } @@ -130,7 +139,9 @@ object HoppityEggsCompactChat { } } - fun clickableCompact(onClick: () -> Unit): Boolean = if (hoppityEggChat.isNotEmpty()) { + fun clickableCompact(onClick: () -> Unit): Boolean = if (hoppityEggChat.isNotEmpty() && !rabbitBought && lastChatMeal != null && + HoppityEggType.resettingEntries.contains(lastChatMeal) + ) { val hover = hoppityEggChat.joinToString("\n") + " \n§eClick here to share the location of this chocolate egg with the server!" hoppityEggChat.clear() ChatUtils.clickableChat( diff --git a/src/main/java/at/hannibal2/skyhanni/features/event/hoppity/HoppityEggsManager.kt b/src/main/java/at/hannibal2/skyhanni/features/event/hoppity/HoppityEggsManager.kt index 93ae797e7..fcf4b17f0 100644 --- a/src/main/java/at/hannibal2/skyhanni/features/event/hoppity/HoppityEggsManager.kt +++ b/src/main/java/at/hannibal2/skyhanni/features/event/hoppity/HoppityEggsManager.kt @@ -25,6 +25,7 @@ import at.hannibal2.skyhanni.utils.StringUtils.removeColor import at.hannibal2.skyhanni.utils.TimeUtils.format import net.minecraftforge.fml.common.eventhandler.SubscribeEvent import java.util.regex.Matcher +import java.util.regex.Pattern import kotlin.time.Duration.Companion.minutes import kotlin.time.Duration.Companion.seconds @@ -37,13 +38,37 @@ object HoppityEggsManager { * REGEX-TEST: §d§lHOPPITY'S HUNT §r§dYou found a §r§9Chocolate Lunch Egg §r§don a ledge next to the stairs up§r§d! * REGEX-TEST: §d§lHOPPITY'S HUNT §r§dYou found a §r§aChocolate Dinner Egg §r§dbehind Emissary Sisko§r§d! * REGEX-TEST: §d§lHOPPITY'S HUNT §r§dYou found a §r§9Chocolate Lunch Egg §r§dnear the Diamond Essence Shop§r§d! + * REGEX-TEST: §d§lHOPPITY'S HUNT §r§dYou found a §r§6§lSide Dish §r§6Egg §r§din the Chocolate Factory§r§d! */ - val eggFoundPattern by ChocolateFactoryAPI.patternGroup.pattern( + private val eggFoundPattern by ChocolateFactoryAPI.patternGroup.pattern( "egg.found", "§d§lHOPPITY'S HUNT §r§dYou found a §r§.Chocolate (?<meal>\\w+) Egg §r§d(?<note>.*)§r§d!", ) /** + * REGEX-TEST: §d§lHOPPITY'S HUNT §r§dYou found a §r§6§lSide Dish §r§6Egg §r§din the Chocolate Factory§r§d! + */ + private val sideDishEggFoundPattern by ChocolateFactoryAPI.patternGroup.pattern( + "sidedish.found", + "§d§lHOPPITY'S HUNT §r§dYou found a §r§6§l(?<meal>.*) §r§6Egg §r§din the Chocolate Factory§r§d!", + ) + + /** + * REGEX-TEST: §d§lHOPPITY'S HUNT §r§dYou claimed a §r§6§lShop Milestone Rabbit §r§din the Chocolate Factory§r§d! + * REGEX-TEST: §d§lHOPPITY'S HUNT §r§dYou claimed a §r§6§lChocolate Milestone Rabbit §r§din the Chocolate Factory§r§d! + */ + private val milestoneRabbitFoundPattern by ChocolateFactoryAPI.patternGroup.pattern( + "milestone.claimed", + "§d§lHOPPITY'S HUNT §r§dYou claimed a §r§6§l(?<meal>[\\w ]+) Rabbit §r§din the Chocolate Factory§r§d!", + ) + + val eggFoundPatterns: List<Pattern> = listOf( + eggFoundPattern, + sideDishEggFoundPattern, + milestoneRabbitFoundPattern, + ) + + /** * REGEX-TEST: §aYou bought §r§9Casanova §r§afor §r§6970,000 Coins§r§a! * REGEX-TEST: §aYou bought §r§fHeidie §r§afor §r§6194,000 Coins§r§a! */ @@ -74,7 +99,7 @@ object HoppityEggsManager { val duplicateRabbitFound by ChocolateFactoryAPI.patternGroup.pattern( "rabbit.duplicate", - "§7§lDUPLICATE RABBIT! §6\\+(?<amount>[\\d,]+) Chocolate" + "§7§lDUPLICATE RABBIT! §6\\+(?<amount>[\\d,]+) Chocolate", ) private val noEggsLeftPattern by ChocolateFactoryAPI.patternGroup.pattern( @@ -123,8 +148,9 @@ object HoppityEggsManager { } HoppityEggsCompactChat.handleChat(event) + HoppityAPI.handleChat(event) - if (!ChocolateFactoryAPI.isHoppityEvent()) return + if (!HoppityAPI.isHoppityEvent()) return eggFoundPattern.matchMatcher(event.message) { HoppityEggLocations.saveNearestEgg() @@ -141,7 +167,7 @@ object HoppityEggsManager { HoppityEggType.allFound() if (config.timeInChat) { - val nextEgg = HoppityEggType.entries.minByOrNull { it.timeUntil() } ?: return + val nextEgg = HoppityEggType.resettingEntries.minByOrNull { it.timeUntil() } ?: return ChatUtils.chat("§eNext egg available in §b${nextEgg.timeUntil().format()}§e.") event.blockedReason = "hoppity_egg" } @@ -151,7 +177,7 @@ object HoppityEggsManager { eggAlreadyCollectedPattern.matchMatcher(event.message) { getEggType(event).markClaimed() if (config.timeInChat) { - val nextEgg = HoppityEggType.entries.minByOrNull { it.timeUntil() } ?: return + val nextEgg = HoppityEggType.resettingEntries.minByOrNull { it.timeUntil() } ?: return ChatUtils.chat("§eNext egg available in §b${nextEgg.timeUntil().format()}§e.") event.blockedReason = "hoppity_egg" } @@ -222,7 +248,7 @@ object HoppityEggsManager { if (lastWarnTime.passedSince() < 1.minutes) return lastWarnTime = now() - val amount = HoppityEggType.entries.size + val amount = HoppityEggType.resettingEntries.size val message = "All $amount Hoppity Eggs are ready to be found!" if (config.warpUnclaimedEggs) { if (LorenzUtils.inSkyBlock) { @@ -255,5 +281,5 @@ object HoppityEggsManager { } fun isActive() = (LorenzUtils.inSkyBlock || (LorenzUtils.onHypixel && config.showOutsideSkyblock)) && - ChocolateFactoryAPI.isHoppityEvent() + HoppityAPI.isHoppityEvent() } diff --git a/src/main/java/at/hannibal2/skyhanni/features/event/hoppity/HoppityEventSummary.kt b/src/main/java/at/hannibal2/skyhanni/features/event/hoppity/HoppityEventSummary.kt new file mode 100644 index 000000000..f334e407e --- /dev/null +++ b/src/main/java/at/hannibal2/skyhanni/features/event/hoppity/HoppityEventSummary.kt @@ -0,0 +1,277 @@ +package at.hannibal2.skyhanni.features.event.hoppity + +import at.hannibal2.skyhanni.SkyHanniMod +import at.hannibal2.skyhanni.api.event.HandleEvent +import at.hannibal2.skyhanni.config.features.event.hoppity.HoppityEventSummaryConfig.HoppityStat +import at.hannibal2.skyhanni.config.storage.ProfileSpecificStorage.HoppityEventStats +import at.hannibal2.skyhanni.config.storage.ProfileSpecificStorage.HoppityEventStats.RabbitData +import at.hannibal2.skyhanni.data.ProfileStorageData +import at.hannibal2.skyhanni.events.ProfileJoinEvent +import at.hannibal2.skyhanni.events.SecondPassedEvent +import at.hannibal2.skyhanni.events.hoppity.RabbitFoundEvent +import at.hannibal2.skyhanni.features.inventory.chocolatefactory.ChocolateFactoryAPI +import at.hannibal2.skyhanni.skyhannimodule.SkyHanniModule +import at.hannibal2.skyhanni.test.command.ErrorManager +import at.hannibal2.skyhanni.utils.ChatUtils +import at.hannibal2.skyhanni.utils.CollectionUtils.addOrPut +import at.hannibal2.skyhanni.utils.CollectionUtils.sumAllValues +import at.hannibal2.skyhanni.utils.LorenzRarity +import at.hannibal2.skyhanni.utils.LorenzUtils +import at.hannibal2.skyhanni.utils.NumberUtil.addSeparators +import at.hannibal2.skyhanni.utils.SkyBlockTime +import at.hannibal2.skyhanni.utils.SkyBlockTime.Companion.SKYBLOCK_DAY_MILLIS +import at.hannibal2.skyhanni.utils.SkyBlockTime.Companion.SKYBLOCK_HOUR_MILLIS +import at.hannibal2.skyhanni.utils.SkyblockSeason +import at.hannibal2.skyhanni.utils.StringUtils +import at.hannibal2.skyhanni.utils.TimeUtils.format +import net.minecraftforge.fml.common.eventhandler.SubscribeEvent +import kotlin.time.Duration.Companion.milliseconds + +@SkyHanniModule +object HoppityEventSummary { + private val config get() = SkyHanniMod.feature.event.hoppityEggs + private val lineHeader = " ".repeat(4) + + private var firstInCf = 0L + private var inCfNow = false + + @HandleEvent + fun onRabbitFound(event: RabbitFoundEvent) { + if (!HoppityAPI.isHoppityEvent()) return + val stats = getYearStats().first ?: return + + stats.mealsFound.addOrPut(event.eggType, 1) + val rarity = HoppityAPI.rarityByRabbit(event.rabbitName) ?: return + val rarityMap = stats.rabbitsFound.getOrPut(rarity) { RabbitData() } + if (event.duplicate) rarityMap.dupes++ + else rarityMap.uniques++ + if (event.chocGained > 0) stats.dupeChocolateGained += event.chocGained + } + + @SubscribeEvent + fun onSecondPassed(event: SecondPassedEvent) { + if (!LorenzUtils.inSkyBlock) return + checkEnded() + if (!HoppityAPI.isHoppityEvent()) return + checkInit() + checkAddCfTime() + } + + @SubscribeEvent + fun onProfileJoin(event: ProfileJoinEvent) { + checkEnded() + } + + private fun checkInit() { + val statStorage = ProfileStorageData.profileSpecific?.hoppityEventStats ?: return + val currentYear = SkyBlockTime.now().year + if (statStorage.containsKey(currentYear)) return + statStorage[currentYear] = HoppityEventStats() + } + + private fun getYearStats(year: Int? = null): Pair<HoppityEventStats?, Int> { + val queryYear = year ?: SkyBlockTime.now().year + val storage = ProfileStorageData.profileSpecific?.hoppityEventStats ?: return Pair(null, queryYear) + if (!storage.containsKey(queryYear)) return Pair(null, queryYear) + return Pair(storage[queryYear], queryYear) + } + + private fun checkAddCfTime() { + if (ChocolateFactoryAPI.inChocolateFactory && !this.inCfNow) { + this.inCfNow = true + firstInCf = SkyBlockTime.now().toMillis() + } else if (!ChocolateFactoryAPI.inChocolateFactory && this.inCfNow) { + val stats = getYearStats().first ?: return + stats.millisInCf += (SkyBlockTime.now().toMillis() - firstInCf) + this.inCfNow = false + firstInCf = 0 + } + } + + private fun checkEnded() { + val (stats, year) = getYearStats() + if (stats == null || stats.summarized) return + + val currentYear = SkyBlockTime.now().year + val currentSeason = SkyblockSeason.currentSeason + val isSpring = currentSeason == SkyblockSeason.SPRING + + if (year < currentYear || (year == currentYear && !isSpring) && config.eventSummary.enabled) { + sendStatsMessage(stats, year) + (ProfileStorageData.profileSpecific?.hoppityEventStats?.get(year)?.also { it.summarized = true } + ?: ErrorManager.skyHanniError("Could not save summarization state in Hoppity Event Summarization.")) + } + } + + // First event was year 346 -> #1, 20th event was year 365, etc. + private fun getHoppityEventNumber(skyblockYear: Int): Int = (skyblockYear - 345) + + fun addStrayCaught(rarity: LorenzRarity, chocGained: Long) { + if (!HoppityAPI.isHoppityEvent()) return + val stats = getYearStats().first ?: return + val rarityMap = stats.rabbitsFound.getOrPut(rarity) { RabbitData() } + rarityMap.strays++ + stats.strayChocolateGained += chocGained + } + + private fun StringBuilder.appendHeadedLine(line: String) { + appendLine("$lineHeader$line") + } + + private fun StringBuilder.addExtraChocFormatLine(chocGained: Long) { + if (chocGained <= 0) return + appendHeadedLine( + buildString { + append(" §6+${chocGained.addSeparators()} Chocolate") + if (SkyHanniMod.feature.inventory.chocolateFactory.showDuplicateTime) { + val timeFormatted = ChocolateFactoryAPI.timeUntilNeed(chocGained).format(maxUnits = 2) + append(" §7(§a+§b$timeFormatted§7)") + } + }, + ) + } + + private val summaryOperationList by lazy { + buildMap<HoppityStat, (sb: StringBuilder, stats: HoppityEventStats, year: Int) -> Unit> { + put(HoppityStat.MEAL_EGGS_FOUND) { sb, stats, year -> + stats.getEggsFoundFormat(year).takeIf { it != null }?.let { + sb.appendHeadedLine(it) + } + } + + put(HoppityStat.HOPPITY_RABBITS_BOUGHT) { sb, stats, _ -> + stats.mealsFound[HoppityEggType.BOUGHT]?.let { + sb.appendHeadedLine("§7You bought §b$it §f${StringUtils.pluralize(it, "Rabbit")} §7from §aHoppity§7.") + } + } + + put(HoppityStat.SIDE_DISH_EGGS) { sb, stats, _ -> + stats.mealsFound[HoppityEggType.SIDE_DISH]?.let { + sb.appendHeadedLine( + "§7You found §b$it §6§lSide Dish §r§6${StringUtils.pluralize(it, "Egg")}§7 " + + "§7in the §6Chocolate Factory§7.", + ) + } + } + + put(HoppityStat.MILESTONE_RABBITS) { sb, stats, _ -> + ((stats.mealsFound[HoppityEggType.CHOCOLATE_FACTORY_MILESTONE] ?: 0) + + (stats.mealsFound[HoppityEggType.CHOCOLATE_SHOP_MILESTONE] ?: 0)).takeIf { it > 0 }?.let { + sb.appendHeadedLine("§7You claimed §b$it §6§lMilestone §r§6${StringUtils.pluralize(it, "Rabbit")}§7.") + } + } + + put(HoppityStat.NEW_RABBITS) { sb, stats, _ -> + getRabbitsFormat(stats.rabbitsFound.mapValues { m -> m.value.uniques }, "Unique").forEach { + sb.appendHeadedLine(it) + } + } + + put(HoppityStat.DUPLICATE_RABBITS) { sb, stats, _ -> + getRabbitsFormat(stats.rabbitsFound.mapValues { m -> m.value.dupes }, "Duplicate").forEach { + sb.appendHeadedLine(it) + } + sb.addExtraChocFormatLine(stats.dupeChocolateGained) + } + + put(HoppityStat.STRAY_RABBITS) { sb, stats, _ -> + getRabbitsFormat(stats.rabbitsFound.mapValues { m -> m.value.strays }, "Stray").forEach { + sb.appendHeadedLine(it) + } + sb.addExtraChocFormatLine(stats.strayChocolateGained) + } + + put(HoppityStat.TIME_IN_CF) { sb, stats, _ -> + sb.appendHeadedLine("§7You spent §b${stats.millisInCf.milliseconds.format(maxUnits = 2)} §7in the §6Chocolate Factory§7.") + } + + put(HoppityStat.EMPTY_1) { sb, _, _ -> sb.appendLine() } + put(HoppityStat.EMPTY_2) { sb, _, _ -> sb.appendLine() } + put(HoppityStat.EMPTY_3) { sb, _, _ -> sb.appendLine() } + put(HoppityStat.EMPTY_4) { sb, _, _ -> sb.appendLine() } + } + } + + fun sendStatsMessage(it: Array<String>) { + val statsStorage = ProfileStorageData.profileSpecific?.hoppityEventStats ?: return + val currentYear = SkyBlockTime.now().year + val statsYearList = statsStorage.keys.takeIf { it.isNotEmpty() } ?: mutableListOf() + val statsYearFormatList = statsStorage.keys.takeIf { it.isNotEmpty() }?.map { + "§b$it${if (it == currentYear) " §a(Current)§r" else ""}" + }?.toMutableList() ?: mutableListOf() + + val parsedInt: Int? = if (it.size == 1) it[0].toIntOrNull() else null + + val availableYearsFormat = "§eStats are available for the following years:§af\n${statsYearFormatList.joinToString("§e,") { it }}" + + if (parsedInt == null) { + if (HoppityAPI.isHoppityEvent()) { + val stats = getYearStats(currentYear).first ?: return + sendStatsMessage(stats, currentYear) + } else ChatUtils.chat(availableYearsFormat) + } else if (!statsYearList.contains(parsedInt)) { + ChatUtils.chat("Could not find stats for year §b$parsedInt§e.\n$availableYearsFormat") + } else { + val stats = getYearStats(parsedInt).first ?: return + sendStatsMessage(stats, parsedInt) + } + } + + private fun sendStatsMessage(stats: HoppityEventStats, eventYear: Int?) { + if (eventYear == null) return + val summaryBuilder: StringBuilder = StringBuilder() + summaryBuilder.appendLine("§d§l${"▬".repeat(64)}") + + // Header + summaryBuilder.appendLine("${" ".repeat(26)}§d§lHoppity's Hunt #${getHoppityEventNumber(eventYear)} Stats") + summaryBuilder.appendLine() + + // Various stats from config + val statsBuilder: StringBuilder = StringBuilder() + config.eventSummary.statDisplayList.forEach { + summaryOperationList[it]?.invoke(statsBuilder, stats, eventYear) + } + if (statsBuilder.toString().replace("\n", "").isEmpty()) { + statsBuilder.appendHeadedLine("§c§lNothing to show!\n§c§oGo find some eggs!") + } + + // Append stats + summaryBuilder.append(statsBuilder) + + // Footer + summaryBuilder.append("§d§l${"▬".repeat(64)}") + + ChatUtils.chat(summaryBuilder.toString(), prefix = false) + } + + private fun HoppityEventStats.getEggsFoundFormat(year: Int): String? = + mealsFound.filterKeys { it in HoppityEggType.resettingEntries }.sumAllValues().toInt().takeIf { it > 0 }?.let { + val milliDifference = SkyBlockTime.now().toMillis() - SkyBlockTime.fromSbYear(year).toMillis() + val pastEvent = milliDifference > SkyBlockTime.SKYBLOCK_SEASON_MILLIS + // Calculate total eggs from complete days and incomplete day periods + val previousEggs = if (pastEvent) 279 else (milliDifference / SKYBLOCK_DAY_MILLIS).toInt() * 3 + val currentEggs = when { + pastEvent -> 0 + // Add eggs for the current day based on time of day + milliDifference % SKYBLOCK_DAY_MILLIS >= SKYBLOCK_HOUR_MILLIS * 21 -> 3 // Dinner egg, 9 PM + milliDifference % SKYBLOCK_DAY_MILLIS >= SKYBLOCK_HOUR_MILLIS * 14 -> 2 // Lunch egg, 2 PM + milliDifference % SKYBLOCK_DAY_MILLIS >= SKYBLOCK_HOUR_MILLIS * 7 -> 1 // Breakfast egg, 7 AM + else -> 0 + } + val spawnedMealsEggs = previousEggs + currentEggs + "§7You found §b$it§7/§a$spawnedMealsEggs §6Chocolate Meal ${StringUtils.pluralize(it, "Egg")}§7." + } + + + private fun getRabbitsFormat(rarityMap: Map<LorenzRarity, Int>, name: String): List<String> { + val rabbitsSum = rarityMap.values.sum() + if (rabbitsSum == 0) return emptyList() + + return mutableListOf( + "§7$name Rabbits: §f$rabbitsSum", + HoppityAPI.hoppityRarities.joinToString(" §7-") { + " ${it.chatColorCode}${rarityMap[it] ?: 0}" + }, + ) + } +} diff --git a/src/main/java/at/hannibal2/skyhanni/features/event/hoppity/HoppityNpc.kt b/src/main/java/at/hannibal2/skyhanni/features/event/hoppity/HoppityNpc.kt index 877e161ef..217e93bb4 100644 --- a/src/main/java/at/hannibal2/skyhanni/features/event/hoppity/HoppityNpc.kt +++ b/src/main/java/at/hannibal2/skyhanni/features/event/hoppity/HoppityNpc.kt @@ -50,7 +50,7 @@ object HoppityNpc { if (!isReminderEnabled()) return if (ReminderUtils.isBusy()) return if (hoppityYearOpened == SkyBlockTime.now().year) return - if (!ChocolateFactoryAPI.isHoppityEvent()) return + if (!HoppityAPI.isHoppityEvent()) return if (lastReminderSent.passedSince() <= 2.minutes) return ChatUtils.clickableChat( @@ -59,7 +59,7 @@ object HoppityNpc { disableReminder() ChatUtils.chat("§eHoppity's Shop reminder disabled.") }, - oneTimeClick = true + oneTimeClick = true, ) lastReminderSent = SimpleTimeMark.now() |