package com.badbones69.crazyauctions.api; import org.bukkit.Bukkit; import org.bukkit.configuration.file.FileConfiguration; import org.bukkit.configuration.file.YamlConfiguration; import org.bukkit.plugin.Plugin; import org.bukkit.scheduler.BukkitRunnable; import java.io.File; import java.io.FileOutputStream; import java.io.IOException; import java.io.InputStream; import java.util.ArrayList; import java.util.HashMap; /** * * @author BadBones69 * @version v1.0 * */ public class FileManager { private static FileManager instance = new FileManager(); private Plugin plugin; private String prefix = ""; private Boolean log = false; private HashMap files = new HashMap<>(); private ArrayList homeFolders = new ArrayList<>(); private ArrayList customFiles = new ArrayList<>(); private HashMap autoGenerateFiles = new HashMap<>(); private HashMap configurations = new HashMap<>(); public static FileManager getInstance() { return instance; } /** * Sets up the plugin and loads all necessary files. * @param plugin The plugin this is getting loading for. */ public FileManager setup(Plugin plugin) { prefix = "[" + plugin.getName() + "] "; this.plugin = plugin; if (!plugin.getDataFolder().exists()) { plugin.getDataFolder().mkdirs(); } files.clear(); customFiles.clear(); //Loads all the normal static files. for (Files file : Files.values()) { File newFile = new File(plugin.getDataFolder(), file.getFileLocation()); if (log) Bukkit.getLogger().info("Loading the " + file.getFileName()); if (!newFile.exists()) { try { String fileLocation = file.getFileLocation(); //Switch between 1.12.2- and 1.13+ config version. if (file == Files.CONFIG) { if (Version.isOlder(Version.v1_13_R2)) { fileLocation = "config1.12.2-Down.yml"; } else { fileLocation = "config1.13-Up.yml"; } } File serverFile = new File(plugin.getDataFolder(), "/" + file.getFileLocation()); InputStream jarFile = getClass().getResourceAsStream("/" + fileLocation); copyFile(jarFile, serverFile); } catch (Exception e) { if (log) Bukkit.getLogger().info("Failed to load " + file.getFileName()); e.printStackTrace(); continue; } } files.put(file, newFile); configurations.put(file, YamlConfiguration.loadConfiguration(newFile)); if (log) Bukkit.getLogger().info("Successfully loaded " + file.getFileName()); } //Starts to load all the custom files. if (homeFolders.size() > 0) { if (log) Bukkit.getLogger().info("Loading custom files."); for (String homeFolder : homeFolders) { File homeFile = new File(plugin.getDataFolder(), "/" + homeFolder); if (homeFile.exists()) { String[] list = homeFile.list(); if (list != null) { for (String name : list) { if (name.endsWith(".yml")) { CustomFile file = new CustomFile(name, homeFolder, plugin); if (file.exists()) { customFiles.add(file); if (log) Bukkit.getLogger().info("Loaded new custom file: " + homeFolder + "/" + name + "."); } } } } } else { homeFile.mkdir(); if (log) Bukkit.getLogger().info("The folder " + homeFolder + "/ was not found so it was created."); for (String fileName : autoGenerateFiles.keySet()) { if (autoGenerateFiles.get(fileName).equalsIgnoreCase(homeFolder)) { homeFolder = autoGenerateFiles.get(fileName); try { File serverFile = new File(plugin.getDataFolder(), homeFolder + "/" + fileName); InputStream jarFile = getClass().getResourceAsStream(homeFolder + "/" + fileName); copyFile(jarFile, serverFile); if (fileName.toLowerCase().endsWith(".yml")) { customFiles.add(new CustomFile(fileName, homeFolder, plugin)); } if (log) Bukkit.getLogger().info("Created new default file: " + homeFolder + "/" + fileName + "."); } catch (Exception e) { if (log) Bukkit.getLogger().info("Failed to create new default file: " + homeFolder + "/" + fileName + "!"); e.printStackTrace(); } } } } } if (log) Bukkit.getLogger().info("Finished loading custom files."); } return this; } /** * Turn on the logger system for the FileManager. * @param log True to turn it on and false for it to be off. */ public FileManager logInfo(Boolean log) { this.log = log; return this; } /** * Check if the logger is logging in console. * @return True if it is and false if it isn't. */ public Boolean isLogging() { return log; } /** * Register a folder that has custom files in it. Make sure to have a "/" in front of the folder name. * @param homeFolder The folder that has custom files in it. */ public FileManager registerCustomFilesFolder(String homeFolder) { homeFolders.add(homeFolder); return this; } /** * Unregister a folder that has custom files in it. Make sure to have a "/" in front of the folder name. * @param homeFolder The folder with custom files in it. */ public FileManager unregisterCustomFilesFolder(String homeFolder) { homeFolders.remove(homeFolder); return this; } /** * Register a file that needs to be generated when it's home folder doesn't exist. Make sure to have a "/" in front of the home folder's name. * @param fileName The name of the file you want to auto-generate when the folder doesn't exist. * @param homeFolder The folder that has custom files in it. */ public FileManager registerDefaultGenerateFiles(String fileName, String homeFolder) { autoGenerateFiles.put(fileName, homeFolder); return this; } /** * Unregister a file that doesn't need to be generated when it's home folder doesn't exist. Make sure to have a "/" in front of the home folder's name. * @param fileName The file that you want to remove from auto-generating. */ public FileManager unregisterDefaultGenerateFiles(String fileName) { autoGenerateFiles.remove(fileName); return this; } /** * Gets the file from the system. * @return The file from the system. */ public FileConfiguration getFile(Files file) { return configurations.get(file); } /** * Get a custom file from the loaded custom files instead of a hardcoded one. * This allows you to get custom files like Per player data files. * @param name Name of the crate you want. (Without the .yml) * @return The custom file you wanted otherwise if not found will return null. */ public CustomFile getFile(String name) { for (CustomFile file : customFiles) { if (file.getName().equalsIgnoreCase(name)) { return file; } } return null; } /** * Saves the file from the loaded state to the file system. */ public void saveFile(Files file, boolean sync) { try { File targetFile = files.get(file); FileConfiguration configuration = configurations.get(file); YamlConfiguration copy = new YamlConfiguration(); configuration.getValues(false).forEach(copy :: set); BukkitRunnable runnable = new BukkitRunnable() { @Override public void run() { try { copy.save(targetFile); } catch (IOException e) { Bukkit.getLogger().info("Could not save " + file.getFileName() + "!"); e.printStackTrace(); } } }; if (sync) { runnable.run(); } else { runnable.runTaskAsynchronously(plugin); } } catch (NullPointerException e) { Bukkit.getLogger().info("File is null " + file.getFileName() + "!"); e.printStackTrace(); } } /** * Save a custom file. * @param name The name of the custom file. */ public void saveFile(String name) { CustomFile file = getFile(name); if (file != null) { try { file.getFile().save(new File(plugin.getDataFolder(), file.getHomeFolder() + "/" + file.getFileName())); if (log) Bukkit.getLogger().info("Successfuly saved the " + file.getFileName() + "."); } catch (Exception e) { Bukkit.getLogger().info("Could not save " + file.getFileName() + "!"); e.printStackTrace(); } } else { if (log) Bukkit.getLogger().info("The file " + name + ".yml could not be found!"); } } /** * Save a custom file. * @param file The custom file you are saving. * @return True if the file saved correct and false if there was an error. */ public Boolean saveFile(CustomFile file) { return file.saveFile(); } /** * Overrides the loaded state file and loads the file systems file. */ public void reloadFile(Files file) { configurations.put(file, YamlConfiguration.loadConfiguration(files.get(file))); } /** * Overrides the loaded state file and loads the file systems file. */ public void reloadFile(String name) { CustomFile file = getFile(name); if (file != null) { try { file.file = YamlConfiguration.loadConfiguration(new File(plugin.getDataFolder(), "/" + file.getHomeFolder() + "/" + file.getFileName())); if (log) Bukkit.getLogger().info("Successfuly reload the " + file.getFileName() + "."); } catch (Exception e) { Bukkit.getLogger().info("Could not reload the " + file.getFileName() + "!"); e.printStackTrace(); } } else { if (log) Bukkit.getLogger().info("The file " + name + ".yml could not be found!"); } } /** * Overrides the loaded state file and loads the filesystems file. * @return True if it reloaded correct and false if the file wasn't found. */ public Boolean reloadFile(CustomFile file) { return file.reloadFile(); } /** * Was found here: https://bukkit.org/threads/extracting-file-from-jar.16962 */ private void copyFile(InputStream in, File out) throws Exception { try (FileOutputStream fos = new FileOutputStream(out)) { byte[] buf = new byte[1024]; int i; while ((i = in.read(buf)) != -1) { fos.write(buf, 0, i); } } finally { if (in != null) { in.close(); } } } public enum Files { //ENUM_NAME("FileName.yml", "FilePath.yml"), CONFIG("config.yml", "config.yml"), DATA("Data.yml", "Data.yml"), MESSAGES("Messages.yml", "Messages.yml"), TEST_FILE("Test-File.yml", "Test-File.yml"); private final String fileName; private final String fileLocation; /** * The files that the server will try and load. * @param fileName The file name that will be in the plugin's folder. * @param fileLocation The location the file is in while in the Jar. */ private Files(String fileName, String fileLocation) { this.fileName = fileName; this.fileLocation = fileLocation; } /** * Get the name of the file. * @return The name of the file. */ public String getFileName() { return fileName; } /** * The location the jar it is at. * @return The location in the jar the file is in. */ public String getFileLocation() { return fileLocation; } /** * Gets the file from the system. * @return The file from the system. */ public FileConfiguration getFile() { return getInstance().getFile(this); } /** * Saves the file from the loaded state to the file system. */ public void saveFile(boolean sync) { getInstance().saveFile(this, sync); } public void saveFile() { getInstance().saveFile(this, false); } /** * Overrides the loaded state file and loads the file systems file. */ public void relaodFile() { getInstance().reloadFile(this); } } public class CustomFile { private String name; private Plugin plugin; private String fileName; private String homeFolder; private FileConfiguration file; /** * A custom file that is being made. * @param name Name of the file. * @param homeFolder The home folder of the file. * @param plugin The plugin the files belong to. */ public CustomFile(String name, String homeFolder, Plugin plugin) { this.name = name.replace(".yml", ""); this.plugin = plugin; this.fileName = name; this.homeFolder = homeFolder; if (new File(plugin.getDataFolder(), "/" + homeFolder).exists()) { if (new File(plugin.getDataFolder(), "/" + homeFolder + "/" + name).exists()) { file = YamlConfiguration.loadConfiguration(new File(plugin.getDataFolder(), "/" + homeFolder + "/" + name)); } else { file = null; } } else { new File(plugin.getDataFolder(), "/" + homeFolder).mkdir(); if (log) Bukkit.getLogger().info("The folder " + homeFolder + "/ was not found so it was created."); file = null; } } /** * Get the name of the file without the .yml part. * @return The name of the file without the .yml. */ public String getName() { return name; } /** * Get the full name of the file. * @return Full name of the file. */ public String getFileName() { return fileName; } /** * Get the name of the home folder of the file. * @return The name of the home folder the files are in. */ public String getHomeFolder() { return homeFolder; } /** * Get the plugin the file belongs to. * @return The plugin the file belongs to. */ public Plugin getPlugin() { return plugin; } /** * Get the ConfigurationFile. * @return The ConfigurationFile of this file. */ public FileConfiguration getFile() { return file; } /** * Check if the file actually exists in the file system. * @return True if it does and false if it doesn't. */ public Boolean exists() { return file != null; } /** * Save the custom file. * @return True if it saved correct and false if something went wrong. */ public Boolean saveFile() { if (file != null) { try { file.save(new File(plugin.getDataFolder(), homeFolder + "/" + fileName)); if (log) Bukkit.getLogger().info("Successfuly saved the " + fileName + "."); return true; } catch (Exception e) { Bukkit.getLogger().info("Could not save " + fileName + "!"); e.printStackTrace(); return false; } } else { if (log) Bukkit.getLogger().info("There was a null custom file that could not be found!"); } return false; } /** * Overrides the loaded state file and loads the filesystems file. * @return True if it reloaded correct and false if the file wasn't found or errored. */ public Boolean reloadFile() { if (file != null) { try { file = YamlConfiguration.loadConfiguration(new File(plugin.getDataFolder(), "/" + homeFolder + "/" + fileName)); if (log) Bukkit.getLogger().info("Successfuly reload the " + fileName + "."); return true; } catch (Exception e) { Bukkit.getLogger().info("Could not reload the " + fileName + "!"); e.printStackTrace(); } } else { if (log) Bukkit.getLogger().info("There was a null custom file that was not found!"); } return false; } } }