Advertisement
Guest User

config

a guest
Apr 25th, 2015
213
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.95 KB | None | 0 0
  1. package com;
  2.  
  3. import java.io.File;
  4. import java.io.FileOutputStream;
  5. import java.io.InputStream;
  6. import java.io.OutputStream;
  7.  
  8. import org.bukkit.configuration.file.FileConfiguration;
  9. import org.bukkit.plugin.java.JavaPlugin;
  10.  
  11. public class Main extends JavaPlugin {
  12.  
  13.     public static Main instance;
  14.  
  15.     public static File configf;
  16.     public static FileConfiguration config;
  17.  
  18.     public void onEnable() {
  19.  
  20.         instance = this;
  21.         createFiles();
  22.         /*
  23.          * Call createFiles() to make a run through and create config files if
  24.          * they are missing
  25.          */
  26.         try {
  27.            
  28.             config.load(configf);
  29.             /*
  30.              * This is VERY IMPORTANT. This assigns your FileConfiguration
  31.              * object to the file so that you can read/write to it.
  32.              */
  33.            
  34.         } catch (Exception e) {
  35.             e.printStackTrace();
  36.         }
  37.  
  38.     }
  39.  
  40.     public void onDisable() {
  41.  
  42.         instance = null;
  43.  
  44.     }
  45.  
  46.     /*
  47.      * The createFiles method creates the config files from the files saved in
  48.      * the jar. To create a file in your jar, right click on your 'src' folder
  49.      * in your project in eclipse and click New > File. Select the name of your
  50.      * file with the file extension Instructions will follow inside the method
  51.      */
  52.     public void createFiles() {
  53.  
  54.         configf = new File(getDataFolder(), "config.yml");
  55.         /*
  56.          * ^^ This here is your 'File' object of the config. The constructor
  57.          * takes in 2 parameters: the Path(where the file is stored), and the
  58.          * name of the file that will be created in the folder. The
  59.          * getDataFolder() is a built-in method in JavaPlugin that returns a
  60.          * path to the folder where your plugin's data will be stored
  61.          */
  62.  
  63.         if (!configf.exists()) {
  64.             configf.getParentFile().mkdirs();
  65.             copy(getResource("config.yml"), configf);
  66.         }
  67.         /*
  68.          * ^^ This is the copying part. The 'if' statement checks if the file
  69.          * was already created or not. The mkdirs() method creates a folder for
  70.          * that directory if it does not yet exist. The copy() method takes in
  71.          * two parameters. The first, getResource("config.yml"), is calling the
  72.          * method getResource() to return an InputStream of the file in your jar
  73.          * file. The "config.yml" inside this should be the name of the file
  74.          * which you want to copy from inside the jar. The second parameter is
  75.          * the file that you created earlier. It is the file to which you will
  76.          * copy the data from the default config in your jar.
  77.          */
  78.  
  79.     }
  80.  
  81.     /*
  82.      * The copy method is a bit more complicated so I won't get so in-depth with
  83.      * it. It basically gets an InputStream and copies it to the file in the
  84.      * second parameter
  85.      */
  86.  
  87.     public void copy(InputStream in, File file) {
  88.  
  89.         try {
  90.  
  91.             OutputStream out = new FileOutputStream(file);
  92.             byte[] buf = new byte[1024];
  93.             int len;
  94.             while ((len = in.read(buf)) > 0) {
  95.  
  96.                 out.write(buf, 0, len);
  97.  
  98.             }
  99.             out.close();
  100.             in.close();
  101.  
  102.         } catch (Exception e) {
  103.  
  104.             e.printStackTrace();
  105.  
  106.         }
  107.  
  108.     }
  109.  
  110.     public static Main getInstance() {
  111.         return instance;
  112.     }
  113.  
  114. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement