Sueta

[1.8-1.14] CoreData - A lightweight, centralized data soluti

1.0.5

A lightweight, centralized data solution for your plugins! Upgrade today!

·Plugin


What is CoreData?

The majority of plugins in the Spigot resources section involve some level of data storage regarding the users who use the plugin. I often find myself creating new methods in each of my projects for storing data in regards to users. This plugin is designed to make the storage of data in regards to players fast and lightweight so your plugins do not take up precious storage space.

How is CoreData better than my current data solution?
CoreData's purpose is to save storage space by centralizing the location of all plugin data. Instead of each plugin creating their own files to store user data, our API creates a file for each user and allows your plugin to store data under a label in that user's file. This allows your plugins to not have to worry about file management or data storage.

Along with centralized data, our data solution is faster than traditional flat file data storage. Before, you would save all data in a file and traverse it to find the user's data. CoreData will find the user's file and directly access their data. This is very important as plugins that store data for thousands of users will see increases in the time it takes to find a user's data under traditional flat file methods while CoreData sees no change at all.

How do I use CoreData?
The first step is ensuring your plugin depends on CoreData.

depend   :   [  CoreData  ]

The next step is creating a reference to our user handler.

if   (   !   this  .  getServer   (   )  .  getPluginManager   (   )  .  isPluginEnabled   (   "CoreData"   )   )   {  
     this  .  getLogger   (   )  .  severe   (   "[Hook] CoreData not found. Disabling plugin..."   )   ;  
     return   ;  
 }  
UserHandler userHandler  =   (   (  com.  ethanpritchard  .  data  .  Main   )   this  .  getServer   (   )  .  getPluginManager   (   )  .  getPlugin   (   "CoreData"   )   )  .  getUserHandler   (   )   ;

Once you have hooked into our plugin and gathered the user handler instance, you have access to a few functions.

userHandler.  createUserFile   (  uuid  )   ;   // Returns void  

userHandler.  setUserData   (  uuid, label, data  )   ;   // Returns void  
userHandler.  getUserData   (  uuid, label  )   ;   // Returns object  
userHandler.  getAllUsersData   (  label  )   ;   // Returns Map<UUID, Object>

Data is stored in a user's file under a label. A label is a String that acts as an identifier to later find that piece of data. Good labels will be more complex strings that further describe the data being stored. Bad labels will be generic words or phrases that other plugins may also attempt to store data under. Here is an example of labels:

balance = BAD 
myPluginName.balance = GOOD

Please note that you should only use periods to separate your strings. The data is stored using Spigot's YamlConfiguration.

How do I transfer existing data to CoreData?
To transfer data to CoreData, you must have a method to get all of your users and their data as well as an iterator to navigate this data. Here is an example piece of code:

private  UserHandler coreDataUserHandler  ;  

@Override 
 public   void  onEnable  (   )   {  
         if   (   !   this  .  getServer   (   )  .  getPluginManager   (   )  .  isPluginEnabled   (   "CoreData"   )   )   {  
             this  .  getLogger   (   )  .  severe   (   "[Hook] CoreData not found. Disabling plugin..."   )   ;  
             return   ;  
         }  
         this  .  coreDataUserHandler   =   (   (  com.  ethanpritchard  .  data  .  Main   )   this  .  getServer   (   )  .  getPluginManager   (   )  .  getPlugin   (   "CoreData"   )   )  .  getUserHandler   (   )   ;  
 }  

 public   void  transferToCoreData  (   )   {  
     // Establish a data label  
      String   label  =   "myPlugin.tokenbalance"   ;  
  
     // Get the user data  
    Map  <  UUID, Double  >  balances  =   this  .  getUserBalance   (   )   ;  
  
     // Iterate over the user data  
     for   (  UUID uuid  :  balances.  keySet   (   )   )   {  
         // Get the user's data  
         double  data  =  balances.  get   (  uuid  )   ;  
  
         // Call the CoreData method  
         this  .  coreDataUserHandler  .  set   (  uuid, label, data  )   ;  
     }  
 }

Plugins that use CoreData
CorePoints - A simple alternative currency plugin
CorePlaytime - The best plugin for rewarding players for playing!

Similar projects

Plugin →