Add some helpers and wrappers, bump libac-dart dep
This commit is contained in:
parent
edca96f33f
commit
61a9ecd382
6 changed files with 63 additions and 9 deletions
36
lib/nbt/nbtHelpers.dart
Normal file
36
lib/nbt/nbtHelpers.dart
Normal file
|
@ -0,0 +1,36 @@
|
|||
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();
|
||||
}
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue