-
Notifications
You must be signed in to change notification settings - Fork 1
Using the Player Data Manager
The UserDataManager class can be used for saving multiple user data files all in the one folder.
To let the library know you are utilizing the user data you must call LuaCore.setUserDataManager(new UserDataManager("DATA FOLDER")); inside your onEnable and after the LuaCore.setupCore(this)
@Override
public void onEnable() {
LuaCore.setupCore(this);
LuaCore.setVerbose(true);
LuaCore.setUserDataManager(new UserDataManager("data"));
}In order to use the UserDataManager you need to make a class that extends UserFolder, This class will be the parent class for the users data files
//Example class UserFolder class
public class TestUserDataFolder extends UserFolder {
// These are the users data files
@Getter
private TestMappedUserData1 data1;
@Getter
private TestMappedUserData2 data2;
// This method is important and must call super(uuid)
public TestUserData(UUID uuid) {
super(uuid);
data1 = loadData(TestMappedUserData1.class, "Test1.json"); // You must call at least one loadData() method otherwise it will throw NoUserJsonFoundException
data2 = loadData(TestMappedUserData2.class, "Test2.json");
}
}The loadData(TestMappedUserData1.class, "Test1.json"); line uses the inherited function loadData, This method will either create a new data file with the given file name.
more can be seen on the docs
public class TestMappedUserData1 {
@Getter
@Setter
private Boolean test = false;
// Add as many fields as you want here
}The class with the actual user data does not need to extend any parent class hence the loadData is of type T.
There are two methods of saving all user data (one will also save future stuff that requires saving once added)
The first and recommended method of saving all users data would be adding LuaCore.disable() in onDisable() the other method would be calling the UserDataManager#saveAllUserData()
Next is saving individual users data. you can call UserDataManager#saveUserData(UserFolder) this will save all of the users data files
@Override
@SneakyThrows
public void onDisable() {
// You can call either save() as follows
LuaCore.save(); // Save all and other things that may come later on
// Or you can call saveAllUserData() as follows
LuaCore.getUserDataManager().saveAllUserData();
}To access the users data you again use the UserDataManager class.
You can start by getting the UserDataManager instance using LuaCore.getUserDataManager() once you have the instance you can call UserDataManager#getUserDataFolder(Class<T>, UUID) where the class of type T is the class extending UserFolder. Now that you have access to the user data folder (assuming you are using Lombok and have @Getter for the files) you can call the getter inside the UserFolder eg userDataFolder.getData1() allowing you to access the specific user data file (In this case data1 or Test1.json). Finally once you have the data files class you can call dataClass.getTest()
@SneakyThrows // SneakyThrows because getUserDataFolder can throw NoSuchMethodException, InvocationTargetException, InstantiationException, IllegalAccessException and/or NoUserJsonFoundException
@EventHandler
public void onJoin(PlayerJoinEvent event) {
TestUserData data = LuaCore.getUserDataManager().getUserDataFolder(TestUserData.class, event.getPlayer().getUniqueId());
LuaMessageUtils.verboseMessage(event.getPlayer().getName() + " Has joined the sever with the following data");
LuaMessageUtils.verboseMessage("Starting value: " + data.getData1().getTest().toString());
data.getData1().setTest(!data.getData1().getTest());
LuaMessageUtils.verboseMessage("Edited value: " + data.getData1().getTest().toString());
}