Created
June 16, 2019 07:27
-
-
Save CarrlosM/a4bc1dabb6374be4a4a3b23b316e5943 to your computer and use it in GitHub Desktop.
Neat little manager and wrapper for handling custom files using the Bukkit Configuration API
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package us.carlosmendez.gists.file; | |
import org.bukkit.configuration.file.FileConfiguration; | |
import org.bukkit.configuration.file.YamlConfiguration; | |
import org.bukkit.plugin.java.JavaPlugin; | |
import java.io.File; | |
import java.io.IOException; | |
import java.io.InputStream; | |
import java.io.InputStreamReader; | |
import java.util.logging.Level; | |
public class CustomFile { | |
private JavaPlugin plugin; | |
private String fileName; | |
private File file; | |
private FileConfiguration fileConfig; | |
public CustomFile(JavaPlugin plugin, String fileName) { | |
this.plugin = plugin; | |
this.fileName = fileName; | |
} | |
public void reloadConfig(){ | |
if (file == null){ | |
file = new File(plugin.getDataFolder(), fileName + ".yml"); | |
} | |
fileConfig = YamlConfiguration.loadConfiguration(file); | |
InputStream defConfigStream = plugin.getResource(fileName + ".yml"); | |
if(defConfigStream != null){ | |
YamlConfiguration defConfig = YamlConfiguration.loadConfiguration(new InputStreamReader(defConfigStream)); | |
fileConfig.setDefaults(defConfig); | |
} | |
} | |
public FileConfiguration getConfig(){ | |
if(fileConfig == null){ | |
reloadConfig(); | |
} | |
return fileConfig; | |
} | |
public void saveConfig(){ | |
if(fileConfig == null || file == null){ | |
return; | |
} | |
try{ | |
getConfig().save(file); | |
} catch (IOException ex){ | |
plugin.getLogger().log(Level.SEVERE, "Could not save config to " + file, ex); | |
} | |
} | |
public void saveDefaultConfig(){ | |
if(file == null){ | |
file = new File(plugin.getDataFolder(), fileName + ".yml"); | |
} | |
if(!file.exists()){ | |
plugin.saveResource(fileName + ".yml", false); | |
} | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package us.carlosmendez.gists.file; | |
import org.bukkit.plugin.java.JavaPlugin; | |
import java.util.HashMap; | |
public class FileManager { | |
private JavaPlugin plugin; | |
private HashMap<String, CustomFile> fileMap = new HashMap<>(); | |
public FileManager(JavaPlugin plugin) { | |
this.plugin = plugin; | |
} | |
public boolean fileExists(String fileName) { return fileMap.containsKey(fileName); } | |
public CustomFile getFile(String fileName) { | |
if (!fileExists(fileName)) registerFile(fileName); | |
return fileMap.get(fileName); | |
} | |
public CustomFile registerFile(String fileName) { | |
if (fileExists(fileName)) return getFile(fileName); | |
CustomFile customFile = new CustomFile(plugin, fileName); | |
fileMap.put(fileName, customFile); | |
customFile.saveDefaultConfig(); | |
return customFile; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment