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
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
|
package at.hannibal2.skyhanni.features.garden.pests
import at.hannibal2.skyhanni.api.event.HandleEvent
import at.hannibal2.skyhanni.data.IslandType
import at.hannibal2.skyhanni.data.ScoreboardData
import at.hannibal2.skyhanni.events.DebugDataCollectEvent
import at.hannibal2.skyhanni.events.InventoryFullyOpenedEvent
import at.hannibal2.skyhanni.events.ItemInHandChangeEvent
import at.hannibal2.skyhanni.events.LorenzChatEvent
import at.hannibal2.skyhanni.events.LorenzTickEvent
import at.hannibal2.skyhanni.events.LorenzWorldChangeEvent
import at.hannibal2.skyhanni.events.ScoreboardUpdateEvent
import at.hannibal2.skyhanni.events.TabListUpdateEvent
import at.hannibal2.skyhanni.events.garden.pests.PestSpawnEvent
import at.hannibal2.skyhanni.events.garden.pests.PestUpdateEvent
import at.hannibal2.skyhanni.features.garden.GardenAPI
import at.hannibal2.skyhanni.features.garden.GardenPlotAPI
import at.hannibal2.skyhanni.features.garden.GardenPlotAPI.isBarn
import at.hannibal2.skyhanni.features.garden.GardenPlotAPI.isPestCountInaccurate
import at.hannibal2.skyhanni.features.garden.GardenPlotAPI.locked
import at.hannibal2.skyhanni.features.garden.GardenPlotAPI.name
import at.hannibal2.skyhanni.features.garden.GardenPlotAPI.pests
import at.hannibal2.skyhanni.features.garden.GardenPlotAPI.uncleared
import at.hannibal2.skyhanni.skyhannimodule.SkyHanniModule
import at.hannibal2.skyhanni.test.command.ErrorManager
import at.hannibal2.skyhanni.utils.ChatUtils
import at.hannibal2.skyhanni.utils.DelayedRun
import at.hannibal2.skyhanni.utils.InventoryUtils
import at.hannibal2.skyhanni.utils.ItemUtils.getLore
import at.hannibal2.skyhanni.utils.LocationUtils.distanceSqToPlayer
import at.hannibal2.skyhanni.utils.NEUInternalName.Companion.asInternalName
import at.hannibal2.skyhanni.utils.NumberUtil.formatInt
import at.hannibal2.skyhanni.utils.RegexUtils.matchFirst
import at.hannibal2.skyhanni.utils.RegexUtils.matchMatcher
import at.hannibal2.skyhanni.utils.RegexUtils.matches
import at.hannibal2.skyhanni.utils.SimpleTimeMark
import at.hannibal2.skyhanni.utils.StringUtils.removeColor
import at.hannibal2.skyhanni.utils.repopatterns.RepoPattern
import net.minecraftforge.fml.common.eventhandler.SubscribeEvent
import org.lwjgl.input.Keyboard
import kotlin.time.Duration.Companion.seconds
@SkyHanniModule
object PestAPI {
val config get() = GardenAPI.config.pests
val storage get() = GardenAPI.storage
var scoreboardPests: Int
get() = storage?.scoreboardPests ?: 0
set(value) {
storage?.scoreboardPests = value
}
var lastPestKillTime = SimpleTimeMark.farPast()
var lastTimeVacuumHold = SimpleTimeMark.farPast()
// TODO move into repo
val vacuumVariants = listOf(
"SKYMART_VACUUM".asInternalName(),
"SKYMART_TURBO_VACUUM".asInternalName(),
"SKYMART_HYPER_VACUUM".asInternalName(),
"INFINI_VACUUM".asInternalName(),
"INFINI_VACUUM_HOOVERIUS".asInternalName(),
)
fun hasVacuumInHand() = InventoryUtils.itemInHandId in vacuumVariants
fun SprayType.getPests() = PestType.entries.filter { it.spray == this }
private val patternGroup = RepoPattern.group("garden.pestsapi")
private val pestsInScoreboardPattern by patternGroup.pattern(
"scoreboard.pests",
" §7⏣ §[ac]The Garden §4§lൠ§7 x(?<pests>.*)"
)
/**
* REGEX-TEST: §7⏣ §aPlot §7- §b22a
* REGEX-TEST: §7⏣ §aThe Garden
*/
private val noPestsInScoreboardPattern by patternGroup.pattern(
"scoreboard.nopests",
" §7⏣ §a(?:The Garden|Plot §7- §b.+)$"
)
/**
* REGEX-TEST: §aPlot §7- §b4 §4§lൠ§7 x1
*/
private val pestsInPlotScoreboardPattern by patternGroup.pattern(
"scoreboard.plot.pests",
"\\s*(?:§.)*Plot (?:§.)*- (?:§.)*(?<plot>.+) (?:§.)*ൠ(?:§.)* x(?<pests>\\d+)"
)
/**
* REGEX-TEST: §aPlot §7- §b3
*/
private val noPestsInPlotScoreboardPattern by patternGroup.pattern(
"scoreboard.plot.nopests",
"\\s*(?:§.)*Plot (?:§.)*- (?:§.)*(?<plot>.{1,3})$"
)
private val pestInventoryPattern by patternGroup.pattern(
"inventory",
"§4§lൠ §cThis plot has §6(?<amount>\\d) Pests?§c!"
)
/**
* REGEX-TEST: Plots: §r§b4§r§f, §r§b12§r§f, §r§b13§r§f, §r§b18§r§f, §r§b20
*/
private val infectedPlotsTablistPattern by patternGroup.pattern(
"tablist.infectedplots",
"\\sPlots: (?<plots>.*)"
)
/**
* REGEX-TEST: §eYou received §a7x Enchanted Potato §efor killing a §6Locust§e!
* REGEX-TEST: §eYou received §a6x Enchanted Cocoa Beans §efor killing a §6Moth§e!
*/
val pestDeathChatPattern by patternGroup.pattern(
"chat.pestdeath",
"§eYou received §a(?<amount>[0-9]*)x (?<item>.*) §efor killing an? §6(?<pest>.*)§e!"
)
private val noPestsChatPattern by patternGroup.pattern(
"chat.nopests",
"§cThere are not any Pests on your Garden right now! Keep farming!"
)
var gardenJoinTime = SimpleTimeMark.farPast()
var firstScoreboardCheck = false
private fun fixPests(loop: Int = 2) {
DelayedRun.runDelayed(2.seconds) {
val accurateAmount = getPlotsWithAccuratePests().sumOf { it.pests }
val inaccurateAmount = getPlotsWithInaccuratePests().size
if (scoreboardPests == accurateAmount + inaccurateAmount) { // if we can assume all inaccurate plots have 1 pest each
for (plot in getPlotsWithInaccuratePests()) {
plot.pests = 1
plot.isPestCountInaccurate = false
}
} else if (inaccurateAmount == 1) { // if we can assume all the inaccurate pests are in the only inaccurate plot
val plot = getPlotsWithInaccuratePests().firstOrNull() ?: return@runDelayed
plot.pests = scoreboardPests - accurateAmount
plot.isPestCountInaccurate = false
} else if (accurateAmount + inaccurateAmount > scoreboardPests) { // when logic fails and we reach impossible pest counts
getInfestedPlots().forEach {
it.pests = 0
it.isPestCountInaccurate = true
}
if (loop > 0) {
fixPests(loop - 1)
} else sendPestError()
}
}
}
private fun updatePests() {
if (!firstScoreboardCheck) return
fixPests()
PestUpdateEvent().post()
}
@HandleEvent(onlyOnIslands = [IslandType.GARDEN])
fun onPestSpawn(event: PestSpawnEvent) {
PestSpawnTimer.lastSpawnTime = SimpleTimeMark.now()
val plotNames = event.plotNames
for (plotName in plotNames) {
val plot = GardenPlotAPI.getPlotByName(plotName)
if (plot == null) {
ChatUtils.userError("Open Plot Management Menu to load plot names and pest locations!")
return
}
if (event.unknownAmount) {
plot.isPestCountInaccurate = true
} else {
plot.pests += event.amountPests
plot.isPestCountInaccurate = false
}
}
if (!event.unknownAmount) scoreboardPests += event.amountPests
updatePests()
}
@SubscribeEvent
fun onInventoryOpen(event: InventoryFullyOpenedEvent) {
if (!GardenAPI.inGarden()) return
if (event.inventoryName != "Configure Plots") return
for (plot in GardenPlotAPI.plots) {
if (plot.isBarn() || plot.locked || plot.uncleared) continue
plot.pests = 0
plot.isPestCountInaccurate = false
val item = event.inventoryItems[plot.inventorySlot] ?: continue
item.getLore().matchFirst(pestInventoryPattern) {
plot.pests = group("amount").toInt()
}
}
updatePests()
}
@SubscribeEvent
fun onTabListUpdate(event: TabListUpdateEvent) {
if (!GardenAPI.inGarden()) return
for (line in event.tabList) {
infectedPlotsTablistPattern.matchMatcher(line) {
val plotList = group("plots").removeColor().split(", ").map { it.toInt() }
if (plotList.sorted() == getInfestedPlots().map { it.id }.sorted()) return
for (plot in GardenPlotAPI.plots) {
if (plotList.contains(plot.id)) {
if (!plot.isPestCountInaccurate && plot.pests == 0) {
plot.isPestCountInaccurate = true
}
} else {
plot.pests = 0
plot.isPestCountInaccurate = false
}
}
updatePests()
}
}
}
@SubscribeEvent
fun onScoreboardChange(event: ScoreboardUpdateEvent) {
if (!GardenAPI.inGarden()) return
if (!firstScoreboardCheck) return
checkScoreboardLines(event.scoreboard)
}
@SubscribeEvent
fun onChat(event: LorenzChatEvent) {
if (!GardenAPI.inGarden()) return
if (pestDeathChatPattern.matches(event.message)) {
lastPestKillTime = SimpleTimeMark.now()
removeNearestPest()
}
if (noPestsChatPattern.matches(event.message)) {
resetAllPests()
}
}
@SubscribeEvent
fun onTick(event: LorenzTickEvent) {
if (!GardenAPI.inGarden()) return
if (!firstScoreboardCheck && gardenJoinTime.passedSince() > 5.seconds) {
checkScoreboardLines(ScoreboardData.sidebarLinesFormatted
|