73 lines
2.2 KiB
Java

package de.steev.bm.utils;
import de.steev.bm.BetterMinecraft;
import de.steev.bm.utils.exceptions.ConfigEntryExceptions;
import org.bukkit.configuration.file.FileConfiguration;
import org.bukkit.configuration.file.YamlConfiguration;
import java.io.File;
import java.io.IOException;
import static de.steev.bm.utils.Constants.playerDataFilename;
public class Config {
private String name;
private File configFile;
private FileConfiguration fileConfiguration;
private BetterMinecraft plugin;
public Config(String name, BetterMinecraft plugin) {
this.name = name;
this.plugin = plugin;
configFile = new File(this.plugin.getDataFolder(), playerDataFilename);
fileConfiguration = YamlConfiguration.loadConfiguration(configFile);
}
public void saveConfig(String name) throws IOException {
try {
fileConfiguration.save(configFile);
} catch (IOException e) {
this.plugin.getLogger().warning("Unable to save " + playerDataFilename); // shouldn't really happen, but save
// throws the exception
throw e;
}
}
public void writeStringToConfig(String path, String value) {
this.fileConfiguration.set(path, value);
}
public String readStringFromConfig(String path) throws ConfigEntryExceptions {
if (this.fileConfiguration.get(path) == null) {
throw new ConfigEntryExceptions("entry at: " + path + " found null");
}
return this.fileConfiguration.getString(path);
}
public int readIntFromConfig(String path) throws ConfigEntryExceptions {
if (this.fileConfiguration.get(path) == null) {
throw new ConfigEntryExceptions("entry at: " + path + " found null");
}
return this.fileConfiguration.getInt(path);
}
public boolean readBooleanFromConfig(String path) throws ConfigEntryExceptions {
if (this.fileConfiguration.get(path) == null) {
throw new ConfigEntryExceptions("entry at: " + path + " found null");
}
return this.fileConfiguration.getBoolean(path);
}
public FileConfiguration getConfig() {
return this.fileConfiguration;
}
public String getName() {
return this.name;
}
}