mirror of
https://github.com/TotalFreedomMC/TF-ProjectKorra.git
synced 2025-02-11 11:40:40 +00:00
Update Config, ConfigLoadable, ConfigManager
This commit is contained in:
parent
2bd1d312b8
commit
ca84318f5a
3 changed files with 44 additions and 19 deletions
|
@ -7,21 +7,35 @@ import org.bukkit.configuration.file.YamlConfiguration;
|
|||
|
||||
import com.projectkorra.ProjectKorra.ProjectKorra;
|
||||
|
||||
/**
|
||||
* A config utility class for Project Korra. To get the config itself
|
||||
* use {@link #get()}
|
||||
*/
|
||||
public class Config {
|
||||
|
||||
ProjectKorra plugin;
|
||||
private ProjectKorra plugin;
|
||||
|
||||
private File file;
|
||||
public FileConfiguration config;
|
||||
private FileConfiguration config;
|
||||
|
||||
/**
|
||||
* Creates a new {@link Config} with the file being the configuration file.
|
||||
*
|
||||
* @param file The file to create/load
|
||||
*/
|
||||
public Config(File file) {
|
||||
this.plugin = ProjectKorra.plugin;
|
||||
this.file = new File(plugin.getDataFolder() + File.separator + file);
|
||||
this.config = YamlConfiguration.loadConfiguration(this.file);
|
||||
reloadConfig();
|
||||
reload();
|
||||
}
|
||||
|
||||
public void createConfig() {
|
||||
/**
|
||||
* Creates a file for the {@link FileConfiguration} object.
|
||||
* If there are missing folders, this method will try to create them
|
||||
* before create a file for the config.
|
||||
*/
|
||||
public void create() {
|
||||
if (!file.getParentFile().exists()) {
|
||||
try {
|
||||
file.getParentFile().mkdir();
|
||||
|
@ -43,12 +57,22 @@ public class Config {
|
|||
}
|
||||
}
|
||||
|
||||
public FileConfiguration getConfig() {
|
||||
/**
|
||||
* Gets the {@link FileConfiguration} object from the {@link Config}.
|
||||
*
|
||||
* @return the file configuration object
|
||||
*/
|
||||
public FileConfiguration get() {
|
||||
return config;
|
||||
}
|
||||
|
||||
public void reloadConfig() {
|
||||
createConfig();
|
||||
/**
|
||||
* Reloads the {@link FileConfiguration} object.
|
||||
* If the config object does not exist it will run
|
||||
* {@link #create()} first before loading the config.
|
||||
*/
|
||||
public void reload() {
|
||||
create();
|
||||
try {
|
||||
config.load(file);
|
||||
} catch (Exception e) {
|
||||
|
@ -56,7 +80,12 @@ public class Config {
|
|||
}
|
||||
}
|
||||
|
||||
public void saveConfig() {
|
||||
/**
|
||||
* Saves the {@link FileConfiguration} object.
|
||||
* {@code config.options().copyDefaults(true)} is called
|
||||
* before saving the config.
|
||||
*/
|
||||
public void save() {
|
||||
try {
|
||||
config.options().copyDefaults(true);
|
||||
config.save(file);
|
||||
|
|
|
@ -1,16 +1,14 @@
|
|||
package com.projectkorra.ProjectKorra.configuration;
|
||||
|
||||
import org.bukkit.configuration.file.FileConfiguration;
|
||||
|
||||
import com.projectkorra.ProjectKorra.ProjectKorra;
|
||||
|
||||
/**
|
||||
* Represents something that loads values from configs.
|
||||
*/
|
||||
public interface ConfigLoadable {
|
||||
|
||||
FileConfiguration config = ProjectKorra.plugin.getConfig();
|
||||
Config config = ConfigManager.defaultConfig;
|
||||
|
||||
// public FileConfiguration getConfig();
|
||||
|
||||
/**
|
||||
* Reload/Loads variables from the configuration.
|
||||
*/
|
||||
|
|
|
@ -6,8 +6,6 @@ import java.util.ArrayList;
|
|||
import org.bukkit.configuration.file.FileConfiguration;
|
||||
|
||||
import com.projectkorra.ProjectKorra.ProjectKorra;
|
||||
import com.projectkorra.ProjectKorra.firebending.FireMethods;
|
||||
import com.projectkorra.ProjectKorra.waterbending.WaterMethods;
|
||||
|
||||
public class ConfigManager {
|
||||
|
||||
|
@ -27,7 +25,7 @@ public class ConfigManager {
|
|||
FileConfiguration config;
|
||||
switch (type) {
|
||||
case DEATH_MESSAGE:
|
||||
config = deathMsgConfig.getConfig();
|
||||
config = deathMsgConfig.get();
|
||||
|
||||
config.addDefault("Properties.Enabled", true);
|
||||
config.addDefault("Properties.Default", "{victim} was slain by {attacker}'s {ability}");
|
||||
|
@ -69,10 +67,10 @@ public class ConfigManager {
|
|||
config.addDefault("Chi.RapidPunch", "{victim} took all the hits against {attacker}'s {ability}");
|
||||
config.addDefault("Chi.ChiCombo", "{victim} was overwhelmed by {attacker}'s skill {ability}");
|
||||
|
||||
deathMsgConfig.saveConfig();
|
||||
deathMsgConfig.save();
|
||||
break;
|
||||
case DEFAULT:
|
||||
config = defaultConfig.getConfig();
|
||||
config = defaultConfig.get();
|
||||
|
||||
ArrayList<String> earthbendable = new ArrayList<String>();
|
||||
earthbendable.add("STONE");
|
||||
|
@ -921,7 +919,7 @@ public class ConfigManager {
|
|||
|
||||
config.addDefault("debug", false);
|
||||
|
||||
defaultConfig.saveConfig();
|
||||
defaultConfig.save();
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue