aboutsummaryrefslogtreecommitdiff
path: root/src/main/java/at/hannibal2/skyhanni/test/TestCopyBestiaryValues.kt
blob: 4bf183d74fbf789603a9e16bcd015a06010b4ff6 (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
107
108
109
110
111
112
113
114
115
116
117
118
119
package at.hannibal2.skyhanni.test

import at.hannibal2.skyhanni.SkyHanniMod
import at.hannibal2.skyhanni.config.ConfigUpdaterMigrator
import at.hannibal2.skyhanni.events.InventoryUpdatedEvent
import at.hannibal2.skyhanni.skyhannimodule.SkyHanniModule
import at.hannibal2.skyhanni.utils.CollectionUtils.nextAfter
import at.hannibal2.skyhanni.utils.ItemUtils.getLore
import at.hannibal2.skyhanni.utils.ItemUtils.getSkullOwner
import at.hannibal2.skyhanni.utils.ItemUtils.getSkullTexture
import at.hannibal2.skyhanni.utils.ItemUtils.name
import at.hannibal2.skyhanni.utils.NumberUtil.formatInt
import at.hannibal2.skyhanni.utils.OSUtils
import at.hannibal2.skyhanni.utils.RegexUtils.matchMatcher
import at.hannibal2.skyhanni.utils.StringUtils.removeColor
import at.hannibal2.skyhanni.utils.StringUtils.removeWordsAtEnd
import at.hannibal2.skyhanni.utils.repopatterns.RepoPattern
import com.google.gson.GsonBuilder
import com.google.gson.annotations.Expose
import net.minecraft.item.ItemStack
import net.minecraftforge.fml.common.eventhandler.EventPriority
import net.minecraftforge.fml.common.eventhandler.SubscribeEvent

@SkyHanniModule
object TestCopyBestiaryValues {

    class BestiaryObject { // TODO fix typo

        @Expose
        var name: String = ""

        @Expose
        var skullOwner: String = ""

        @Expose
        var texture: String = ""

        @Expose
        var cap: Int = 0

        @Expose
        var mobs: Array<String> = emptyArray()

        @Expose
        var bracket: Int = 0
    }

    private val bestiaryTypePattern by RepoPattern.pattern(
        "test.bestiary.type",
        "\\[Lv(?<lvl>.*)] (?<text>.*)"
    )

    @SubscribeEvent(priority = EventPriority.LOW)
    fun onInventoryUpdated(event: InventoryUpdatedEvent) {
        if (!SkyHanniMod.feature.dev.debug.copyBestiaryData) return
        SkyHanniDebugsAndTests.displayLine = ""

        val backItem = event.inventoryItems[3 + 9 * 5 + 3] ?: return
        if (backItem.getLore().none { it.contains("Bestiary Milestone") }) {
            return
        }

        val rankingItem = event.inventoryItems[3 + 9 * 5 + 2] ?: return
        if (rankingItem.getLore().none { it.contains("Ranking") }) {
            return
        }

        val titleItem = event.inventoryItems[4] ?: return
        copy(titleItem, event.inventoryItems)
    }

    private fun copy(titleItem: ItemStack, inventoryItems: Map<Int, ItemStack>) {
        val titleName = titleItem.name.removeWordsAtEnd(1)

        val obj = BestiaryObject()
        obj.name = titleName
        obj.texture = titleItem.getSkullTexture() ?: "no texture found"
        obj.skullOwner = titleItem.getSkullOwner() ?: "no skullOwner found"

        val lore = titleItem.getLore()
        val overallProgress = lore.find { it.contains("Overall Progress") }
        if (overallProgress == null) {
            println("overallProgress not found!")
            return
        }
        val capLine = lore.nextAfter(overallProgress) ?: return
        val rawCap = capLine.substringAfter("/").removeColor().formatInt()
        obj.cap = rawCap

        val mobs = mutableListOf<String>()
        for (i in 10..43) {
            val stack = inventoryItems[i] ?: continue
            bestiaryTypePattern.matchMatcher(stack.name.removeColor()) {
                val lvl = group("lvl").toInt()
                var text = group("text").lowercase().replace(" ", "_")

                val master = text.endsWith("(master)")
                val masterText = if (master) "master_" else ""
                if (master) {
                    text = text.split("_").dropLast(1).joinToString("_")
                }
                val result = "$masterText${text}_$lvl"
                mobs.add(result)
            }
        }
        obj.mobs = mobs.toTypedArray()

        val gson = GsonBuilder().disableHtmlEscaping().setPrettyPrinting().create()
        val text = gson.toJson(obj)
        OSUtils.copyToClipboard(text)

        SkyHanniDebugsAndTests.displayLine = "Bestiary for $titleName"
    }

    @SubscribeEvent
    fun onConfigFix(event: ConfigUpdaterMigrator.ConfigFixEvent) {
        event.move(3, "dev.copyBestiaryData", "dev.debug.copyBestiaryData")
    }
}