LibAC-flutter/lib/nbt/nbtHelpers.dart

36 lines
1.3 KiB
Dart

import 'package:libac_dart/nbt/SnbtIo.dart';
import 'package:libac_dart/nbt/impl/CompoundTag.dart';
import 'package:shared_preferences/shared_preferences.dart';
class NBTHelper {
/// Commits NBT Data to Shared Preferences, performing all the secret magic that makes it happen
///
/// [data] - The data to be encoded and saved
/// [name] - The name to give the entry.
static Future<void> CommitNBT(
{required CompoundTag data, required String name}) async {
String encoded = SnbtIo.writeToString(data);
SharedPreferences prefs = await SharedPreferences.getInstance();
await prefs.setString(name, encoded);
}
/// This function retrieves the NBT Data and turns it back into a CompoundTag
///
/// [name] - The name of the entry to retrieve
static Future<CompoundTag> GetNBT({required String name}) async {
SharedPreferences prefs = await SharedPreferences.getInstance();
String data = prefs.getString(name) ?? "";
if (data.isEmpty) {
return CompoundTag();
} else {
return await SnbtIo.readFromString(data) as CompoundTag;
}
}
/// Resets and clears the persistent storage.
static Future<void> ResetPersistentNBTStore() async {
SharedPreferences prefs = await SharedPreferences.getInstance();
await prefs.clear();
}
}