From 275288447fd1aca8e695fc6d0fcbd0b9f05d7df9 Mon Sep 17 00:00:00 2001 From: zontreck Date: Sun, 21 Jan 2024 17:10:26 -0700 Subject: [PATCH] Add some new APIs for file and snbt --- gradle.properties | 4 +- .../dev/zontreck/libzontreck/util/SNbtIo.java | 46 +++++++++++++++++++ 2 files changed, 48 insertions(+), 2 deletions(-) create mode 100644 src/main/java/dev/zontreck/libzontreck/util/SNbtIo.java diff --git a/gradle.properties b/gradle.properties index 45e2100..84387da 100644 --- a/gradle.properties +++ b/gradle.properties @@ -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 diff --git a/src/main/java/dev/zontreck/libzontreck/util/SNbtIo.java b/src/main/java/dev/zontreck/libzontreck/util/SNbtIo.java new file mode 100644 index 0000000..40fcbb6 --- /dev/null +++ b/src/main/java/dev/zontreck/libzontreck/util/SNbtIo.java @@ -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); + } +}