Mavenized project

This commit is contained in:
JeromSar
2015-11-18 21:41:51 +01:00
parent 0c3bc40b03
commit 89a317b7df
207 changed files with 247 additions and 1569 deletions

View File

@ -0,0 +1,34 @@
package me.totalfreedom.totalfreedommod.config;
import java.io.File;
import java.util.UUID;
import me.totalfreedom.totalfreedommod.TotalFreedomMod;
public class ConfigConverter
{
public static void convert()
{
for (File file : new File(TotalFreedomMod.plugin.getDataFolder(), "players").listFiles())
{
if (file.isDirectory())
{
continue;
}
final UUID uuid;
try
{
uuid = UUID.fromString(file.getName().split(".")[0]);
}
catch (IllegalArgumentException ex)
{
continue;
}
}
}
}

View File

@ -0,0 +1,169 @@
package me.totalfreedom.totalfreedommod.config;
import java.util.List;
public enum ConfigEntry
{
FORCE_IP_ENABLED(Boolean.class, "forceip.enabled"),
FORCE_IP_PORT(Integer.class, "forceip.port"),
FORCE_IP_KICKMSG(String.class, "forceip.kickmsg"),
//
ALLOW_EXPLOSIONS(Boolean.class, "allow.explosions"),
ALLOW_FIRE_PLACE(Boolean.class, "allow.fire_place"),
ALLOW_FIRE_SPREAD(Boolean.class, "allow.fire_spread"),
ALLOW_FLUID_SPREAD(Boolean.class, "allow.fluid_spread"),
ALLOW_LAVA_DAMAGE(Boolean.class, "allow.lava_damage"),
ALLOW_LAVA_PLACE(Boolean.class, "allow.lava_place"),
ALLOW_TNT_MINECARTS(Boolean.class, "allow.tnt_minecarts"),
ALLOW_WATER_PLACE(Boolean.class, "allow.water_place"),
//
MOB_LIMITER_ENABLED(Boolean.class, "moblimiter.enabled"),
MOB_LIMITER_MAX(Integer.class, "moblimiter.max"),
MOB_LIMITER_DISABLE_DRAGON(Boolean.class, "moblimiter.disable.dragon"),
MOB_LIMITER_DISABLE_GHAST(Boolean.class, "moblimiter.disable.ghast"),
MOB_LIMITER_DISABLE_GIANT(Boolean.class, "moblimiter.disable.giant"),
MOB_LIMITER_DISABLE_SLIME(Boolean.class, "moblimiter.disable.slime"),
//
HTTPD_ENABLED(Boolean.class, "httpd.enabled"),
HTTPD_PORT(Integer.class, "httpd.port"),
HTTPD_PUBLIC_FOLDER(String.class, "httpd.public_folder"),
//
SERVER_COLORFUL_MOTD(Boolean.class, "server.colorful_motd"),
SERVER_NAME(String.class, "server.name"),
SERVER_ADDRESS(String.class, "server.address"),
SERVER_MOTD(String.class, "server.motd"),
SERVER_OWNERS(List.class, "server.owners"),
SERVER_BAN_URL(String.class, "server.ban_url"),
SERVER_PERMBAN_URL(String.class, "server.permban_url"),
//
TWITTERBOT_ENABLED(Boolean.class, "twitterbot.enabled"),
TWITTERBOT_SECRET(String.class, "twitterbot.secret"),
TWITTERBOT_URL(String.class, "twitterbot.url"),
//
DISABLE_NIGHT(Boolean.class, "disable.night"),
DISABLE_WEATHER(Boolean.class, "disable.weather"),
//
ENABLE_PREPROCESS_LOG(Boolean.class, "preprocess_log"),
ENABLE_PET_PROTECT(Boolean.class, "petprotect.enabled"),
//
LANDMINES_ENABLED(Boolean.class, "landmines_enabled"),
TOSSMOB_ENABLED(Boolean.class, "tossmob_enabled"),
AUTOKICK_ENABLED(Boolean.class, "autokick.enabled"),
MP44_ENABLED(Boolean.class, "mp44_enabled"),
//
PROTECTAREA_ENABLED(Boolean.class, "protectarea.enabled"),
PROTECTAREA_SPAWNPOINTS(Boolean.class, "protectarea.auto_protect_spawnpoints"),
PROTECTAREA_RADIUS(Double.class, "protectarea.auto_protect_radius"),
//
NUKE_MONITOR_ENABLED(Boolean.class, "nukemonitor.enabled"),
NUKE_MONITOR_COUNT_BREAK(Integer.class, "nukemonitor.count_break"),
NUKE_MONITOR_COUNT_PLACE(Integer.class, "nukemonitor.count_place"),
NUKE_MONITOR_RANGE(Double.class, "nukemonitor.range"),
//
AUTOKICK_THRESHOLD(Double.class, "autokick.threshold"),
AUTOKICK_TIME(Integer.class, "autokick.time"),
//
LOGS_SECRET(String.class, "logs.secret"),
LOGS_URL(String.class, "logs.url"),
//
FLATLANDS_GENERATE(Boolean.class, "flatlands.generate"),
FLATLANDS_GENERATE_PARAMS(String.class, "flatlands.generate_params"),
//
ANNOUNCER_ENABLED(Boolean.class, "announcer.enabled"),
ANNOUNCER_INTERVAL(Integer.class, "announcer.interval"),
ANNOUNCER_PREFIX(String.class, "announcer.prefix"),
ANNOUNCER_ANNOUNCEMENTS(List.class, "announcer.announcements"),
//
EXPLOSIVE_RADIUS(Double.class, "explosive_radius"),
FREECAM_TRIGGER_COUNT(Integer.class, "freecam_trigger_count"),
SERVICE_CHECKER_URL(String.class, "service_checker_url"),
BLOCKED_COMMANDS(List.class, "blocked_commands"),
HOST_SENDER_NAMES(List.class, "host_sender_names"),
UNBANNABLE_USERNAMES(List.class, "unbannable_usernames"),
OVERLORD_IPS(List.class, "overlord_ips"),
NOADMIN_IPS(List.class, "noadmin_ips"),
ADMIN_ONLY_MODE(Boolean.class, "admin_only_mode"),
AUTO_ENTITY_WIPE(Boolean.class, "auto_wipe"),
CONSOLE_IS_SENIOR(Boolean.class, "console_is_senior");
//
private final Class<?> type;
private final String configName;
private ConfigEntry(Class<?> type, String configName)
{
this.type = type;
this.configName = configName;
}
public Class<?> getType()
{
return type;
}
public String getConfigName()
{
return configName;
}
public String getString()
{
return MainConfig.getString(this);
}
public String setString(String value)
{
MainConfig.setString(this, value);
return value;
}
public Double getDouble()
{
return MainConfig.getDouble(this);
}
public Double setDouble(Double value)
{
MainConfig.setDouble(this, value);
return value;
}
public Boolean getBoolean()
{
return MainConfig.getBoolean(this);
}
public Boolean setBoolean(Boolean value)
{
MainConfig.setBoolean(this, value);
return value;
}
public Integer getInteger()
{
return MainConfig.getInteger(this);
}
public Integer setInteger(Integer value)
{
MainConfig.setInteger(this, value);
return value;
}
public List<?> getList()
{
return MainConfig.getList(this);
}
public static ConfigEntry findConfigEntry(String name)
{
name = name.toLowerCase().replace("_", "");
for (ConfigEntry entry : values())
{
if (entry.toString().toLowerCase().replace("_", "").equals(name))
{
return entry;
}
}
return null;
}
}

