mirror of
https://github.com/TotalFreedomMC/TF-ProjectKorra.git
synced 2025-02-14 21:07:54 +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;
|
import com.projectkorra.ProjectKorra.ProjectKorra;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* A config utility class for Project Korra. To get the config itself
|
||||||
|
* use {@link #get()}
|
||||||
|
*/
|
||||||
public class Config {
|
public class Config {
|
||||||
|
|
||||||
ProjectKorra plugin;
|
private ProjectKorra plugin;
|
||||||
|
|
||||||
private File file;
|
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) {
|
public Config(File file) {
|
||||||
this.plugin = ProjectKorra.plugin;
|
this.plugin = ProjectKorra.plugin;
|
||||||
this.file = new File(plugin.getDataFolder() + File.separator + file);
|
this.file = new File(plugin.getDataFolder() + File.separator + file);
|
||||||
this.config = YamlConfiguration.loadConfiguration(this.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()) {
|
if (!file.getParentFile().exists()) {
|
||||||
try {
|
try {
|
||||||
file.getParentFile().mkdir();
|
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;
|
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 {
|
try {
|
||||||
config.load(file);
|
config.load(file);
|
||||||
} catch (Exception e) {
|
} 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 {
|
try {
|
||||||
config.options().copyDefaults(true);
|
config.options().copyDefaults(true);
|
||||||
config.save(file);
|
config.save(file);
|
||||||
|
|
|
@ -1,15 +1,13 @@
|
||||||
package com.projectkorra.ProjectKorra.configuration;
|
package com.projectkorra.ProjectKorra.configuration;
|
||||||
|
|
||||||
import org.bukkit.configuration.file.FileConfiguration;
|
|
||||||
|
|
||||||
import com.projectkorra.ProjectKorra.ProjectKorra;
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Represents something that loads values from configs.
|
* Represents something that loads values from configs.
|
||||||
*/
|
*/
|
||||||
public interface ConfigLoadable {
|
public interface ConfigLoadable {
|
||||||
|
|
||||||
FileConfiguration config = ProjectKorra.plugin.getConfig();
|
Config config = ConfigManager.defaultConfig;
|
||||||
|
|
||||||
|
// public FileConfiguration getConfig();
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Reload/Loads variables from the configuration.
|
* Reload/Loads variables from the configuration.
|
||||||
|
|
|
@ -6,8 +6,6 @@ import java.util.ArrayList;
|
||||||
import org.bukkit.configuration.file.FileConfiguration;
|
import org.bukkit.configuration.file.FileConfiguration;
|
||||||
|
|
||||||
import com.projectkorra.ProjectKorra.ProjectKorra;
|
import com.projectkorra.ProjectKorra.ProjectKorra;
|
||||||
import com.projectkorra.ProjectKorra.firebending.FireMethods;
|
|
||||||
import com.projectkorra.ProjectKorra.waterbending.WaterMethods;
|
|
||||||
|
|
||||||
public class ConfigManager {
|
public class ConfigManager {
|
||||||
|
|
||||||
|
@ -27,7 +25,7 @@ public class ConfigManager {
|
||||||
FileConfiguration config;
|
FileConfiguration config;
|
||||||
switch (type) {
|
switch (type) {
|
||||||
case DEATH_MESSAGE:
|
case DEATH_MESSAGE:
|
||||||
config = deathMsgConfig.getConfig();
|
config = deathMsgConfig.get();
|
||||||
|
|
||||||
config.addDefault("Properties.Enabled", true);
|
config.addDefault("Properties.Enabled", true);
|
||||||
config.addDefault("Properties.Default", "{victim} was slain by {attacker}'s {ability}");
|
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.RapidPunch", "{victim} took all the hits against {attacker}'s {ability}");
|
||||||
config.addDefault("Chi.ChiCombo", "{victim} was overwhelmed by {attacker}'s skill {ability}");
|
config.addDefault("Chi.ChiCombo", "{victim} was overwhelmed by {attacker}'s skill {ability}");
|
||||||
|
|
||||||
deathMsgConfig.saveConfig();
|
deathMsgConfig.save();
|
||||||
break;
|
break;
|
||||||
case DEFAULT:
|
case DEFAULT:
|
||||||
config = defaultConfig.getConfig();
|
config = defaultConfig.get();
|
||||||
|
|
||||||
ArrayList<String> earthbendable = new ArrayList<String>();
|
ArrayList<String> earthbendable = new ArrayList<String>();
|
||||||
earthbendable.add("STONE");
|
earthbendable.add("STONE");
|
||||||
|
@ -921,7 +919,7 @@ public class ConfigManager {
|
||||||
|
|
||||||
config.addDefault("debug", false);
|
config.addDefault("debug", false);
|
||||||
|
|
||||||
defaultConfig.saveConfig();
|
defaultConfig.save();
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in a new issue