Advertisement
Guest User

Untitled

a guest
Apr 25th, 2014
49
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.84 KB | None | 0 0
  1. package scoreboard;
  2.  
  3. import java.io.File;
  4. import java.io.IOException;
  5.  
  6. import org.bukkit.configuration.file.FileConfiguration;
  7. import org.bukkit.configuration.file.YamlConfiguration;
  8. import org.bukkit.plugin.Plugin;
  9.  
  10. /**
  11. * A class to create (and get) configuration files for individual players.
  12. * <br>
  13. * Use this as opposed to storing all of your players' data in one config.yml.
  14. * @author Wizehh
  15. */
  16.  
  17. public class PlayerData {
  18.  
  19.     private String name;
  20.     private FileConfiguration config;
  21.     private File file;
  22.     private static File dir;
  23.     private static Plugin plugin;
  24.     private boolean debug;
  25.  
  26.  
  27.     /**
  28.     * Configures the directory and allows access to the plugin.
  29.     * <br>
  30.     * There <em>will</em> be a NullPointerException if you don't have this in your onEnable() method!
  31.     * @param instance your plugin's main class
  32.     */
  33.  
  34.     public static void setup(Plugin instance) {
  35.         plugin = instance;
  36.         dir = new File(plugin.getDataFolder() + File.separator + "players");
  37.         if (!dir.exists()) {
  38.             dir.mkdir();
  39.             plugin.getLogger().fine("The player data directories have been setup.");
  40.         }
  41.     }
  42.  
  43.     /**
  44.     * Creates a new player data object.
  45.     * If the file doesn't exist, it will be created
  46.     * @param name the name of the player
  47.     */
  48.  
  49.     public PlayerData(String name) {
  50.         this.name = name;
  51.         this.file = new File(dir + File.separator + this.name + ".yml");
  52.         if (file.exists()) {
  53.             file = new File(dir + File.separator + this.name + ".yml");
  54.             try {
  55.                 file.createNewFile();
  56.                 if (this.debug)
  57.                     plugin.getLogger().fine("The data file for " + this.name + " has been created.");
  58.             } catch (IOException e) {
  59.                 plugin.getLogger().severe("The data file for " + this.name + " could not be created! Reason: " + e.getMessage());
  60.                 if (this.debug)
  61.                     e.printStackTrace();
  62.             }
  63.         }
  64.         this.config = YamlConfiguration.loadConfiguration(file);
  65.     }
  66.  
  67.     /**
  68.     * Returns the configuration object
  69.     * @return the config
  70.     */
  71.  
  72.     public FileConfiguration getData() {
  73.         return this.config;
  74.     }
  75.  
  76.     /**
  77.     * Returns the hard file
  78.     * @return the file
  79.     */
  80.  
  81.     public File getFile() {
  82.         return this.file;
  83.     }
  84.  
  85.     /**
  86.     * Attempts to save the configuration file.
  87.     * <h1>You must do this after making any edits to the file!
  88.     */
  89.  
  90.     public void save() {
  91.         try {
  92.             config.save(this.file);
  93.         } catch (IOException e) {
  94.             plugin.getLogger().severe("The data file for " + this.name + " could not be saved! Reason: " + e.getMessage());
  95.             if (this.debug)
  96.                 e.printStackTrace();
  97.         }
  98.     }
  99.  
  100. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement