Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Multi file config class for Spigot / Bukkit plugins
package com.github.dwesolowski.testing;
import org.bukkit.configuration.InvalidConfigurationException;
import org.bukkit.configuration.file.FileConfiguration;
import org.bukkit.configuration.file.YamlConfiguration;
import org.bukkit.plugin.Plugin;
import org.bukkit.plugin.java.JavaPlugin;
import java.io.File;
import java.io.IOException;
public class YamlConfig {
private final Plugin plugin;
File file1, file2;
FileConfiguration messages, data;
public YamlConfig(JavaPlugin plugin) {
this.file1 = new File(plugin.getDataFolder(), "messages.yml");
this.file2 = new File(plugin.getDataFolder() + File.separator + "data" + File.separator, "data.yml");
this.plugin = plugin;
}
protected void init() {
if (!file1.exists()) {
if (file1.getParentFile().mkdirs()) plugin.getLogger().info("Directory created");
plugin.saveResource("messages.yml", false);
}
if (!file2.exists()) {
if (file2.getParentFile().mkdirs()) plugin.getLogger().info("Directory created");
plugin.saveResource("data/data.yml", false);
}
messages = new YamlConfiguration();
data = new YamlConfiguration();
try {
messages.load(file1);
data.load(file2);
} catch (IOException | InvalidConfigurationException e) {
e.printStackTrace();
}
}
public FileConfiguration getMsgs() {
return messages;
}
public FileConfiguration getData() {
return data;
}
public void saveData() {
try {
data.save(file2);
} catch (IOException e) {
e.printStackTrace();
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment