Skip to content

Commit 26ba17d

Browse files
committed
Feature: Crystal damage utils
1 parent 3010d83 commit 26ba17d

File tree

2 files changed

+122
-0
lines changed

2 files changed

+122
-0
lines changed
Lines changed: 87 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,87 @@
1+
package com.lambda.util.combat
2+
3+
import com.lambda.context.SafeContext
4+
import com.lambda.util.combat.DamageUtils.applyProtection
5+
import com.lambda.util.math.VecUtils.minus
6+
import com.lambda.util.math.VecUtils.times
7+
import net.minecraft.enchantment.EnchantmentHelper
8+
import net.minecraft.enchantment.ProtectionEnchantment
9+
import net.minecraft.entity.LivingEntity
10+
import net.minecraft.entity.damage.DamageSource
11+
import net.minecraft.entity.effect.StatusEffects
12+
import net.minecraft.registry.tag.DamageTypeTags
13+
import net.minecraft.server.network.ServerPlayerEntity
14+
import net.minecraft.stat.Stats
15+
import net.minecraft.util.math.MathHelper
16+
import net.minecraft.util.math.MathHelper.clamp
17+
import net.minecraft.util.math.Vec3d
18+
import net.minecraft.world.World.ExplosionSourceType
19+
import net.minecraft.world.explosion.Explosion
20+
import kotlin.math.max
21+
22+
/**
23+
* Utility functions related to explosion effects and calculations.
24+
*/
25+
object CrystalUtils {
26+
/**
27+
* Calculates the radius of an explosion based on its power.
28+
* @param power The strength of the explosion.
29+
* @return The radius of the explosion.
30+
*/
31+
fun SafeContext.explosionRadius(power: Double): Double =
32+
1.3 * (power / 0.225) * 0.3
33+
34+
/**
35+
* Calculates the damage dealt by an explosion to a living entity.
36+
* @param source The source of the explosion.
37+
* @param entity The entity to calculate the damage for.
38+
* @return The damage dealt by the explosion.
39+
*/
40+
fun SafeContext.explosionDamage(source: Explosion, entity: LivingEntity): Double =
41+
explosionDamage(source.position, entity, source.power.toDouble())
42+
43+
/**
44+
* Calculates the damage dealt by an explosion to a living entity.
45+
* @param position The position of the explosion.
46+
* @param entity The entity to calculate the damage for.
47+
* @param power The strength of the explosion.
48+
* @return The damage dealt by the explosion.
49+
*/
50+
fun SafeContext.explosionDamage(position: Vec3d, entity: LivingEntity, power: Double): Double {
51+
val distance = entity.pos.distanceTo(position)
52+
if (explosionRadius(power) < distance) return 0.0 // Avoid unnecessary calculations
53+
val size = power * 2.0
54+
val impact = (1.0 - distance / size) * Explosion.getExposure(position, entity)
55+
val damage = (impact * impact + impact) / 2.0 * 7.0 * size + 1
56+
return applyProtection(entity, damage, Explosion.createDamageSource(world, null))
57+
}
58+
59+
/**
60+
* Calculates the velocity of a living entity affected by an explosion.
61+
* @param entity The entity to calculate the velocity for.
62+
* @param explosion The explosion to calculate the velocity for.
63+
* @return The velocity of the entity.
64+
*/
65+
fun SafeContext.explosionVelocity(entity: LivingEntity, explosion: Explosion): Vec3d =
66+
explosionVelocity(entity, explosion.position, explosion.power.toDouble())
67+
68+
/**
69+
* Calculates the velocity of a living entity affected by an explosion.
70+
* @param entity The entity to calculate the velocity for.
71+
* @param position The position of the explosion.
72+
* @param power The strength of the explosion.
73+
* @return The velocity of the entity.
74+
*/
75+
fun SafeContext.explosionVelocity(entity: LivingEntity, position: Vec3d, power: Double): Vec3d {
76+
val distance = entity.pos.distanceTo(position)
77+
if (explosionRadius(power) < distance) return Vec3d.ZERO // Avoid unnecessary calculations
78+
val size = power * 2.0
79+
val vel = ProtectionEnchantment.transformExplosionKnockback(
80+
entity,
81+
(1.0 - distance / size) * Explosion.getExposure(position, entity)
82+
)
83+
84+
val diff = entity.eyePos - position
85+
return diff.normalize() * vel
86+
}
87+
}
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
package com.lambda.util.combat
2+
3+
import com.lambda.context.SafeContext
4+
import net.minecraft.enchantment.EnchantmentHelper
5+
import net.minecraft.entity.LivingEntity
6+
import net.minecraft.entity.damage.DamageSource
7+
import net.minecraft.entity.effect.StatusEffects
8+
import net.minecraft.registry.tag.DamageTypeTags
9+
import kotlin.math.max
10+
import kotlin.math.min
11+
12+
object DamageUtils {
13+
/**
14+
* @param entity The entity to calculate the damage for
15+
* @param damage The damage to apply
16+
* @return The damage dealt by the explosion
17+
*/
18+
fun SafeContext.applyProtection(entity: LivingEntity, damage: Double, source: DamageSource): Double {
19+
val resistanceAmplifier = entity.getStatusEffect(StatusEffects.RESISTANCE)?.amplifier ?: -1
20+
21+
if (source.isIn(DamageTypeTags.BYPASSES_EFFECTS)) return damage
22+
23+
if (entity.hasStatusEffect(StatusEffects.RESISTANCE) && !source.isIn(DamageTypeTags.BYPASSES_RESISTANCE))
24+
return (damage - max(damage * (25 - (resistanceAmplifier + 1) * 5) / 25.0, 0.0)).coerceAtLeast(0.0)
25+
26+
if (source.isIn(DamageTypeTags.BYPASSES_ENCHANTMENTS)) return damage
27+
28+
val protectionAmount = EnchantmentHelper.getProtectionAmount(entity.armorItems, source)
29+
30+
if (protectionAmount > 0) return damage * (1.0 - min(protectionAmount, 20) / 25.0)
31+
32+
return damage
33+
}
34+
35+
}

0 commit comments

Comments
 (0)