How can I detect a player's death and what killed it - Fabric MC 1.20.4

253 Views Asked by At

I've tried using a mixin yet it doesn't seem to run at all, the following is my code,

import net.minecraft.entity.damage.DamageSource;
import net.minecraft.entity.player.PlayerEntity;
import net.spamta.pvpoverhaul.PvPOverhaul;
import org.spongepowered.asm.mixin.Mixin;
import org.spongepowered.asm.mixin.injection.At;
import org.spongepowered.asm.mixin.injection.Inject;
import org.spongepowered.asm.mixin.injection.callback.CallbackInfo;

import net.fabricmc.fabric.api.event.lifecycle.v1.ServerTickEvents;
import net.minecraft.server.MinecraftServer;
import net.minecraft.server.network.ServerPlayerEntity;
import net.minecraft.text.Text;

@Mixin(PlayerEntity.class)
public class GearKeeper {
    
    @Inject(method="onDeath", at=@At("TAIL"))
    private void died(DamageSource damageSource, CallbackInfo info){
        PvPOverhaul.LOGGER.info(damageSource.getName().toString());

        PlayerEntity player = (PlayerEntity) (Object) this;
        sendDeathMessageToAllPlayers(player);
    }

    private void sendDeathMessageToAllPlayers(PlayerEntity player) {
        MinecraftServer server = player.getServer();
        if (server != null) {
            server.getPlayerManager().getPlayerList().forEach(serverPlayer -> {
                serverPlayer.sendMessage(Text.literal(player.getName().toString() + " has perished!"), false);
            });
        }
    }

}

Tried detecting if a player was killed and expected it to say "Piglin Brute" or whatever was killing me at that time yet it didn't end up logging anything.

1

There are 1 best solutions below

1
Kapila Shobit On
import net.minecraft.entity.damage.DamageSource;
import net.minecraft.entity.player.PlayerEntity;
import net.spamta.pvpoverhaul.PvPOverhaul;
import org.spongepowered.asm.mixin.Mixin;
import org.spongepowered.asm.mixin.injection.At;
import org.spongepowered.asm.mixin.injection.Inject;
import org.spongepowered.asm.mixin.injection.callback.CallbackInfo;

import net.fabricmc.fabric.api.event.lifecycle.v1.ServerTickEvents;
import net.minecraft.server.MinecraftServer;
import net.minecraft.server.network.ServerPlayerEntity;
import net.minecraft.text.Text;

@Mixin(PlayerEntity.class)
public class GearKeeper {

    @Inject(method="onDeath", at=@At("TAIL"))
    private void died(DamageSource damageSource, CallbackInfo info){
        PvPOverhaul.LOGGER.info(damageSource.getName().toString());

        PlayerEntity player = (PlayerEntity) (Object) this;
        sendDeathMessageToAllPlayers(player);
    }

    private void sendDeathMessageToAllPlayers(PlayerEntity player) {
        MinecraftServer server = player.getServer();
        if (server != null) {
            server.getPlayerManager().getPlayerList().forEach(serverPlayer -> {
            
serverPlayer.sendMessage(Text.literal(player.getName().toString() + " has 
perished!"), false);
            });
        }
    }

}

You should also create a mixin configuration file in your resources folder named pvpoverhaul.mixins.json

{
 "required": 1,
 "package": "net.spamta.pvpoverhaul.mixin",
 "compatibilityLevel": "JAVA_16",
 "mixins": [
    "GearKeeper"
 ]
}

If you have set up everything correctly, your mixin should now be loaded, and the code inside the died method should be executed when a player dies. The damageSource.getName().toString() should then log the name of the thing that killed the player.

hope this answer works✨✨