Cleaned up Fillr

By: Taylor Kelly <tkelly910@gmail.com>
This commit is contained in:
Bukkit/Spigot 2011-01-10 06:05:43 +08:00
parent c97111ca58
commit a27364193d
6 changed files with 231 additions and 255 deletions

View File

@ -6,7 +6,7 @@ import org.bukkit.*;
import org.bukkit.plugin.*; import org.bukkit.plugin.*;
public class Checker { public class Checker {
private static String directory = Fillr.directory; private static String DIRECTORY = Fillr.DIRECTORY;
/** /**
* Checks all the plugins in plugins/ for updates * Checks all the plugins in plugins/ for updates
@ -15,13 +15,12 @@ public class Checker {
* The player to send info to * The player to send info to
*/ */
void check(Player player) { void check(Player player) {
File folder = new File(directory); File folder = new File(DIRECTORY);
File[] files = folder.listFiles(new PluginFilter()); File[] files = folder.listFiles(new PluginFilter());
if (files.length == 0) { if (files.length == 0) {
player.sendMessage("No plugins to update."); player.sendMessage("No plugins to update.");
} else { } else {
player.sendMessage("Status for " + files.length player.sendMessage("Status for " + files.length + " plugins:");
+ " plugins:");
for (File file : files) { for (File file : files) {
PluginDescriptionFile pdfFile = Checker.getPDF(file); PluginDescriptionFile pdfFile = Checker.getPDF(file);
if (pdfFile == null) { if (pdfFile == null) {
@ -44,12 +43,9 @@ public class Checker {
PluginDescriptionFile pdfFile = Checker.getPDF(file); PluginDescriptionFile pdfFile = Checker.getPDF(file);
FillReader reader = needsUpdate(pdfFile); FillReader reader = needsUpdate(pdfFile);
if (reader != null) { if (reader != null) {
player.sendMessage(Color.RED + reader.getName() + " " player.sendMessage(Color.RED + reader.getName() + " " + pdfFile.getVersion() + " has an update to " + reader.getCurrVersion());
+ pdfFile.getVersion() + " has an update to "
+ reader.getCurrVersion());
} else { } else {
player.sendMessage(reader.getName() + " " + reader.getCurrVersion() player.sendMessage(pdfFile.getName() + " " + pdfFile.getVersion() + " is up to date!");
+ " is up to date!");
} }
} }
@ -58,15 +54,15 @@ public class Checker {
* *
* @param file * @param file
* The .yml file to check * The .yml file to check
* @return The FillReader for the online repo info on the plugin * @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) { static FillReader needsUpdate(PluginDescriptionFile file) {
FillReader reader = new FillReader(file.getName()); FillReader reader = new FillReader(file.getName());
String version = file.getVersion(); String version = file.getVersion();
String currVersion = reader.getCurrVersion(); String currVersion = reader.getCurrVersion();
String name = reader.getName(); String name = reader.getName();
if (currVersion.equalsIgnoreCase(version) if (currVersion.equalsIgnoreCase(version) && new File(DIRECTORY, name + ".jar").exists()) {
&& new File(directory, name + ".jar").exists()) {
return null; return null;
} else { } else {
return reader; return reader;

View File

@ -7,9 +7,9 @@ import java.io.*;
import java.net.URL; import java.net.URL;
public class Downloader { public class Downloader {
private final static String directory = Fillr.directory; private final static String DIRECTORY = Fillr.DIRECTORY;
private final static String downloads = directory + File.separator + "downloads"; private final static String DOWNLOAD_DIR = DIRECTORY + File.separator + "downloads";
private final static String backup = "backup"; private final static String BACKUP = DIRECTORY + File.separator + "backups";
/** /**
* Downloads the jar from a given url. If it is a compressed archive, it * Downloads the jar from a given url. If it is a compressed archive, it
@ -22,18 +22,13 @@ public class Downloader {
int index = url.lastIndexOf('/'); int index = url.lastIndexOf('/');
String name = url.substring(index + 1); String name = url.substring(index + 1);
File file = new File(directory, name); File file = new File(DIRECTORY, name);
if (url.endsWith(".jar") && file.exists()) { if (url.endsWith(".jar") && file.exists()) {
backupFile(file); backupFile(file);
} }
download(new URL(url), name, directory); download(new URL(url), name, DIRECTORY);
file = new File("plugins", name); file = new File("plugins", name);
/*if (name.endsWith(".zip") || name.endsWith(".tar")
|| name.endsWith(".rar") || name.endsWith(".7z")) {
unzipPlugin(file);
file.delete();
}*/
} }
/** /**
@ -45,7 +40,7 @@ public class Downloader {
* The player to send info to * The player to send info to
*/ */
void downloadFile(String name, Player player) throws Exception { void downloadFile(String name, Player player) throws Exception {
File file = new File(directory, name + ".jar"); File file = new File(DIRECTORY, name + ".jar");
if (file.exists()) { if (file.exists()) {
player.sendMessage("Downloading " + name + "'s file"); player.sendMessage("Downloading " + name + "'s file");
PluginDescriptionFile pdfFile = Checker.getPDF(file); PluginDescriptionFile pdfFile = Checker.getPDF(file);
@ -67,7 +62,7 @@ public class Downloader {
String name = u.getFile(); String name = u.getFile();
int index = name.lastIndexOf('/'); int index = name.lastIndexOf('/');
name = name.substring(index + 1); name = name.substring(index + 1);
download(u, name, downloads); download(u, name, DOWNLOAD_DIR);
} }
/** /**
@ -80,8 +75,7 @@ public class Downloader {
* @param directory * @param directory
* The directory to put the file * The directory to put the file
*/ */
private static void download(URL u, String name, String directory) private static void download(URL u, String name, String directory) throws Exception {
throws Exception {
InputStream inputStream = null; InputStream inputStream = null;
// try { // try {
inputStream = u.openStream(); inputStream = u.openStream();
@ -96,8 +90,7 @@ public class Downloader {
} }
f.createNewFile(); f.createNewFile();
copyInputStream(inputStream, new BufferedOutputStream( copyInputStream(inputStream, new BufferedOutputStream(new FileOutputStream(f)));
new FileOutputStream(f)));
try { try {
if (inputStream != null) { if (inputStream != null) {
@ -109,21 +102,6 @@ public class Downloader {
// } // }
} }
/**
* 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! * Copies an InputStream to an OutputStream!
* *
@ -133,8 +111,7 @@ public class Downloader {
* OutputStream * OutputStream
* @throws IOException * @throws IOException
*/ */
private static final void copyInputStream(InputStream in, OutputStream out) private static final void copyInputStream(InputStream in, OutputStream out) throws IOException {
throws IOException {
byte[] buffer = new byte[1024]; byte[] buffer = new byte[1024];
int len; int len;
@ -155,10 +132,10 @@ public class Downloader {
private static void backupFile(File file) { private static void backupFile(File file) {
if (file.exists()) { if (file.exists()) {
System.out.println("Backing up old file: " + file.getName()); System.out.println("Backing up old file: " + file.getName());
if (!new File(backup).exists()) { if (!new File(BACKUP).exists()) {
new File(backup).mkdir(); new File(BACKUP).mkdir();
} }
file.renameTo(new File(backup, file.getName() + ".bak")); file.renameTo(new File(BACKUP, file.getName() + ".bak"));
} }
} }
} }

View File

@ -13,7 +13,7 @@ import org.json.simple.parser.ParseException;
*/ */
public class FillReader { public class FillReader {
//TODO change this to what it will actually be... //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 currVersion;
private String file; private String file;
private String name; private String name;
@ -24,8 +24,8 @@ public class FillReader {
try { try {
String result = ""; String result = "";
try { try {
URL url = new URL(baseUrl + "?name=" + name); URL url = new URL(BASE_URL + "?name=" + name);
System.out.println(baseUrl + "?name=" + name); System.out.println(BASE_URL + "?name=" + name);
URLConnection conn = url.openConnection(); URLConnection conn = url.openConnection();
StringBuilder buf = new StringBuilder(); StringBuilder buf = new StringBuilder();
BufferedReader rd = new BufferedReader(new InputStreamReader( BufferedReader rd = new BufferedReader(new InputStreamReader(

View File

@ -9,17 +9,20 @@ import java.io.File;
public class Fillr extends JavaPlugin { public class Fillr extends JavaPlugin {
private FillrListener listener; private FillrListener listener;
public static String name = "Fillr"; public static final String NAME = "Fillr";
public static String version = "1.0"; public static final String VERSION = "1.0";
public static String directory = "plugins"; public static final String DIRECTORY = "plugins";
public Fillr(PluginLoader pluginLoader, Server instance, PluginDescriptionFile desc, File plugin, ClassLoader cLoader) { public Fillr(PluginLoader pluginLoader, Server instance, PluginDescriptionFile desc, File plugin, ClassLoader cLoader) {
super(pluginLoader, instance, desc, plugin, cLoader); super(pluginLoader, instance, desc, plugin, cLoader);
registerEvents();
} }
public void onDisable() {} public void onDisable() {
public void onEnable() {} }
public void onEnable() {
registerEvents();
}
private void registerEvents() { private void registerEvents() {
listener = new FillrListener(getServer()); listener = new FillrListener(getServer());

View File

@ -10,7 +10,7 @@ import org.bukkit.plugin.InvalidPluginException;
public class Getter { public class Getter {
private Server server; private Server server;
private static String directory = Fillr.directory; private static String DIRECTORY = Fillr.DIRECTORY;
public Getter(Server server) { public Getter(Server server) {
this.server = server; this.server = server;
@ -36,7 +36,7 @@ public class Getter {
private void enablePlugin(FillReader update) { private void enablePlugin(FillReader update) {
final String name = update.getName(); final String name = update.getName();
//TODO again with the implicit jar support... //TODO again with the implicit jar support...
File plugin = new File(directory, name + ".jar"); File plugin = new File(DIRECTORY, name + ".jar");
try { try {
server.getPluginManager().loadPlugin(plugin); server.getPluginManager().loadPlugin(plugin);
} catch (InvalidPluginException ex) { } catch (InvalidPluginException ex) {

View File

@ -8,7 +8,7 @@ import java.util.logging.Level;
import java.util.logging.Logger; import java.util.logging.Logger;
public class Updater { public class Updater {
public static String directory = Fillr.directory; public static String DIRECTORY = Fillr.DIRECTORY;
private final Server server; private final Server server;
Updater(Server server) { Updater(Server server) {
@ -22,7 +22,7 @@ public class Updater {
* The player to send info to * The player to send info to
*/ */
void updateAll(Player player) { void updateAll(Player player) {
File folder = new File(directory); File folder = new File(DIRECTORY);
File[] files = folder.listFiles(new PluginFilter()); File[] files = folder.listFiles(new PluginFilter());
if (files.length == 0) { if (files.length == 0) {
player.sendMessage("No plugins to update."); player.sendMessage("No plugins to update.");
@ -52,7 +52,7 @@ public class Updater {
*/ */
void update(String string, Player player) { void update(String string, Player player) {
//TODO so much .jars //TODO so much .jars
File file = new File(directory, string + ".jar"); File file = new File(DIRECTORY, string + ".jar");
if (file.exists()) { if (file.exists()) {
PluginDescriptionFile pdfFile = Checker.getPDF(file); PluginDescriptionFile pdfFile = Checker.getPDF(file);
FillReader reader = Checker.needsUpdate(pdfFile); FillReader reader = Checker.needsUpdate(pdfFile);
@ -94,7 +94,7 @@ public class Updater {
void enablePlugin(FillReader update) { void enablePlugin(FillReader update) {
final String name = update.getName(); final String name = update.getName();
//TODO again with the implicit jar support... //TODO again with the implicit jar support...
File plugin = new File(directory, name + ".jar"); File plugin = new File(DIRECTORY, name + ".jar");
try { try {
server.getPluginManager().loadPlugin(plugin); server.getPluginManager().loadPlugin(plugin);
} catch (InvalidPluginException ex) { } catch (InvalidPluginException ex) {