TFM-4.3-Reloaded/src/main/java/me/StevenLawson/TotalFreedomMod/config/MainConfig.java

310 lines
7.7 KiB
Java
Raw Normal View History

2022-03-20 12:35:43 +00:00
package me.StevenLawson.TotalFreedomMod.config;
2013-08-17 22:07:57 +00:00
2022-03-29 21:44:45 +00:00
import com.avaje.ebean.validation.NotNull;
import com.google.common.collect.ImmutableList;
2022-03-20 12:35:43 +00:00
import me.StevenLawson.TotalFreedomMod.Log;
import me.StevenLawson.TotalFreedomMod.TotalFreedomMod;
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;
2022-03-20 12:35:43 +00:00
import java.io.*;
import java.util.EnumMap;
import java.util.List;
//@Deprecated
public class MainConfig
2013-08-17 22:07:57 +00:00
{
public static final File CONFIG_FILE = new File(TotalFreedomMod.plugin.getDataFolder(), TotalFreedomMod.CONFIG_FILENAME);
2013-08-17 22:07:57 +00:00
//
2022-03-20 12:35:43 +00:00
private static final EnumMap<ConfigurationEntry, 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
static
2013-08-17 22:07:57 +00:00
{
2022-03-20 12:35:43 +00:00
ENTRY_MAP = new EnumMap<ConfigurationEntry, Object>(ConfigurationEntry.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);
2022-03-20 12:35:43 +00:00
for (ConfigurationEntry entry : ConfigurationEntry.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)
{
2022-03-20 12:35:43 +00:00
Log.severe(ex);
2013-08-17 22:07:57 +00:00
}
copyDefaultConfig(CONFIG_FILE);
load();
}
catch (Exception ex)
{
2022-03-20 12:35:43 +00:00
Log.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
}
2022-03-20 12:35:43 +00:00
private MainConfig()
{
throw new AssertionError();
}
public static void load()
2013-08-17 22:07:57 +00:00
{
try
{
YamlConfiguration config = new YamlConfiguration();
config.load(CONFIG_FILE);
2022-03-20 12:35:43 +00:00
for (ConfigurationEntry entry : ConfigurationEntry.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()))
{
ENTRY_MAP.put(entry, value);
2013-08-17 22:07:57 +00:00
}
else
{
2022-03-20 12:35:43 +00:00
Log.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
{
2022-03-20 12:35:43 +00:00
Log.warning("Missing configuration entry " + entry.getConfigName() + ". Using default value.");
2013-08-17 22:07:57 +00:00
}
}
}
catch (FileNotFoundException ex)
{
2022-03-20 12:35:43 +00:00
Log.severe(ex);
2013-08-17 22:07:57 +00:00
}
catch (IOException ex)
{
2022-03-20 12:35:43 +00:00
Log.severe(ex);
2013-08-17 22:07:57 +00:00
}
catch (InvalidConfigurationException ex)
{
2022-03-20 12:35:43 +00:00
Log.severe(ex);
2013-08-17 22:07:57 +00:00
}
}
2022-03-29 21:44:45 +00:00
@NotNull
2022-03-20 12:35:43 +00:00
public static String getString(ConfigurationEntry entry)
2013-08-17 22:07:57 +00:00
{
try
{
return get(entry, String.class);
}
catch (IllegalArgumentException ex)
{
2022-03-20 12:35:43 +00:00
Log.severe(ex);
2013-08-17 22:07:57 +00:00
}
2022-03-29 21:44:45 +00:00
return "";
2013-08-17 22:07:57 +00:00
}
2022-03-20 12:35:43 +00:00
public static void setString(ConfigurationEntry entry, String value)
2013-08-17 22:07:57 +00:00
{
try
{
set(entry, value, String.class);
}
catch (IllegalArgumentException ex)
{
2022-03-20 12:35:43 +00:00
Log.severe(ex);
2013-08-17 22:07:57 +00:00
}
}
2022-03-29 21:44:45 +00:00
@NotNull
2022-03-20 12:35:43 +00:00
public static Double getDouble(ConfigurationEntry entry)
2013-08-17 22:07:57 +00:00
{
try
{
return get(entry, Double.class);
}
catch (IllegalArgumentException ex)
{
2022-03-20 12:35:43 +00:00
Log.severe(ex);
2013-08-17 22:07:57 +00:00
}
2022-03-29 21:44:45 +00:00
return 0D;
2013-08-17 22:07:57 +00:00
}
2022-03-20 12:35:43 +00:00
public static void setDouble(ConfigurationEntry entry, Double value)
2013-08-17 22:07:57 +00:00
{
try
{
set(entry, value, Double.class);
}
catch (IllegalArgumentException ex)
{
2022-03-20 12:35:43 +00:00
Log.severe(ex);
2013-08-17 22:07:57 +00:00
}
}
2022-03-29 21:44:45 +00:00
@NotNull
2022-03-20 12:35:43 +00:00
public static Boolean getBoolean(ConfigurationEntry entry)
2013-08-17 22:07:57 +00:00
{
try
{
return get(entry, Boolean.class);
}
catch (IllegalArgumentException ex)
{
2022-03-20 12:35:43 +00:00
Log.severe(ex);
2013-08-17 22:07:57 +00:00
}
2022-03-29 21:44:45 +00:00
return false;
2013-08-17 22:07:57 +00:00
}
2022-03-20 12:35:43 +00:00
public static void setBoolean(ConfigurationEntry entry, Boolean value)
2013-08-17 22:07:57 +00:00
{
try
{
set(entry, value, Boolean.class);
}
catch (IllegalArgumentException ex)
{
2022-03-20 12:35:43 +00:00
Log.severe(ex);
2013-08-17 22:07:57 +00:00
}
}
2022-03-29 21:44:45 +00:00
@NotNull
2022-03-20 12:35:43 +00:00
public static Integer getInteger(ConfigurationEntry entry)
2013-08-17 22:07:57 +00:00
{
try
{
return get(entry, Integer.class);
}
catch (IllegalArgumentException ex)
{
2022-03-20 12:35:43 +00:00
Log.severe(ex);
2013-08-17 22:07:57 +00:00
}
2022-03-29 21:44:45 +00:00
return 0;
2013-08-17 22:07:57 +00:00
}
2022-03-20 12:35:43 +00:00
public static void setInteger(ConfigurationEntry entry, Integer value)
2013-08-17 22:07:57 +00:00
{
try
{
set(entry, value, Integer.class);
}
catch (IllegalArgumentException ex)
{
2022-03-20 12:35:43 +00:00
Log.severe(ex);
2013-08-17 22:07:57 +00:00
}
}
2022-03-29 21:44:45 +00:00
@NotNull
2022-03-20 12:35:43 +00:00
public static List getList(ConfigurationEntry entry)
2013-08-17 22:07:57 +00:00
{
try
{
return get(entry, List.class);
}
catch (IllegalArgumentException ex)
{
2022-03-20 12:35:43 +00:00
Log.severe(ex);
2013-08-17 22:07:57 +00:00
}
2022-03-29 21:44:45 +00:00
return ImmutableList.of();
2013-08-17 22:07:57 +00:00
}
2013-08-28 19:40:14 +00:00
2022-03-29 21:44:45 +00:00
@NotNull
2022-03-20 12:35:43 +00:00
public static <T> T get(ConfigurationEntry entry, Class<T> type) throws IllegalArgumentException
2013-08-17 22:07:57 +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());
}
}
2022-03-20 12:35:43 +00:00
public static <T> void set(ConfigurationEntry 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());
}
ENTRY_MAP.put(entry, value);
2013-08-17 22:07:57 +00:00
}
private static void copyDefaultConfig(File targetFile)
{
if (targetFile.exists())
{
return;
}
2022-03-20 12:35:43 +00:00
Log.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)
{
2022-03-20 12:35:43 +00:00
Log.severe(ex);
2013-08-17 22:07:57 +00:00
}
}
private static InputStream getDefaultConfig()
{
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
{
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();
final InputStreamReader isr = new InputStreamReader(defaultConfig);
defaults.load(isr);
isr.close();
2013-08-17 22:07:57 +00:00
}
catch (IOException ex)
{
2022-03-20 12:35:43 +00:00
Log.severe(ex);
2013-08-17 22:07:57 +00:00
}
catch (InvalidConfigurationException ex)
{
2022-03-20 12:35:43 +00:00
Log.severe(ex);
2013-08-17 22:07:57 +00:00
}
}
public Object get(String path)
{
return defaults.get(path);
}
}
}