Advertisement
Dudemister1999

PackagedFile class (With logging)

Mar 27th, 2014
99
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.01 KB | None | 0 0
  1. package test;
  2.  
  3. import java.io.File;
  4. import java.util.logging.Level;
  5. import java.util.logging.Logger;
  6. import org.bukkit.configuration.file.YamlConfiguration;
  7. import org.bukkit.plugin.java.JavaPlugin;
  8.  
  9. public final class PackagedFile
  10. {
  11.     private String filename;
  12.     private File file;
  13.     private YamlConfiguration cfg;
  14.    
  15.     /**
  16.      * @param level   Level needed for logging.
  17.      * @param message    The message you want to log.
  18.      */
  19.     private void log(Level level, String message)
  20.     {
  21.         Logger.getLogger("Minecraft").log(level, message);
  22.     }
  23.    
  24.     /**
  25.      * @param pl   The plugin, needed to get the folder.
  26.      * @param fileName    The file you want, OMIT .yml!
  27.      */
  28.     public PackagedFile(JavaPlugin pl, String fileName)
  29.     {
  30.         filename = fileName + ".yml";
  31.         file = new File(pl.getDataFolder(), filename);
  32.        
  33.         if(pl.getDataFolder().exists())
  34.         {
  35.             check(pl, file);
  36.         }
  37.         else
  38.         {
  39.             pl.getDataFolder().mkdir();
  40.             check(pl, file);
  41.         }
  42.     }
  43.    
  44.     private void check(JavaPlugin pl, File file)
  45.     {
  46.         log(Level.INFO, "Checking if file is unpacked.");
  47.         if (file.exists())
  48.         {
  49.             log(Level.INFO, "File " + filename + " is already unpacked, loading data.");
  50.             cfg = YamlConfiguration.loadConfiguration(file);
  51.         }
  52.         else
  53.         {
  54.             log(Level.INFO, "Unpacking file " + filename + ".");
  55.             cfg = YamlConfiguration.loadConfiguration(pl.getResource(filename));    
  56.         }
  57.     }
  58.    
  59.     /**
  60.      * @return The created YamlConfiguration
  61.      */
  62.     public YamlConfiguration getCFG()
  63.     {
  64.         return cfg;
  65.     }
  66.    
  67.     /**
  68.      * @return The name of the file
  69.      */
  70.     public String getFilename()
  71.     {
  72.         return filename;
  73.     }
  74.    
  75.     /**
  76.      * @return The file that the YamlConfiguration is saved in
  77.      */
  78.     public File getFile()
  79.     {
  80.         return file;
  81.     }
  82. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement