diff --git a/src/main/java/kaptainwutax/seedcrackerX/config/StructureSave.java b/src/main/java/kaptainwutax/seedcrackerX/config/StructureSave.java index f823bb36..67df889c 100644 --- a/src/main/java/kaptainwutax/seedcrackerX/config/StructureSave.java +++ b/src/main/java/kaptainwutax/seedcrackerX/config/StructureSave.java @@ -12,6 +12,9 @@ import net.minecraft.util.WorldSavePath; import java.io.*; +import java.nio.file.Files; +import java.nio.file.Path; +import java.nio.file.Paths; import java.util.ArrayList; import java.util.List; import java.util.Scanner; @@ -22,65 +25,73 @@ public class StructureSave { private static final Logger logger = LoggerFactory.getLogger("structureSave"); - public static final File saveDir = new File(FabricLoader.getInstance().getConfigDir().toFile(), "SeedCrackerX saved structures"); + public static final Path saveDir = Paths.get(FabricLoader.getInstance().getConfigDir().toFile().toString(), "SeedCrackerX saved structures"); private static final List> structureTypes = List.of(Features.IGLOO,Features.BURIED_TREASURE, Features.PILLAGER_OUTPOST,Features.DESERT_PYRAMID, Features.JUNGLE_PYRAMID, Features.END_CITY, Features.MONUMENT, Features.SHIPWRECK, Features.SWAMP_HUT); public static void saveStructures(ScheduledSet>> baseData) { - saveDir.mkdirs(); - File saveFile = new File(saveDir, getWorldName()); try { - saveFile.createNewFile(); - FileWriter writer = new FileWriter(saveFile); - for (DataStorage.Entry> dataEntry : baseData) { - if (dataEntry.data.feature instanceof Structure structure) { - String data = Structure.getName(structure.getClass()) + ";" + dataEntry.data.chunkX + + Files.createDirectories(saveDir); + Path saveFile = saveDir.resolve(getWorldName()); + Files.deleteIfExists(saveFile); + Files.createFile(saveFile); + try (FileWriter writer = new FileWriter(saveFile.toFile())) { + for (DataStorage.Entry> dataEntry : baseData) { + if (dataEntry.data.feature instanceof Structure structure) { + String data = Structure.getName(structure.getClass()) + + ";" + dataEntry.data.chunkX + ";" + dataEntry.data.chunkZ + "\n"; - writer.write(data); + writer.write(data); + } } } - writer.close(); } catch (IOException e) { - logger.error("seedcracker could't save structures", e); + logger.error("seedcracker couldn't save structures", e); } } public static List> loadStructures() { - saveDir.mkdirs(); - File saveFile = new File(saveDir, getWorldName()); List> result = new ArrayList<>(); - if (!saveFile.exists()) return result; try { - FileInputStream fis = new FileInputStream(saveFile); - Scanner sc = new Scanner(fis); - while (sc.hasNextLine()) { - String line = sc.nextLine(); - String[] info = line.split(";"); - if (info.length != 3) continue; - String structureName = info[0]; - for (RegionStructure idk : structureTypes) { - if (structureName.equals(idk.getName())) { - result.add(idk.at(Integer.parseInt(info[1]), Integer.parseInt(info[2]))); - break; + Files.createDirectories(saveDir); + Path saveFile = saveDir.resolve(getWorldName()); + try ( + FileInputStream fis = new FileInputStream(saveFile.toFile()); + Scanner sc = new Scanner(fis) + ) { + while (sc.hasNextLine()) { + String line = sc.nextLine(); + String[] info = line.split(";"); + if (info.length != 3) continue; + String structureName = info[0]; + for (RegionStructure idk : structureTypes) { + if (structureName.equals(idk.getName())) { + result.add(idk.at(Integer.parseInt(info[1]), Integer.parseInt(info[2]))); + break; + } } } } + } catch (FileNotFoundException e) { + logger.warn("seedcracker couldn't find the structures file", e); + return result; } catch (IOException e) { - logger.error("seedcracker could't load previous structures", e); + logger.error("seedcracker couldn't load previous structures", e); } return result; } private static String getWorldName() { - if (MinecraftClient.getInstance().getNetworkHandler() != null) { - ClientConnection connection = MinecraftClient.getInstance().getNetworkHandler().getConnection(); + MinecraftClient minecraftClient = MinecraftClient.getInstance(); + if (minecraftClient.getNetworkHandler() != null) { + ClientConnection connection = minecraftClient.getNetworkHandler().getConnection(); if (connection.isLocal()) { - String address = MinecraftClient.getInstance().getServer().getSavePath(WorldSavePath.ROOT).getParent().getFileName().toString(); - return address.replace("/","_")+".txt"; + String address = minecraftClient.getServer().getSavePath(WorldSavePath.ROOT).getParent().getFileName().toString(); + return address.replace("/","_").replace(":", "_")+".txt"; } else { - return connection.getAddress().toString().replace("/","_")+".txt"; + return connection.getAddress().toString().replace("/","_").replace(":","_")+".txt"; } } return "Invalid.txt";