Posted June 5, 20241 yr 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) { dispatcher.register(Commands.literal("savecoords") .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()); saveCoordinatesToFile(); player.sendSystemMessage(Component.literal("Coordinates saved: " + name + " " + coords)); return 1; })) ); loadCoordinatesFromFile(); } 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) { e.printStackTrace(); } } private static void loadCoordinatesFromFile() { if (saveFile == null || !saveFile.exists()) { return; } 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) { e.printStackTrace(); } } public static void clearCoordinates() { namedCoordinates.clear(); saveCoordinatesToFile(); } private static File getSaveFile(MinecraftServer server) { File worldDirectory = server.getWorldPath(LevelResource.ROOT).toFile(); return new File(worldDirectory, "coordinates.txt"); } }
June 9, 20241 yr the world capability works for the current world and its different for every dimension sound like what you need the only thing is you cannot access data from a diferent world things like reading a data of the overworld from the nether would nwork
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.