zombies-utils_fabric/src/main/java/xyz/stachel/zombiesutils/game/GameFile.java
2025-05-25 18:47:50 +03:00

65 lines
2.1 KiB
Java

package xyz.stachel.zombiesutils.game;
import net.minecraft.client.MinecraftClient;
import net.minecraft.text.Style;
import net.minecraft.text.Text;
import net.minecraft.util.Formatting;
import org.apache.commons.lang3.exception.ExceptionUtils;
import org.jetbrains.annotations.NotNull;
import xyz.stachel.zombiesutils.ZombiesUtils;
import xyz.stachel.zombiesutils.game.recorder.FileManager;
import xyz.stachel.zombiesutils.game.recorder.data.GameData;
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.time.temporal.ChronoUnit;
class GameFile extends File {
private final GameData data;
private FileWriter writer;
GameFile(@NotNull final String serverNumber, @NotNull final GameMode mode) {
super(new File("zombies", "runs"), formattedTime() + "_" + serverNumber + ".seg2");
this.data = new GameData(mode.getMap());
FileManager.createDataFile(this, this.data);
}
private static String formattedTime() {
final LocalDateTime dateTime = LocalDateTime.now().truncatedTo(ChronoUnit.MINUTES);
return dateTime.format(DateTimeFormatter.ISO_LOCAL_DATE_TIME).replace(':', '-').replaceFirst("T", "_");
}
void init() throws IOException {
if (!this.exists()) {
this.getParentFile().mkdirs();
this.createNewFile();
}
if (this.isFile()) {
this.setWritable(true);
}
this.writer = new FileWriter(this);
}
void addSplit(final long ticks) throws IOException {
this.writer.append(String.format("%d", ticks));
}
void clean() throws IOException {
this.writer.close();
}
public void setSegment(int round, int ticks) {
this.data.setSegment(round - 1, ticks);
try {
FileManager.writeDataToFile(this, this.data);
} catch (Exception e) {
ZombiesUtils.LOGGER.error(ExceptionUtils.getStackTrace(e));
MinecraftClient.getInstance().player.sendMessage(Text.literal("Error saving segment to run-file. Please Contact Stachelbeere1248.").setStyle(Style.EMPTY.withColor(Formatting.RED)), false);
}
}
}