Batch format

This commit is contained in:
unknown 2014-03-18 16:29:55 +01:00
parent a8a429817c
commit b90f0fbd9f
4 changed files with 111 additions and 77 deletions

View file

@ -9,40 +9,40 @@ public class BT_Config
{ {
private YamlConfig config = null; private YamlConfig config = null;
private final SimpleConfigEntries configEntries; private final SimpleConfigEntries configEntries;
private BT_Config() private BT_Config()
{ {
configEntries = new SimpleConfigEntries(); configEntries = new SimpleConfigEntries();
} }
public void loadConfig() public void loadConfig()
{ {
if (config == null) if (config == null)
{ {
config = new YamlConfig(BukkitTelnet.plugin, "config.yml", true); config = new YamlConfig(BukkitTelnet.plugin, "config.yml", true);
} }
config.load(); config.load();
configEntries.setAddress(config.getString("address")); configEntries.setAddress(config.getString("address"));
configEntries.setPort(config.getInt("port")); configEntries.setPort(config.getInt("port"));
configEntries.setPassword(config.getString("password")); configEntries.setPassword(config.getString("password"));
configEntries.getAdmins().clear(); configEntries.getAdmins().clear();
if (config.isConfigurationSection("admins")) if (config.isConfigurationSection("admins"))
{ {
for (String admin : config.getConfigurationSection("admins").getKeys(false)) for (String admin : config.getConfigurationSection("admins").getKeys(false))
{ {
if (!config.isList("admins." + admin)) if (!config.isList("admins." + admin))
{ {
continue; continue;
} }
configEntries.getAdmins().put(admin, config.getStringList("admins." + admin)); configEntries.getAdmins().put(admin, config.getStringList("admins." + admin));
} }
} }
if (configEntries.getPassword().equals("")) if (configEntries.getPassword().equals(""))
{ {
configEntries.setPassword(config.getDefaultConfig().getString("password")); configEntries.setPassword(config.getDefaultConfig().getString("password"));
@ -50,65 +50,65 @@ public class BT_Config
BT_Log.warning("Defaulting to " + configEntries.getPassword()); BT_Log.warning("Defaulting to " + configEntries.getPassword());
} }
} }
public SimpleConfigEntries getConfigEntries() public SimpleConfigEntries getConfigEntries()
{ {
return configEntries; return configEntries;
} }
public static final class SimpleConfigEntries public static final class SimpleConfigEntries
{ {
private int port; private int port;
private String address; private String address;
private String password; private String password;
private final Map<String, List<String>> admins; private final Map<String, List<String>> admins;
private SimpleConfigEntries() private SimpleConfigEntries()
{ {
admins = new HashMap<String, List<String>>(); admins = new HashMap<String, List<String>>();
} }
public int getPort() public int getPort()
{ {
return port; return port;
} }
public void setPort(int port) public void setPort(int port)
{ {
this.port = port; this.port = port;
} }
public String getAddress() public String getAddress()
{ {
return address; return address;
} }
public void setAddress(String address) public void setAddress(String address)
{ {
this.address = address; this.address = address;
} }
public String getPassword() public String getPassword()
{ {
return password; return password;
} }
public void setPassword(String password) public void setPassword(String password)
{ {
this.password = password; this.password = password;
} }
public Map<String, List<String>> getAdmins() public Map<String, List<String>> getAdmins()
{ {
return admins; return admins;
} }
} }
public static BT_Config getInstance() public static BT_Config getInstance()
{ {
return BT_ConfigHolder.INSTANCE; return BT_ConfigHolder.INSTANCE;
} }
private static class BT_ConfigHolder private static class BT_ConfigHolder
{ {
private static final BT_Config INSTANCE = new BT_Config(); private static final BT_Config INSTANCE = new BT_Config();

View file

@ -13,7 +13,7 @@ public class BukkitTelnet extends JavaPlugin
{ {
plugin = this; plugin = this;
server = plugin.getServer(); server = plugin.getServer();
BT_Log.setPluginLogger(plugin.getLogger()); BT_Log.setPluginLogger(plugin.getLogger());
BT_Log.setServerLogger(server.getLogger()); BT_Log.setServerLogger(server.getLogger());
} }

View file

@ -17,17 +17,18 @@ import org.bukkit.plugin.Plugin;
/** /**
* Represents all File-related utilities. * Represents all File-related utilities.
*/ */
public class FileUtils { public class FileUtils
{
/** /**
* Downloads a file from the specified URIL and saves it at the specified location. * Downloads a file from the specified URIL and saves it at the specified location.
* *
* @param url The URL from where to download the file from. * @param url The URL from where to download the file from.
* @param output The file where the file will be stored. * @param output The file where the file will be stored.
* @throws MalformedURLException * @throws MalformedURLException
* @throws IOException * @throws IOException
*/ */
public static void downloadFile(String url, File output) throws MalformedURLException, IOException { public static void downloadFile(String url, File output) throws MalformedURLException, IOException
{
final URL website = new URL(url); final URL website = new URL(url);
final ReadableByteChannel rbc = Channels.newChannel(website.openStream()); final ReadableByteChannel rbc = Channels.newChannel(website.openStream());
final FileOutputStream fos = new FileOutputStream(output); final FileOutputStream fos = new FileOutputStream(output);
@ -37,13 +38,15 @@ public class FileUtils {
/** /**
* Saves a raw Object to a file. * Saves a raw Object to a file.
* *
* @param object The object to save. * @param object The object to save.
* @param file The file where the object will be stored. * @param file The file where the object will be stored.
* @throws IOException * @throws IOException
*/ */
public static void saveObject(Object object, File file) throws IOException { public static void saveObject(Object object, File file) throws IOException
if (!file.exists()) { {
if (!file.exists())
{
file.getParentFile().mkdirs(); file.getParentFile().mkdirs();
} }
@ -54,12 +57,14 @@ public class FileUtils {
/** /**
* Attempts to load a raw Object from a file. * Attempts to load a raw Object from a file.
* *
* @param file The file where the object is stored. * @param file The file where the object is stored.
* @throws IOException * @throws IOException
*/ */
public static Object loadObject(File file) throws IOException, ClassNotFoundException { public static Object loadObject(File file) throws IOException, ClassNotFoundException
if (!file.exists()) { {
if (!file.exists())
{
throw new IllegalStateException(); throw new IllegalStateException();
} }
@ -69,57 +74,63 @@ public class FileUtils {
return object; return object;
} }
/** /**
* Returns a file at located at the Plugins Data folder. * Returns a file at located at the Plugins Data folder.
* *
* @param plugin The plugin to use * @param plugin The plugin to use
* @param name The name of the file. * @param name The name of the file.
* @return The requested file. * @return The requested file.
*/ */
public static File getPluginFile(Plugin plugin, String name) { public static File getPluginFile(Plugin plugin, String name)
{
return new File(plugin.getDataFolder(), name); return new File(plugin.getDataFolder(), name);
} }
/** /**
* Returns the root location of the CraftBukkit server. * Returns the root location of the CraftBukkit server.
* *
* @return The current working directory. * @return The current working directory.
*/ */
public static File getRoot() { public static File getRoot()
{
return new File("."); return new File(".");
} }
/** /**
* Returns the folder where all plugins are stored. * Returns the folder where all plugins are stored.
* *
* @return The plugins folder. * @return The plugins folder.
*/ */
public static File getPluginsFolder() { public static File getPluginsFolder()
{
return new File(getRoot(), "plugins"); return new File(getRoot(), "plugins");
} }
/** /**
* Returns a file at the root of the CraftBukkit server. * Returns a file at the root of the CraftBukkit server.
* *
* @param name The name of the file. * @param name The name of the file.
* @return The requested file. * @return The requested file.
*/ */
public static File getRootFile(String name) { public static File getRootFile(String name)
{
return new File(getRoot(), name); return new File(getRoot(), name);
} }
/** /**
* Delete a specified folder and all contents quietly. * Delete a specified folder and all contents quietly.
* *
* <p><b>Warning</b>: This method will delete files, only folders!</p> * <p><b>Warning</b>: This method will delete files, only folders!</p>
* *
* @param file The folder to delete. * @param file The folder to delete.
* @return true if the delete was successful. * @return true if the delete was successful.
* @deprecated Not in use; Relies on CraftBukkit source * @deprecated Not in use; Relies on CraftBukkit source
*/ */
public static boolean deleteFolder(File file) { public static boolean deleteFolder(File file)
if (file.exists() && file.isDirectory()) { {
if (file.exists() && file.isDirectory())
{
//return net.minecraft.util.org.apache.commons.io.FileUtils.deleteQuietly(file); //return net.minecraft.util.org.apache.commons.io.FileUtils.deleteQuietly(file);
} }
return false; return false;
@ -127,20 +138,23 @@ public class FileUtils {
/** /**
* Write the specified InputStream to a file. * Write the specified InputStream to a file.
* *
* @param in The InputStream from which to read. * @param in The InputStream from which to read.
* @param file The File to write to. * @param file The File to write to.
* @throws IOException * @throws IOException
*/ */
public static void copy(InputStream in, File file) throws IOException { public static void copy(InputStream in, File file) throws IOException
if (!file.exists()) { {
if (!file.exists())
{
file.getParentFile().mkdirs(); file.getParentFile().mkdirs();
} }
OutputStream out = new FileOutputStream(file); OutputStream out = new FileOutputStream(file);
byte[] buf = new byte[1024]; byte[] buf = new byte[1024];
int len; int len;
while ((len = in.read(buf)) > 0) { while ((len = in.read(buf)) > 0)
{
out.write(buf, 0, len); out.write(buf, 0, len);
} }
out.close(); out.close();

View file

@ -11,8 +11,8 @@ import org.bukkit.plugin.Plugin;
* *
* @see YamlConfiguration * @see YamlConfiguration
*/ */
public class YamlConfig extends YamlConfiguration { public class YamlConfig extends YamlConfiguration
{
private final Plugin PLUGIN; private final Plugin PLUGIN;
private final File CONFIG_FILE; private final File CONFIG_FILE;
private final boolean COPY_DEFAULTS; private final boolean COPY_DEFAULTS;
@ -30,7 +30,8 @@ public class YamlConfig extends YamlConfiguration {
* @param fileName The filename of the config file. * @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. * @param copyDefaults If the defaults should be copied and/loaded from a config in the plugin jar-file.
*/ */
public YamlConfig(Plugin plugin, String fileName, boolean copyDefaults) { public YamlConfig(Plugin plugin, String fileName, boolean copyDefaults)
{
this(plugin, FileUtils.getPluginFile(plugin, fileName), copyDefaults); this(plugin, FileUtils.getPluginFile(plugin, fileName), copyDefaults);
} }
@ -47,7 +48,8 @@ public class YamlConfig extends YamlConfiguration {
* @param file The file of the config file. * @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. * @param copyDefaults If the defaults should be copied and/loaded from a config in the plugin jar-file.
*/ */
public YamlConfig(Plugin plugin, File file, boolean copyDefaults) { public YamlConfig(Plugin plugin, File file, boolean copyDefaults)
{
this.PLUGIN = plugin; this.PLUGIN = plugin;
this.CONFIG_FILE = file; this.CONFIG_FILE = file;
this.COPY_DEFAULTS = copyDefaults; this.COPY_DEFAULTS = copyDefaults;
@ -55,13 +57,17 @@ public class YamlConfig extends YamlConfiguration {
/** /**
* Saves the configuration to the predefined file. * Saves the configuration to the predefined file.
* *
* @see #YamlConfig(Plugin, String, boolean) * @see #YamlConfig(Plugin, String, boolean)
*/ */
public void save() { public void save()
try { {
try
{
super.save(CONFIG_FILE); super.save(CONFIG_FILE);
} catch (Exception ex) { }
catch (Exception ex)
{
PLUGIN.getLogger().severe("Could not save configuration file: " + CONFIG_FILE.getName()); PLUGIN.getLogger().severe("Could not save configuration file: " + CONFIG_FILE.getName());
PLUGIN.getLogger().severe(ExceptionUtils.getStackTrace(ex)); PLUGIN.getLogger().severe(ExceptionUtils.getStackTrace(ex));
} }
@ -69,19 +75,26 @@ public class YamlConfig extends YamlConfiguration {
/** /**
* Loads the configuration from the predefined file. * 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> * <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) * @see #YamlConfig(Plugin, String, boolean)
*/ */
public void load() { public void load()
try { {
if (COPY_DEFAULTS) { try
if (!CONFIG_FILE.exists()) { {
if (COPY_DEFAULTS)
{
if (!CONFIG_FILE.exists())
{
CONFIG_FILE.getParentFile().mkdirs(); CONFIG_FILE.getParentFile().mkdirs();
try { try
{
FileUtils.copy(PLUGIN.getResource(CONFIG_FILE.getName()), CONFIG_FILE); FileUtils.copy(PLUGIN.getResource(CONFIG_FILE.getName()), CONFIG_FILE);
} catch (IOException ex) { }
catch (IOException ex)
{
PLUGIN.getLogger().severe("Could not write default configuration file: " + CONFIG_FILE.getName()); PLUGIN.getLogger().severe("Could not write default configuration file: " + CONFIG_FILE.getName());
PLUGIN.getLogger().severe(ExceptionUtils.getStackTrace(ex)); PLUGIN.getLogger().severe(ExceptionUtils.getStackTrace(ex));
} }
@ -92,7 +105,9 @@ public class YamlConfig extends YamlConfiguration {
} }
super.load(CONFIG_FILE); super.load(CONFIG_FILE);
} catch (Exception ex) { }
catch (Exception ex)
{
PLUGIN.getLogger().severe("Could not load configuration file: " + CONFIG_FILE.getName()); PLUGIN.getLogger().severe("Could not load configuration file: " + CONFIG_FILE.getName());
PLUGIN.getLogger().severe(ExceptionUtils.getStackTrace(ex)); PLUGIN.getLogger().severe(ExceptionUtils.getStackTrace(ex));
} }
@ -100,11 +115,12 @@ public class YamlConfig extends YamlConfiguration {
/** /**
* Returns the raw YamlConfiguration this config is based on. * Returns the raw YamlConfiguration this config is based on.
* *
* @return The YamlConfiguration. * @return The YamlConfiguration.
* @see YamlConfiguration * @see YamlConfiguration
*/ */
public YamlConfiguration getConfig() { public YamlConfiguration getConfig()
{
return this; return this;
} }
@ -112,15 +128,19 @@ public class YamlConfig extends YamlConfiguration {
* Returns the default configuration as been stored in the jar-file of the owning plugin. * Returns the default configuration as been stored in the jar-file of the owning plugin.
* @return The default configuration. * @return The default configuration.
*/ */
public YamlConfiguration getDefaultConfig() { public YamlConfiguration getDefaultConfig()
{
final YamlConfiguration DEFAULT_CONFIG = new YamlConfiguration(); final YamlConfiguration DEFAULT_CONFIG = new YamlConfiguration();
try { try
{
DEFAULT_CONFIG.load(PLUGIN.getResource(CONFIG_FILE.getName())); DEFAULT_CONFIG.load(PLUGIN.getResource(CONFIG_FILE.getName()));
} catch (Throwable ex) { }
catch (Throwable ex)
{
PLUGIN.getLogger().severe("Could not load default configuration: " + CONFIG_FILE.getName()); PLUGIN.getLogger().severe("Could not load default configuration: " + CONFIG_FILE.getName());
PLUGIN.getLogger().severe(ExceptionUtils.getStackTrace(ex)); PLUGIN.getLogger().severe(ExceptionUtils.getStackTrace(ex));
return null; return null;
} }
return DEFAULT_CONFIG; return DEFAULT_CONFIG;
} }
} }