aboutsummaryrefslogtreecommitdiff
path: root/src/main/java/at/hannibal2/skyhanni/utils/MobUtils.kt
blob: 4e21804439889fe99b3d0d12d3c9f9792282e514 (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
package at.hannibal2.skyhanni.utils

import at.hannibal2.skyhanni.data.mob.Mob
import at.hannibal2.skyhanni.data.mob.MobData
import at.hannibal2.skyhanni.utils.EntityUtils.cleanName
import at.hannibal2.skyhanni.utils.LocationUtils.distanceTo
import at.hannibal2.skyhanni.utils.LocationUtils.rayIntersects
import at.hannibal2.skyhanni.utils.RegexUtils.matches
import at.hannibal2.skyhanni.utils.repopatterns.RepoPattern
import net.minecraft.entity.Entity
import net.minecraft.entity.EntityLivingBase
import net.minecraft.entity.item.EntityArmorStand
import net.minecraft.entity.player.EntityPlayer

object MobUtils {
    val defaultArmorStandName by RepoPattern.pattern("armorstand.default", "Armou?r Stand")

    // The corresponding ArmorStand for a mob has always the ID + 1 (with some exceptions)
    fun getArmorStand(entity: Entity, offset: Int = 1) = getNextEntity(entity, offset) as? EntityArmorStand

    fun getNextEntity(entity: Entity, offset: Int) = EntityUtils.getEntityByID(entity.entityId + offset)

    fun getArmorStandByRangeAll(entity: Entity, range: Double) =
        EntityUtils.getEntitiesNearby<EntityArmorStand>(entity.getLorenzVec(), range)

    fun getClosedArmorStand(entity: Entity, range: Double) =
        getArmorStandByRangeAll(entity, range).sortedBy { it.distanceTo(entity) }.firstOrNull()

    fun getClosedArmorStandWithName(entity: Entity, range: Double, name: String) =
        getArmorStandByRangeAll(entity, range).filter { it.cleanName().startsWith(name) }
            .sortedBy { it.distanceTo(entity) }.firstOrNull()

    fun EntityArmorStand.isDefaultValue() = defaultArmorStandName.matches(this.name)

    fun EntityArmorStand?.takeNonDefault() = this?.takeIf { !it.isDefaultValue() }

    class OwnerShip(val ownerName: String) {
        val ownerPlayer = MobData.players.firstOrNull { it.name == ownerName }
        override fun equals(other: Any?): Boolean {
            if (other is EntityPlayer) return ownerPlayer == other || ownerName == other.name
            if (other is String) return ownerName == other
            return false
        }

        override fun hashCode(): Int {
            return ownerName.hashCode()
        }
    }

    fun rayTraceForMob(entity: Entity, distance: Double, partialTicks: Float, offset: LorenzVec = LorenzVec()) =
        rayTraceForMob(entity, partialTicks, offset)?.takeIf {
            it.baseEntity.distanceTo(entity.getLorenzVec()) <= distance
        }

    fun rayTraceForMobs(
        entity: Entity,
        distance: Double,
        partialTicks: Float,
        offset: LorenzVec = LorenzVec(),
    ) =
        rayTraceForMobs(entity, partialTicks, offset)?.filter {
            it.baseEntity.distanceTo(entity.getLorenzVec()) <= distance
        }.takeIf { it?.isNotEmpty() ?: false }

    fun rayTraceForMob(entity: Entity, partialTicks: Float, offset: LorenzVec = LorenzVec()) =
        rayTraceForMobs(entity, partialTicks, offset)?.firstOrNull()

    fun rayTraceForMobs(entity: Entity, partialTicks: Float, offset: LorenzVec = LorenzVec()): List<Mob>? {
        val pos = entity.getPositionEyes(partialTicks).toLorenzVec() + offset
        val look = entity.getLook(partialTicks).toLorenzVec().normalize()
        val possibleEntities = MobData.entityToMob.filterKeys {
            it !is EntityArmorStand && it.entityBoundingBox.rayIntersects(
                pos, look
            )
        }.values
        if (possibleEntities.isEmpty()) return null
        return possibleEntities.distinct().sortedBy { it.baseEntity.distanceTo(pos) }
    }

    val EntityLivingBase.mob get() = MobData.entityToMob[this]

}