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

@ -17,8 +17,8 @@ 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.
* *
@ -27,7 +27,8 @@ public class FileUtils {
* @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);
@ -42,8 +43,10 @@ public class FileUtils {
* @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();
} }
@ -58,8 +61,10 @@ public class FileUtils {
* @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();
} }
@ -77,7 +82,8 @@ public class FileUtils {
* @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);
} }
@ -86,7 +92,8 @@ public class FileUtils {
* *
* @return The current working directory. * @return The current working directory.
*/ */
public static File getRoot() { public static File getRoot()
{
return new File("."); return new File(".");
} }
@ -95,7 +102,8 @@ public class FileUtils {
* *
* @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");
} }
@ -105,7 +113,8 @@ public class FileUtils {
* @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);
} }
@ -118,8 +127,10 @@ public class FileUtils {
* @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;
@ -132,15 +143,18 @@ public class FileUtils {
* @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;
@ -58,10 +60,14 @@ public class YamlConfig extends YamlConfiguration {
* *
* @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));
} }
@ -74,14 +80,21 @@ public class YamlConfig extends YamlConfiguration {
* *
* @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));
} }
@ -104,7 +119,8 @@ public class YamlConfig extends YamlConfiguration {
* @return The YamlConfiguration. * @return The YamlConfiguration.
* @see YamlConfiguration * @see YamlConfiguration
*/ */
public YamlConfiguration getConfig() { public YamlConfiguration getConfig()
{
return this; return this;
} }
@ -112,11 +128,15 @@ 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;