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())); } }