diff --git a/src/main/java/ru/dbotthepony/mc/otm/OverdriveThatMatters.java b/src/main/java/ru/dbotthepony/mc/otm/OverdriveThatMatters.java index 972fadf72..4b410adfc 100644 --- a/src/main/java/ru/dbotthepony/mc/otm/OverdriveThatMatters.java +++ b/src/main/java/ru/dbotthepony/mc/otm/OverdriveThatMatters.java @@ -160,6 +160,7 @@ public final class OverdriveThatMatters { EVENT_BUS.addListener(EventPriority.NORMAL, MatteryPlayerCapability.Companion::onPlayerTick); EVENT_BUS.addListener(EventPriority.NORMAL, MatteryPlayerCapability.Companion::isMobEffectApplicable); EVENT_BUS.addListener(EventPriority.LOW, MatteryPlayerCapability.Companion::onHurtEvent); + EVENT_BUS.addListener(EventPriority.LOW, MatteryPlayerCapability.Companion::onAttackEvent); EVENT_BUS.addGenericListener(Entity.class, EventPriority.NORMAL, MatteryPlayerCapability.Companion::onAttachCapabilityEvent); EVENT_BUS.addListener(EventPriority.NORMAL, MatteryPlayerCapability.Companion::onPlayerChangeDimensionEvent); EVENT_BUS.addListener(EventPriority.LOWEST, MatteryPlayerCapability.Companion::onPlayerDeath); diff --git a/src/main/kotlin/ru/dbotthepony/mc/otm/android/AndroidFeature.kt b/src/main/kotlin/ru/dbotthepony/mc/otm/android/AndroidFeature.kt index fc9fb84d9..a681d2a20 100644 --- a/src/main/kotlin/ru/dbotthepony/mc/otm/android/AndroidFeature.kt +++ b/src/main/kotlin/ru/dbotthepony/mc/otm/android/AndroidFeature.kt @@ -3,6 +3,7 @@ package ru.dbotthepony.mc.otm.android import it.unimi.dsi.fastutil.io.FastByteArrayOutputStream import net.minecraft.nbt.CompoundTag import net.minecraftforge.common.util.INBTSerializable +import net.minecraftforge.event.entity.living.LivingAttackEvent import net.minecraftforge.event.entity.living.LivingHurtEvent import ru.dbotthepony.mc.otm.capability.MatteryPlayerCapability import ru.dbotthepony.mc.otm.core.nbt.set @@ -34,6 +35,7 @@ abstract class AndroidFeature(val type: AndroidFeatureType<*>, val android: Matt open fun removeModifiers() {} open fun onHurt(event: LivingHurtEvent) {} + open fun onAttack(event: LivingAttackEvent) {} open fun collectNetworkPayload(): FastByteArrayOutputStream? { return synchronizer.collectNetworkPayload() diff --git a/src/main/kotlin/ru/dbotthepony/mc/otm/android/feature/FallDampenersFeature.kt b/src/main/kotlin/ru/dbotthepony/mc/otm/android/feature/FallDampenersFeature.kt index c0db601b9..4b1b75900 100644 --- a/src/main/kotlin/ru/dbotthepony/mc/otm/android/feature/FallDampenersFeature.kt +++ b/src/main/kotlin/ru/dbotthepony/mc/otm/android/feature/FallDampenersFeature.kt @@ -3,6 +3,7 @@ package ru.dbotthepony.mc.otm.android.feature import net.minecraft.ChatFormatting import net.minecraft.resources.ResourceLocation import net.minecraft.server.level.ServerPlayer +import net.minecraftforge.event.entity.living.LivingAttackEvent import net.minecraftforge.event.entity.living.LivingHurtEvent import ru.dbotthepony.mc.otm.OverdriveThatMatters import ru.dbotthepony.mc.otm.android.AndroidFeature @@ -23,13 +24,10 @@ class FallDampenersFeature(capability: MatteryPlayerCapability) : AndroidFeature val flat = (AndroidConfig.FALL_DAMAGE_REDUCTION_PER_LEVEL_F * (level + 1)).toFloat().coerceIn(0f, Float.MAX_VALUE) val old = event.amount + event.amount = ((event.amount - flat) * (1f - reduction)).coerceAtLeast(0f) - if (reduction >= 1f || event.amount <= flat) { + if (event.amount == 0f) { event.isCanceled = true - event.amount = 0f - } else { - event.amount -= flat - event.amount *= (1f - reduction) } if (ply is ServerPlayer && ply.health > event.amount && ply.health <= old) { @@ -37,4 +35,15 @@ class FallDampenersFeature(capability: MatteryPlayerCapability) : AndroidFeature } } } + + override fun onAttack(event: LivingAttackEvent) { + if (event.source.isFall) { + val reduction = (AndroidConfig.FALL_DAMAGE_REDUCTION_PER_LEVEL_P * (level + 1)).toFloat().coerceIn(0f, 1f) + val flat = (AndroidConfig.FALL_DAMAGE_REDUCTION_PER_LEVEL_F * (level + 1)).toFloat().coerceIn(0f, Float.MAX_VALUE) + + if (reduction >= 1f || event.amount <= flat) { + event.isCanceled = true + } + } + } } diff --git a/src/main/kotlin/ru/dbotthepony/mc/otm/capability/MatteryPlayerCapability.kt b/src/main/kotlin/ru/dbotthepony/mc/otm/capability/MatteryPlayerCapability.kt index de6ebb734..5341a58b4 100644 --- a/src/main/kotlin/ru/dbotthepony/mc/otm/capability/MatteryPlayerCapability.kt +++ b/src/main/kotlin/ru/dbotthepony/mc/otm/capability/MatteryPlayerCapability.kt @@ -42,6 +42,7 @@ import net.minecraftforge.event.AttachCapabilitiesEvent import net.minecraftforge.event.RegisterCommandsEvent import net.minecraftforge.event.TickEvent import net.minecraftforge.event.TickEvent.PlayerTickEvent +import net.minecraftforge.event.entity.living.LivingAttackEvent import net.minecraftforge.event.entity.living.LivingDeathEvent import net.minecraftforge.event.entity.living.LivingHurtEvent import net.minecraftforge.event.entity.living.MobEffectEvent @@ -653,6 +654,14 @@ class MatteryPlayerCapability(val ply: Player) : ICapabilityProvider, INBTSerial } } + internal fun onAttack(event: LivingAttackEvent) { + if (isAndroid) { + for (feature in featureMap.values) { + feature.onAttack(event) + } + } + } + internal fun computeIfAbsent(feature: AndroidFeatureType): T { return getFeature(feature) ?: addFeature(feature) } @@ -1307,6 +1316,10 @@ class MatteryPlayerCapability(val ply: Player) : ICapabilityProvider, INBTSerial event.entity.getCapability(MatteryCapability.MATTERY_PLAYER).ifPresentK { it.onHurt(event) } } + fun onAttackEvent(event: LivingAttackEvent) { + event.entity.getCapability(MatteryCapability.MATTERY_PLAYER).ifPresentK { it.onAttack(event) } + } + val CAPABILITY_LOCATION = ResourceLocation(OverdriveThatMatters.MOD_ID, "player") fun onAttachCapabilityEvent(event: AttachCapabilitiesEvent) {