From 6783a5599cf45ef16a4db8fcba34356617d212b8 Mon Sep 17 00:00:00 2001 From: Roelymole Date: Tue, 7 Jan 2025 12:10:09 +0000 Subject: [PATCH] feat: wrote mixin to make fall damage proportional to gravity (resolves #389) --- .../mod/mixin/LivingEntityMixin.java | 23 +++++++++++++++++++ 1 file changed, 23 insertions(+) diff --git a/src/main/java/dev/galacticraft/mod/mixin/LivingEntityMixin.java b/src/main/java/dev/galacticraft/mod/mixin/LivingEntityMixin.java index 025f0df32..1957b7ee5 100644 --- a/src/main/java/dev/galacticraft/mod/mixin/LivingEntityMixin.java +++ b/src/main/java/dev/galacticraft/mod/mixin/LivingEntityMixin.java @@ -22,16 +22,22 @@ package dev.galacticraft.mod.mixin; +import dev.galacticraft.api.universe.celestialbody.CelestialBody; import dev.galacticraft.mod.Constant; import dev.galacticraft.mod.accessor.CryogenicAccessor; import net.minecraft.core.BlockPos; +import net.minecraft.core.Holder; import net.minecraft.nbt.CompoundTag; import net.minecraft.network.syncher.EntityDataAccessor; import net.minecraft.network.syncher.EntityDataSerializers; import net.minecraft.network.syncher.SynchedEntityData; +import net.minecraft.tags.EntityTypeTags; +import net.minecraft.util.Mth; import net.minecraft.world.entity.Entity; import net.minecraft.world.entity.EntityType; import net.minecraft.world.entity.LivingEntity; +import net.minecraft.world.entity.ai.attributes.Attribute; +import net.minecraft.world.entity.ai.attributes.Attributes; import net.minecraft.world.level.Level; import net.minecraft.world.phys.Vec3; import org.spongepowered.asm.mixin.Mixin; @@ -39,10 +45,14 @@ import org.spongepowered.asm.mixin.Unique; import org.spongepowered.asm.mixin.injection.At; import org.spongepowered.asm.mixin.injection.Inject; +import org.spongepowered.asm.mixin.injection.Redirect; import org.spongepowered.asm.mixin.injection.callback.CallbackInfo; +import org.spongepowered.asm.mixin.injection.callback.CallbackInfoReturnable; @Mixin(LivingEntity.class) public abstract class LivingEntityMixin extends Entity implements CryogenicAccessor { + @Shadow public abstract double getAttributeValue(Holder holder); + @Unique @SuppressWarnings("WrongEntityDataParameterClass") private static final EntityDataAccessor IS_IN_CRYO_SLEEP_ID = SynchedEntityData.defineId( @@ -118,4 +128,17 @@ public void setCryogenicChamberCooldown(int cryogenicChamberCooldown) { if (isInCryoSleep()) ci.cancel(); } + + @Inject(method = "calculateFallDamage", at = @At(value = "HEAD"), cancellable = true) + protected void calculateFallDamage(float f, float f2, CallbackInfoReturnable cir) { + if (this.getType().is(EntityTypeTags.FALL_DAMAGE_IMMUNE)) { + cir.setReturnValue(0); + return; + } + Holder> holder = this.level().galacticraft$getCelestialBody(); + float gravity = (float)(holder != null ? holder.value().gravity() : 1.0d); + float f3 = (float)this.getAttributeValue(Attributes.SAFE_FALL_DISTANCE); + float f4 = f - f3 / gravity; + cir.setReturnValue(Mth.ceil((double)(f4 * f2 * gravity) * this.getAttributeValue(Attributes.FALL_DAMAGE_MULTIPLIER))); + } }