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
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
|
package at.hannibal2.skyhanni.data
import at.hannibal2.skyhanni.events.LorenzActionBarEvent
import at.hannibal2.skyhanni.events.ProfileApiDataLoadedEvent
import at.hannibal2.skyhanni.events.ProfileJoinEvent
import at.hannibal2.skyhanni.utils.InventoryUtils
import at.hannibal2.skyhanni.utils.ItemUtils.getLore
import at.hannibal2.skyhanni.utils.ItemUtils.name
import at.hannibal2.skyhanni.utils.LorenzUtils
import at.hannibal2.skyhanni.utils.NumberUtil.romanToDecimal
import at.hannibal2.skyhanni.utils.StringUtils.removeColor
import net.minecraftforge.event.world.WorldEvent
import net.minecraftforge.fml.common.eventhandler.SubscribeEvent
import net.minecraftforge.fml.common.gameevent.TickEvent
import java.util.regex.Pattern
class SkillExperience {
private val actionBarPattern = Pattern.compile("(?:.*)§3\\+(?:.*) (.*) \\((.*)\\/(.*)\\)(?:.*)")
private val inventoryPattern = Pattern.compile("(?:.*) §e(.*)§6\\/(?:.*)")
@SubscribeEvent
fun onProfileDataLoad(event: ProfileApiDataLoadedEvent) {
val profileData = event.profileData
for ((key, value) in profileData.entrySet()) {
if (key.startsWith("experience_skill_")) {
val label = key.substring(17)
val exp = value.asLong
skillExp[label] = exp
}
}
}
@SubscribeEvent
fun onProfileJoin(event: ProfileJoinEvent) {
skillExp.clear()
}
@SubscribeEvent
fun onActionBar(event: LorenzActionBarEvent) {
if (!LorenzUtils.inSkyBlock) return
val matcher = actionBarPattern.matcher(event.message)
if (!matcher.matches()) return
val skill = matcher.group(1).lowercase()
val overflow = matcher.group(2).formatNumber()
val neededForNextLevel = matcher.group(3).formatNumber()
val nextLevel = getLevelForExp(neededForNextLevel)
val baseExp = getExpForLevel(nextLevel - 1)
skillExp[skill] = baseExp + overflow
}
private var tick = 0
private var dirty = true
@SubscribeEvent
fun onTick(event: TickEvent.ClientTickEvent) {
if (event.phase != TickEvent.Phase.START) return
tick++
if (tick % 20 != 0) return
if (InventoryUtils.openInventoryName() != "Your Skills") return
if (!dirty) return
dirty = false
for (slot in InventoryUtils.getItemsInOpenChest()) {
val stack = slot.stack
val name = stack.name?.removeColor() ?: continue
val lore = stack.getLore()
var next = false
for (line in lore) {
if (line.contains("Progress to Level")) {
next = true
continue
}
if (next) {
if (!name.contains(" ")) continue
val split = name.split(" ")
val skillName = split[0].lowercase()
val level = split[1].romanToDecimal()
val baseExp = getExpForLevel(level)
val matcher = inventoryPattern.matcher(line)
if (matcher.matches()) {
val rawNumber = matcher.group(1)
val overflow = rawNumber.formatNumber()
val experience = baseExp + overflow
skillExp[skillName] = experience
}
next = false
}
}
}
if (skillExp.isNotEmpty()) return
}
@SubscribeEvent
fun onWorldChange(event: WorldEvent.Load) {
dirty = true
}
companion object {
private val skillExp = mutableMapOf<String, Long>()
private fun getLevelForExp(experience: Long): Int {
var level = 1
for (levelXp in levelingExp) {
if (levelXp.toLong() == experience) {
return level
}
level++
}
return 0
}
fun getExpForLevel(requestedLevel: Int): Long {
var total = 0L
var level = 0
for (levelXp in levelingExp) {
total += levelXp
level++
if (level == requestedLevel) {
return total
}
}
return 0
}
//TODO create additional event
fun getExpForSkill(skillName: String) = skillExp[skillName.lowercase()] ?: 0
private val levelingExp = listOf(
50,
125,
200,
300,
500,
750,
1000,
1500,
2000,
3500,
5000,
7500,
10000,
15000,
20000,
30000,
50000,
75000,
100000,
200000,
300000,
400000,
500000,
600000,
700000,
800000,
900000,
1000000,
1100000,
1200000,
1300000,
1400000,
1500000,
1600000,
1700000,
1800000,
1900000,
2000000,
2100000,
2200000,
2300000,
2400000,
2500000,
2600000,
2750000,
2900000,
3100000,
3400000,
3700000,
4000000,
4300000,
4600000,
4900000,
5200000,
5500000,
5800000,
6100000,
6400000,
6700000,
7000000
)
}
}
private fun String.formatNumber(): Long {
var text = replace(",", "")
val multiplier = if (text.endsWith("k")) {
text = text.substring(0, text.length - 1)
1_000
} else if (text.endsWith("m")) {
text = text.substring(0, text.length - 1)
1_000_000
} else {
1
}
val d = text.toDouble()
return (d * multiplier).toLong()
}
|