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
|
package at.hannibal2.skyhanni.features.garden.pests
import at.hannibal2.skyhanni.config.ConfigUpdaterMigrator
import at.hannibal2.skyhanni.config.features.garden.pests.PestSpawnConfig
import at.hannibal2.skyhanni.config.features.garden.pests.PestSpawnConfig.ChatMessageFormatEntry
import at.hannibal2.skyhanni.events.LorenzChatEvent
import at.hannibal2.skyhanni.events.garden.pests.PestSpawnEvent
import at.hannibal2.skyhanni.features.garden.GardenAPI
import at.hannibal2.skyhanni.skyhannimodule.SkyHanniModule
import at.hannibal2.skyhanni.utils.ChatUtils
import at.hannibal2.skyhanni.utils.ConfigUtils
import at.hannibal2.skyhanni.utils.HypixelCommands
import at.hannibal2.skyhanni.utils.LorenzUtils
import at.hannibal2.skyhanni.utils.RegexUtils.matchMatcher
import at.hannibal2.skyhanni.utils.StringUtils
import at.hannibal2.skyhanni.utils.StringUtils.removeColor
import at.hannibal2.skyhanni.utils.repopatterns.RepoPattern
import net.minecraftforge.fml.common.eventhandler.SubscribeEvent
import kotlin.time.Duration.Companion.seconds
@SkyHanniModule
object PestSpawn {
private val config get() = PestAPI.config.pestSpawn
private val patternGroup = RepoPattern.group("garden.pests.spawn")
/**
* REGEX-TEST: §6§lGROSS! §7A §6Pest §7has appeared in §aPlot §7- §b4§7!
*/
private val onePestPattern by patternGroup.pattern(
"one",
"§6§l.*! §7A §6Pest §7has appeared in §aPlot §7- §b(?<plot>.*)§7!"
)
/**
* REGEX-TEST: §6§lGROSS! §7A §6Pest §7has appeared in §aThe Barn§7!
*/
private val onePestBarnPattern by patternGroup.pattern(
"onebarn",
"§6§l.*! §7A §6Pest §7has appeared in §a(?<plot>The Barn)§7!"
)
/**
* REGEX-TEST: §6§lEWW! §62 Pests §7have spawned in §aPlot §7- §b2§7!
*/
private val multiplePestsSpawn by patternGroup.pattern(
"multiple",
"§6§l.*! §6(?<amount>\\d) Pests §7have spawned in §aPlot §7- §b(?<plot>.*)§7!"
)
/**
* REGEX-TEST: §6§lEWW! §62 Pests §7have spawned in §aThe Barn§7!
*/
private val multiplePestsBarnSpawn by patternGroup.pattern(
"multiplebarn",
"§6§l.*! §6(?<amount>\\d) Pests §7have spawned in §a(?<plot>The Barn)§7!"
)
/**
* REGEX-TEST: §6§lGROSS! §7While you were offline, §6Pests §7spawned in §aPlots §r§b12§r§7, §r§b9§r§7, §r§b5§r§7, §r§b11§r§7 and §r§b3§r§r§7!
*/
private val offlinePestsSpawn by patternGroup.pattern(
"offline",
"§6§l.*! §7While you were offline, §6Pests §7spawned in §aPlots (?<plots>.*)!"
)
private var plotNames = mutableListOf<String>()
@SubscribeEvent
fun onChat(event: LorenzChatEvent) {
if (!GardenAPI.inGarden()) return
val message = event.message
var blocked = false
plotNames.clear()
onePestPattern.matchMatcher(message) {
val plotName = group("plot")
plotNames.add(plotName)
pestSpawn(1, plotNames, false)
blocked = true
}
onePestBarnPattern.matchMatcher(message) {
val plotName = group("plot")
plotNames.add(plotName)
pestSpawn(1, plotNames, false)
blocked = true
}
multiplePestsSpawn.matchMatcher(message) {
val plotName = group("plot")
plotNames.add(plotName)
val amount = group("amount").toInt()
pestSpawn(amount, plotNames, false)
blocked = true
}
multiplePestsBarnSpawn.matchMatcher(message) {
val plotName = group("plot")
plotNames.add(plotName)
val amount = group("amount").toInt()
pestSpawn(amount, plotNames, false)
blocked = true
}
offlinePestsSpawn.matchMatcher(message) {
val plots = group("plots")
plotNames = plots.removeColor().split(", ", " and ").toMutableList()
pestSpawn(0, plotNames, true)
// blocked = true
}
if (event.message == " §r§e§lCLICK HERE §eto teleport to the plot!") {
if (PestSpawnTimer.lastSpawnTime.passedSince() < 1.seconds) {
blocked = true
}
}
if (blocked && config.chatMessageFormat != PestSpawnConfig.ChatMessageFormatEntry.HYPIXEL) {
event.blockedReason = "pests_spawn"
}
}
private fun pestSpawn(amount: Int, plotNames: List<String>, unknownAmount: Boolean) {
PestSpawnEvent(amount, plotNames, unknownAmount).post()
if (unknownAmount) return // todo make this work with offline pest spawn messages
val plotName = plotNames.firstOrNull() ?: error("first plot name is null")
val pestName = StringUtils.pluralize(amount, "Pest")
val message = "§e$amount §a$pestName Spawned in §b$plotName§a!"
if (config.showTitle) {
LorenzUtils.sendTitle(message, 7.seconds)
}
if (config.chatMessageFormat == PestSpawnConfig.ChatMessageFormatEntry.COMPACT) {
ChatUtils.clickableChat(
message,
onClick = {
HypixelCommands.teleportToPlot(plotName)
},
"§eClick to run /plottp $plotName!",
)
}
}
@SubscribeEvent
fun onConfigFix(event: ConfigUpdaterMigrator.ConfigFixEvent) {
event.transform(15, "garden.pests.pestSpawn.chatMessageFormat") { element ->
ConfigUtils.migrateIntToEnum(element, ChatMessageFormatEntry::class.java)
}
}
}
|