aboutsummaryrefslogtreecommitdiff
path: root/src/main/java/at/hannibal2/skyhanni/data/mob/MobDetection.kt
blob: 9b7ba2cfd5e10dae01ff3d4bd15285fc6ca076c0 (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
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
385
386
387
388
389
390
391
392
393
package at.hannibal2.skyhanni.data.mob

import at.hannibal2.skyhanni.SkyHanniMod
import at.hannibal2.skyhanni.api.event.HandleEvent
import at.hannibal2.skyhanni.data.IslandType
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.minecraft.ClientDisconnectEvent
import at.hannibal2.skyhanni.events.minecraft.packet.PacketReceivedEvent
import at.hannibal2.skyhanni.skyhannimodule.SkyHanniModule
import at.hannibal2.skyhanni.utils.ChatUtils
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.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.S01PacketJoinGame
import net.minecraft.network.play.server.S0CPacketSpawnPlayer
import net.minecraft.network.play.server.S0FPacketSpawnMob
import net.minecraft.world.World
import net.minecraftforge.fml.common.eventhandler.SubscribeEvent
import java.util.concurrent.ConcurrentLinkedQueue
import java.util.concurrent.atomic.AtomicBoolean

@SkyHanniModule
object MobDetection {

    /* Unsupported Entities
        Nicked Players
        Odonata
        Silk Worm
        Fairy (in Dungeon)
        Totem of Corruption
        Worm
        Scatha
        Butterfly
        Exe
        Wai
        Zee
     */

    private const val MAX_RETRIES = 20 * 5

    private val forceReset get() = !SkyHanniMod.feature.dev.mobDebug.enable

    private var shouldClear: AtomicBoolean = AtomicBoolean(false)

    private fun mobDetectionReset() {
        MobData.currentMobs.map {
            it.createDeSpawnEvent()
        }.forEach { it.postAndCatch() }
        MobData.retries.clear()
    }

    // TODO this is a unused debug funciton. maybe connect with a debug commmand or remove
    private fun watchdog() {
        val world = LorenzUtils.getPlayer()?.worldObj ?: return
        if (MobData.retries.any { it.value.entity.worldObj != world }) {
            ChatUtils.chat("Watchdog: Retires")
        }
        if (MobData.currentMobs.any { it.watchdogCheck(world) }) {
            ChatUtils.chat("Watchdog: Current Mobs")
        }
        if (MobData.players.any { it.watchdogCheck(world) }) {
            ChatUtils.chat("Watchdog: Players")
        }
        if (MobData.displayNPCs.any { it.watchdogCheck(world) }) {
            ChatUtils.chat("Watchdog: Display NPCs")
        }
        if (MobData.skyblockMobs.any { it.watchdogCheck(world) }) {
            ChatUtils.chat("Watchdog: SkyBlockMobs")
        }
        if (MobData.summoningMobs.any { it.watchdogCheck(world) }) {
            ChatUtils.chat("Watchdog: Summoning")
        }
        if (MobData.special.any { it.watchdogCheck(world) }) {
            ChatUtils.chat("Watchdog: Special")
        }
        if (MobData.notSeenMobs.any { it.watchdogCheck(world) }) {
            ChatUtils.chat("Watchdog: Not Seen Mobs")
        }
    }

    private fun Mob.watchdogCheck(world: World): Boolean =
        this.baseEntity.worldObj != world || (this.armorStand?.let { it.worldObj != world }
            ?: false) || this.extraEntities.any { it.worldObj != world }

    @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

        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

        MobData.notSeenMobs.removeIf(::canBeSeen)

        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) = MobData.logger.log(string).let { true }

    private fun canBeSeen(mob: Mob): Boolean {
        val isVisible = !mob.isInvisible() && mob.canBeSeen()
        if (isVisible) when (mob.mobType) {
            Mob.Type.PLAYER -> MobEvent.FirstSeen.Player(mob)
            Mob.Type.SUMMON -> MobEvent.FirstSeen.Summon(mob)
            Mob.Type.SPECIAL -> MobEvent.FirstSeen.Special(mob)
            Mob.Type.PROJECTILE -> MobEvent.FirstSeen.Projectile(mob)
            Mob.Type.DISPLAY_NPC -> MobEvent.FirstSeen.DisplayNPC(mob)
            Mob.Type.BASIC, Mob.Type.DUNGEON, Mob.Type.BOSS, Mob.Type.SLAYER -> MobEvent.FirstSeen.SkyblockMob(mob)
        }
        return isVisible
    }

    /**@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.add(EntityPacketType.SPIRIT_BAT to event.entity.entityId)
            }

            event.entity is EntityVillager && event.health != 20 -> {
                entityFromPacket.add(EntityPacketType.VILLAGER to event.entity.entityId)
            }

            event.entity is EntityCreeper && event.health == 20 -> {
                entityFromPacket.add(EntityPacketType.CREEPER_VAIL to event.entity.entityId)
            }
        }
    }

    private fun islandException(): Boolean = when (LorenzUtils.skyBlockIsland) {
        IslandType.GARDEN_GUEST -> true
        IslandType.PRIVATE_ISLAND_GUEST -> true
        else -> false
    }

    private fun entityDeSpawn(entity: EntityLivingBase) {
        MobData.entityToMob[entity]?.createDeSpawnEvent()?.postAndCatch() ?: removeRetry(entity)
        allEntitiesViaPacketId.remove(entity.entityId)
    }

    private fun Mob.createDeSpawnEvent() = when (this.mobType) {
        Mob.Type.PLAYER -> MobEvent.DeSpawn.Player(this)
        Mob.Type.SUMMON -> MobEvent.DeSpawn.Summon(this)
        Mob.Type.SPECIAL -> MobEvent.DeSpawn.Special(this)
        Mob.Type.PROJECTILE -> MobEvent.DeSpawn.Projectile(this)
        Mob.Type.DISPLAY_NPC -> MobEvent.DeSpawn.DisplayNPC(this)
        Mob.Type.BASIC, Mob.Type.DUNGEON, Mob.Type.BOSS, Mob.Type.SLAYER -> MobEvent.DeSpawn.SkyblockMob(this)
    }

    private fun handleRetries() {
        val iterator = MobData.retries.iterator()
        while (iterator.hasNext()) {
            val (_, retry) = iterator.next()

            if (MobData.externRemoveOfRetryAmount > 0) {
                iterator.remove()
                MobData.externRemoveOfRetryAmount--
                continue
            }

            if (retry.outsideRange()) continue

            val entity = retry.entity
            if (retry.times == MAX_RETRIES) {
                MobData.logger.log(
                    "`${retry.entity.name}`${retry.entity.entityId} missed {\n "
                        + "is already Found: ${MobData.entityToMob[retry.entity] != null})."
                        + "\n Position: ${retry.entity.getLorenzVec()}\n "
                        + "DistanceC: ${
                        entity.getLorenzVec().distanceChebyshevIgnoreY(LocationUtils.playerLocation())
                    }\n"
                        + "Relative Position: ${entity.getLorenzVec() - LocationUtils.playerLocation()}\n " +
                        "}",
                )
                // Uncomment this to make it closed a loop
                // iterator.remove()
                // continue
            }
            if (!entitySpawn(entity, retry.roughType)) {
                retry.times++
                continue
            }
            iterator.remove()
        }
    }

    private val entityUpdatePackets = ConcurrentLinkedQueue<Int>()
    private val entitiesThatRequireUpdate = mutableSetOf<Int>() // needs to be distinct, therefore not using a queue

    /** Refreshes the references of the entities in entitiesThatRequireUpdate */
    private fun makeEntityReferenceUpdate() {
        entitiesThatRequireUpdate.iterator().let { iterator ->
            while (iterator.hasNext()) {
                if (handleEntityUpdate(iterator.next())) iterator.remove()
            }
        }
        entityUpdatePackets.drainTo(entitiesThatRequireUpdate)
    }

    private fun handleEntityUpdate(entityID: Int): Boolean {
        val entity = EntityUtils.getEntityByID(entityID) as? EntityLivingBase ?: return false
        getRetry(entity)?.apply { this.entity = entity }
        MobData.currentEntityLiving.refreshReference(entity)
        MobData.previousEntityLiving.refreshReference(entity)
        // update map
        MobData.entityToMob[entity]?.internalUpdateOfEntity(entity)
        return true
    }

    @HandleEvent
    fun onEntitySpawnPacket(event: PacketReceivedEvent) {
        when (val packet = event.packet) {
            is S0FPacketSpawnMob -> addEntityUpdate(packet.entityID)
            is S0CPacketSpawnPlayer -> addEntityUpdate(packet.entityID)
            // is S0EPacketSpawnObject -> addEntityUpdate(packet.entityID)
            is S01PacketJoinGame -> // one of the first packets that is sent when switching servers inside the BungeeCord Network (please some prove this, I just found it out via Testing)
            {
                shouldClear.set(true)
                allEntitiesViaPacketId.clear()
            }
        }
    }

    private val allEntitiesViaPacketId = mutableSetOf<Int>()

    private fun addEntityUpdate(id: Int) = if (allEntitiesViaPacketId.contains(id)) {
        entityUpdatePackets.add(id)
    } else {
        allEntitiesViaPacketId.add(id)
    }

    @HandleEvent
    fun onDisconnect(event: ClientDisconnectEvent) {
        shouldClear.set(true)
    }

    @SubscribeEvent
    fun onDebugDataCollect(event: DebugDataCollectEvent) {
        event.title("Mob Detection")
        if (forceReset) {
            event.addData("Mob Detection is manually disabled!")
        } else {
            event.addIrrelevant {
                add("normal enabled")
                add("Active Mobs: ${MobData.currentMobs.size}")
                val inDistanceMobs = MobData.retries.count { it.value.outsideRange() }
                add("Searching for Mobs: ${MobData.retries.size - inDistanceMobs}")
                add("Mobs over Max Search Count: ${MobData.retries.count { it.value.times > MAX_RETRIES }}")
                add("Mobs outside of Range: $inDistanceMobs")
            }
        }
    }

}