Skip to content

Commit fe199ba

Browse files
committed
Fixed damage calculation
1 parent bba0792 commit fe199ba

File tree

1 file changed

+20
-4
lines changed

1 file changed

+20
-4
lines changed

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

Lines changed: 20 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ package com.lambda.util.combat
2020
import com.lambda.context.SafeContext
2121
import com.lambda.util.math.distSq
2222
import com.lambda.util.world.fastEntitySearch
23+
import net.minecraft.client.world.ClientWorld
2324
import net.minecraft.entity.EquipmentSlot
2425
import net.minecraft.entity.LivingEntity
2526
import net.minecraft.entity.damage.DamageSource
@@ -30,8 +31,11 @@ import net.minecraft.registry.tag.DamageTypeTags.IS_FIRE
3031
import net.minecraft.registry.tag.DamageTypeTags.IS_FREEZING
3132
import net.minecraft.registry.tag.EntityTypeTags.FREEZE_HURTS_EXTRA_TYPES
3233
import net.minecraft.util.math.Vec3d
34+
import net.minecraft.world.Difficulty
35+
import net.minecraft.world.World
3336
import net.minecraft.world.explosion.Explosion
3437
import net.minecraft.world.explosion.ExplosionImpl
38+
import kotlin.math.min
3539

3640
object CombatUtils {
3741
/**
@@ -40,7 +44,7 @@ object CombatUtils {
4044
* @param entity The entity to calculate the damage for
4145
* @param damage The damage to apply
4246
*/
43-
fun DamageSource.scale(entity: LivingEntity, damage: Double): Double {
47+
fun DamageSource.scale(world: ClientWorld, entity: LivingEntity, damage: Double): Double {
4448
if (damage.isNaN() || damage.isInfinite())
4549
return Double.MAX_VALUE
4650

@@ -55,10 +59,22 @@ object CombatUtils {
5559
if (isIn(DAMAGES_HELMET) && !entity.getEquippedStack(EquipmentSlot.HEAD).isEmpty)
5660
return damage * 0.75
5761

58-
return entity.applyArmorToDamage(this,
59-
entity.modifyAppliedDamage(this, damage.toFloat())).toDouble()
62+
return world.scaleDamage(
63+
entity.applyArmorToDamage(this,
64+
entity.modifyAppliedDamage(this, damage.toFloat())).toDouble()
65+
)
6066
}
6167

68+
/**
69+
* Scales the damage depending on the world difficulty
70+
*/
71+
fun World.scaleDamage(damage: Double): Double =
72+
when (difficulty) {
73+
Difficulty.EASY -> min(damage / 2 + 1, damage)
74+
Difficulty.HARD -> damage * 3 / 2
75+
else -> damage
76+
}
77+
6278
/**
6379
* Returns whether there is a deadly end crystal in proximity of the player
6480
*
@@ -99,6 +115,6 @@ object CombatUtils {
99115
val impact = (1 - distance / range) * ExplosionImpl.calculateReceivedDamage(position, entity) * 0.4
100116
val damage = (impact * impact + impact) / 2.0 * 7.0 * range + 1
101117

102-
return Explosion.createDamageSource(world, null).scale(entity, damage)
118+
return Explosion.createDamageSource(world, null).scale(world, entity, damage)
103119
}
104120
}

0 commit comments

Comments
 (0)