aboutsummaryrefslogtreecommitdiff
path: root/src/main/kotlin/io/github/moulberry/notenoughupdates/miscfeatures/BazaarPriceWarning.kt
blob: f0aace543d8150ee716dc0c340bbf5eba73e0da4 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
/*
 * 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.core.util.StringUtils
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.init.Blocks
import net.minecraft.init.Items
import net.minecraft.inventory.Slot
import net.minecraft.item.Item
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) {
        val openSlots = Minecraft.getMinecraft().thePlayer?.openContainer?.inventorySlots ?: return
        if (openSlots.size < 17) return
        //both insta buy and buy order screens have this sign
        //we check the name of the buy order page and return if its that
        //however the custom amount insta buy page doesnt have a sign so we also have to check its title
        val signStack = openSlots[16]?.stack ?: return
        val hasCustomAmount =
            (signStack.item == Items.sign || signStack.item == Item.getItemFromBlock(Blocks.redstone_block)) &&
                    ItemUtils.getDisplayName(signStack) != "§aCustom Amount"
        val isBuyOrder = Utils.getOpenChestName().contains("How many do you want?")
        val isConfirmInstantBuy = Utils.getOpenChestName().contains("Confirm Instant Buy")
        if ((hasCustomAmount && !isBuyOrder && !isConfirmInstantBuy)) 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${StringUtils.formatNumber(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
        )
    }
}