Skip to content

Instantly share code, notes, and snippets.

@Xyene
Last active December 11, 2015 16:58
Show Gist options
  • Save Xyene/4630683 to your computer and use it in GitHub Desktop.
Save Xyene/4630683 to your computer and use it in GitHub Desktop.
A cleaner dependency manager for Bukkit plugins.
public class PluginDownloader {
public boolean exists(String path) {
try {
HttpURLConnection urlConn = (HttpURLConnection) new URL(path).openConnection();
urlConn.setConnectTimeout(1000 * 10);
urlConn.connect();
return urlConn.getResponseCode() == HttpURLConnection.HTTP_OK;
} catch (MalformedURLException e) {
System.err.println("The URL given <" + path + "> was not found!");
} catch (IOException e) {
e.printStackTrace();
}
return false;
}
public void downloadZip(URL from, String to) {
try {
BufferedOutputStream out = new BufferedOutputStream(new FileOutputStream(to));
ZipInputStream zin = new ZipInputStream(from.openConnection().getInputStream());
byte[] buffer = new byte[65536];
while ((ZipEntry ze = zin.getNextEntry()) != null) {
while ((int read = zin.read(buffer)) != -1)
out.write(buffer, 0, read);
zin.closeEntry();
}
System.out.println("The file at <" + from + "> was downloaded to the path <" + to + ">.");
File base = new File(to);
for (File file : base.listFiles())
if (file.getName().endsWith(".jar"))
Bukkit.getPluginManager().loadPlugin(file);
} catch (MalformedURLException e) {
System.err.println("The URL given <" + from + "> was not found!");
} catch (FileNotFoundException e) {
System.err.println("The file path given <" + to + "> was not found!");
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
zin.close();
out.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
public void downloadJar(URL from, String to) {
try {
BufferedOutputStream out = new BufferedOutputStream(new FileOutputStream(to));
InputStream in = from.openConnection().getInputStream();
byte[] buffer = new byte[65536];
while ((int read = in.read(buffer)) != -1)
out.write(buffer, 0, read);
System.out.println("The file at <" + from + "> was downloaded to the path <" + to + ">.");
Bukkit.getPluginManager().loadPlugin(new File(to));
} catch (MalformedURLException e) {
System.err.println("The URL given <" + from + "> was not found!");
} catch (FileNotFoundException e) {
System.err.println("The file path given <" + to + "> was not found!");
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
out.close();
in.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment