Add some new APIs for file and snbt

This commit is contained in:
zontreck 2024-01-21 17:10:26 -07:00
parent 55acbd4868
commit 275288447f
2 changed files with 48 additions and 2 deletions

View file

@ -7,7 +7,7 @@ org.gradle.daemon=false
parchment_version=2022.11.06
# luckperms_api_version=5.4
libac=1.4.18
libac=1.4.19
eventsbus=1.0.31
## Environment Properties
@ -53,7 +53,7 @@ mod_name=Zontreck Library Mod
# The license of the mod. Review your options at https://choosealicense.com/. All Rights Reserved is the default.
mod_license=GPLv3
# The mod version. See https://semver.org/
mod_version=1.10.011624.1712
mod_version=1.10.012124.1709
# The group ID for the mod. It is only important when publishing as an artifact to a Maven repository.
# This should match the base package used for the mod sources.
# See https://maven.apache.org/guides/mini/guide-naming-conventions.html

View file

@ -0,0 +1,46 @@
package dev.zontreck.libzontreck.util;
import com.mojang.brigadier.exceptions.CommandSyntaxException;
import dev.zontreck.ariaslib.util.FileIO;
import net.minecraft.nbt.CompoundTag;
import net.minecraft.nbt.NbtIo;
import net.minecraft.nbt.NbtUtils;
import java.io.File;
import java.nio.file.Path;
/**
* Provides helpers for reading and writing snbt to file
*/
public class SNbtIo
{
/**
* Read the file at the path, and deserialize from snbt
* @param path The file to load
* @return The deserialized compound tag, or a blank tag
*/
public static CompoundTag loadSnbt(Path path)
{
if(!path.toFile().exists())
return new CompoundTag();
else {
File fi = path.toFile();
try {
return NbtUtils.snbtToStructure(FileIO.readFile(fi.getAbsolutePath()));
} catch (CommandSyntaxException e) {
return new CompoundTag();
}
}
}
/**
* Writes the tag to the file specified
* @param path The file to write
* @param tag The tag to serialize
*/
public static void writeSnbt(Path path, CompoundTag tag)
{
String snbt = NbtUtils.structureToSnbt(tag);
FileIO.writeFile(path.toFile().getAbsolutePath(), snbt);
}
}