View File

@ -0,0 +1,170 @@
package me.totalfreedom.totalfreedommod.config;
import java.io.File;
import java.io.IOException;
import java.io.InputStreamReader;
import me.totalfreedom.totalfreedommod.util.FUtil;
import org.apache.commons.lang3.exception.ExceptionUtils;
import org.bukkit.configuration.InvalidConfigurationException;
import org.bukkit.configuration.file.YamlConfiguration;
import org.bukkit.plugin.Plugin;
/**
* Represents a definable YAML configuration.
*
* @see YamlConfiguration
*/
public class FConfig extends YamlConfiguration // BukkitLib @ https://github.com/Pravian/BukkitLib
{
private final Plugin plugin;
private final File configFile;
private final boolean copyDefaults;
/**
* Creates a new YamlConfig instance.
*
* <p>Example:
* <pre>
* YamlConfig config = new YamlConfig(this, "config.yml", true);
* config.load();
* </pre></p>
*
* @param plugin The plugin to which the config belongs.
* @param fileName The filename of the config file.
* @param copyDefaults If the defaults should be copied and/loaded from a config in the plugin jar-file.
*/
public FConfig(Plugin plugin, String fileName, boolean copyDefaults)
{
this(plugin, FUtil.getPluginFile(plugin, fileName), copyDefaults);
}
/**
* Creates a new YamlConfig instance.
*
* <p>Example:
* <pre>
* YamlConfig config = new YamlConfig(this, new File(plugin.getDataFolder() + "/players", "Prozza.yml"), false);
* config.load();
* </pre></p>
*
* @param plugin The plugin to which the config belongs.
* @param file The file of the config file.
* @param copyDefaults If the defaults should be copied and/loaded from a config in the plugin jar-file.
*/
public FConfig(Plugin plugin, File file, boolean copyDefaults)
{
this.plugin = plugin;
this.configFile = file;
this.copyDefaults = copyDefaults;
}
/**
* Validates if the configuration exists.
*
* @return True if the configuration exists.
*/
public boolean exists()
{
return configFile.exists();
}
/**
* Saves the configuration to the predefined file.
*
* @see #YamlConfig(Plugin, String, boolean)
*/
public void save()
{
try
{
super.save(configFile);
}
catch (Exception ex)
{
plugin.getLogger().severe("Could not save configuration file: " + configFile.getName());
plugin.getLogger().severe(ExceptionUtils.getStackTrace(ex));
}
}
/**
* Loads the configuration from the predefined file.
*
* <p>Optionally, if loadDefaults has been set to true, the file will be copied over from the default inside the jar-file of the owning plugin.</p>
*
* @see #YamlConfig(Plugin, String, boolean)
*/
public void load()
{
try
{
if (copyDefaults)
{
if (!configFile.exists())
{
configFile.getParentFile().mkdirs();
try
{
FUtil.copy(plugin.getResource(configFile.getName()), configFile);
}
catch (IOException ex)
{
plugin.getLogger().severe("Could not write default configuration file: " + configFile.getName());
plugin.getLogger().severe(ExceptionUtils.getStackTrace(ex));
}
plugin.getLogger().info("Installed default configuration " + configFile.getName());
}
super.addDefaults(getDefaultConfig());
}
if (configFile.exists())
{
super.load(configFile);
}
}
catch (Exception ex)
{
plugin.getLogger().severe("Could not load configuration file: " + configFile.getName());
plugin.getLogger().severe(ExceptionUtils.getStackTrace(ex));
}
}
/**
* Returns the raw YamlConfiguration this config is based on.
*
* @return The YamlConfiguration.
* @see YamlConfiguration
*/
public YamlConfiguration getConfig()
{
return this;
}
/**
* Returns the default configuration as been stored in the jar-file of the owning plugin.
* @return The default configuration.
*/
public YamlConfiguration getDefaultConfig()
{
final YamlConfiguration DEFAULT_CONFIG = new YamlConfiguration();
try
{
final InputStreamReader isr = new InputStreamReader(plugin.getResource(configFile.getName()));
DEFAULT_CONFIG.load(isr);
isr.close();
}
catch (IOException ex)
{
plugin.getLogger().severe("Could not load default configuration: " + configFile.getName());
plugin.getLogger().severe(ExceptionUtils.getStackTrace(ex));
return null;
}
catch (InvalidConfigurationException ex)
{
plugin.getLogger().severe("Could not load default configuration: " + configFile.getName());
plugin.getLogger().severe(ExceptionUtils.getStackTrace(ex));
return null;
}
return DEFAULT_CONFIG;
}
}

