|
| 1 | +package me.jonakls.miniannouncer; |
| 2 | + |
| 3 | + |
| 4 | +import org.bukkit.configuration.file.FileConfiguration; |
| 5 | +import org.bukkit.configuration.file.YamlConfiguration; |
| 6 | +import org.bukkit.plugin.Plugin; |
| 7 | + |
| 8 | +import java.io.File; |
| 9 | +import java.io.IOException; |
| 10 | +import java.io.InputStream; |
| 11 | +import java.io.UncheckedIOException; |
| 12 | +import java.nio.file.Files; |
| 13 | + |
| 14 | +public class BukkitConfiguration { |
| 15 | + |
| 16 | + private final File file; |
| 17 | + private FileConfiguration config; |
| 18 | + |
| 19 | + public BukkitConfiguration(File folder, String fileName) { |
| 20 | + if (!folder.exists() && !folder.mkdirs()) { |
| 21 | + throw new IllegalStateException("Plugin folder" + folder.getName() + "cannot be created"); |
| 22 | + } |
| 23 | + |
| 24 | + this.file = new File(folder, fileName + ".yml"); |
| 25 | + |
| 26 | + if (!file.exists()) { |
| 27 | + try (InputStream stream = getClass().getClassLoader().getResourceAsStream(file.getName())) { |
| 28 | + if (stream != null) { |
| 29 | + Files.copy(stream, file.toPath()); |
| 30 | + } |
| 31 | + } catch (IOException e) { |
| 32 | + throw new UncheckedIOException("An error occurred while loading file '" + fileName + "'.", e); |
| 33 | + } |
| 34 | + } |
| 35 | + reload(); |
| 36 | + } |
| 37 | + |
| 38 | + public BukkitConfiguration(Plugin plugin, String fileName) { |
| 39 | + this(plugin.getDataFolder(), fileName); |
| 40 | + } |
| 41 | + |
| 42 | + public FileConfiguration get() { |
| 43 | + return config; |
| 44 | + } |
| 45 | + |
| 46 | + public void reload() { |
| 47 | + this.config = YamlConfiguration.loadConfiguration(file); |
| 48 | + } |
| 49 | + |
| 50 | + public void save() { |
| 51 | + try { |
| 52 | + config.save(file); |
| 53 | + } catch (IOException e) { |
| 54 | + throw new UncheckedIOException("An error occurred while saving file '" + file.getName() + "'.", e); |
| 55 | + } |
| 56 | + } |
| 57 | +} |
0 commit comments