TFM-4.3-Reloaded/src/main/java/me/totalfreedom/totalfreedommod/config/MainConfig.java
Jerom van der Sar aca3398d21 TotalFreedomMod Electrum
Version 5.0

This TotalFreedomMod release implements many changes. Most notably, the
internals have been completely revamped. TotalFreedomMod now relies on the
Aero library for core mechanics such as command handling and services.

Another important change is the UUID system. In TotalFreedomMod Electrum,
it has been completely removed. The core reason for this is that the
system as a whole was very bugged. Additionally, it did not solve the
primary reason for its conception: preserving player data when the player
changes their username. This is because TotalFreedomMod servers usually
run in offline-mode. This meaning that some of the players joining do not
have a registerd Mojang UUID whatsoever. All in all, the UUID system was
buggy, and it did not fix the reason it was implemented, so it has been
completely removed. The admin list and the ban list now use usernames and
IPs again.

Lastly, many smaller changes have been implemented. Due to the amount of
changes, they have not been named individualy. Please refer to the issues
below for more details.

Fixes #342
Fixes #350
Fixes #380
Fixes #684
Fixes #704
Fixes #716
Fixes #735
Fixes #745
Fixes #784
Fixes #765
Fixes #791
Fixes #805
Fixes #826
Fixes #883
Fixes #1524
Fixes #1534
Fixes #1536
Fixes #1538
Fixes #1545
Fixes #1546
Fixes #1568
Fixes #1627
Resolves #403
Resolves #435
Resolves #597
Resolves #603
Resolves #628
Resolves #690
Resolves #708
Resolves #747
Resolves #748
Resolves #749
Resolves #764
Resolves #767
Resolves #782
Resolves #809
Resolves #803
Resolves #811
Resolves #813
Resolves #830
Resolves #848
Resolves #856
Resolves #876
Resolves #908
Resolves #992
Resolves #1018
Resolves #1432
Resolves #1446
Resolves #1494
Resolves #1501
Resolves #1526
Resolves #1540
Resolves #1550
Resolves #1560
Resolves #1561
Resolves #1578
Resolves #1613
2016-05-12 21:51:58 +02:00

305 lines
7.4 KiB
Java

package me.totalfreedom.totalfreedommod.config;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.EnumMap;
import java.util.List;
import me.totalfreedom.totalfreedommod.TotalFreedomMod;
import me.totalfreedom.totalfreedommod.util.FLog;
import net.pravian.aero.component.PluginComponent;
import org.apache.commons.io.FileUtils;
import org.bukkit.configuration.InvalidConfigurationException;
import org.bukkit.configuration.file.YamlConfiguration;
public class MainConfig extends PluginComponent<TotalFreedomMod>
{
public static final String CONFIG_FILENAME = "config.yml";
//
private final EnumMap<ConfigEntry, Object> entries;
private final ConfigDefaults defaults;
public MainConfig(TotalFreedomMod plugin)
{
super(plugin);
entries = new EnumMap<>(ConfigEntry.class);
ConfigDefaults tempDefaults = null;
try
{
try
{
try (InputStream defaultConfig = getDefaultConfig())
{
tempDefaults = new ConfigDefaults(defaultConfig);
for (ConfigEntry entry : ConfigEntry.values())
{
entries.put(entry, tempDefaults.get(entry.getConfigName()));
}
}
}
catch (IOException ex)
{
FLog.severe(ex);
}
copyDefaultConfig(getConfigFile());
load();
}
catch (Exception ex)
{
FLog.severe(ex);
}
defaults = tempDefaults;
}
public void load()
{
try
{
YamlConfiguration config = new YamlConfiguration();
config.load(getConfigFile());
for (ConfigEntry entry : ConfigEntry.values())
{
String path = entry.getConfigName();
if (config.contains(path))
{
Object value = config.get(path);
if (value == null || entry.getType().isAssignableFrom(value.getClass()))
{
entries.put(entry, value);
}
else
{
FLog.warning("Value for " + entry.getConfigName() + " is of type "
+ value.getClass().getSimpleName() + ". Needs to be " + entry.getType().getSimpleName() + ". Using default value.");
}
}
else
{
FLog.warning("Missing configuration entry " + entry.getConfigName() + ". Using default value.");
}
}
}
catch (IOException | InvalidConfigurationException ex)
{
FLog.severe(ex);
}
}
private File getConfigFile()
{
return new File(plugin.getDataFolder(), "config.yml");
}
public String getString(ConfigEntry entry)
{
try
{
return get(entry, String.class);
}
catch (IllegalArgumentException ex)
{
FLog.severe(ex);
}
return null;
}
public void setString(ConfigEntry entry, String value)
{
try
{
set(entry, value, String.class);
}
catch (IllegalArgumentException ex)
{
FLog.severe(ex);
}
}
public Double getDouble(ConfigEntry entry)
{
try
{
return get(entry, Double.class);
}
catch (IllegalArgumentException ex)
{
FLog.severe(ex);
}
return null;
}
public void setDouble(ConfigEntry entry, Double value)
{
try
{
set(entry, value, Double.class);
}
catch (IllegalArgumentException ex)
{
FLog.severe(ex);
}
}
public Boolean getBoolean(ConfigEntry entry)
{
try
{
return get(entry, Boolean.class);
}
catch (IllegalArgumentException ex)
{
FLog.severe(ex);
}
return null;
}
public void setBoolean(ConfigEntry entry, Boolean value)
{
try
{
set(entry, value, Boolean.class);
}
catch (IllegalArgumentException ex)
{
FLog.severe(ex);
}
}
public Integer getInteger(ConfigEntry entry)
{
try
{
return get(entry, Integer.class);
}
catch (IllegalArgumentException ex)
{
FLog.severe(ex);
}
return null;
}
public void setInteger(ConfigEntry entry, Integer value)
{
try
{
set(entry, value, Integer.class);
}
catch (IllegalArgumentException ex)
{
FLog.severe(ex);
}
}
public List getList(ConfigEntry entry)
{
try
{
return get(entry, List.class);
}
catch (IllegalArgumentException ex)
{
FLog.severe(ex);
}
return null;
}
public <T> T get(ConfigEntry entry, Class<T> type) throws IllegalArgumentException
{
Object value = entries.get(entry);
try
{
return type.cast(value);
}
catch (ClassCastException ex)
{
throw new IllegalArgumentException(entry.name() + " is not of type " + type.getSimpleName());
}
}
public <T> void set(ConfigEntry entry, T value, Class<T> type) throws IllegalArgumentException
{
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());
}
entries.put(entry, value);
}
private void copyDefaultConfig(File targetFile)
{
if (targetFile.exists())
{
return;
}
FLog.info("Installing default configuration file template: " + targetFile.getPath());
try
{
try (InputStream defaultConfig = getDefaultConfig())
{
FileUtils.copyInputStreamToFile(defaultConfig, targetFile);
}
}
catch (IOException ex)
{
FLog.severe(ex);
}
}
private InputStream getDefaultConfig()
{
return plugin.getResource(TotalFreedomMod.CONFIG_FILENAME);
}
public ConfigDefaults getDefaults()
{
return defaults;
}
public static class ConfigDefaults
{
private YamlConfiguration defaults = null;
private ConfigDefaults(InputStream defaultConfig)
{
try
{
defaults = new YamlConfiguration();
final InputStreamReader isr = new InputStreamReader(defaultConfig);
defaults.load(isr);
isr.close();
}
catch (IOException ex)
{
FLog.severe(ex);
}
catch (InvalidConfigurationException ex)
{
FLog.severe(ex);
}
}
public Object get(String path)
{
return defaults.get(path);
}
}
}