From e17e70b6933982ca417577f441928f77cbbf9ef1 Mon Sep 17 00:00:00 2001 From: J10a1n15 <45315647+j10a1n15@users.noreply.github.com> Date: Mon, 9 Sep 2024 15:24:39 +0200 Subject: Feature: Current Minister in Calendar (#2342) Co-authored-by: hannibal2 <24389977+hannibal00212@users.noreply.github.com> --- .../config/features/inventory/InventoryConfig.java | 6 ++ .../java/at/hannibal2/skyhanni/data/MayorAPI.kt | 18 +++-- src/main/java/at/hannibal2/skyhanni/data/Mayors.kt | 20 +++-- .../features/inventory/MinisterInCalendar.kt | 86 ++++++++++++++++++++++ .../java/at/hannibal2/skyhanni/utils/ItemUtils.kt | 13 ++++ .../at/hannibal2/skyhanni/utils/StringUtils.kt | 1 - 6 files changed, 130 insertions(+), 14 deletions(-) create mode 100644 src/main/java/at/hannibal2/skyhanni/features/inventory/MinisterInCalendar.kt (limited to 'src/main/java') diff --git a/src/main/java/at/hannibal2/skyhanni/config/features/inventory/InventoryConfig.java b/src/main/java/at/hannibal2/skyhanni/config/features/inventory/InventoryConfig.java index dfb8f1f9c..cf162ad7b 100644 --- a/src/main/java/at/hannibal2/skyhanni/config/features/inventory/InventoryConfig.java +++ b/src/main/java/at/hannibal2/skyhanni/config/features/inventory/InventoryConfig.java @@ -275,6 +275,12 @@ public class InventoryConfig { @FeatureToggle public boolean stonkOfStonkPrice = true; + @Expose + @ConfigOption(name = "Minister in Calendar", desc = "Show the Minister with their perk in the Calendar.") + @ConfigEditorBoolean + @FeatureToggle + public boolean ministerInCalendar = true; + @Expose @ConfigOption(name = "Show hex as actual color", desc = "Changes the color of hex codes to the actual color.") @ConfigEditorBoolean diff --git a/src/main/java/at/hannibal2/skyhanni/data/MayorAPI.kt b/src/main/java/at/hannibal2/skyhanni/data/MayorAPI.kt index a8024ea36..2f89b340b 100644 --- a/src/main/java/at/hannibal2/skyhanni/data/MayorAPI.kt +++ b/src/main/java/at/hannibal2/skyhanni/data/MayorAPI.kt @@ -24,6 +24,7 @@ import at.hannibal2.skyhanni.utils.ConditionalUtils.onToggle import at.hannibal2.skyhanni.utils.HypixelCommands import at.hannibal2.skyhanni.utils.ItemUtils.getLore import at.hannibal2.skyhanni.utils.LorenzUtils +import at.hannibal2.skyhanni.utils.RegexUtils.matchMatcher import at.hannibal2.skyhanni.utils.RegexUtils.matches import at.hannibal2.skyhanni.utils.SimpleTimeMark import at.hannibal2.skyhanni.utils.SimpleTimeMark.Companion.asTimeMark @@ -63,17 +64,18 @@ object MayorAPI { /** * REGEX-TEST: Calendar and Events */ - private val calendarGuiPattern by group.pattern( + val calendarGuiPattern by group.pattern( "calendar.gui", "Calendar and Events", ) /** * REGEX-TEST: §dMayor Jerry + * REGEX-TEST: §cMayor Aatrox */ - private val jerryHeadPattern by group.pattern( - "jerry.head", - "§dMayor Jerry", + private val mayorHeadPattern by group.pattern( + "mayor.head", + "§.Mayor (?.*)", ) /** @@ -162,7 +164,11 @@ object MayorAPI { if (!calendarGuiPattern.matches(event.inventoryName)) return - val stack: ItemStack = event.inventoryItems.values.firstOrNull { jerryHeadPattern.matches(it.displayName) } ?: return + val stack: ItemStack = event.inventoryItems.values.firstOrNull { + mayorHeadPattern.matchMatcher(it.displayName) { + group("name") == "Jerry" + } ?: false + } ?: return val perk = stack.getLore().nextAfter({ perkpocalypsePerksPattern.matches(it) }) ?: return // This is the first Perk of the Perkpocalypse Mayor @@ -248,7 +254,7 @@ object MayorAPI { event.addIrrelevant { add("Current Mayor: ${currentMayor?.name ?: "Unknown"}") add("Active Perks: ${currentMayor?.activePerks}") - add("Last Update: $lastUpdate (${lastUpdate.passedSince()} ago)") + add("Last Update: ${lastUpdate.formattedDate("EEEE, MMM d h:mm a")} (${lastUpdate.passedSince()} ago)") add("Time Till Next Mayor: ${nextMayorTimestamp.timeUntil()}") add("Current Minister: ${currentMinister?.name ?: "Unknown"}") add("Current Minister Perk: ${currentMinister?.activePerks}") diff --git a/src/main/java/at/hannibal2/skyhanni/data/Mayors.kt b/src/main/java/at/hannibal2/skyhanni/data/Mayors.kt index 2215f620c..78647944c 100644 --- a/src/main/java/at/hannibal2/skyhanni/data/Mayors.kt +++ b/src/main/java/at/hannibal2/skyhanni/data/Mayors.kt @@ -132,8 +132,12 @@ enum class Mayor( ) return null } - val perks = perksJson.mapNotNull { perk -> - Perk.entries.firstOrNull { it.perkName == perk.renameIfFoxyExtraEventPerkFound() } + + val perks = perksJson.mapNotNull { perkJson -> + val perk = Perk.entries.firstOrNull { it.perkName == perkJson.renameIfFoxyExtraEventPerkFound() } + perk?.also { + it.description = perkJson.description + } } mayor.addPerks(perks) @@ -149,17 +153,16 @@ enum class Mayor( } } - private fun MayorPerk.renameIfFoxyExtraEventPerkFound(): String? { + private fun MayorPerk.renameIfFoxyExtraEventPerkFound(): String { val foxyExtraEventPairs = mapOf( "Spooky Festival" to "Extra Event (Spooky)", "Mining Fiesta" to "Extra Event (Mining)", "Fishing Festival" to "Extra Event (Fishing)", ) - foxyExtraEventPattern.matchMatcher(this.description) { - return foxyExtraEventPairs.entries.firstOrNull { it.key == group("event") }?.value - } - return this.name + return foxyExtraEventPattern.matchMatcher(this.description) { + foxyExtraEventPairs.entries.firstOrNull { it.key == group("event") }?.value + } ?: this.name } } } @@ -230,6 +233,9 @@ enum class Perk(val perkName: String) { ; var isActive = false + var description = "§cDescription failed to load from the API." + + override fun toString(): String = "$perkName: $description" companion object { fun getPerkFromName(name: String): Perk? = entries.firstOrNull { it.perkName == name } diff --git a/src/main/java/at/hannibal2/skyhanni/features/inventory/MinisterInCalendar.kt b/src/main/java/at/hannibal2/skyhanni/features/inventory/MinisterInCalendar.kt new file mode 100644 index 000000000..d078f0d1a --- /dev/null +++ b/src/main/java/at/hannibal2/skyhanni/features/inventory/MinisterInCalendar.kt @@ -0,0 +1,86 @@ +package at.hannibal2.skyhanni.features.inventory + +import at.hannibal2.skyhanni.SkyHanniMod +import at.hannibal2.skyhanni.data.Mayor +import at.hannibal2.skyhanni.data.MayorAPI +import at.hannibal2.skyhanni.events.InventoryCloseEvent +import at.hannibal2.skyhanni.events.InventoryOpenEvent +import at.hannibal2.skyhanni.events.render.gui.ReplaceItemEvent +import at.hannibal2.skyhanni.skyhannimodule.SkyHanniModule +import at.hannibal2.skyhanni.utils.InventoryUtils +import at.hannibal2.skyhanni.utils.ItemUtils.setLore +import at.hannibal2.skyhanni.utils.LorenzUtils +import at.hannibal2.skyhanni.utils.NEUInternalName.Companion.asInternalName +import at.hannibal2.skyhanni.utils.NEUItems.getItemStack +import at.hannibal2.skyhanni.utils.RegexUtils.matches +import at.hannibal2.skyhanni.utils.StringUtils.splitLines +import net.minecraft.client.player.inventory.ContainerLocalMenu +import net.minecraft.item.ItemStack +import net.minecraftforge.fml.common.eventhandler.SubscribeEvent + +@SkyHanniModule +object MinisterInCalendar { + + private const val MINISTER_SLOT = 38 + private var ministerItemStack: ItemStack? = null + + private val prefix = listOf( + "§8(from SkyHanni)", + "", + "§8§m--------------------------", + ) + private val suffix = listOf( + "§8§m--------------------------", + "", + "§7The Minister is who came in 2nd place", + "§7during the election. They have one", + "§7of their perks active.", + ) + + @SubscribeEvent + fun onInventoryOpen(event: InventoryOpenEvent) { + if (!isEnabled()) return + if (!MayorAPI.calendarGuiPattern.matches(InventoryUtils.openInventoryName())) return + val minister = MayorAPI.currentMinister ?: return + + val itemStack = "${minister.name}_MAYOR_MONSTER".asInternalName().getItemStack() + val ministerColor = MayorAPI.mayorNameToColorCode(minister.mayorName) + + ministerItemStack = changeItem(ministerColor, minister, itemStack) + } + + @SubscribeEvent + fun onInventoryClose(event: InventoryCloseEvent) { + if (!isEnabled()) return + if (!MayorAPI.calendarGuiPattern.matches(InventoryUtils.openInventoryName())) return + ministerItemStack = null + } + + @SubscribeEvent + fun replaceItem(event: ReplaceItemEvent) { + if (!isEnabled()) return + if (event.inventory !is ContainerLocalMenu || event.slot != MINISTER_SLOT) return + if (!MayorAPI.calendarGuiPattern.matches(InventoryUtils.openInventoryName())) return + event.replace(ministerItemStack ?: return) + } + + private fun changeItem( + ministerColor: String, + minister: Mayor, + item: ItemStack, + ): ItemStack? { + val ministerDisplayName = "${ministerColor}Minister ${minister.mayorName}" + val ministerLore = buildList { + addAll(prefix) + for (perk in minister.activePerks) { + add("$ministerColor${perk.perkName}") + addAll(perk.description.splitLines(170).removePrefix("§r").split("\n").map { "§7$it" }) + } + addAll(suffix) + } + + return item.setLore(ministerLore).setStackDisplayName(ministerDisplayName) + } + + fun isEnabled() = LorenzUtils.inSkyBlock && SkyHanniMod.feature.inventory.ministerInCalendar +} diff --git a/src/main/java/at/hannibal2/skyhanni/utils/ItemUtils.kt b/src/main/java/at/hannibal2/skyhanni/utils/ItemUtils.kt index 774638c5d..168a12d95 100644 --- a/src/main/java/at/hannibal2/skyhanni/utils/ItemUtils.kt +++ b/src/main/java/at/hannibal2/skyhanni/utils/ItemUtils.kt @@ -55,6 +55,19 @@ object ItemUtils { return list } + fun ItemStack.setLore(lore: List): ItemStack { + val tagCompound = this.tagCompound ?: NBTTagCompound() + val display = tagCompound.getCompoundTag("display") + val tagList = NBTTagList() + for (line in lore) { + tagList.appendTag(NBTTagString(line)) + } + display.setTag("Lore", tagList) + tagCompound.setTag("display", display) + this.tagCompound = tagCompound + return this + } + var ItemStack.extraAttributes: NBTTagCompound get() = this.tagCompound?.getCompoundTag("ExtraAttributes") ?: NBTTagCompound() set(value) { diff --git a/src/main/java/at/hannibal2/skyhanni/utils/StringUtils.kt b/src/main/java/at/hannibal2/skyhanni/utils/StringUtils.kt index e1a01e5df..f3076284d 100644 --- a/src/main/java/at/hannibal2/skyhanni/utils/StringUtils.kt +++ b/src/main/java/at/hannibal2/skyhanni/utils/StringUtils.kt @@ -247,7 +247,6 @@ object StringUtils { } ?: text } - /** * Creates a comma-separated list using natural formatting (a, b, and c). * @param list - the list of strings to join into a string, containing 0 or more elements. -- cgit