2015-10-19 17:43:46 +00:00
|
|
|
package me.totalfreedom.totalfreedommod.config;
|
2013-08-17 22:07:57 +00:00
|
|
|
|
|
|
|
import java.io.File;
|
|
|
|
import java.io.FileNotFoundException;
|
|
|
|
import java.io.IOException;
|
|
|
|
import java.io.InputStream;
|
2014-07-20 01:02:00 +00:00
|
|
|
import java.io.InputStreamReader;
|
2013-08-17 22:07:57 +00:00
|
|
|
import java.util.EnumMap;
|
|
|
|
import java.util.List;
|
2015-10-19 17:43:46 +00:00
|
|
|
import me.totalfreedom.totalfreedommod.TotalFreedomMod;
|
2016-03-06 15:56:15 +00:00
|
|
|
import me.totalfreedom.totalfreedommod.util.FLog;
|
2014-11-29 19:16:00 +00:00
|
|
|
import org.apache.commons.io.FileUtils;
|
2013-08-17 22:07:57 +00:00
|
|
|
import org.bukkit.configuration.InvalidConfigurationException;
|
|
|
|
import org.bukkit.configuration.file.YamlConfiguration;
|
|
|
|
|
2015-10-19 17:43:46 +00:00
|
|
|
public class MainConfig
|
2013-08-17 22:07:57 +00:00
|
|
|
{
|
2015-11-22 18:26:47 +00:00
|
|
|
|
2014-08-27 08:58:26 +00:00
|
|
|
public static final File CONFIG_FILE = new File(TotalFreedomMod.plugin.getDataFolder(), TotalFreedomMod.CONFIG_FILENAME);
|
2013-08-17 22:07:57 +00:00
|
|
|
//
|
2015-10-19 17:43:46 +00:00
|
|
|
private static final EnumMap<ConfigEntry, Object> ENTRY_MAP;
|
2015-05-13 12:52:01 +00:00
|
|
|
private static final TFM_Defaults DEFAULTS;
|
2013-08-17 22:07:57 +00:00
|
|
|
|
2014-05-19 17:32:25 +00:00
|
|
|
static
|
2013-08-17 22:07:57 +00:00
|
|
|
{
|
2016-03-02 19:28:01 +00:00
|
|
|
ENTRY_MAP = new EnumMap<>(ConfigEntry.class);
|
2015-05-13 12:52:01 +00:00
|
|
|
|
|
|
|
TFM_Defaults tempDefaults = null;
|
2013-08-17 22:07:57 +00:00
|
|
|
try
|
|
|
|
{
|
|
|
|
try
|
|
|
|
{
|
|
|
|
InputStream defaultConfig = getDefaultConfig();
|
2015-05-13 12:52:01 +00:00
|
|
|
tempDefaults = new TFM_Defaults(defaultConfig);
|
2015-10-19 17:43:46 +00:00
|
|
|
for (ConfigEntry entry : ConfigEntry.values())
|
2013-08-17 22:07:57 +00:00
|
|
|
{
|
2015-05-13 12:52:01 +00:00
|
|
|
ENTRY_MAP.put(entry, tempDefaults.get(entry.getConfigName()));
|
2013-08-17 22:07:57 +00:00
|
|
|
}
|
|
|
|
defaultConfig.close();
|
|
|
|
}
|
|
|
|
catch (IOException ex)
|
|
|
|
{
|
2015-10-19 17:43:46 +00:00
|
|
|
FLog.severe(ex);
|
2013-08-17 22:07:57 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
copyDefaultConfig(CONFIG_FILE);
|
|
|
|
|
|
|
|
load();
|
|
|
|
}
|
|
|
|
catch (Exception ex)
|
|
|
|
{
|
2015-10-19 17:43:46 +00:00
|
|
|
FLog.severe(ex);
|
2013-08-17 22:07:57 +00:00
|
|
|
}
|
2015-05-13 12:52:01 +00:00
|
|
|
|
|
|
|
DEFAULTS = tempDefaults;
|
2013-08-17 22:07:57 +00:00
|
|
|
}
|
|
|
|
|
2015-10-19 17:43:46 +00:00
|
|
|
private MainConfig()
|
2014-05-19 17:32:25 +00:00
|
|
|
{
|
|
|
|
throw new AssertionError();
|
|
|
|
}
|
|
|
|
|
2014-08-27 08:58:26 +00:00
|
|
|
public static void load()
|
2013-08-17 22:07:57 +00:00
|
|
|
{
|
|
|
|
try
|
|
|
|
{
|
|
|
|
YamlConfiguration config = new YamlConfiguration();
|
|
|
|
|
|
|
|
config.load(CONFIG_FILE);
|
|
|
|
|
2015-10-19 17:43:46 +00:00
|
|
|
for (ConfigEntry entry : ConfigEntry.values())
|
2013-08-17 22:07:57 +00:00
|
|
|
{
|
|
|
|
String path = entry.getConfigName();
|
|
|
|
if (config.contains(path))
|
|
|
|
{
|
|
|
|
Object value = config.get(path);
|
|
|
|
if (value == null || entry.getType().isAssignableFrom(value.getClass()))
|
|
|
|
{
|
2014-05-19 17:32:25 +00:00
|
|
|
ENTRY_MAP.put(entry, value);
|
2013-08-17 22:07:57 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2015-11-22 18:26:47 +00:00
|
|
|
FLog.warning("Value for " + entry.getConfigName() + " is of type "
|
|
|
|
+ value.getClass().getSimpleName() + ". Needs to be " + entry.getType().getSimpleName() + ". Using default value.");
|
2013-08-17 22:07:57 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2015-10-19 17:43:46 +00:00
|
|
|
FLog.warning("Missing configuration entry " + entry.getConfigName() + ". Using default value.");
|
2013-08-17 22:07:57 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
catch (FileNotFoundException ex)
|
|
|
|
{
|
2015-10-19 17:43:46 +00:00
|
|
|
FLog.severe(ex);
|
2013-08-17 22:07:57 +00:00
|
|
|
}
|
|
|
|
catch (IOException ex)
|
|
|
|
{
|
2015-10-19 17:43:46 +00:00
|
|
|
FLog.severe(ex);
|
2013-08-17 22:07:57 +00:00
|
|
|
}
|
|
|
|
catch (InvalidConfigurationException ex)
|
|
|
|
{
|
2015-10-19 17:43:46 +00:00
|
|
|
FLog.severe(ex);
|
2013-08-17 22:07:57 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-10-19 17:43:46 +00:00
|
|
|
public static String getString(ConfigEntry entry)
|
2013-08-17 22:07:57 +00:00
|
|
|
{
|
|
|
|
try
|
|
|
|
{
|
|
|
|
return get(entry, String.class);
|
|
|
|
}
|
|
|
|
catch (IllegalArgumentException ex)
|
|
|
|
{
|
2015-10-19 17:43:46 +00:00
|
|
|
FLog.severe(ex);
|
2013-08-17 22:07:57 +00:00
|
|
|
}
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
2015-10-19 17:43:46 +00:00
|
|
|
public static void setString(ConfigEntry entry, String value)
|
2013-08-17 22:07:57 +00:00
|
|
|
{
|
|
|
|
try
|
|
|
|
{
|
|
|
|
set(entry, value, String.class);
|
|
|
|
}
|
|
|
|
catch (IllegalArgumentException ex)
|
|
|
|
{
|
2015-10-19 17:43:46 +00:00
|
|
|
FLog.severe(ex);
|
2013-08-17 22:07:57 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-10-19 17:43:46 +00:00
|
|
|
public static Double getDouble(ConfigEntry entry)
|
2013-08-17 22:07:57 +00:00
|
|
|
{
|
|
|
|
try
|
|
|
|
{
|
|
|
|
return get(entry, Double.class);
|
|
|
|
}
|
|
|
|
catch (IllegalArgumentException ex)
|
|
|
|
{
|
2015-10-19 17:43:46 +00:00
|
|
|
FLog.severe(ex);
|
2013-08-17 22:07:57 +00:00
|
|
|
}
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
2015-10-19 17:43:46 +00:00
|
|
|
public static void setDouble(ConfigEntry entry, Double value)
|
2013-08-17 22:07:57 +00:00
|
|
|
{
|
|
|
|
try
|
|
|
|
{
|
|
|
|
set(entry, value, Double.class);
|
|
|
|
}
|
|
|
|
catch (IllegalArgumentException ex)
|
|
|
|
{
|
2015-10-19 17:43:46 +00:00
|
|
|
FLog.severe(ex);
|
2013-08-17 22:07:57 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-10-19 17:43:46 +00:00
|
|
|
public static Boolean getBoolean(ConfigEntry entry)
|
2013-08-17 22:07:57 +00:00
|
|
|
{
|
|
|
|
try
|
|
|
|
{
|
|
|
|
return get(entry, Boolean.class);
|
|
|
|
}
|
|
|
|
catch (IllegalArgumentException ex)
|
|
|
|
{
|
2015-10-19 17:43:46 +00:00
|
|
|
FLog.severe(ex);
|
2013-08-17 22:07:57 +00:00
|
|
|
}
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
2015-10-19 17:43:46 +00:00
|
|
|
public static void setBoolean(ConfigEntry entry, Boolean value)
|
2013-08-17 22:07:57 +00:00
|
|
|
{
|
|
|
|
try
|
|
|
|
{
|
|
|
|
set(entry, value, Boolean.class);
|
|
|
|
}
|
|
|
|
catch (IllegalArgumentException ex)
|
|
|
|
{
|
2015-10-19 17:43:46 +00:00
|
|
|
FLog.severe(ex);
|
2013-08-17 22:07:57 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-10-19 17:43:46 +00:00
|
|
|
public static Integer getInteger(ConfigEntry entry)
|
2013-08-17 22:07:57 +00:00
|
|
|
{
|
|
|
|
try
|
|
|
|
{
|
|
|
|
return get(entry, Integer.class);
|
|
|
|
}
|
|
|
|
catch (IllegalArgumentException ex)
|
|
|
|
{
|
2015-10-19 17:43:46 +00:00
|
|
|
FLog.severe(ex);
|
2013-08-17 22:07:57 +00:00
|
|
|
}
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
2015-10-19 17:43:46 +00:00
|
|
|
public static void setInteger(ConfigEntry entry, Integer value)
|
2013-08-17 22:07:57 +00:00
|
|
|
{
|
|
|
|
try
|
|
|
|
{
|
|
|
|
set(entry, value, Integer.class);
|
|
|
|
}
|
|
|
|
catch (IllegalArgumentException ex)
|
|
|
|
{
|
2015-10-19 17:43:46 +00:00
|
|
|
FLog.severe(ex);
|
2013-08-17 22:07:57 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-10-19 17:43:46 +00:00
|
|
|
public static List getList(ConfigEntry entry)
|
2013-08-17 22:07:57 +00:00
|
|
|
{
|
|
|
|
try
|
|
|
|
{
|
|
|
|
return get(entry, List.class);
|
|
|
|
}
|
|
|
|
catch (IllegalArgumentException ex)
|
|
|
|
{
|
2015-10-19 17:43:46 +00:00
|
|
|
FLog.severe(ex);
|
2013-08-17 22:07:57 +00:00
|
|
|
}
|
|
|
|
return null;
|
|
|
|
}
|
2013-08-28 19:40:14 +00:00
|
|
|
|
2015-10-19 17:43:46 +00:00
|
|
|
public static <T> T get(ConfigEntry entry, Class<T> type) throws IllegalArgumentException
|
2013-08-17 22:07:57 +00:00
|
|
|
{
|
2014-05-19 17:32:25 +00:00
|
|
|
Object value = ENTRY_MAP.get(entry);
|
2013-08-17 22:07:57 +00:00
|
|
|
try
|
|
|
|
{
|
|
|
|
return type.cast(value);
|
|
|
|
}
|
|
|
|
catch (ClassCastException ex)
|
|
|
|
{
|
|
|
|
throw new IllegalArgumentException(entry.name() + " is not of type " + type.getSimpleName());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-10-19 17:43:46 +00:00
|
|
|
public static <T> void set(ConfigEntry entry, T value, Class<T> type) throws IllegalArgumentException
|
2013-08-17 22:07:57 +00:00
|
|
|
{
|
|
|
|
if (!type.isAssignableFrom(entry.getType()))
|
|
|
|
{
|
|
|
|
throw new IllegalArgumentException(entry.name() + " is not of type " + type.getSimpleName());
|
|
|
|
}
|
|
|
|
if (value != null && !type.isAssignableFrom(value.getClass()))
|
|
|
|
{
|
|
|
|
throw new IllegalArgumentException("Value is not of type " + type.getSimpleName());
|
|
|
|
}
|
2014-05-19 17:32:25 +00:00
|
|
|
ENTRY_MAP.put(entry, value);
|
2013-08-17 22:07:57 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
private static void copyDefaultConfig(File targetFile)
|
|
|
|
{
|
|
|
|
if (targetFile.exists())
|
|
|
|
{
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2015-10-19 17:43:46 +00:00
|
|
|
FLog.info("Installing default configuration file template: " + targetFile.getPath());
|
2013-08-17 22:07:57 +00:00
|
|
|
|
|
|
|
try
|
|
|
|
{
|
|
|
|
InputStream defaultConfig = getDefaultConfig();
|
2013-09-21 17:51:09 +00:00
|
|
|
FileUtils.copyInputStreamToFile(defaultConfig, targetFile);
|
2013-08-17 22:07:57 +00:00
|
|
|
defaultConfig.close();
|
|
|
|
}
|
|
|
|
catch (IOException ex)
|
|
|
|
{
|
2015-10-19 17:43:46 +00:00
|
|
|
FLog.severe(ex);
|
2013-08-17 22:07:57 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
private static InputStream getDefaultConfig()
|
|
|
|
{
|
2014-08-27 08:58:26 +00:00
|
|
|
return TotalFreedomMod.plugin.getResource(TotalFreedomMod.CONFIG_FILENAME);
|
2013-08-17 22:07:57 +00:00
|
|
|
}
|
|
|
|
|
2015-05-13 12:52:01 +00:00
|
|
|
public static TFM_Defaults getDefaults()
|
|
|
|
{
|
|
|
|
return DEFAULTS;
|
|
|
|
}
|
|
|
|
|
|
|
|
public static class TFM_Defaults
|
2013-08-17 22:07:57 +00:00
|
|
|
{
|
2015-11-22 18:26:47 +00:00
|
|
|
|
2013-08-17 22:07:57 +00:00
|
|
|
private YamlConfiguration defaults = null;
|
|
|
|
|
2015-05-13 12:52:01 +00:00
|
|
|
private TFM_Defaults(InputStream defaultConfig)
|
2013-08-17 22:07:57 +00:00
|
|
|
{
|
|
|
|
try
|
|
|
|
{
|
|
|
|
defaults = new YamlConfiguration();
|
2014-07-20 01:02:00 +00:00
|
|
|
final InputStreamReader isr = new InputStreamReader(defaultConfig);
|
|
|
|
defaults.load(isr);
|
|
|
|
isr.close();
|
2013-08-17 22:07:57 +00:00
|
|
|
}
|
|
|
|
catch (IOException ex)
|
|
|
|
{
|
2015-10-19 17:43:46 +00:00
|
|
|
FLog.severe(ex);
|
2013-08-17 22:07:57 +00:00
|
|
|
}
|
|
|
|
catch (InvalidConfigurationException ex)
|
|
|
|
{
|
2015-10-19 17:43:46 +00:00
|
|
|
FLog.severe(ex);
|
2013-08-17 22:07:57 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
public Object get(String path)
|
|
|
|
{
|
|
|
|
return defaults.get(path);
|
|
|
|
}
|
|
|
|
}
|
2015-11-22 18:26:47 +00:00
|
|
|
|
2013-08-17 22:07:57 +00:00
|
|
|
}
|