aboutsummaryrefslogtreecommitdiff
path: root/src/main/java/at/hannibal2/skyhanni/utils/MobUtils.kt
diff options
context:
space:
mode:
authorThunderblade73 <85900443+Thunderblade73@users.noreply.github.com>2024-04-03 20:50:31 +0200
committerGitHub <noreply@github.com>2024-04-03 20:50:31 +0200
commit2f85351bacddb9ab3704a53c778d558a755bcc06 (patch)
treef479045f271f04a66a1ba69a61cb1b9893261eb5 /src/main/java/at/hannibal2/skyhanni/utils/MobUtils.kt
parent76be6ad6de39c7078550394e8ec24a494ddb3bcc (diff)
downloadskyhanni-2f85351bacddb9ab3704a53c778d558a755bcc06.tar.gz
skyhanni-2f85351bacddb9ab3704a53c778d558a755bcc06.tar.bz2
skyhanni-2f85351bacddb9ab3704a53c778d558a755bcc06.zip
Backend: Mob Detection (#712)
Co-authored-by: hannibal2 <24389977+hannibal00212@users.noreply.github.com> Co-authored-by: Cal <cwolfson58@gmail.com>
Diffstat (limited to 'src/main/java/at/hannibal2/skyhanni/utils/MobUtils.kt')
-rw-r--r--src/main/java/at/hannibal2/skyhanni/utils/MobUtils.kt80
1 files changed, 80 insertions, 0 deletions
diff --git a/src/main/java/at/hannibal2/skyhanni/utils/MobUtils.kt b/src/main/java/at/hannibal2/skyhanni/utils/MobUtils.kt
new file mode 100644
index 000000000..e1b165d25
--- /dev/null
+++ b/src/main/java/at/hannibal2/skyhanni/utils/MobUtils.kt
@@ -0,0 +1,80 @@
+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 net.minecraft.entity.Entity
+import net.minecraft.entity.EntityLivingBase
+import net.minecraft.entity.item.EntityArmorStand
+import net.minecraft.entity.player.EntityPlayer
+
+object MobUtils {
+ private const val defaultArmorStandName = "Armor 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() = this.name == defaultArmorStandName
+
+ 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().add(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) }.drop(1) // drop to remove player
+ }
+
+ val EntityLivingBase.mob get() = MobData.entityToMob[this]
+
+}