Skip to content

Commit

Permalink
Merge pull request #232 from Pommyd/save-load-optimisations
Browse files Browse the repository at this point in the history
Optimising save and load structures functionality
  • Loading branch information
19MisterX98 authored Jun 14, 2023
2 parents 1d9d429 + 1930637 commit e21eabd
Showing 1 changed file with 42 additions and 31 deletions.
73 changes: 42 additions & 31 deletions src/main/java/kaptainwutax/seedcrackerX/config/StructureSave.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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<RegionStructure<?,?>> 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<DataStorage.Entry<Feature.Data<?>>> baseData) {
saveDir.mkdirs();
File saveFile = new File(saveDir, getWorldName());
try {
saveFile.createNewFile();
FileWriter writer = new FileWriter(saveFile);
for (DataStorage.Entry<Feature.Data<?>> 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<Feature.Data<?>> 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<RegionStructure.Data<?>> loadStructures() {
saveDir.mkdirs();
File saveFile = new File(saveDir, getWorldName());
List<RegionStructure.Data<?>> 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";
Expand Down

0 comments on commit e21eabd

Please sign in to comment.