Jump to content

I need help using Saved data (SD)


Recommended Posts

So i have a 1.20.4 mod that saves data and loads it using commands. but the problem is that its not saving separately for each world. so i was looking through the docs and stumbled upon (SD) but the docs arent really helpful. 
heres my script to save data

package com.example.coordinatesmod.commands;

import com.mojang.brigadier.CommandDispatcher;
import com.mojang.brigadier.arguments.StringArgumentType;
import net.minecraft.commands.CommandSourceStack;
import net.minecraft.commands.Commands;
import net.minecraft.network.chat.Component;
import net.minecraft.server.level.ServerPlayer;
import net.minecraft.server.MinecraftServer;
import net.minecraft.world.level.storage.LevelResource;

import java.io.*;
import java.util.HashMap;
import java.util.Map;

public class SaveCoordsCommand {
    private static final Map<String, String> namedCoordinates = new HashMap<>();
    private static File saveFile;

    public static void register(CommandDispatcher<CommandSourceStack> dispatcher) {
                .then(Commands.argument("name", StringArgumentType.string())
                        .executes(context -> {
                            ServerPlayer player = context.getSource().getPlayerOrException();
                            String name = StringArgumentType.getString(context, "name");
                            String coords = String.format("X: %.1f Y: %.1f Z: %.1f", player.getX(), player.getY(), player.getZ());
                            namedCoordinates.put(name, coords);
                            saveFile = getSaveFile(context.getSource().getServer());
                            player.sendSystemMessage(Component.literal("Coordinates saved: " + name + " " + coords));
                            return 1;


    public static Map<String, String> getNamedCoordinates() {
        return namedCoordinates;

    private static void saveCoordinatesToFile() {
        try (PrintWriter writer = new PrintWriter(new FileWriter(saveFile))) {
            for (Map.Entry<String, String> entry : namedCoordinates.entrySet()) {
                writer.println(entry.getKey() + ":" + entry.getValue());
        } catch (IOException e) {

    private static void loadCoordinatesFromFile() {
        if (saveFile == null || !saveFile.exists()) {
        try (BufferedReader reader = new BufferedReader(new FileReader(saveFile))) {
            String line;
            while ((line = reader.readLine()) != null) {
                String[] parts = line.split(":", 2);
                if (parts.length == 2) {
                    namedCoordinates.put(parts[0], parts[1]);
        } catch (IOException e) {

    public static void clearCoordinates() {

    private static File getSaveFile(MinecraftServer server) {
        File worldDirectory = server.getWorldPath(LevelResource.ROOT).toFile();
        return new File(worldDirectory, "coordinates.txt");


Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Unfortunately, your content contains terms that we do not allow. Please edit your content to remove the highlighted words below.
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

  • Create New...

Important Information

By using this site, you agree to our Terms of Use.