From 2aea746a4b54fe72b80afc70016b2d0f1df36637 Mon Sep 17 00:00:00 2001 From: CalMWolfs Date: Tue, 1 Oct 2024 11:02:14 +1000 Subject: [PATCH 1/5] Backend: Add skyhanni notifications --- .../skyhanni/data/NotificationManager.kt | 22 +++++++++++++++++++ 1 file changed, 22 insertions(+) create mode 100644 src/main/java/at/hannibal2/skyhanni/data/NotificationManager.kt diff --git a/src/main/java/at/hannibal2/skyhanni/data/NotificationManager.kt b/src/main/java/at/hannibal2/skyhanni/data/NotificationManager.kt new file mode 100644 index 000000000000..973fa140bdda --- /dev/null +++ b/src/main/java/at/hannibal2/skyhanni/data/NotificationManager.kt @@ -0,0 +1,22 @@ +package at.hannibal2.skyhanni.data + +import at.hannibal2.skyhanni.skyhannimodule.SkyHanniModule +import kotlin.time.Duration + +@SkyHanniModule +object NotificationManager { + + val notificationQueue = mutableListOf() + + +} + + +data class SkyhanniNotification(val title: String, val message: String, val length: Duration, val showOverInventory: Boolean = false) { + constructor(title: String, message: List, length: Duration, showOverInventory: Boolean = false) : this( + title, + message.joinToString(" "), + length, + false, + ) +} From 50e7a939f9af69420da96637323c6894fe0b6e40 Mon Sep 17 00:00:00 2001 From: CalMWolfs Date: Tue, 1 Oct 2024 18:28:37 +1000 Subject: [PATCH 2/5] finish and use SkyHanniNotification --- .../skyhanni/config/commands/Commands.kt | 7 ++ .../skyhanni/data/NotificationManager.kt | 89 +++++++++++++++++-- .../at/hannibal2/skyhanni/utils/ItemUtils.kt | 28 +++--- 3 files changed, 104 insertions(+), 20 deletions(-) diff --git a/src/main/java/at/hannibal2/skyhanni/config/commands/Commands.kt b/src/main/java/at/hannibal2/skyhanni/config/commands/Commands.kt index 4974c80f2d51..fbcd6f0eacbb 100644 --- a/src/main/java/at/hannibal2/skyhanni/config/commands/Commands.kt +++ b/src/main/java/at/hannibal2/skyhanni/config/commands/Commands.kt @@ -8,9 +8,11 @@ import at.hannibal2.skyhanni.config.features.About.UpdateStream import at.hannibal2.skyhanni.data.ChatManager import at.hannibal2.skyhanni.data.GardenCropMilestonesCommunityFix import at.hannibal2.skyhanni.data.GuiEditManager +import at.hannibal2.skyhanni.data.NotificationManager import at.hannibal2.skyhanni.data.PartyAPI import at.hannibal2.skyhanni.data.SackAPI import at.hannibal2.skyhanni.data.ScoreboardData +import at.hannibal2.skyhanni.data.SkyHanniNotification import at.hannibal2.skyhanni.data.TitleManager import at.hannibal2.skyhanni.data.TrackerManager import at.hannibal2.skyhanni.data.bazaar.HypixelBazaarFetcher @@ -107,6 +109,7 @@ import at.hannibal2.skyhanni.utils.repopatterns.RepoPatternGui import net.minecraft.command.ICommandSender import net.minecraft.util.BlockPos import net.minecraftforge.client.ClientCommandHandler +import kotlin.time.Duration object Commands { @@ -573,6 +576,10 @@ object Commands { "shtestrainbow", "Sends a rainbow in chat", ) { ExtendedChatColor.testCommand() } + registerCommand( + "shtestnotification", + "Shows a test notification", + ) { NotificationManager.queueNotification(SkyHanniNotification(it.joinToString(" ").replace("\\n", "\n"), Duration.INFINITE)) } registerCommand( "shcopyinternalname", "Copies the internal name of the item in hand to the clipboard.", diff --git a/src/main/java/at/hannibal2/skyhanni/data/NotificationManager.kt b/src/main/java/at/hannibal2/skyhanni/data/NotificationManager.kt index 973fa140bdda..7df4bf7b8009 100644 --- a/src/main/java/at/hannibal2/skyhanni/data/NotificationManager.kt +++ b/src/main/java/at/hannibal2/skyhanni/data/NotificationManager.kt @@ -1,22 +1,97 @@ package at.hannibal2.skyhanni.data +import at.hannibal2.skyhanni.events.GuiRenderEvent +import at.hannibal2.skyhanni.events.LorenzKeyPressEvent import at.hannibal2.skyhanni.skyhannimodule.SkyHanniModule +import at.hannibal2.skyhanni.utils.GuiRenderUtils +import at.hannibal2.skyhanni.utils.InventoryUtils +import at.hannibal2.skyhanni.utils.SimpleTimeMark +import at.hannibal2.skyhanni.utils.TimeUtils.format +import at.hannibal2.skyhanni.utils.compat.GuiScreenUtils +import io.github.notenoughupdates.moulconfig.internal.RenderUtils +import net.minecraft.client.Minecraft +import net.minecraftforge.fml.common.eventhandler.SubscribeEvent +import org.lwjgl.input.Keyboard import kotlin.time.Duration +import kotlin.time.Duration.Companion.milliseconds @SkyHanniModule object NotificationManager { - val notificationQueue = mutableListOf() + private val notificationQueue = mutableListOf() + private var currentNotification: SkyHanniNotification? = null + private var lastNotificationClosed = SimpleTimeMark.farPast() -} + private const val CLOSE_TEXT = "§c[X] Close" + + @SubscribeEvent + fun onKeyClick(event: LorenzKeyPressEvent) { + currentNotification ?: return + if (lastNotificationClosed.passedSince() < 200.milliseconds) return + if (event.keyCode != Keyboard.KEY_X) return + currentNotification = null + lastNotificationClosed = SimpleTimeMark.now() + } + + @SubscribeEvent + fun onRenderOverlay(event: GuiRenderEvent) { + val notification = getCurrentNotification() ?: return + + if (InventoryUtils.inInventory() && !notification.showOverInventory) return + + val midX = GuiScreenUtils.scaledWindowWidth / 2 + val topY = (GuiScreenUtils.scaledWindowHeight * 0.75 - notification.height / 2).toInt() + + RenderUtils.drawFloatingRectDark(midX - notification.width / 2, topY, notification.width, notification.height) + val closeTextWidth = Minecraft.getMinecraft().fontRendererObj.getStringWidth(CLOSE_TEXT) + + GuiRenderUtils.drawString(CLOSE_TEXT, midX + notification.width / 2 - 3 - closeTextWidth, topY + 4) + if (notification.length.isFinite()) { + val remainingTime = "§8" + notification.endTime.timeUntil().format() + GuiRenderUtils.drawString(remainingTime, midX - notification.width / 2 + 4, topY + 4) + } -data class SkyhanniNotification(val title: String, val message: String, val length: Duration, val showOverInventory: Boolean = false) { - constructor(title: String, message: List, length: Duration, showOverInventory: Boolean = false) : this( - title, - message.joinToString(" "), + notification.message.forEachIndexed { index, line -> + GuiRenderUtils.drawStringCentered("§7$line", midX, topY + 19 + index * 10) + } + } + + private fun getCurrentNotification(): SkyHanniNotification? { + currentNotification?.let { + if (it.endTime.isInPast()) currentNotification = null + } + if (currentNotification == null) { + currentNotification = notificationQueue.removeFirstOrNull() + currentNotification?.setEndTime() + } + return currentNotification + } + + fun queueNotification(notification: SkyHanniNotification) { + notificationQueue.add(notification) + } +} + +data class SkyHanniNotification( + val message: List, + val length: Duration, + val showOverInventory: Boolean = false, +) { + constructor(message: String, length: Duration, showOverInventory: Boolean = false) : this( + message.lines(), length, - false, + showOverInventory, ) + + var endTime = SimpleTimeMark.farFuture() + + val width by lazy { (message.maxOfOrNull { Minecraft.getMinecraft().fontRendererObj.getStringWidth(it) } ?: 0) + 8 } + val height by lazy { message.size * 10 + 18 } + + fun setEndTime() { + if (length.isInfinite()) return + endTime = SimpleTimeMark.now() + length + } } diff --git a/src/main/java/at/hannibal2/skyhanni/utils/ItemUtils.kt b/src/main/java/at/hannibal2/skyhanni/utils/ItemUtils.kt index d39b37c8abb1..b4bb6262418c 100644 --- a/src/main/java/at/hannibal2/skyhanni/utils/ItemUtils.kt +++ b/src/main/java/at/hannibal2/skyhanni/utils/ItemUtils.kt @@ -1,6 +1,8 @@ package at.hannibal2.skyhanni.utils +import at.hannibal2.skyhanni.data.NotificationManager import at.hannibal2.skyhanni.data.PetAPI +import at.hannibal2.skyhanni.data.SkyHanniNotification import at.hannibal2.skyhanni.events.DebugDataCollectEvent import at.hannibal2.skyhanni.features.misc.items.EstimatedItemValueCalculator.getAttributeName import at.hannibal2.skyhanni.skyhannimodule.SkyHanniModule @@ -19,8 +21,6 @@ import at.hannibal2.skyhanni.utils.SkyBlockItemModifierUtils.isRecombobulated import at.hannibal2.skyhanni.utils.StringUtils.removeColor import at.hannibal2.skyhanni.utils.StringUtils.removeResets import at.hannibal2.skyhanni.utils.repopatterns.RepoPattern -import com.google.common.collect.Lists -import io.github.moulberry.notenoughupdates.util.NotificationHandler import net.minecraft.client.Minecraft import net.minecraft.init.Items import net.minecraft.item.Item @@ -32,6 +32,8 @@ import net.minecraftforge.common.util.Constants import net.minecraftforge.fml.common.eventhandler.SubscribeEvent import java.util.LinkedList import java.util.regex.Matcher +import kotlin.time.Duration.Companion.INFINITE +import kotlin.time.Duration.Companion.minutes import kotlin.time.Duration.Companion.seconds @SkyHanniModule @@ -40,6 +42,7 @@ object ItemUtils { private val itemNameCache = mutableMapOf() // internal name -> item name private val missingRepoItems = mutableSetOf() + private var lastRepoWarning = SimpleTimeMark.farPast() fun ItemStack.cleanName() = this.displayName.removeColor() @@ -544,20 +547,19 @@ object ItemUtils { fun addMissingRepoItem(name: String, message: String) { if (!missingRepoItems.add(name)) return ChatUtils.debug(message) -// showRepoWarning() + + if (lastRepoWarning.passedSince() < 3.minutes) return + lastRepoWarning = SimpleTimeMark.now() + showRepoWarning() } - // Running NEU's function `Utils.showOutdatedRepoNotification()` caused a NoSuchMethodError in dev env. - // Therefore we run NotificationHandler.displayNotification directly private fun showRepoWarning() { - NotificationHandler.displayNotification( - Lists.newArrayList( - "§c§lMissing repo data", - "§cData used for some SkyHanni features is not up to date, this should normally not be the case.", - "§cYou can try §l/neuresetrepo§r§c and restart your game to see if that fixes the issue.", - "§cIf the problem persists please join the SkyHanni Discord and message in §l#support§r§c to get support.", - ), - true, true, + val text = listOf( + "§c§lMissing repo data", + "§cData used for some SkyHanni features is not up to date, this should normally not be the case.", + "§cYou can try §l/neuresetrepo§r§c and restart your game to see if that fixes the issue.", + "§cIf the problem persists please join the SkyHanni Discord and message in §l#support§r§c to get support.", ) + NotificationManager.queueNotification(SkyHanniNotification(text, INFINITE, true)) } } From faa5c81a2a90a765fcebe385fccdf62fc342a9e2 Mon Sep 17 00:00:00 2001 From: CalMWolfs Date: Tue, 1 Oct 2024 18:39:44 +1000 Subject: [PATCH 3/5] display more info in the notification --- src/main/java/at/hannibal2/skyhanni/utils/ItemUtils.kt | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/src/main/java/at/hannibal2/skyhanni/utils/ItemUtils.kt b/src/main/java/at/hannibal2/skyhanni/utils/ItemUtils.kt index b4bb6262418c..1198ed7ae6a2 100644 --- a/src/main/java/at/hannibal2/skyhanni/utils/ItemUtils.kt +++ b/src/main/java/at/hannibal2/skyhanni/utils/ItemUtils.kt @@ -550,12 +550,12 @@ object ItemUtils { if (lastRepoWarning.passedSince() < 3.minutes) return lastRepoWarning = SimpleTimeMark.now() - showRepoWarning() + showRepoWarning(name) } - private fun showRepoWarning() { + private fun showRepoWarning(item: String) { val text = listOf( - "§c§lMissing repo data", + "§c§lMissing repo data for item: $item", "§cData used for some SkyHanni features is not up to date, this should normally not be the case.", "§cYou can try §l/neuresetrepo§r§c and restart your game to see if that fixes the issue.", "§cIf the problem persists please join the SkyHanni Discord and message in §l#support§r§c to get support.", From 154319e9b3575a2e0ef7cdb780195cd79a5d77fd Mon Sep 17 00:00:00 2001 From: Cal Date: Mon, 14 Oct 2024 19:11:46 +1100 Subject: [PATCH 4/5] use new command system --- .../skyhanni/data/NotificationManager.kt | 15 +++++++++++++++ 1 file changed, 15 insertions(+) diff --git a/src/main/java/at/hannibal2/skyhanni/data/NotificationManager.kt b/src/main/java/at/hannibal2/skyhanni/data/NotificationManager.kt index 7df4bf7b8009..a5ec2e39dc06 100644 --- a/src/main/java/at/hannibal2/skyhanni/data/NotificationManager.kt +++ b/src/main/java/at/hannibal2/skyhanni/data/NotificationManager.kt @@ -1,5 +1,8 @@ package at.hannibal2.skyhanni.data +import at.hannibal2.skyhanni.api.event.HandleEvent +import at.hannibal2.skyhanni.config.commands.CommandCategory +import at.hannibal2.skyhanni.config.commands.RegisterCommandsEvent import at.hannibal2.skyhanni.events.GuiRenderEvent import at.hannibal2.skyhanni.events.LorenzKeyPressEvent import at.hannibal2.skyhanni.skyhannimodule.SkyHanniModule @@ -72,6 +75,18 @@ object NotificationManager { fun queueNotification(notification: SkyHanniNotification) { notificationQueue.add(notification) } + + @HandleEvent + fun onRegisterCommands(event: RegisterCommandsEvent) { + event.register("shtestnotification") { + description = "Shows a test notification" + category = CommandCategory.DEVELOPER_TEST + callback { + val testingText = it.joinToString(" ").replace("\\n", "\n") + queueNotification(SkyHanniNotification(testingText, Duration.INFINITE)) + } + } + } } data class SkyHanniNotification( From 68409ee216fae93281278783e2cf11e753308acc Mon Sep 17 00:00:00 2001 From: Cal Date: Mon, 14 Oct 2024 19:26:48 +1100 Subject: [PATCH 5/5] use new event name --- .../java/at/hannibal2/skyhanni/data/NotificationManager.kt | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/main/java/at/hannibal2/skyhanni/data/NotificationManager.kt b/src/main/java/at/hannibal2/skyhanni/data/NotificationManager.kt index a5ec2e39dc06..748e46892865 100644 --- a/src/main/java/at/hannibal2/skyhanni/data/NotificationManager.kt +++ b/src/main/java/at/hannibal2/skyhanni/data/NotificationManager.kt @@ -2,7 +2,7 @@ package at.hannibal2.skyhanni.data import at.hannibal2.skyhanni.api.event.HandleEvent import at.hannibal2.skyhanni.config.commands.CommandCategory -import at.hannibal2.skyhanni.config.commands.RegisterCommandsEvent +import at.hannibal2.skyhanni.config.commands.CommandRegistrationEvent import at.hannibal2.skyhanni.events.GuiRenderEvent import at.hannibal2.skyhanni.events.LorenzKeyPressEvent import at.hannibal2.skyhanni.skyhannimodule.SkyHanniModule @@ -77,7 +77,7 @@ object NotificationManager { } @HandleEvent - fun onRegisterCommands(event: RegisterCommandsEvent) { + fun onCommandRegistration(event: CommandRegistrationEvent) { event.register("shtestnotification") { description = "Shows a test notification" category = CommandCategory.DEVELOPER_TEST