-
Notifications
You must be signed in to change notification settings - Fork 4
Using the new Clans PersistentDataContainer
Hempfest edited this page Dec 11, 2020
·
12 revisions
Below you'll see a rough usage of the PDC, keep note that you should never have to grab a saved instance to get meta data.
When creating new meta data you should make sure that you store the temp instance aswell.
// Get and check if a clan has meta data through a present HUID.
// An HUID is similar to a UUID but only contains 6 characters.
// HUID was added to HempCore in version 2.0.6
// unlike v2.0.8-R1; v2.0.8-R4 has the advantage of multiple stored objects.
// using this method is most beneficial as the method getId() should be reserved for clans addons.
//
// The int id used is the custom one you set when creating the initial meta instance.
Clan clan = Clan.clanUtil.getClan(clanID);
HUID id = clan.getId(420);
// Creating a clan instance to get the location.
if(id !=null)
{
clan.messageClan("Value already set retrieving..");
// Attempting to load from a cached instance.
ClanMeta meta = PersistentClan.loadTempInstance(id);
// If the cached instance is null then well try to load a saved instance otherwise
// loading from the cache. As long as you store a temp instance you should have no need for loading a saved instance.
if (meta == null) {
meta = PersistentClan.loadSavedInstance(id);
clan.messageClan(meta.getId().toString() + "<- Current ID");
for (String m : clan.getMembers()) {
if (Bukkit.getPlayer(m) != null) {
try {
// Now using the HFEncoded class well deserialize the object and cast it to the object we need
// which for us is a Location.
//
// new HFEncoded(Object).serialized(); will convert any object into string format
// but you wont have to use it with the meta data as that is handled automatically
// just mentioning this for future notice.
//
// new HFEncoded(String).deserialized(); will convert any known object back into a usable
// object maintaing the original objects values. Which is what we're using to get the location
// back from the meta's string value. Then were simply teleporting our online clan members there.
//
// Here we're indexing the object array to get our object. This is not done by custom delimiter
// but by array position.
Bukkit.getPlayer(m).teleport((Location) new HFEncoded(meta.value(0)).deserialized());
} catch (IOException | ClassNotFoundException e) {
e.printStackTrace();
}
}
}
} else {
clan.messageClan(meta.getId().toString() + "<- Current ID (Cache)");
for (String m : clan.getMembers()) {
if (Bukkit.getPlayer(m) != null) {
try {
Bukkit.getPlayer(m).teleport((Location) new HFEncoded(meta.value(0)).deserialized());
} catch (IOException | ClassNotFoundException e) {
e.printStackTrace();
}
}
}
}
} else
{
// If an HUID isnt present for the specified clan then they dont yet have any
// meta data stored so well make a new instance and save it.
PersistentClan metaData = new PersistentClan(clanID);
Location base = clan.getBase();
// Well set an object here it can be any serializable object but in this example well
// use a clans base location.
//
// The index for the object value stored is for array position.
// It's recommended you build the array from 0 up
// There will soon be a clans addons information page. And within the information for each addon using the PDC there
// will be information regarding its delimiter within the container.
metaData.setValue(base, 0);
// Save it to the cache
metaData.storeTemp();
// Save it directly to a data file under a specified delimiter (suggested) leave the normal saveMeta method open for
// official clans addons.
metaData.saveMeta(420);
}
// Other things can be accessed staticly aswell through the class PersistentClan
// You can delete a stored Instance using PersistentClan.deleteInstance(HUID)
// Heres an example snippet of how i deleted the instance when i was done using it.
Clan clan = HempfestClans.clanManager(p);
ClanMeta meta = PersistentClan.loadTempInstance(clan.getId(420));
if(meta !=null)
{
PersistentClan.deleteInstance(meta.getId());
lib.sendMessage(p, "&c&oInstance deleted");
return true;
}