@@ -7,96 +7,103 @@ import org.bukkit.command.CommandSender;
|
||||
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 sender
|
||||
* The player to send info to
|
||||
*/
|
||||
void check(CommandSender sender) {
|
||||
File folder = new File(DIRECTORY);
|
||||
File[] files = folder.listFiles(new PluginFilter());
|
||||
if (files.length == 0) {
|
||||
sender.sendMessage("No plugins to update.");
|
||||
} else {
|
||||
sender.sendMessage("Status for " + files.length + " plugins:");
|
||||
for (File file : files) {
|
||||
PluginDescriptionFile pdfFile = Checker.getPDF(file);
|
||||
if (pdfFile == null) {
|
||||
continue;
|
||||
}
|
||||
checkForUpdate(file, sender);
|
||||
}
|
||||
}
|
||||
}
|
||||
/**
|
||||
* Checks all the plugins in plugins/ for updates
|
||||
*
|
||||
* @param sender
|
||||
* The player to send info to
|
||||
*/
|
||||
void check(CommandSender sender) {
|
||||
File folder = new File(DIRECTORY);
|
||||
File[] files = folder.listFiles(new PluginFilter());
|
||||
|
||||
/**
|
||||
* Checks for an update for a given plugin
|
||||
*
|
||||
* @param file
|
||||
* The plugin file to check for an update
|
||||
* @param sender
|
||||
* The player to send info to
|
||||
*/
|
||||
private void checkForUpdate(File file, CommandSender sender) {
|
||||
PluginDescriptionFile pdfFile = Checker.getPDF(file);
|
||||
FillReader reader = needsUpdate(pdfFile);
|
||||
if (reader != null) {
|
||||
sender.sendMessage(ChatColor.RED + reader.getName() + " " + pdfFile.getVersion() + " has an update to " + reader.getCurrVersion());
|
||||
} else {
|
||||
sender.sendMessage(pdfFile.getName() + " " + pdfFile.getVersion() + " is up to date!");
|
||||
}
|
||||
}
|
||||
if (files.length == 0) {
|
||||
sender.sendMessage("No plugins to update.");
|
||||
} else {
|
||||
sender.sendMessage("Status for " + files.length + " plugins:");
|
||||
for (File file : files) {
|
||||
PluginDescriptionFile pdfFile = Checker.getPDF(file);
|
||||
|
||||
/**
|
||||
* 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;
|
||||
}
|
||||
}
|
||||
if (pdfFile == null) {
|
||||
continue;
|
||||
}
|
||||
checkForUpdate(file, sender);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 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;
|
||||
}
|
||||
}
|
||||
/**
|
||||
* Checks for an update for a given plugin
|
||||
*
|
||||
* @param file
|
||||
* The plugin file to check for an update
|
||||
* @param sender
|
||||
* The player to send info to
|
||||
*/
|
||||
private void checkForUpdate(File file, CommandSender sender) {
|
||||
PluginDescriptionFile pdfFile = Checker.getPDF(file);
|
||||
FillReader reader = needsUpdate(pdfFile);
|
||||
|
||||
if (reader != null) {
|
||||
sender.sendMessage(ChatColor.RED + reader.getName() + " " + pdfFile.getVersion() + " has an update to " + reader.getCurrVersion());
|
||||
} else {
|
||||
sender.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 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;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -7,135 +7,139 @@ import java.io.*;
|
||||
import java.net.URL;
|
||||
|
||||
public class Downloader {
|
||||
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";
|
||||
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);
|
||||
|
||||
download(new URL(url), name, DIRECTORY);
|
||||
file = new File("plugins", name);
|
||||
}
|
||||
if (url.endsWith(".jar") && file.exists()) {
|
||||
backupFile(file);
|
||||
}
|
||||
|
||||
/**
|
||||
* 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);
|
||||
}
|
||||
}
|
||||
download(new URL(url), name, DIRECTORY);
|
||||
file = new File("plugins", 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, DOWNLOAD_DIR);
|
||||
}
|
||||
/**
|
||||
* 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");
|
||||
|
||||
/**
|
||||
* 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 (file.exists()) {
|
||||
player.sendMessage("Downloading " + name + "'s file");
|
||||
PluginDescriptionFile pdfFile = Checker.getPDF(file);
|
||||
FillReader reader = Checker.needsUpdate(pdfFile);
|
||||
|
||||
if (!new File(directory).exists()) {
|
||||
new File(directory).mkdir();
|
||||
}
|
||||
downloadFile(new URL(reader.getFile()));
|
||||
player.sendMessage("Finished download");
|
||||
} else {
|
||||
System.out.println("Can't find " + name);
|
||||
}
|
||||
}
|
||||
|
||||
File f = new File(directory, name);
|
||||
if (f.exists()) {
|
||||
f.delete();
|
||||
}
|
||||
f.createNewFile();
|
||||
/**
|
||||
* 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('/');
|
||||
|
||||
copyInputStream(inputStream, new BufferedOutputStream(new FileOutputStream(f)));
|
||||
name = name.substring(index + 1);
|
||||
download(u, name, DOWNLOAD_DIR);
|
||||
}
|
||||
|
||||
try {
|
||||
if (inputStream != null) {
|
||||
inputStream.close();
|
||||
}
|
||||
} catch (IOException ioe) {
|
||||
System.out.println("[UPDATR]: Error closing inputStream");
|
||||
}
|
||||
// }
|
||||
}
|
||||
/**
|
||||
* 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;
|
||||
|
||||
/**
|
||||
* 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;
|
||||
inputStream = u.openStream();
|
||||
|
||||
while ((len = in.read(buffer)) >= 0) {
|
||||
out.write(buffer, 0, len);
|
||||
}
|
||||
if (!new File(directory).exists()) {
|
||||
new File(directory).mkdir();
|
||||
}
|
||||
|
||||
in.close();
|
||||
out.close();
|
||||
}
|
||||
File f = new File(directory, name);
|
||||
|
||||
/**
|
||||
* Moves the file to the backup folder.
|
||||
*
|
||||
* @param file
|
||||
* The file to backup
|
||||
*/
|
||||
private static void backupFile(File file) {
|
||||
if (file != null && 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"));
|
||||
}
|
||||
}
|
||||
if (f.exists()) {
|
||||
f.delete();
|
||||
}
|
||||
f.createNewFile();
|
||||
|
||||
copyInputStream(inputStream, new BufferedOutputStream(new FileOutputStream(f)));
|
||||
|
||||
try {
|
||||
if (inputStream != null) {
|
||||
inputStream.close();
|
||||
}
|
||||
} catch (IOException ioe) {
|
||||
System.out.println("[UPDATR]: Error closing inputStream");
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 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);
|
||||
}
|
||||
|
||||
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 != null && 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"));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -12,7 +12,8 @@ import org.json.simple.parser.ParseException;
|
||||
* Grabs the latest info for a given plugin from fill.bukkit.org
|
||||
*/
|
||||
public class FillReader {
|
||||
//TODO change this to what it will actually be...
|
||||
// TODO change this to what it will actually be...
|
||||
|
||||
private static final String BASE_URL = "http://taylorkelly.me/pnfo.php";
|
||||
private String currVersion;
|
||||
private String file;
|
||||
@@ -23,14 +24,16 @@ public class FillReader {
|
||||
public FillReader(String name) {
|
||||
try {
|
||||
String result = "";
|
||||
|
||||
try {
|
||||
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(
|
||||
conn.getInputStream()));
|
||||
BufferedReader rd = new BufferedReader(new InputStreamReader(conn.getInputStream()));
|
||||
String line;
|
||||
|
||||
while ((line = rd.readLine()) != null) {
|
||||
buf.append(line);
|
||||
}
|
||||
@@ -38,8 +41,10 @@ public class FillReader {
|
||||
rd.close();
|
||||
JSONParser parser = new JSONParser();
|
||||
Object obj;
|
||||
|
||||
obj = parser.parse(result);
|
||||
JSONObject jsonObj = (JSONObject) obj;
|
||||
|
||||
this.currVersion = (String) jsonObj.get("plugin_version");
|
||||
this.name = (String) jsonObj.get("plugin_name");
|
||||
this.file = (String) jsonObj.get("plugin_file");
|
||||
|
||||
@@ -9,14 +9,11 @@ public class Fillr extends JavaPlugin {
|
||||
public static final String VERSION = "1.0";
|
||||
public static final String DIRECTORY = "plugins";
|
||||
|
||||
public void onDisable() {
|
||||
}
|
||||
public void onDisable() {}
|
||||
|
||||
public void onEnable() {
|
||||
}
|
||||
public void onEnable() {}
|
||||
|
||||
public void onLoad() {
|
||||
}
|
||||
public void onLoad() {}
|
||||
|
||||
@Override
|
||||
public boolean onCommand(CommandSender sender, Command cmd, String commandLabel, String[] args) {
|
||||
|
||||
@@ -19,8 +19,8 @@ public class Getter {
|
||||
|
||||
public void get(String string, CommandSender sender) {
|
||||
FillReader reader = new FillReader(string);
|
||||
sender.sendMessage("Downloading " + reader.getName() + " "
|
||||
+ reader.getCurrVersion());
|
||||
|
||||
sender.sendMessage("Downloading " + reader.getName() + " " + reader.getCurrVersion());
|
||||
try {
|
||||
Downloader.downloadJar(reader.getFile());
|
||||
if (reader.getNotes() != null && !reader.getNotes().equals("")) {
|
||||
@@ -36,8 +36,10 @@ public class Getter {
|
||||
|
||||
private void enablePlugin(FillReader update) {
|
||||
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");
|
||||
|
||||
try {
|
||||
server.getPluginManager().loadPlugin(plugin);
|
||||
} catch (UnknownDependencyException ex) {
|
||||
|
||||
@@ -24,17 +24,19 @@ public class Updater {
|
||||
void updateAll(CommandSender sender) {
|
||||
File folder = new File(DIRECTORY);
|
||||
File[] files = folder.listFiles(new PluginFilter());
|
||||
|
||||
if (files.length == 0) {
|
||||
sender.sendMessage("No plugins to update.");
|
||||
} else {
|
||||
sender.sendMessage("Updating "
|
||||
+ files.length + " plugins:");
|
||||
sender.sendMessage("Updating " + files.length + " plugins:");
|
||||
for (File file : files) {
|
||||
PluginDescriptionFile pdfFile = Checker.getPDF(file);
|
||||
|
||||
if (pdfFile == null) {
|
||||
continue;
|
||||
}
|
||||
FillReader reader = Checker.needsUpdate(pdfFile);
|
||||
|
||||
if (reader != null) {
|
||||
update(reader, sender);
|
||||
}
|
||||
@@ -51,11 +53,14 @@ public class Updater {
|
||||
* The player to send info to
|
||||
*/
|
||||
void update(String string, CommandSender player) {
|
||||
//TODO so much .jars
|
||||
// TODO so much .jars
|
||||
|
||||
File file = new File(DIRECTORY, string + ".jar");
|
||||
|
||||
if (file.exists()) {
|
||||
PluginDescriptionFile pdfFile = Checker.getPDF(file);
|
||||
FillReader reader = Checker.needsUpdate(pdfFile);
|
||||
|
||||
if (reader != null) {
|
||||
update(reader, player);
|
||||
} else {
|
||||
@@ -76,8 +81,7 @@ public class Updater {
|
||||
private void update(FillReader update, CommandSender sender) {
|
||||
disablePlugin(update);
|
||||
sender.sendMessage("Disabling " + update.getName() + " for update");
|
||||
sender.sendMessage("Downloading " + update.getName() + " "
|
||||
+ update.getCurrVersion());
|
||||
sender.sendMessage("Downloading " + update.getName() + " " + update.getCurrVersion());
|
||||
try {
|
||||
Downloader.downloadJar(update.getFile());
|
||||
if (update.getNotes() != null && !update.getNotes().equals("")) {
|
||||
@@ -93,8 +97,9 @@ 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");
|
||||
|
||||
try {
|
||||
server.getPluginManager().loadPlugin(plugin);
|
||||
} catch (UnknownDependencyException ex) {
|
||||
@@ -109,6 +114,7 @@ public class Updater {
|
||||
private void disablePlugin(FillReader update) {
|
||||
String name = update.getName();
|
||||
Plugin plugin = server.getPluginManager().getPlugin(name);
|
||||
|
||||
server.getPluginManager().disablePlugin(plugin);
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user