View File

@ -0,0 +1,303 @@
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.util.FLog;
import me.totalfreedom.totalfreedommod.TotalFreedomMod;
import org.apache.commons.io.FileUtils;
import org.bukkit.configuration.InvalidConfigurationException;
import org.bukkit.configuration.file.YamlConfiguration;
public class MainConfig
{
public static final File CONFIG_FILE = new File(TotalFreedomMod.plugin.getDataFolder(), TotalFreedomMod.CONFIG_FILENAME);
//
private static final EnumMap<ConfigEntry, Object> ENTRY_MAP;
private static final TFM_Defaults DEFAULTS;
static
{
ENTRY_MAP = new EnumMap<ConfigEntry, Object>(ConfigEntry.class);
TFM_Defaults tempDefaults = null;
try
{
try
{
InputStream defaultConfig = getDefaultConfig();
tempDefaults = new TFM_Defaults(defaultConfig);
for (ConfigEntry entry : ConfigEntry.values())
{
ENTRY_MAP.put(entry, tempDefaults.get(entry.getConfigName()));
}
defaultConfig.close();
}
catch (IOException ex)
{
FLog.severe(ex);
}
copyDefaultConfig(CONFIG_FILE);
load();
}
catch (Exception ex)
{
FLog.severe(ex);
}
DEFAULTS = tempDefaults;
}
private MainConfig()
{
throw new AssertionError();
}
public static void load()
{
try
{
YamlConfiguration config = new YamlConfiguration();
config.load(CONFIG_FILE);
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()))
{
ENTRY_MAP.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 (FileNotFoundException ex)
{
FLog.severe(ex);
}
catch (IOException ex)
{
FLog.severe(ex);
}
catch (InvalidConfigurationException ex)
{
FLog.severe(ex);
}
}
public static String getString(ConfigEntry entry)
{
try
{
return get(entry, String.class);
}
catch (IllegalArgumentException ex)
{
FLog.severe(ex);
}
return null;
}
public static void setString(ConfigEntry entry, String value)
{
try
{
set(entry, value, String.class);
}
catch (IllegalArgumentException ex)
{
FLog.severe(ex);
}
}
public static Double getDouble(ConfigEntry entry)
{
try
{
return get(entry, Double.class);
}
catch (IllegalArgumentException ex)
{
FLog.severe(ex);
}
return null;
}
public static void setDouble(ConfigEntry entry, Double value)
{
try
{
set(entry, value, Double.class);
}
catch (IllegalArgumentException ex)
{
FLog.severe(ex);
}
}
public static Boolean getBoolean(ConfigEntry entry)
{
try
{
return get(entry, Boolean.class);
}
catch (IllegalArgumentException ex)
{
FLog.severe(ex);
}
return null;
}
public static void setBoolean(ConfigEntry entry, Boolean value)
{
try
{
set(entry, value, Boolean.class);
}
catch (IllegalArgumentException ex)
{
FLog.severe(ex);
}
}
public static Integer getInteger(ConfigEntry entry)
{
try
{
return get(entry, Integer.class);
}
catch (IllegalArgumentException ex)
{
FLog.severe(ex);
}
return null;
}
public static void setInteger(ConfigEntry entry, Integer value)
{
try
{
set(entry, value, Integer.class);
}
catch (IllegalArgumentException ex)
{
FLog.severe(ex);
}
}
public static List getList(ConfigEntry entry)
{
try
{
return get(entry, List.class);
}
catch (IllegalArgumentException ex)
{
FLog.severe(ex);
}
return null;
}
public static <T> T get(ConfigEntry entry, Class<T> type) throws IllegalArgumentException
{
Object value = ENTRY_MAP.get(entry);
try
{
return type.cast(value);
}
catch (ClassCastException ex)
{
throw new IllegalArgumentException(entry.name() + " is not of type " + type.getSimpleName());
}
}
public static <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());
}
ENTRY_MAP.put(entry, value);
}
private static void copyDefaultConfig(File targetFile)
{
if (targetFile.exists())
{
return;
}
FLog.info("Installing default configuration file template: " + targetFile.getPath());
try
{
InputStream defaultConfig = getDefaultConfig();
FileUtils.copyInputStreamToFile(defaultConfig, targetFile);
defaultConfig.close();
}
catch (IOException ex)
{
FLog.severe(ex);
}
}
private static InputStream getDefaultConfig()
{
return TotalFreedomMod.plugin.getResource(TotalFreedomMod.CONFIG_FILENAME);
}
public static TFM_Defaults getDefaults()
{
return DEFAULTS;
}
public static class TFM_Defaults
{
private YamlConfiguration defaults = null;
private TFM_Defaults(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);
}
}
}