From a27364193d9ac46dd66dcf2f7ab00d29c5887b67 Mon Sep 17 00:00:00 2001 From: Bukkit/Spigot Date: Mon, 10 Jan 2011 06:05:43 +0800 Subject: [PATCH] Cleaned up Fillr By: Taylor Kelly --- .../main/java/org/bukkit/fillr/Checker.java | 180 ++++++------ .../java/org/bukkit/fillr/Downloader.java | 257 ++++++++---------- .../java/org/bukkit/fillr/FillReader.java | 6 +- .../src/main/java/org/bukkit/fillr/Fillr.java | 31 ++- .../main/java/org/bukkit/fillr/Getter.java | 4 +- .../main/java/org/bukkit/fillr/Updater.java | 8 +- 6 files changed, 231 insertions(+), 255 deletions(-) diff --git a/paper-api/src/main/java/org/bukkit/fillr/Checker.java b/paper-api/src/main/java/org/bukkit/fillr/Checker.java index 612957b2c1..8ae176bcfc 100644 --- a/paper-api/src/main/java/org/bukkit/fillr/Checker.java +++ b/paper-api/src/main/java/org/bukkit/fillr/Checker.java @@ -6,100 +6,96 @@ import org.bukkit.*; import org.bukkit.plugin.*; public class Checker { - private static String directory = Fillr.directory; + private static String DIRECTORY = Fillr.DIRECTORY; - /** - * Checks all the plugins in plugins/ for updates - * - * @param player - * The player to send info to - */ - void check(Player player) { - File folder = new File(directory); - File[] files = folder.listFiles(new PluginFilter()); - if (files.length == 0) { - player.sendMessage("No plugins to update."); - } else { - player.sendMessage("Status for " + files.length - + " plugins:"); - for (File file : files) { - PluginDescriptionFile pdfFile = Checker.getPDF(file); - if (pdfFile == null) { - continue; - } - checkForUpdate(file, player); - } - } - } + /** + * Checks all the plugins in plugins/ for updates + * + * @param player + * The player to send info to + */ + void check(Player player) { + File folder = new File(DIRECTORY); + File[] files = folder.listFiles(new PluginFilter()); + if (files.length == 0) { + player.sendMessage("No plugins to update."); + } else { + player.sendMessage("Status for " + files.length + " plugins:"); + for (File file : files) { + PluginDescriptionFile pdfFile = Checker.getPDF(file); + if (pdfFile == null) { + continue; + } + checkForUpdate(file, player); + } + } + } - /** - * Checks for an update for a given plugin - * - * @param file - * The plugin file to check for an update - * @param player - * The player to send info to - */ - private void checkForUpdate(File file, Player player) { - PluginDescriptionFile pdfFile = Checker.getPDF(file); - FillReader reader = needsUpdate(pdfFile); - if (reader != null) { - player.sendMessage(Color.RED + reader.getName() + " " - + pdfFile.getVersion() + " has an update to " - + reader.getCurrVersion()); - } else { - player.sendMessage(reader.getName() + " " + reader.getCurrVersion() - + " is up to date!"); - } - } + /** + * Checks for an update for a given plugin + * + * @param file + * The plugin file to check for an update + * @param player + * The player to send info to + */ + private void checkForUpdate(File file, Player player) { + PluginDescriptionFile pdfFile = Checker.getPDF(file); + FillReader reader = needsUpdate(pdfFile); + if (reader != null) { + player.sendMessage(Color.RED + reader.getName() + " " + pdfFile.getVersion() + " has an update to " + reader.getCurrVersion()); + } else { + player.sendMessage(pdfFile.getName() + " " + pdfFile.getVersion() + " is up to date!"); + } + } - /** - * Checks if a given plugin needs an update - * - * @param file - * The .yml file to check - * @return The FillReader for the online repo info on the plugin - */ - static FillReader needsUpdate(PluginDescriptionFile file) { - FillReader reader = new FillReader(file.getName()); - String version = file.getVersion(); - String currVersion = reader.getCurrVersion(); - String name = reader.getName(); - if (currVersion.equalsIgnoreCase(version) - && new File(directory, name + ".jar").exists()) { - return null; - } else { - return reader; - } - } + /** + * Checks if a given plugin needs an update + * + * @param file + * The .yml file to check + * @return The FillReader for the online repo info on the plugin if the plugin needs an update + * Returns null if no update is needed. + */ + static FillReader needsUpdate(PluginDescriptionFile file) { + FillReader reader = new FillReader(file.getName()); + String version = file.getVersion(); + String currVersion = reader.getCurrVersion(); + String name = reader.getName(); + if (currVersion.equalsIgnoreCase(version) && new File(DIRECTORY, name + ".jar").exists()) { + return null; + } else { + return reader; + } + } - /** - * Will grab the plugin's .yml file from the give file (hopefully a plugin). - * It'll throw it into a PluginDescriptionFile - * - * @param file - * The plugin (jar) file - * @return The PluginDescriptionFile representing the .yml - */ - static PluginDescriptionFile getPDF(File file) { - // TODO supports only jar files for now. how will yml's be stored in - // different languages? - if (file.getName().endsWith(".jar")) { - JarFile jarFile; - try { - jarFile = new JarFile(file); - JarEntry entry = jarFile.getJarEntry("plugin.yml"); - InputStream input = jarFile.getInputStream(entry); - return new PluginDescriptionFile(input); - } catch (IOException e) { - e.printStackTrace(); - return null; - } catch (InvalidDescriptionException e) { - e.printStackTrace(); - return null; - } - } else { - return null; - } - } + /** + * Will grab the plugin's .yml file from the give file (hopefully a plugin). + * It'll throw it into a PluginDescriptionFile + * + * @param file + * The plugin (jar) file + * @return The PluginDescriptionFile representing the .yml + */ + static PluginDescriptionFile getPDF(File file) { + // TODO supports only jar files for now. how will yml's be stored in + // different languages? + if (file.getName().endsWith(".jar")) { + JarFile jarFile; + try { + jarFile = new JarFile(file); + JarEntry entry = jarFile.getJarEntry("plugin.yml"); + InputStream input = jarFile.getInputStream(entry); + return new PluginDescriptionFile(input); + } catch (IOException e) { + e.printStackTrace(); + return null; + } catch (InvalidDescriptionException e) { + e.printStackTrace(); + return null; + } + } else { + return null; + } + } } diff --git a/paper-api/src/main/java/org/bukkit/fillr/Downloader.java b/paper-api/src/main/java/org/bukkit/fillr/Downloader.java index 8dc44c9543..3b9d8e7573 100644 --- a/paper-api/src/main/java/org/bukkit/fillr/Downloader.java +++ b/paper-api/src/main/java/org/bukkit/fillr/Downloader.java @@ -7,158 +7,135 @@ import java.io.*; import java.net.URL; public class Downloader { - private final static String directory = Fillr.directory; - private final static String downloads = directory + File.separator + "downloads"; - private final static String backup = "backup"; + private final static String DIRECTORY = Fillr.DIRECTORY; + private final static String DOWNLOAD_DIR = DIRECTORY + File.separator + "downloads"; + private final static String BACKUP = DIRECTORY + File.separator + "backups"; - /** - * Downloads the jar from a given url. If it is a compressed archive, it - * tries to get the .jars out of it - * - * @param url - * The url to download from - */ - static void downloadJar(String url) throws Exception { - int index = url.lastIndexOf('/'); - String name = url.substring(index + 1); + /** + * Downloads the jar from a given url. If it is a compressed archive, it + * tries to get the .jars out of it + * + * @param url + * The url to download from + */ + static void downloadJar(String url) throws Exception { + int index = url.lastIndexOf('/'); + String name = url.substring(index + 1); - File file = new File(directory, name); - if (url.endsWith(".jar") && file.exists()) { - backupFile(file); - } + File file = new File(DIRECTORY, name); + if (url.endsWith(".jar") && file.exists()) { + backupFile(file); + } - download(new URL(url), name, directory); - file = new File("plugins", name); - /*if (name.endsWith(".zip") || name.endsWith(".tar") - || name.endsWith(".rar") || name.endsWith(".7z")) { - unzipPlugin(file); - file.delete(); - }*/ - } + download(new URL(url), name, DIRECTORY); + file = new File("plugins", name); + } - /** - * Downloads the file for a given plugin - * - * @param name - * The name of the plugin to download - * @param player - * The player to send info to - */ - void downloadFile(String name, Player player) throws Exception { - File file = new File(directory, name + ".jar"); - if (file.exists()) { - player.sendMessage("Downloading " + name + "'s file"); - PluginDescriptionFile pdfFile = Checker.getPDF(file); - FillReader reader = Checker.needsUpdate(pdfFile); - downloadFile(new URL(reader.getFile())); - player.sendMessage("Finished download"); - } else { - System.out.println("Can't find " + name); - } - } + /** + * Downloads the file for a given plugin + * + * @param name + * The name of the plugin to download + * @param player + * The player to send info to + */ + void downloadFile(String name, Player player) throws Exception { + File file = new File(DIRECTORY, name + ".jar"); + if (file.exists()) { + player.sendMessage("Downloading " + name + "'s file"); + PluginDescriptionFile pdfFile = Checker.getPDF(file); + FillReader reader = Checker.needsUpdate(pdfFile); + downloadFile(new URL(reader.getFile())); + player.sendMessage("Finished download"); + } else { + System.out.println("Can't find " + name); + } + } - /** - * Downloads the file to the plugin/downloads directory - * - * @param u - * The url of the file to download - */ - private void downloadFile(URL u) throws Exception { - String name = u.getFile(); - int index = name.lastIndexOf('/'); - name = name.substring(index + 1); - download(u, name, downloads); - } + /** + * Downloads the file to the plugin/downloads directory + * + * @param u + * The url of the file to download + */ + private void downloadFile(URL u) throws Exception { + String name = u.getFile(); + int index = name.lastIndexOf('/'); + name = name.substring(index + 1); + download(u, name, DOWNLOAD_DIR); + } - /** - * Downloads the file to a given directory with a given name - * - * @param u - * The url of the file to download - * @param name - * The name to give the file - * @param directory - * The directory to put the file - */ - private static void download(URL u, String name, String directory) - throws Exception { - InputStream inputStream = null; - // try { - inputStream = u.openStream(); + /** + * Downloads the file to a given directory with a given name + * + * @param u + * The url of the file to download + * @param name + * The name to give the file + * @param directory + * The directory to put the file + */ + private static void download(URL u, String name, String directory) throws Exception { + InputStream inputStream = null; + // try { + inputStream = u.openStream(); - if (!new File(directory).exists()) { - new File(directory).mkdir(); - } + if (!new File(directory).exists()) { + new File(directory).mkdir(); + } - File f = new File(directory, name); - if (f.exists()) { - f.delete(); - } - f.createNewFile(); + File f = new File(directory, name); + if (f.exists()) { + f.delete(); + } + f.createNewFile(); - copyInputStream(inputStream, new BufferedOutputStream( - new FileOutputStream(f))); + copyInputStream(inputStream, new BufferedOutputStream(new FileOutputStream(f))); - try { - if (inputStream != null) { - inputStream.close(); - } - } catch (IOException ioe) { - System.out.println("[UPDATR]: Error closing inputStream"); - } - // } - } + try { + if (inputStream != null) { + inputStream.close(); + } + } catch (IOException ioe) { + System.out.println("[UPDATR]: Error closing inputStream"); + } + // } + } - /** - * Decompresses a file! How nice. - * - * @param f - * the file to decompress - */ - private static void unzipPlugin(File f) { - try { - System.out.println("Extracting jars out of " + f.getName()); - //ExtractorUtil.extract(f, f.getAbsolutePath()); - } catch (Exception e) { - System.out.println("[UPDATR]: Error decompressing " + f.getName()); - } - } + /** + * Copies an InputStream to an OutputStream! + * + * @param in + * InputStream + * @param out + * OutputStream + * @throws IOException + */ + private static final void copyInputStream(InputStream in, OutputStream out) throws IOException { + byte[] buffer = new byte[1024]; + int len; - /** - * Copies an InputStream to an OutputStream! - * - * @param in - * InputStream - * @param out - * OutputStream - * @throws IOException - */ - private static final void copyInputStream(InputStream in, OutputStream out) - throws IOException { - byte[] buffer = new byte[1024]; - int len; + while ((len = in.read(buffer)) >= 0) { + out.write(buffer, 0, len); + } - while ((len = in.read(buffer)) >= 0) { - out.write(buffer, 0, len); - } + in.close(); + out.close(); + } - in.close(); - out.close(); - } - - /** - * Moves the file to the backup folder. - * - * @param file - * The file to backup - */ - private static void backupFile(File file) { - if (file.exists()) { - System.out.println("Backing up old file: " + file.getName()); - if (!new File(backup).exists()) { - new File(backup).mkdir(); - } - file.renameTo(new File(backup, file.getName() + ".bak")); - } - } + /** + * Moves the file to the backup folder. + * + * @param file + * The file to backup + */ + private static void backupFile(File file) { + if (file.exists()) { + System.out.println("Backing up old file: " + file.getName()); + if (!new File(BACKUP).exists()) { + new File(BACKUP).mkdir(); + } + file.renameTo(new File(BACKUP, file.getName() + ".bak")); + } + } } diff --git a/paper-api/src/main/java/org/bukkit/fillr/FillReader.java b/paper-api/src/main/java/org/bukkit/fillr/FillReader.java index ce94283a72..d32e8a4e50 100644 --- a/paper-api/src/main/java/org/bukkit/fillr/FillReader.java +++ b/paper-api/src/main/java/org/bukkit/fillr/FillReader.java @@ -13,7 +13,7 @@ import org.json.simple.parser.ParseException; */ public class FillReader { //TODO change this to what it will actually be... - private static String baseUrl = "http://taylorkelly.me/pnfo.php"; + private static final String BASE_URL = "http://taylorkelly.me/pnfo.php"; private String currVersion; private String file; private String name; @@ -24,8 +24,8 @@ public class FillReader { try { String result = ""; try { - URL url = new URL(baseUrl + "?name=" + name); - System.out.println(baseUrl + "?name=" + name); + URL url = new URL(BASE_URL + "?name=" + name); + System.out.println(BASE_URL + "?name=" + name); URLConnection conn = url.openConnection(); StringBuilder buf = new StringBuilder(); BufferedReader rd = new BufferedReader(new InputStreamReader( diff --git a/paper-api/src/main/java/org/bukkit/fillr/Fillr.java b/paper-api/src/main/java/org/bukkit/fillr/Fillr.java index b185fea24a..4cb0654d92 100644 --- a/paper-api/src/main/java/org/bukkit/fillr/Fillr.java +++ b/paper-api/src/main/java/org/bukkit/fillr/Fillr.java @@ -8,21 +8,24 @@ import org.bukkit.event.*; import java.io.File; public class Fillr extends JavaPlugin { - private FillrListener listener; - public static String name = "Fillr"; - public static String version = "1.0"; - public static String directory = "plugins"; + private FillrListener listener; + public static final String NAME = "Fillr"; + public static final String VERSION = "1.0"; + public static final String DIRECTORY = "plugins"; - public Fillr(PluginLoader pluginLoader, Server instance, PluginDescriptionFile desc, File plugin, ClassLoader cLoader) { - super(pluginLoader, instance, desc, plugin, cLoader); - registerEvents(); - } + public Fillr(PluginLoader pluginLoader, Server instance, PluginDescriptionFile desc, File plugin, ClassLoader cLoader) { + super(pluginLoader, instance, desc, plugin, cLoader); + } - public void onDisable() {} - public void onEnable() {} + public void onDisable() { + } - private void registerEvents() { - listener = new FillrListener(getServer()); - getServer().getPluginManager().registerEvent(Event.Type.PLAYER_COMMAND, listener, Event.Priority.Normal, this); - } + public void onEnable() { + registerEvents(); + } + + private void registerEvents() { + listener = new FillrListener(getServer()); + getServer().getPluginManager().registerEvent(Event.Type.PLAYER_COMMAND, listener, Event.Priority.Normal, this); + } } diff --git a/paper-api/src/main/java/org/bukkit/fillr/Getter.java b/paper-api/src/main/java/org/bukkit/fillr/Getter.java index 1bbd5b4c27..c1197d483a 100644 --- a/paper-api/src/main/java/org/bukkit/fillr/Getter.java +++ b/paper-api/src/main/java/org/bukkit/fillr/Getter.java @@ -10,7 +10,7 @@ import org.bukkit.plugin.InvalidPluginException; public class Getter { private Server server; - private static String directory = Fillr.directory; + private static String DIRECTORY = Fillr.DIRECTORY; public Getter(Server server) { this.server = server; @@ -36,7 +36,7 @@ public class Getter { private void enablePlugin(FillReader update) { final String name = update.getName(); //TODO again with the implicit jar support... - File plugin = new File(directory, name + ".jar"); + File plugin = new File(DIRECTORY, name + ".jar"); try { server.getPluginManager().loadPlugin(plugin); } catch (InvalidPluginException ex) { diff --git a/paper-api/src/main/java/org/bukkit/fillr/Updater.java b/paper-api/src/main/java/org/bukkit/fillr/Updater.java index 1ca26b60a2..3a301538a4 100644 --- a/paper-api/src/main/java/org/bukkit/fillr/Updater.java +++ b/paper-api/src/main/java/org/bukkit/fillr/Updater.java @@ -8,7 +8,7 @@ import java.util.logging.Level; import java.util.logging.Logger; public class Updater { - public static String directory = Fillr.directory; + public static String DIRECTORY = Fillr.DIRECTORY; private final Server server; Updater(Server server) { @@ -22,7 +22,7 @@ public class Updater { * The player to send info to */ void updateAll(Player player) { - File folder = new File(directory); + File folder = new File(DIRECTORY); File[] files = folder.listFiles(new PluginFilter()); if (files.length == 0) { player.sendMessage("No plugins to update."); @@ -52,7 +52,7 @@ public class Updater { */ void update(String string, Player player) { //TODO so much .jars - File file = new File(directory, string + ".jar"); + File file = new File(DIRECTORY, string + ".jar"); if (file.exists()) { PluginDescriptionFile pdfFile = Checker.getPDF(file); FillReader reader = Checker.needsUpdate(pdfFile); @@ -94,7 +94,7 @@ public class Updater { void enablePlugin(FillReader update) { final String name = update.getName(); //TODO again with the implicit jar support... - File plugin = new File(directory, name + ".jar"); + File plugin = new File(DIRECTORY, name + ".jar"); try { server.getPluginManager().loadPlugin(plugin); } catch (InvalidPluginException ex) {