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

View File

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

View File

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

View File

@ -11,8 +11,8 @@ import org.bukkit.plugin.Plugin;
*
* @see YamlConfiguration
*/
public class YamlConfig extends YamlConfiguration {
public class YamlConfig extends YamlConfiguration
{
private final Plugin PLUGIN;
private final File CONFIG_FILE;
private final boolean COPY_DEFAULTS;
@ -30,7 +30,8 @@ public class YamlConfig extends YamlConfiguration {
* @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 YamlConfig(Plugin plugin, String fileName, boolean copyDefaults) {
public YamlConfig(Plugin plugin, String fileName, boolean 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 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.CONFIG_FILE = file;
this.COPY_DEFAULTS = copyDefaults;
@ -55,13 +57,17 @@ public class YamlConfig extends YamlConfiguration {
/**
* Saves the configuration to the predefined file.
*
* @see #YamlConfig(Plugin, String, boolean)
*
* @see #YamlConfig(Plugin, String, boolean)
*/
public void save() {
try {
public void save()
{
try
{
super.save(CONFIG_FILE);
} catch (Exception ex) {
}
catch (Exception ex)
{
PLUGIN.getLogger().severe("Could not save configuration file: " + CONFIG_FILE.getName());
PLUGIN.getLogger().severe(ExceptionUtils.getStackTrace(ex));
}
@ -69,19 +75,26 @@ public class YamlConfig extends YamlConfiguration {
/**
* 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 (COPY_DEFAULTS) {
if (!CONFIG_FILE.exists()) {
public void load()
{
try
{
if (COPY_DEFAULTS)
{
if (!CONFIG_FILE.exists())
{
CONFIG_FILE.getParentFile().mkdirs();
try {
try
{
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(ExceptionUtils.getStackTrace(ex));
}
@ -92,7 +105,9 @@ public class YamlConfig extends YamlConfiguration {
}
super.load(CONFIG_FILE);
} catch (Exception ex) {
}
catch (Exception ex)
{
PLUGIN.getLogger().severe("Could not load configuration file: " + CONFIG_FILE.getName());
PLUGIN.getLogger().severe(ExceptionUtils.getStackTrace(ex));
}
@ -100,11 +115,12 @@ public class YamlConfig extends YamlConfiguration {
/**
* Returns the raw YamlConfiguration this config is based on.
*
*
* @return The YamlConfiguration.
* @see YamlConfiguration
*/
public YamlConfiguration getConfig() {
public YamlConfiguration getConfig()
{
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.
* @return The default configuration.
*/
public YamlConfiguration getDefaultConfig() {
public YamlConfiguration getDefaultConfig()
{
final YamlConfiguration DEFAULT_CONFIG = new YamlConfiguration();
try {
try
{
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(ExceptionUtils.getStackTrace(ex));
return null;
}
return DEFAULT_CONFIG;
}
}
}