wasoon11

Death coordinates saver

Kodari AI
KodariNov 6 at 9:32 pm 2
DeathCoordinatesSaver
This Fabric mod for Minecraft 1.21.1 automatically saves player death coordinates to a JSON file. When a player dies, the mod captures the exact X, Y, Z coordinates and dimension, storing them in a persistent data file. Players can retrieve their death locations via a simple command, displaying the saved coordinates in chat with a clickable teleport option. The mod supports multiple death records per player, allowing them to track their most recent deaths. It integrates seamlessly with the Fabric API, requiring no client-side installation for server use. The mod provides a clean, lightweight solution for players who frequently lose track of their death locations in challenging environments.
  • Record each player’s death coordinates and dimension in a persistent server file.

  • Show the coordinates in chat when the player dies.

Death coordinates now appear in chat and are saved per player in the world’s death-coordinates folder, including the dimension and timestamp.

Kodari can make mistakes. Review important code before using.
Files
src
build.gradle
gradle.properties
settings.gradle
Code
5 130
src/main/java/com/kodari/deathcoordinates/DeathCoordinatesSaver.java
package com.kodari.deathcoordinates;

import java.io.IOException;
import java.nio.charset.StandardCharsets;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.StandardOpenOption;
import java.time.Instant;

import net.fabricmc.api.ModInitializer;
import net.fabricmc.fabric.api.entity.event.v1.ServerLivingEntityEvents;
import net.minecraft.server.network.ServerPlayerEntity;
import net.minecraft.text.Text;
import net.minecraft.util.WorldSavePath;
import net.minecraft.util.math.BlockPos;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

public final class DeathCoordinatesSaver implements ModInitializer {
    private static final Logger LOGGER = LoggerFactory.getLogger("death_coordinates_saver");

    @Override
    public void onInitialize() {
        ServerLivingEntityEvents.AFTER_DEATH.register((entity, damageSource) -> {
            if (!(entity instanceof ServerPlayerEntity player)) {
                return;
            }

            BlockPos pos = player.getBlockPos();
            String dimension = player.getEntityWorld().getRegistryKey().getValue().toString();
            String coordinates = "X: " + pos.getX() + ", Y: " + pos.getY() + ", Z: " + pos.getZ() + " (" + dimension + ")";
            player.sendMessage(Text.literal("Death coordinates: " + coordinates));

            Path directory = player.getEntityWorld().getServer().getSavePath(WorldSavePath.ROOT)
                    .resolve("death-coordinates");
            try {
                Files.createDirectories(directory);
                Files.writeString(directory.resolve(player.getUuid() + ".txt"),
                        Instant.now() + " " + coordinates + System.lineSeparator(),
                        StandardCharsets.UTF_8, StandardOpenOption.CREATE, StandardOpenOption.APPEND);
            } catch (IOException e) {
                LOGGER.error("Could not save death coordinates for {}", player.getName().getString(), e);
                player.sendMessage(Text.literal("Could not save death coordinates to disk."));
            }
        });
    }
}