Advertisement
acuddlyheadcrab

Non-config YML methods.

Feb 24th, 2012
50
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.45 KB | None | 0 0
  1. public class Example extends JavaPlugin {
  2.  
  3.     //                  Copy from here...
  4.  
  5.     public static FileConfiguration userdata = null;
  6.     private static File userdataConfigFile = null;
  7.    
  8.     // You can rename the methods to anything you like. Userdata is just what I call it.
  9.     // Also, in onEnable(), use loadUserdata() and to save it, use saveUserdata(). Simple enough.
  10.    
  11.     public void reloadUserdata() {
  12.         if (userdataConfigFile == null) {
  13.         userdataConfigFile = new File(getDataFolder(), "userdata.yml");
  14.         }
  15.         userdata = YamlConfiguration.loadConfiguration(userdataConfigFile);
  16.         InputStream defConfigStream = getResource("userdata.yml");
  17.         if (defConfigStream != null) {
  18.             YamlConfiguration defConfig = YamlConfiguration.loadConfiguration(defConfigStream);
  19.             userdata.setDefaults(defConfig);
  20.         }
  21.     }
  22.    
  23.     public void loadUserdata(){
  24.         userdata = getUserdata();
  25.         userdata.options().copyDefaults(true);
  26.         saveUserdata();
  27.     }
  28.    
  29.     public FileConfiguration getUserdata() {
  30.         if (userdata == null) {reloadUserdata();}
  31.         return userdata;
  32.     }
  33.    
  34.     public static void saveUserdata() {
  35.         if (userdata == null || userdataConfigFile == null) {
  36.         return;
  37.         }
  38.         try {
  39.             userdata.save(userdataConfigFile);
  40.         } catch (IOException ex) {
  41.             Util.sendPluginInfo("Could not save config to " + userdataConfigFile.getName() + ex);
  42.         }
  43.     }
  44.    
  45.     //                  ...To here!
  46.    
  47. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement