aboutsummaryrefslogtreecommitdiff
path: root/src/main/java/moe/ymc/acron/mixin/LivingEntityMixin.java
blob: 9e16569413721e2629a3521994d3ab363f425e2f (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
package moe.ymc.acron.mixin;

import moe.ymc.acron.s2c.Entity;
import moe.ymc.acron.s2c.EventQueue;
import moe.ymc.acron.s2c.event.EventEntityDeath;
import net.minecraft.entity.EntityType;
import net.minecraft.entity.LivingEntity;
import net.minecraft.entity.damage.DamageSource;
import net.minecraft.entity.damage.DamageTracker;
import net.minecraft.world.World;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.spongepowered.asm.mixin.Mixin;
import org.spongepowered.asm.mixin.Shadow;
import org.spongepowered.asm.mixin.injection.At;
import org.spongepowered.asm.mixin.injection.Inject;
import org.spongepowered.asm.mixin.injection.callback.CallbackInfo;

@Mixin(LivingEntity.class)
public abstract class LivingEntityMixin extends net.minecraft.entity.Entity {
    private static final Logger AC_LOGGER = LogManager.getLogger();

    @Shadow public abstract DamageTracker getDamageTracker();

    public LivingEntityMixin(EntityType<?> type, World world) {
        super(type, world);
    }

    // The original onDeath() will call getDamageTracker().update(),
    // which clears all recent damages, making the getDeathMessage()
    // output always generic.
    // Thus, we need to use @At("HEAD") to get the injection called
    // before it does anything else.
    @Inject(at = @At("HEAD"), method = "onDeath")
    public void onDeath(DamageSource source, CallbackInfo ci) {
        AC_LOGGER.debug("onDeath[{}]",
                getUuid());
        EventQueue.enqueue(new EventEntityDeath(new Entity(this),
                getDamageTracker().getDeathMessage().getString()));
    }
}