aboutsummaryrefslogtreecommitdiff
path: root/src/main/kotlin/io
diff options
context:
space:
mode:
authorLinnea Gräf <nea@nea.moe>2024-04-11 11:25:35 +0200
committerGitHub <noreply@github.com>2024-04-11 11:25:35 +0200
commitd6128f0dac37768bb295efe3f92c55ba890073de (patch)
tree81063a18b66914b0f31571b395b9158bd9b1eb68 /src/main/kotlin/io
parent4cd130f140655f8741da93b1aa6c7bc7a2ce6d41 (diff)
downloadNotEnoughUpdates-d6128f0dac37768bb295efe3f92c55ba890073de.tar.gz
NotEnoughUpdates-d6128f0dac37768bb295efe3f92c55ba890073de.tar.bz2
NotEnoughUpdates-d6128f0dac37768bb295efe3f92c55ba890073de.zip
Add hex overpay warning (#1072)
Co-authored-by: Lulonaut <lulonaut@lulonaut.dev>
Diffstat (limited to 'src/main/kotlin/io')
-rw-r--r--src/main/kotlin/io/github/moulberry/notenoughupdates/events/IsSlotBeingHoveredEvent.kt46
-rw-r--r--src/main/kotlin/io/github/moulberry/notenoughupdates/miscfeatures/BazaarPriceWarning.kt91
-rw-r--r--src/main/kotlin/io/github/moulberry/notenoughupdates/miscfeatures/HexPriceWarning.kt93
-rw-r--r--src/main/kotlin/io/github/moulberry/notenoughupdates/miscfeatures/WarningPopUp.kt163
-rw-r--r--src/main/kotlin/io/github/moulberry/notenoughupdates/util/ScreenReplacer.kt40
5 files changed, 433 insertions, 0 deletions
diff --git a/src/main/kotlin/io/github/moulberry/notenoughupdates/events/IsSlotBeingHoveredEvent.kt b/src/main/kotlin/io/github/moulberry/notenoughupdates/events/IsSlotBeingHoveredEvent.kt
new file mode 100644
index 00000000..512752b0
--- /dev/null
+++ b/src/main/kotlin/io/github/moulberry/notenoughupdates/events/IsSlotBeingHoveredEvent.kt
@@ -0,0 +1,46 @@
+/*
+ * Copyright (C) 2024 NotEnoughUpdates contributors
+ *
+ * This file is part of NotEnoughUpdates.
+ *
+ * NotEnoughUpdates is free software: you can redistribute it
+ * and/or modify it under the terms of the GNU Lesser General Public
+ * License as published by the Free Software Foundation, either
+ * version 3 of the License, or (at your option) any later version.
+ *
+ * NotEnoughUpdates is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public License
+ * along with NotEnoughUpdates. If not, see <https://www.gnu.org/licenses/>.
+ */
+
+package io.github.moulberry.notenoughupdates.events
+
+import net.minecraftforge.fml.common.eventhandler.Event
+
+
+class IsSlotBeingHoveredEvent : Event() {
+
+ var override = Override.DEFER_TO_DEFAULT
+
+ fun prevent(override: Override = Override.IS_NOT_HOVERED) {
+ this.override = override.or(this.override)
+ }
+
+ enum class Override {
+ DEFER_TO_DEFAULT,
+ IS_HOVERED,
+ IS_NOT_HOVERED, ;
+
+ fun or(override: Override): Override {
+ if (override == DEFER_TO_DEFAULT)
+ return this
+ return override
+ }
+ }
+
+
+}
diff --git a/src/main/kotlin/io/github/moulberry/notenoughupdates/miscfeatures/BazaarPriceWarning.kt b/src/main/kotlin/io/github/moulberry/notenoughupdates/miscfeatures/BazaarPriceWarning.kt
new file mode 100644
index 00000000..d198100a
--- /dev/null
+++ b/src/main/kotlin/io/github/moulberry/notenoughupdates/miscfeatures/BazaarPriceWarning.kt
@@ -0,0 +1,91 @@
+/*
+ * Copyright (C) 2024 NotEnoughUpdates contributors
+ *
+ * This file is part of NotEnoughUpdates.
+ *
+ * NotEnoughUpdates is free software: you can redistribute it
+ * and/or modify it under the terms of the GNU Lesser General Public
+ * License as published by the Free Software Foundation, either
+ * version 3 of the License, or (at your option) any later version.
+ *
+ * NotEnoughUpdates is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public License
+ * along with NotEnoughUpdates. If not, see <https://www.gnu.org/licenses/>.
+ */
+
+package io.github.moulberry.notenoughupdates.miscfeatures
+
+import io.github.moulberry.notenoughupdates.NotEnoughUpdates
+import io.github.moulberry.notenoughupdates.autosubscribe.NEUAutoSubscribe
+import io.github.moulberry.notenoughupdates.events.SlotClickEvent
+import io.github.moulberry.notenoughupdates.util.ItemUtils
+import io.github.moulberry.notenoughupdates.util.Utils
+import net.minecraft.client.Minecraft
+import net.minecraft.client.gui.inventory.GuiChest
+import net.minecraft.inventory.Slot
+import net.minecraftforge.fml.common.eventhandler.SubscribeEvent
+
+@NEUAutoSubscribe
+class BazaarPriceWarning : WarningPopUp() {
+ override fun shouldShow(): Boolean {
+ val openSlots = Minecraft.getMinecraft().thePlayer?.openContainer?.inventorySlots ?: return false
+ return super.shouldShow() && openSlots.contains(clickedSlot ?: return false)
+ }
+
+ var clickedSlot: Slot? = null
+ val priceRegx = "§7Price: §6([0-9.,]+) coins".toPattern()
+ var price = 0.0
+
+ val limit get() = NotEnoughUpdates.INSTANCE.config.bazaarTweaks.bazaarOverpayWarning
+ @SubscribeEvent
+ fun onClick(event: SlotClickEvent) {
+ if (!Utils.getOpenChestName().contains("Instant Buy"))
+ return
+ if (shouldShow()) return
+ val stack = event.slot.stack ?: return
+ val lore = ItemUtils.getLore(stack)
+ if (lore.lastOrNull() != "§7§eClick to buy now!") return
+ val priceMatch = lore.firstNotNullOfOrNull { priceRegx.matcher(it).takeIf { it.matches() } } ?: return
+ val price = priceMatch.group(1).replace(",", "").toDouble()
+
+ if (price <= limit || limit < 1)
+ return
+
+ this.price = price
+ clickedSlot = event.slot
+ show()
+ event.cancel()
+ }
+
+ fun getLore(): List<String> {
+ return clickedSlot?.stack?.let(ItemUtils::getLore) ?: listOf()
+ }
+
+ override fun getItemName(): String {
+ return getLore().firstOrNull() ?: "<unknown>"
+ }
+
+ override fun getWarningLines(): List<String> {
+ return listOf("will cost you §6${price}§r coins")
+ }
+
+ override fun getWarningPopup(): List<String> {
+ val displayName = clickedSlot?.stack?.let(ItemUtils::getDisplayName)
+ val tooltip = getLore().toMutableList()
+ if (displayName != null)
+ tooltip.add(0, displayName)
+ return tooltip
+ }
+
+ override fun confirmClick() {
+ val chest = Minecraft.getMinecraft().currentScreen as GuiChest
+ Minecraft.getMinecraft().playerController.windowClick(
+ chest.inventorySlots.windowId,
+ clickedSlot?.slotNumber ?: return, 0, 0, Minecraft.getMinecraft().thePlayer
+ )
+ }
+}
diff --git a/src/main/kotlin/io/github/moulberry/notenoughupdates/miscfeatures/HexPriceWarning.kt b/src/main/kotlin/io/github/moulberry/notenoughupdates/miscfeatures/HexPriceWarning.kt
new file mode 100644
index 00000000..ff90f1c9
--- /dev/null
+++ b/src/main/kotlin/io/github/moulberry/notenoughupdates/miscfeatures/HexPriceWarning.kt
@@ -0,0 +1,93 @@
+/*
+ * Copyright (C) 2024 NotEnoughUpdates contributors
+ *
+ * This file is part of NotEnoughUpdates.
+ *
+ * NotEnoughUpdates is free software: you can redistribute it
+ * and/or modify it under the terms of the GNU Lesser General Public
+ * License as published by the Free Software Foundation, either
+ * version 3 of the License, or (at your option) any later version.
+ *
+ * NotEnoughUpdates is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public License
+ * along with NotEnoughUpdates. If not, see <https://www.gnu.org/licenses/>.
+ */
+
+package io.github.moulberry.notenoughupdates.miscfeatures
+
+import io.github.moulberry.notenoughupdates.NotEnoughUpdates
+import io.github.moulberry.notenoughupdates.autosubscribe.NEUAutoSubscribe
+import io.github.moulberry.notenoughupdates.events.SlotClickEvent
+import io.github.moulberry.notenoughupdates.util.ItemUtils
+import net.minecraft.client.Minecraft
+import net.minecraft.client.gui.inventory.GuiChest
+import net.minecraftforge.fml.common.eventhandler.SubscribeEvent
+
+/*
+ We are gathered here today to mourn the death of 26m coins that Alea spent on a Cleave 6 book, while trying to take
+ off Cleave 5.
+ */
+@NEUAutoSubscribe
+object HexPriceWarning : WarningPopUp() {
+
+ fun shouldCheck(): Boolean {
+ return getLimit() >= 1
+ }
+
+ fun getLimit(): Double {
+ return NotEnoughUpdates.INSTANCE.config.enchantingSolvers.hexOverpayWarning;
+ }
+
+ var lastClickedSlot = 0
+ var cost = ""
+ var upgradeName = ""
+ override fun shouldShow(): Boolean {
+ return shouldCheck() && super.shouldShow()
+ }
+
+ override fun getItemName(): String {
+ return upgradeName
+ }
+
+ override fun getWarningLines(): List<String> {
+ return listOf("will cost you §6$cost§r coins")
+ }
+
+ override fun confirmClick() {
+ val chest = Minecraft.getMinecraft().currentScreen as GuiChest
+ Minecraft.getMinecraft().playerController.windowClick(
+ chest.inventorySlots.windowId,
+ lastClickedSlot, 0, 0, Minecraft.getMinecraft().thePlayer
+ )
+ }
+
+ @SubscribeEvent
+ fun onClick(event: SlotClickEvent) {
+ if (!shouldCheck()) return
+ if (isShowing) return
+ val stack = event.slot.stack ?: return
+ val lore = ItemUtils.getLore(stack)
+ val bazaarPriceLine = lore.indexOf("§7Bazaar Price")
+ if (bazaarPriceLine >= 0 &&
+ (bazaarPriceLine + 1) in lore.indices
+ ) {
+ val priceLine = lore[bazaarPriceLine + 1]
+ val priceMatcher = coins.matcher(priceLine)
+ if (!priceMatcher.matches()) return
+ val price = priceMatcher.group(1).replace(",", "").toDouble()
+ if (price >= getLimit()) {
+ lastClickedSlot = event.slotId
+ cost = priceMatcher.group(1)
+ upgradeName = ItemUtils.getDisplayName(stack.tagCompound) ?: "<unnamed upgrade>"
+ show()
+ event.cancel()
+ }
+ }
+ }
+
+ val coins = "§6([,.0-9]+) Coins".toPattern()
+}
diff --git a/src/main/kotlin/io/github/moulberry/notenoughupdates/miscfeatures/WarningPopUp.kt b/src/main/kotlin/io/github/moulberry/notenoughupdates/miscfeatures/WarningPopUp.kt
new file mode 100644
index 00000000..73e4c462
--- /dev/null
+++ b/src/main/kotlin/io/github/moulberry/notenoughupdates/miscfeatures/WarningPopUp.kt
@@ -0,0 +1,163 @@
+/*
+ * Copyright (C) 2024 NotEnoughUpdates contributors
+ *
+ * This file is part of NotEnoughUpdates.
+ *
+ * NotEnoughUpdates is free software: you can redistribute it
+ * and/or modify it under the terms of the GNU Lesser General Public
+ * License as published by the Free Software Foundation, either
+ * version 3 of the License, or (at your option) any later version.
+ *
+ * NotEnoughUpdates is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public License
+ * along with NotEnoughUpdates. If not, see <https://www.gnu.org/licenses/>.
+ */
+
+package io.github.moulberry.notenoughupdates.miscfeatures
+
+import io.github.moulberry.notenoughupdates.core.util.render.RenderUtils
+import io.github.moulberry.notenoughupdates.core.util.render.TextRenderUtils
+import io.github.moulberry.notenoughupdates.events.IsSlotBeingHoveredEvent
+import io.github.moulberry.notenoughupdates.util.ScreenReplacer
+import io.github.moulberry.notenoughupdates.util.Utils
+import net.minecraft.client.Minecraft
+import net.minecraft.client.gui.ScaledResolution
+import net.minecraft.client.renderer.GlStateManager
+import net.minecraft.util.EnumChatFormatting
+import net.minecraftforge.fml.common.eventhandler.SubscribeEvent
+import org.lwjgl.input.Keyboard
+import org.lwjgl.input.Mouse
+
+abstract class WarningPopUp : ScreenReplacer() {
+
+ var isShowing = false
+ private set
+ fun show() {
+ isShowing = true
+ }
+
+ override fun shouldShow(): Boolean {
+ return isShowing
+ }
+
+ open fun getWarningPopup(): List<String>? = null
+
+ abstract fun getItemName(): String
+
+ abstract fun getWarningLines(): List<String>
+
+ override fun render() {
+ val scaledResolution = ScaledResolution(Minecraft.getMinecraft())
+ val width = scaledResolution.scaledWidth
+ val height = scaledResolution.scaledHeight
+
+ GlStateManager.disableLighting()
+
+ GlStateManager.pushMatrix()
+ GlStateManager.translate(0f, 0f, 500f)
+
+ drawRect(0, 0, width, height, -0x80000000)
+
+ RenderUtils.drawFloatingRectDark(width / 2 - 90, height / 2 - 45, 180, 90)
+
+ val neuLength = Minecraft.getMinecraft().fontRendererObj.getStringWidth("\u00a7lNEU")
+ Minecraft.getMinecraft().fontRendererObj.drawString(
+ "\u00a7lNEU",
+ width / 2 + 90 - neuLength - 3,
+ height / 2 - 45 + 4,
+ -0x1000000
+ )
+
+ TextRenderUtils.drawStringCenteredScaledMaxWidth(
+ "Are you SURE?",
+ (width / 2).toFloat(), (height / 2 - 45 + 10).toFloat(), false, 170, -0xbfc0
+ )
+
+
+ val itemNameLine = "\u00a77[ §r${getItemName()}\u00a77 ]"
+ TextRenderUtils.drawStringCenteredScaledMaxWidth(
+ itemNameLine,
+ (width / 2).toFloat(), (height / 2 - 45 + 25).toFloat(), false, 170, -0x1
+ )
+ for ((index, line) in getWarningLines().withIndex()) {
+ TextRenderUtils.drawStringCenteredScaledMaxWidth(
+ line,
+ (width / 2).toFloat(), (height / 2 - 45 + 34 + 10 * index).toFloat(),
+ false, 170, -0x5f5f60
+ )
+ }
+
+
+ RenderUtils.drawFloatingRectDark(width / 2 - 43, height / 2 + 23, 40, 16, false)
+ RenderUtils.drawFloatingRectDark(width / 2 + 3, height / 2 + 23, 40, 16, false)
+
+ TextRenderUtils.drawStringCenteredScaledMaxWidth(
+ EnumChatFormatting.GREEN.toString() + "[Y]es",
+ (width / 2 - 23).toFloat(), (height / 2 + 31).toFloat(), true, 36, -0xff0100
+ )
+ TextRenderUtils.drawStringCenteredScaledMaxWidth(
+ EnumChatFormatting.RED.toString() + "[N]o",
+ (width / 2 + 23).toFloat(), (height / 2 + 31).toFloat(), true, 36, -0x10000
+ )
+
+ getWarningPopup()?.let { tooltip ->
+ val mouseX = Mouse.getX() * width / Minecraft.getMinecraft().displayWidth
+ val mouseY = height - Mouse.getY() * height / Minecraft.getMinecraft().displayHeight - 1
+
+ val itemNameLength = Minecraft.getMinecraft().fontRendererObj.getStringWidth(itemNameLine)
+
+ if (mouseX >= width / 2 - itemNameLength / 2 && mouseX <= width / 2 + itemNameLength / 2 && mouseY >= height / 2 - 45 + 20 && mouseY <= height / 2 - 45 + 30) {
+ Utils.drawHoveringText(tooltip, mouseX, mouseY, width, height, -1)
+ }
+ }
+
+ GlStateManager.popMatrix()
+ }
+
+ abstract fun confirmClick()
+
+ override fun mouseInput(mouseX: Int, mouseY: Int): Boolean {
+ val scaledResolution = ScaledResolution(Minecraft.getMinecraft())
+ val width = scaledResolution.scaledWidth
+ val height = scaledResolution.scaledHeight
+
+ if (Mouse.getEventButtonState()) {
+ // Yes and No button
+ if ((mouseX >= width / 2 - 43 && mouseX <= width / 2 - 43 + 40) && (mouseY >= height / 2 + 23 && mouseY <= height / 2 + 23 + 16)) {
+ confirmClick()
+ isShowing = false
+ } else if ((mouseX >= width / 2 + 3 && mouseX <= width / 2 + 3 + 40) && (mouseY >= height / 2 + 23 && mouseY <= height / 2 + 23 + 16)) {
+ isShowing = false
+ }
+
+ // click outside the popup
+ if (mouseX < width / 2 - 90 || mouseX > width / 2 + 90 || mouseY < height / 2 - 45 || mouseY > height / 2 + 45) {
+ isShowing = false
+ }
+ }
+ return false
+ }
+
+ @SubscribeEvent
+ fun onHover(event: IsSlotBeingHoveredEvent) {
+ if (shouldShow()) {
+ event.prevent()
+ }
+ }
+
+ override fun keyboardInput(): Boolean {
+ if (!Keyboard.getEventKeyState()) {
+ if (Keyboard.getEventKey() == Keyboard.KEY_Y || Keyboard.getEventKey() == Keyboard.KEY_RETURN) {
+ confirmClick()
+ }
+ isShowing = false
+ return true
+ }
+
+ return false
+ }
+}
diff --git a/src/main/kotlin/io/github/moulberry/notenoughupdates/util/ScreenReplacer.kt b/src/main/kotlin/io/github/moulberry/notenoughupdates/util/ScreenReplacer.kt
new file mode 100644
index 00000000..d0d6acb4
--- /dev/null
+++ b/src/main/kotlin/io/github/moulberry/notenoughupdates/util/ScreenReplacer.kt
@@ -0,0 +1,40 @@
+/*
+ * Copyright (C) 2024 NotEnoughUpdates contributors
+ *
+ * This file is part of NotEnoughUpdates.
+ *
+ * NotEnoughUpdates is free software: you can redistribute it
+ * and/or modify it under the terms of the GNU Lesser General Public
+ * License as published by the Free Software Foundation, either
+ * version 3 of the License, or (at your option) any later version.
+ *
+ * NotEnoughUpdates is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public License
+ * along with NotEnoughUpdates. If not, see <https://www.gnu.org/licenses/>.
+ */
+
+package io.github.moulberry.notenoughupdates.util
+
+import io.github.moulberry.notenoughupdates.autosubscribe.AutoLoad
+import io.github.moulberry.notenoughupdates.core.GuiElement
+
+abstract class ScreenReplacer : GuiElement() {
+ abstract fun shouldShow(): Boolean
+
+ companion object {
+ val allScreenReplacers by lazy {
+ buildList {
+ AutoLoad.provide {
+ val replacer = it.get()
+ if (replacer is ScreenReplacer) {
+ add(replacer)
+ }
+ }
+ }
+ }
+ }
+}