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.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;
|
2016-05-12 19:40:39 +00:00
|
|
|
import net.pravian.aero.component.PluginComponent;
|
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;
|
|
|
|
|
2016-05-12 19:40:39 +00:00
|
|
|
public class MainConfig extends PluginComponent<TotalFreedomMod>
|
2013-08-17 22:07:57 +00:00
|
|
|
{
|
2015-11-22 18:26:47 +00:00
|
|
|
|
2016-05-12 19:40:39 +00:00
|
|
|
public static final String CONFIG_FILENAME = "config.yml";
|
2013-08-17 22:07:57 +00:00
|
|
|
//
|
2016-05-12 19:40:39 +00:00
|
|
|
private final EnumMap<ConfigEntry, Object> entries;
|
|
|
|
private final ConfigDefaults defaults;
|
2018-06-29 19:15:36 +00:00
|
|
|
public YamlConfiguration configuration;
|
2013-08-17 22:07:57 +00:00
|
|
|
|
2016-05-12 19:40:39 +00:00
|
|
|
public MainConfig(TotalFreedomMod plugin)
|
2013-08-17 22:07:57 +00:00
|
|
|
{
|
2016-05-12 19:40:39 +00:00
|
|
|
super(plugin);
|
2015-05-13 12:52:01 +00:00
|
|
|
|
2016-05-12 19:40:39 +00:00
|
|
|
entries = new EnumMap<>(ConfigEntry.class);
|
|
|
|
|
|
|
|
ConfigDefaults tempDefaults = null;
|
2013-08-17 22:07:57 +00:00
|
|
|
try
|
|
|
|
{
|
|
|
|
try
|
|
|
|
{
|
2016-05-12 19:40:39 +00:00
|
|
|
try (InputStream defaultConfig = getDefaultConfig())
|
2013-08-17 22:07:57 +00:00
|
|
|
{
|
2016-05-12 19:40:39 +00:00
|
|
|
tempDefaults = new ConfigDefaults(defaultConfig);
|
|
|
|
for (ConfigEntry entry : ConfigEntry.values())
|
|
|
|
{
|
|
|
|
entries.put(entry, tempDefaults.get(entry.getConfigName()));
|
|
|
|
}
|
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
|
|
|
}
|
|
|
|
|
2016-05-12 19:40:39 +00:00
|
|
|
copyDefaultConfig(getConfigFile());
|
2013-08-17 22:07:57 +00:00
|
|
|
|
|
|
|
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
|
|
|
|
2016-05-12 19:40:39 +00:00
|
|
|
defaults = tempDefaults;
|
2013-08-17 22:07:57 +00:00
|
|
|
}
|
|
|
|
|
2016-05-12 19:40:39 +00:00
|
|
|
public void load()
|
2013-08-17 22:07:57 +00:00
|
|
|
{
|
|
|
|
try
|
|
|
|
{
|
|
|
|
YamlConfiguration config = new YamlConfiguration();
|
|
|
|
|
2016-05-12 19:40:39 +00:00
|
|
|
config.load(getConfigFile());
|
2013-08-17 22:07:57 +00:00
|
|
|
|
2018-06-29 19:15:36 +00:00
|
|
|
configuration = config;
|
|
|
|
|
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()))
|
|
|
|
{
|
2016-05-12 19:40:39 +00:00
|
|
|
entries.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
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2016-05-12 19:40:39 +00:00
|
|
|
catch (IOException | InvalidConfigurationException ex)
|
2013-08-17 22:07:57 +00:00
|
|
|
{
|
2015-10-19 17:43:46 +00:00
|
|
|
FLog.severe(ex);
|
2013-08-17 22:07:57 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-05-12 19:40:39 +00:00
|
|
|
private File getConfigFile()
|
|
|
|
{
|
|
|
|
return new File(plugin.getDataFolder(), "config.yml");
|
|
|
|
}
|
|
|
|
|
|
|
|
public 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;
|
|
|
|
}
|
|
|
|
|
2016-05-12 19:40:39 +00:00
|
|
|
public 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
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-05-12 19:40:39 +00:00
|
|
|
public 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;
|
|
|
|
}
|
|
|
|
|
2016-05-12 19:40:39 +00:00
|
|
|
public 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
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-05-12 19:40:39 +00:00
|
|
|
public 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;
|
|
|
|
}
|
|
|
|
|
2016-05-12 19:40:39 +00:00
|
|
|
public 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
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-05-12 19:40:39 +00:00
|
|
|
public 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;
|
|
|
|
}
|
|
|
|
|
2016-05-12 19:40:39 +00:00
|
|
|
public 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
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-05-12 19:40:39 +00:00
|
|
|
public 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
|
|
|
|
2016-05-12 19:40:39 +00:00
|
|
|
public <T> T get(ConfigEntry entry, Class<T> type) throws IllegalArgumentException
|
2013-08-17 22:07:57 +00:00
|
|
|
{
|
2016-05-12 19:40:39 +00:00
|
|
|
Object value = entries.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());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-05-12 19:40:39 +00:00
|
|
|
public <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());
|
|
|
|
}
|
2016-05-12 19:40:39 +00:00
|
|
|
entries.put(entry, value);
|
2013-08-17 22:07:57 +00:00
|
|
|
}
|
|
|
|
|
2016-05-12 19:40:39 +00:00
|
|
|
private void copyDefaultConfig(File targetFile)
|
2013-08-17 22:07:57 +00:00
|
|
|
{
|
|
|
|
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
|
|
|
|
{
|
2016-05-12 19:40:39 +00:00
|
|
|
try (InputStream defaultConfig = getDefaultConfig())
|
|
|
|
{
|
|
|
|
FileUtils.copyInputStreamToFile(defaultConfig, targetFile);
|
|
|
|
}
|
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
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-05-12 19:40:39 +00:00
|
|
|
private InputStream getDefaultConfig()
|
2013-08-17 22:07:57 +00:00
|
|
|
{
|
2016-05-12 19:40:39 +00:00
|
|
|
return plugin.getResource(TotalFreedomMod.CONFIG_FILENAME);
|
2013-08-17 22:07:57 +00:00
|
|
|
}
|
|
|
|
|
2016-05-12 19:40:39 +00:00
|
|
|
public ConfigDefaults getDefaults()
|
2015-05-13 12:52:01 +00:00
|
|
|
{
|
2016-05-12 19:40:39 +00:00
|
|
|
return defaults;
|
2015-05-13 12:52:01 +00:00
|
|
|
}
|
|
|
|
|
2016-05-12 19:40:39 +00:00
|
|
|
public static class ConfigDefaults
|
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;
|
|
|
|
|
2016-05-12 19:40:39 +00:00
|
|
|
private ConfigDefaults(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
|
|
|
}
|