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,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;
}
}
}