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
|
package at.hannibal2.skyhanni.data.mob
import at.hannibal2.skyhanni.SkyHanniMod
import at.hannibal2.skyhanni.data.IslandType
import at.hannibal2.skyhanni.data.mob.MobData.Companion.logger
import at.hannibal2.skyhanni.data.mob.MobFilter.isDisplayNPC
import at.hannibal2.skyhanni.data.mob.MobFilter.isRealPlayer
import at.hannibal2.skyhanni.data.mob.MobFilter.isSkyBlockMob
import at.hannibal2.skyhanni.events.DebugDataCollectEvent
import at.hannibal2.skyhanni.events.EntityHealthUpdateEvent
import at.hannibal2.skyhanni.events.LorenzTickEvent
import at.hannibal2.skyhanni.events.MobEvent
import at.hannibal2.skyhanni.events.PacketEvent
import at.hannibal2.skyhanni.utils.CollectionUtils.drainForEach
import at.hannibal2.skyhanni.utils.CollectionUtils.drainTo
import at.hannibal2.skyhanni.utils.CollectionUtils.put
import at.hannibal2.skyhanni.utils.CollectionUtils.refreshReference
import at.hannibal2.skyhanni.utils.EntityUtils
import at.hannibal2.skyhanni.utils.LocationUtils
import at.hannibal2.skyhanni.utils.LorenzUtils
import at.hannibal2.skyhanni.utils.MobUtils
import at.hannibal2.skyhanni.utils.getLorenzVec
import net.minecraft.client.entity.EntityPlayerSP
import net.minecraft.entity.EntityLivingBase
import net.minecraft.entity.item.EntityArmorStand
import net.minecraft.entity.monster.EntityCreeper
import net.minecraft.entity.passive.EntityBat
import net.minecraft.entity.passive.EntityVillager
import net.minecraft.entity.player.EntityPlayer
import net.minecraft.network.play.server.S0CPacketSpawnPlayer
import net.minecraft.network.play.server.S0FPacketSpawnMob
import net.minecraft.network.play.server.S37PacketStatistics
import net.minecraftforge.fml.common.eventhandler.SubscribeEvent
import net.minecraftforge.fml.common.network.FMLNetworkEvent
import java.util.concurrent.ConcurrentLinkedQueue
import java.util.concurrent.atomic.AtomicBoolean
private const val MAX_RETRIES = 20 * 5
class MobDetection {
/* Unsupported "Mobs"
Nicked Players
Odanate
Silk Worm
Fairy (in Dungeon)
Totem of Corruption
Worm
Scatha
Butterfly
Exe
Wai
Zee
*/
private val forceReset get() = !SkyHanniMod.feature.dev.mobDebug.enable
private var shouldClear: AtomicBoolean = AtomicBoolean(false)
init {
MobFilter.bossMobNameFilter
MobFilter.mobNameFilter
MobFilter.dojoFilter
MobFilter.summonFilter
MobFilter.dungeonNameFilter
MobFilter.petCareNamePattern
MobFilter.slayerNameFilter
MobFilter.summonOwnerPattern
MobFilter.wokeSleepingGolemPattern
MobFilter.jerryPattern
MobFilter.jerryMagmaCubePattern
MobUtils.defaultArmorStandName
}
private fun mobDetectionReset() {
MobData.currentMobs.map {
it.createDeSpawnEvent()
}.forEach { it.postAndCatch() }
}
@SubscribeEvent
fun onTick(event: LorenzTickEvent) {
if (shouldClear.get()) { // Needs to work outside skyblock since it needs clearing when leaving skyblock and joining limbo
mobDetectionReset()
shouldClear.set(false)
}
if (!LorenzUtils.inSkyBlock) return
if (event.isMod(2)) return
makeEntityReferenceUpdate()
handleMobsFromPacket()
handleRetries()
MobData.previousEntityLiving.clear()
MobData.previousEntityLiving.addAll(MobData.currentEntityLiving)
MobData.currentEntityLiving.clear()
MobData.currentEntityLiving.addAll(EntityUtils.getEntities<EntityLivingBase>()
.filter { it !is EntityArmorStand && it !is EntityPlayerSP })
if (forceReset) {
MobData.currentEntityLiving.clear() // Naturally removing the mobs using the despawn
}
(MobData.currentEntityLiving - MobData.previousEntityLiving).forEach { addRetry(it) } // Spawn
(MobData.previousEntityLiving - MobData.currentEntityLiving).forEach { entityDeSpawn(it) } // Despawn
if (forceReset) {
mobDetectionReset() // Ensure that all mobs are cleared 100%
}
}
/** Splits the entity into player, displayNPC and other */
private fun EntityLivingBase.getRoughType() = when {
this is EntityPlayer && this.isRealPlayer() -> Mob.Type.PLAYER
this.isDisplayNPC() -> Mob.Type.DISPLAY_NPC
this.isSkyBlockMob() && !islandException() -> Mob.Type.BASIC
else -> null
}
private fun addRetry(entity: EntityLivingBase) = entity.getRoughType()?.let { type ->
val re = MobData.RetryEntityInstancing(entity, 0, type)
MobData.retries.put(re.toKeyValuePair())
}
private fun removeRetry(entity: EntityLivingBase) = MobData.retries.remove(entity.entityId)
private fun getRetry(entity: EntityLivingBase) = MobData.retries[entity.entityId]
/** @return always true */
private fun mobDetectionError(string: String) = logger.log(string).let { true }
/**@return a false means that it should try again (later)*/
private fun entitySpawn(entity: EntityLivingBase, roughType: Mob.Type): Boolean {
when (roughType) {
Mob.Type.PLAYER -> MobEvent.Spawn.Player(MobFactories.player(entity)).postAndCatch()
Mob.Type.DISPLAY_NPC -> return MobFilter.createDisplayNPC(entity)
Mob.Type.BASIC -> {
val (result, mob) = MobFilter.createSkyblockEntity(entity)
when (result) {
MobData.Result.NotYetFound -> return false
MobData.Result.Illegal -> return true // Remove entity from the spawning queue
MobData.Result.SomethingWentWrong -> return mobDetectionError("Something Went Wrong!")
MobData.Result.Found -> {
if (mob == null) return mobDetectionError("Mob is null even though result is Found")
when (mob.mobType) {
Mob.Type.SUMMON -> MobEvent.Spawn.Summon(mob)
Mob.Type.BASIC, Mob.Type.DUNGEON, Mob.Type.BOSS, Mob.Type.SLAYER -> MobEvent.Spawn.SkyblockMob(
mob
)
Mob.Type.SPECIAL -> MobEvent.Spawn.Special(mob)
Mob.Type.PROJECTILE -> MobEvent.Spawn.Projectile(mob)
Mob.Type.DISPLAY_NPC -> MobEvent.Spawn.DisplayNPC(mob) // Needed for some special cases
Mob.Type.PLAYER -> return mobDetectionError("An Player Ended Here. How?")
}.postAndCatch()
}
}
}
else -> return true
}
return true
}
private val entityFromPacket = ConcurrentLinkedQueue<Pair<EntityPacketType, Int>>()
/** For mobs that have default health of the entity */
private enum class EntityPacketType {
SPIRIT_BAT,
VILLAGER,
CREEPER_VAIL,
}
/** Handles some mobs that have default health of the entity, specially using the [EntityHealthUpdateEvent] */
private fun handleMobsFromPacket() = entityFromPacket.drainForEach { (type, id) ->
when (type) {
EntityPacketType.SPIRIT_BAT -> {
val entity = EntityUtils.getEntityByID(id) as? EntityBat ?: return@drainForEach
if (MobData.entityToMob[entity] != null) return@drainForEach
removeRetry(entity)
MobEvent.Spawn.Projectile(MobFactories.projectile(entity, "Spirit Scepter Bat")).postAndCatch()
}
EntityPacketType.VILLAGER -> {
val entity = EntityUtils.getEntityByID(id) as? EntityVillager ?: return@drainForEach
val mob = MobData.entityToMob[entity]
if (mob != null && mob.mobType == Mob.Type.DISPLAY_NPC) {
MobEvent.DeSpawn.DisplayNPC(mob)
addRetry(entity)
return@drainForEach
}
getRetry(entity)?.let {
if (it.roughType == Mob.Type.DISPLAY_NPC) {
removeRetry(entity)
addRetry(entity)
}
}
}
EntityPacketType.CREEPER_VAIL -> {
val entity = EntityUtils.getEntityByID(id) as? EntityCreeper ?: return@drainForEach
if (MobData.entityToMob[entity] != null) return@drainForEach
if (!entity.powered) return@drainForEach
removeRetry(entity)
MobEvent.Spawn.Special(MobFactories.special(entity, "Creeper Veil")).postAndCatch()
}
}
}
@SubscribeEvent
fun onEntityHealthUpdateEvent(event: EntityHealthUpdateEvent) {
when {
event.entity is EntityBat && event.health == 6 -> {
entityFromPacket.a
|