refactored config to it's own class
This commit is contained in:
72
src/main/java/de/steev/bm/utils/Config.java
Normal file
72
src/main/java/de/steev/bm/utils/Config.java
Normal file
@ -0,0 +1,72 @@
|
||||
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;
|
||||
}
|
||||
}
|
9
src/main/java/de/steev/bm/utils/Constants.java
Normal file
9
src/main/java/de/steev/bm/utils/Constants.java
Normal file
@ -0,0 +1,9 @@
|
||||
package de.steev.bm.utils;
|
||||
|
||||
import org.bukkit.ChatColor;
|
||||
|
||||
public class Constants {
|
||||
public static final String prefix = ChatColor.GRAY + "[" + ChatColor.AQUA + "BetterMinecraft" + ChatColor.GRAY + "]";
|
||||
public static final String playerDataFilename = "playerdata.yml";
|
||||
public static final String Version = "1.2.2";
|
||||
}
|
@ -0,0 +1,9 @@
|
||||
package de.steev.bm.utils.exceptions;
|
||||
|
||||
public class ConfigEntryExceptions extends Exception {
|
||||
public ConfigEntryExceptions() {}
|
||||
|
||||
public ConfigEntryExceptions(String message) {
|
||||
super(message);
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user