Add world info page (#1620)
This commit is contained in:
@@ -28,7 +28,6 @@ import org.jackhuang.hmcl.util.io.FileUtils;
|
||||
import org.jackhuang.hmcl.util.io.Unzipper;
|
||||
import org.jackhuang.hmcl.util.io.Zipper;
|
||||
|
||||
import java.io.BufferedInputStream;
|
||||
import java.io.IOException;
|
||||
import java.io.InputStream;
|
||||
import java.io.OutputStream;
|
||||
@@ -77,6 +76,10 @@ public class World {
|
||||
return worldName;
|
||||
}
|
||||
|
||||
public Path getLevelDatFile() {
|
||||
return file.resolve("level.dat");
|
||||
}
|
||||
|
||||
public long getLastPlayed() {
|
||||
return lastPlayed;
|
||||
}
|
||||
@@ -142,14 +145,10 @@ public class World {
|
||||
throw new IOException("Not a valid world directory");
|
||||
|
||||
// Change the name recorded in level.dat
|
||||
Path levelDat = file.resolve("level.dat");
|
||||
CompoundTag nbt = parseLevelDat(levelDat);
|
||||
CompoundTag nbt = readLevelDat();
|
||||
CompoundTag data = nbt.get("Data");
|
||||
data.put(new StringTag("LevelName", newName));
|
||||
|
||||
try (OutputStream os = new GZIPOutputStream(Files.newOutputStream(levelDat))) {
|
||||
NBTIO.writeTag(os, nbt);
|
||||
}
|
||||
writeLevelDat(nbt);
|
||||
|
||||
// then change the folder's name
|
||||
Files.move(file, file.resolveSibling(newName));
|
||||
@@ -203,8 +202,26 @@ public class World {
|
||||
}
|
||||
}
|
||||
|
||||
public CompoundTag readLevelDat() throws IOException {
|
||||
if (!Files.isDirectory(file))
|
||||
throw new IOException("Not a valid world directory");
|
||||
|
||||
return parseLevelDat(getLevelDatFile());
|
||||
}
|
||||
|
||||
public void writeLevelDat(CompoundTag nbt) throws IOException {
|
||||
if (!Files.isDirectory(file))
|
||||
throw new IOException("Not a valid world directory");
|
||||
|
||||
FileUtils.saveSafely(getLevelDatFile(), os -> {
|
||||
try (OutputStream gos = new GZIPOutputStream(os)) {
|
||||
NBTIO.writeTag(gos, nbt);
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
private static CompoundTag parseLevelDat(Path path) throws IOException {
|
||||
try (InputStream is = new BufferedInputStream(new GZIPInputStream(Files.newInputStream(path)))) {
|
||||
try (InputStream is = new GZIPInputStream(Files.newInputStream(path))) {
|
||||
Tag nbt = NBTIO.readTag(is);
|
||||
if (nbt instanceof CompoundTag)
|
||||
return (CompoundTag) nbt;
|
||||
|
||||
@@ -235,6 +235,15 @@ public final class Lang {
|
||||
}
|
||||
}
|
||||
|
||||
public static Double toDoubleOrNull(Object string) {
|
||||
try {
|
||||
if (string == null) return null;
|
||||
return Double.parseDouble(string.toString());
|
||||
} catch (NumberFormatException e) {
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Find the first non-null reference in given list.
|
||||
* @param t nullable references list.
|
||||
|
||||
@@ -19,11 +19,9 @@ package org.jackhuang.hmcl.util.io;
|
||||
|
||||
import org.jackhuang.hmcl.util.Lang;
|
||||
import org.jackhuang.hmcl.util.StringUtils;
|
||||
import org.jackhuang.hmcl.util.function.ExceptionalConsumer;
|
||||
|
||||
import java.io.BufferedWriter;
|
||||
import java.io.File;
|
||||
import java.io.FileNotFoundException;
|
||||
import java.io.IOException;
|
||||
import java.io.*;
|
||||
import java.lang.reflect.Method;
|
||||
import java.nio.charset.Charset;
|
||||
import java.nio.file.*;
|
||||
@@ -445,4 +443,21 @@ public final class FileUtils {
|
||||
|
||||
Files.move(tmpFile, file, StandardCopyOption.REPLACE_EXISTING);
|
||||
}
|
||||
|
||||
public static void saveSafely(Path file, ExceptionalConsumer<? super OutputStream, IOException> action) throws IOException {
|
||||
Path tmpFile = tmpSaveFile(file);
|
||||
|
||||
try (OutputStream os = Files.newOutputStream(tmpFile, StandardOpenOption.TRUNCATE_EXISTING, StandardOpenOption.CREATE)) {
|
||||
action.accept(os);
|
||||
}
|
||||
|
||||
try {
|
||||
if (Files.exists(file) && Files.getAttribute(file, "dos:hidden") == Boolean.TRUE) {
|
||||
Files.setAttribute(tmpFile, "dos:hidden", true);
|
||||
}
|
||||
} catch (Throwable ignored) {
|
||||
}
|
||||
|
||||
Files.move(tmpFile, file, StandardCopyOption.REPLACE_EXISTING);
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user