Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package com;
- import java.io.File;
- import java.io.FileOutputStream;
- import java.io.InputStream;
- import java.io.OutputStream;
- import org.bukkit.configuration.file.FileConfiguration;
- import org.bukkit.plugin.java.JavaPlugin;
- public class Main extends JavaPlugin {
- public static Main instance;
- public static File configf;
- public static FileConfiguration config;
- public void onEnable() {
- instance = this;
- createFiles();
- /*
- * Call createFiles() to make a run through and create config files if
- * they are missing
- */
- try {
- config.load(configf);
- /*
- * This is VERY IMPORTANT. This assigns your FileConfiguration
- * object to the file so that you can read/write to it.
- */
- } catch (Exception e) {
- e.printStackTrace();
- }
- }
- public void onDisable() {
- instance = null;
- }
- /*
- * The createFiles method creates the config files from the files saved in
- * the jar. To create a file in your jar, right click on your 'src' folder
- * in your project in eclipse and click New > File. Select the name of your
- * file with the file extension Instructions will follow inside the method
- */
- public void createFiles() {
- configf = new File(getDataFolder(), "config.yml");
- /*
- * ^^ This here is your 'File' object of the config. The constructor
- * takes in 2 parameters: the Path(where the file is stored), and the
- * name of the file that will be created in the folder. The
- * getDataFolder() is a built-in method in JavaPlugin that returns a
- * path to the folder where your plugin's data will be stored
- */
- if (!configf.exists()) {
- configf.getParentFile().mkdirs();
- copy(getResource("config.yml"), configf);
- }
- /*
- * ^^ This is the copying part. The 'if' statement checks if the file
- * was already created or not. The mkdirs() method creates a folder for
- * that directory if it does not yet exist. The copy() method takes in
- * two parameters. The first, getResource("config.yml"), is calling the
- * method getResource() to return an InputStream of the file in your jar
- * file. The "config.yml" inside this should be the name of the file
- * which you want to copy from inside the jar. The second parameter is
- * the file that you created earlier. It is the file to which you will
- * copy the data from the default config in your jar.
- */
- }
- /*
- * The copy method is a bit more complicated so I won't get so in-depth with
- * it. It basically gets an InputStream and copies it to the file in the
- * second parameter
- */
- public void copy(InputStream in, File file) {
- try {
- OutputStream out = new FileOutputStream(file);
- byte[] buf = new byte[1024];
- int len;
- while ((len = in.read(buf)) > 0) {
- out.write(buf, 0, len);
- }
- out.close();
- in.close();
- } catch (Exception e) {
- e.printStackTrace();
- }
- }
- public static Main getInstance() {
- return instance;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement