DFile
Tested Versions:
- 1.8.x
This API was created with the purpose of making it easier to manage configuration files during the development of a plugin. Spigot provides an excellent tool for handling yaml files, when making use of that tool, many times you have to write several methods for each configuration file, which makes the code longer and less maintainable when the plugin scales, or falling into the practices of copying and pasting existing code and adapting it, with DFile API, DRY (Don't Repeat Yourself) is applied.
DFile API, provides some flexibility, since it does not require reloading the file when there have been manual changes, avoiding the creation or use of the /reload command of a plugin, improving your users' experience.
DFile API is currently tested with SpigotMC API version 1.8. If the current versions do not change the API, specifically the one related to the FileConfiguration and YamlConfiguration classes, it will work correctly.
Methods
getFile
registerFile
saveFile
createFile
deleteFile
Examples
To start using the API, first you have to create a new object of type DFile, this instance can be inside a method or be a private attribute. When creating the object, it requires you to pass it a parameter of type JavaPlugin or some other object that inherits from this class:
//Initializing
DFile dfile = new DFile(JavaPlugin);
//In a class
import df2001.dfileapi.DFile;
public class MainClass extends JavaPlugin {
private final DFile dfile = new DFile(this);
//Code...
}
When you use it in another class:
public class MyCommand implements CommandExecutor {
private final DFile dFile;
//MainClass inherits from JavaPlugin
public MyCommand (MainClass plugin) {
this.dFile = new DFile(plugin);
}
}
Usage of the registerFile() method. Make sure you have created and compiled the file to be registered, except if you want to generate files dynamically.
public class MainClass extends JavaPlugin {
private final DFile dfile = new DFile(this);
@Override
public void onEnable() {
//creates|register config.yml
dfile.registerFile("config");
//creates|register messages.yml
dfile.registerFile("messages");
//Other code...
}
}
Usage of the getFile() and saveFile methods:
public void saveStats (Player player) {
//gets config.yml and {player uuid}.yml
FileConfiguration config = dfile.getFile("config");
FileConfiguration playerFile = dfile.getFile(player.getUniqueId().toString());
String killMsg = config.getString("config.messages.on-game.kill");
String coins = config.getInt("config.on-kill.coins");
String currentCoins = playerFile.getInt("stats.coins");
player.sendMessage("You have earned " + coins + "!");
//Saving the new data
playerFile.set("stats.coins", currentCoins + coins);
dfile.saveFile(player.getUniqueId().toString(), playerFile);
}
In order to create or delete "custom or dynamic" files, it is not necessary to register the files created by the createFile() method, the deleteFile() method deletes any configuration file, whether registered or not.
public class JLEvent implements Listener {
private final DFile dfile;
public JLEvent (JavaPlugin plugin) {
this.dfile = new DFile(plugin);
}
@EventHandler
public void onPlayerJoin (PlayerJoinEvent e) {
String playerUUID = e.getPlayer.getUniqueId().toString();
if (!dfile.createFile(playerUUID)) {
//Error code : a file has not been created
}
}
@EventHandler
public void onPlayerLeave (PlayerQuitEvent e) {
String playerUUID = e.getPlayer.getUniqueId().toString();
if (!dfile.createFile(playerUUID)) {
//Error code : a file has not been deleted
}
}
}
Download
Choose between these two platforms to download the API!