Skip to content

Commit eac8427

Browse files
committed
Fixed damage calculation
1 parent 4519798 commit eac8427

File tree

1 file changed

+22
-12
lines changed

1 file changed

+22
-12
lines changed

common/src/main/kotlin/com/lambda/util/combat/CombatUtils.kt

Lines changed: 22 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -18,13 +18,9 @@
1818
package com.lambda.util.combat
1919

2020
import com.lambda.context.SafeContext
21-
import com.lambda.core.annotations.InternalApi
2221
import com.lambda.util.math.distSq
23-
import com.lambda.util.math.minus
24-
import com.lambda.util.math.times
25-
import com.lambda.util.world.WorldUtils.internalGetFastEntities
2622
import com.lambda.util.world.fastEntitySearch
27-
import com.lambda.util.world.toFastVec
23+
import net.minecraft.client.world.ClientWorld
2824
import net.minecraft.entity.EquipmentSlot
2925
import net.minecraft.entity.LivingEntity
3026
import net.minecraft.entity.damage.DamageSource
@@ -35,8 +31,10 @@ import net.minecraft.registry.tag.DamageTypeTags.IS_FIRE
3531
import net.minecraft.registry.tag.DamageTypeTags.IS_FREEZING
3632
import net.minecraft.registry.tag.EntityTypeTags.FREEZE_HURTS_EXTRA_TYPES
3733
import net.minecraft.util.math.Vec3d
34+
import net.minecraft.world.Difficulty
35+
import net.minecraft.world.World
3836
import net.minecraft.world.explosion.Explosion
39-
import net.minecraft.world.explosion.ExplosionImpl
37+
import kotlin.math.min
4038

4139
object CombatUtils {
4240
/**
@@ -45,11 +43,11 @@ object CombatUtils {
4543
* @param entity The entity to calculate the damage for
4644
* @param damage The damage to apply
4745
*/
48-
fun DamageSource.scale(entity: LivingEntity, damage: Double): Double {
46+
fun DamageSource.scale(world: ClientWorld, entity: LivingEntity, damage: Double): Double {
4947
if (damage.isNaN() || damage.isInfinite())
5048
return Double.MAX_VALUE
5149

52-
if (entity.isAlwaysInvulnerableTo(this) ||
50+
if (entity.isInvulnerableTo(this) ||
5351
entity.isDead ||
5452
entity.blockedByShield(this) ||
5553
isIn(IS_FIRE) && entity.hasStatusEffect(FIRE_RESISTANCE)) return 0.0
@@ -60,10 +58,22 @@ object CombatUtils {
6058
if (isIn(DAMAGES_HELMET) && !entity.getEquippedStack(EquipmentSlot.HEAD).isEmpty)
6159
return damage * 0.75
6260

63-
return entity.applyArmorToDamage(this,
64-
entity.modifyAppliedDamage(this, damage.toFloat())).toDouble()
61+
return world.scaleDamage(
62+
entity.applyArmorToDamage(this,
63+
entity.modifyAppliedDamage(this, damage.toFloat())).toDouble()
64+
)
6565
}
6666

67+
/**
68+
* Scales the damage depending on the world difficulty
69+
*/
70+
fun World.scaleDamage(damage: Double): Double =
71+
when (difficulty) {
72+
Difficulty.EASY -> min(damage / 2 + 1, damage)
73+
Difficulty.HARD -> damage * 3 / 2
74+
else -> damage
75+
}
76+
6777
/**
6878
* Returns whether there is a deadly end crystal in proximity of the player
6979
*
@@ -101,9 +111,9 @@ object CombatUtils {
101111
val distance = entity distSq position
102112

103113
val range = power * 2
104-
val impact = (1 - distance / range) * ExplosionImpl.calculateReceivedDamage(position, entity) * 0.4
114+
val impact = (1 - distance / range) * Explosion.getExposure(position, entity) * 0.4
105115
val damage = (impact * impact + impact) / 2.0 * 7.0 * range + 1
106116

107-
return Explosion.createDamageSource(world, null).scale(entity, damage)
117+
return Explosion.createDamageSource(world, null).scale(world, entity, damage)
108118
}
109119
}

0 commit comments

Comments
 (0)