Skip to content

Commit

Permalink
Add DeathAnimation
Browse files Browse the repository at this point in the history
  • Loading branch information
FirstMegaGame4 committed Apr 7, 2024
1 parent f2fca56 commit e80d1cc
Show file tree
Hide file tree
Showing 5 changed files with 85 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
package com.mmodding.mmodding_lib.library.client.render.entity.animation;

public interface DeathAnimation {

boolean applyRedOverlayOnDeath();

int getDeathTime();

Runnable executeDeathAnimation();
}
Original file line number Diff line number Diff line change
Expand Up @@ -116,6 +116,9 @@ public abstract class EntityMixin implements EntitySyncableDataRegistry, EntityD
@Shadow
public abstract float getPitch();

@Shadow
public abstract void remove(Entity.RemovalReason reason);

@Inject(method = "baseTick", at = @At(value = "INVOKE", target = "Lnet/minecraft/entity/Entity;tickNetherPortal()V", shift = At.Shift.AFTER))
private void baseTickAfterTickNetherPortal(CallbackInfo ci) {
this.tickCustomPortal();
Expand Down
Original file line number Diff line number Diff line change
@@ -1,13 +1,20 @@
package com.mmodding.mmodding_lib.mixin.injectors;

import com.llamalad7.mixinextras.injector.wrapoperation.Operation;
import com.llamalad7.mixinextras.injector.wrapoperation.WrapOperation;
import com.mmodding.mmodding_lib.ducks.LivingEntityDuckInterface;
import com.mmodding.mmodding_lib.library.client.render.entity.animation.DeathAnimation;
import com.mmodding.mmodding_lib.library.entities.data.MModdingTrackedDataHandlers;
import com.mmodding.mmodding_lib.library.entities.data.syncable.SyncableData;
import com.mmodding.mmodding_lib.library.utils.MModdingIdentifier;
import com.mmodding.mmodding_lib.library.utils.ObjectUtils;
import net.minecraft.entity.Entity;
import net.minecraft.entity.EntityStatuses;
import net.minecraft.entity.LivingEntity;
import net.minecraft.util.Identifier;
import net.minecraft.world.World;
import org.spongepowered.asm.mixin.Mixin;
import org.spongepowered.asm.mixin.Shadow;
import org.spongepowered.asm.mixin.Unique;
import org.spongepowered.asm.mixin.injection.At;
import org.spongepowered.asm.mixin.injection.Inject;
Expand All @@ -27,11 +34,39 @@ public abstract class LivingEntityMixin extends EntityMixin implements LivingEnt
MModdingTrackedDataHandlers.IDENTIFIER_LIST
);

@Shadow
public int deathTime;

@Inject(method = "tick", at = @At(value = "INVOKE", target = "Lnet/minecraft/entity/LivingEntity;setStuckArrowCount(I)V"))
private void tick(CallbackInfo ci) {
this.deleteStuckArrowType();
}

@WrapOperation(method = "updatePostDeath", at = @At(value = "INVOKE", target = "Lnet/minecraft/world/World;sendEntityStatus(Lnet/minecraft/entity/Entity;B)V"))
private void conditionallyCancelDeathAnimationFirstPart(World instance, Entity entity, byte status, Operation<Void> original) {
if (!(entity instanceof DeathAnimation animation) || animation.executeDeathAnimation() != null) {
original.call(instance, entity, status);
}
}

@WrapOperation(method = "updatePostDeath", at = @At(value = "INVOKE", target = "Lnet/minecraft/entity/LivingEntity;remove(Lnet/minecraft/entity/Entity$RemovalReason;)V"))
private void conditionallyCancelDeathAnimationSecondPart(LivingEntity instance, Entity.RemovalReason removalReason, Operation<Void> original) {
if (!(instance instanceof DeathAnimation animation) || animation.executeDeathAnimation() != null) {
original.call(instance, removalReason);
}
}

@Inject(method = "updatePostDeath", at = @At(value = "TAIL"))
private void updateDeath(CallbackInfo ci) {
LivingEntity livingEntity = (LivingEntity) (Object) this;
if (livingEntity instanceof DeathAnimation deathAnimation) {
if (this.deathTime == deathAnimation.getDeathTime() && !this.getWorld().isClient()) {
this.world.sendEntityStatus(livingEntity, EntityStatuses.ADD_DEATH_PARTICLES);
this.remove(Entity.RemovalReason.KILLED);
}
}
}

@Override
public List<Identifier> mmodding_lib$getStuckArrowTypes() {
return new ArrayList<>(this.stuckArrowTypes.get());
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
package com.mmodding.mmodding_lib.mixin.injectors.client;

import com.llamalad7.mixinextras.injector.ModifyExpressionValue;
import com.llamalad7.mixinextras.injector.wrapoperation.Operation;
import com.llamalad7.mixinextras.injector.wrapoperation.WrapOperation;
import com.llamalad7.mixinextras.sugar.Local;
import com.mmodding.mmodding_lib.library.client.render.entity.animation.DeathAnimation;
import net.minecraft.client.render.OverlayTexture;
import net.minecraft.client.render.entity.LivingEntityRenderer;
import net.minecraft.client.util.math.MatrixStack;
import net.minecraft.entity.LivingEntity;
import net.minecraft.util.math.Quaternion;
import org.spongepowered.asm.mixin.Mixin;
import org.spongepowered.asm.mixin.injection.At;

@Mixin(LivingEntityRenderer.class)
public class LivingEntityRendererMixin<T extends LivingEntity> {

@ModifyExpressionValue(method = "getOverlay", at = @At(value = "INVOKE", target = "Lnet/minecraft/client/render/OverlayTexture;getV(Z)I"))
private static int conditionallyCancelDeathAnimationRedOverlay(int original, LivingEntity entity, float whiteOverlayProgress) {
if (!(entity instanceof DeathAnimation animation) || animation.executeDeathAnimation() != null) {
return original;
}
else {
boolean bool = entity.hurtTime > 0 || (animation.applyRedOverlayOnDeath() && entity.deathTime > 0);
return OverlayTexture.packUv(OverlayTexture.getU(whiteOverlayProgress), OverlayTexture.getV(bool));
}
}

@WrapOperation(method = "setupTransforms", at = @At(value = "INVOKE", target = "Lnet/minecraft/client/util/math/MatrixStack;multiply(Lnet/minecraft/util/math/Quaternion;)V", ordinal = 1))
private void conditionallyCancelDeathAnimationTransform(MatrixStack instance, Quaternion quaternion, Operation<Void> original, @Local(argsOnly = true) T entity) {
if (!(entity instanceof DeathAnimation animation) || animation.executeDeathAnimation() != null) {
original.call(instance, quaternion);
}
}
}
1 change: 1 addition & 0 deletions src/main/resources/mmodding_lib.mixins.json
Original file line number Diff line number Diff line change
Expand Up @@ -118,6 +118,7 @@
"injectors.client.InGameHudMixin",
"injectors.client.ItemRenderContextMixin",
"injectors.client.ItemRendererMixin",
"injectors.client.LivingEntityRendererMixin",
"injectors.client.StuckArrowsFeatureRendererMixin",
"injectors.client.StuckObjectsFeatureRendererMixin",
"injectors.client.TridentEntityRendererMixin",
Expand Down

0 comments on commit e80d1cc

Please sign in to comment.