Soft Dependency Download
Use this code to download SQLibrary from your plugin as a soft dependency (your users will not have to download SQLibrary):
public static boolean downloadPlugin(Plugin plugin, String id) { InputStreamReader in = null; try { URL url = new URL("https://api.curseforge.com/servermods/files?projectIds=" + id); URLConnection urlConnection = url.openConnection(); in = new InputStreamReader(urlConnection.getInputStream()); int numCharsRead; char[] charArray = new char[1024]; StringBuilder sb = new StringBuilder(); while ((numCharsRead = in.read(charArray)) > 0) { sb.append(charArray, 0, numCharsRead); } String result = sb.toString(); result = result.replace("\\/", "/").replaceAll(".*\"downloadUrl\":\"", "").split("\",\"")[0]; String[] split = result.split("/"); url = new URL(result); final String path = plugin.getDataFolder().getParentFile().getAbsoluteFile() + "/" + split[split.length]; ReadableByteChannel rbc = Channels.newChannel(url.openStream()); FileOutputStream fos = new FileOutputStream(path); fos.getChannel().transferFrom(rbc, 0, Long.MAX_VALUE); Bukkit.getServer().getLogger().log(Level.INFO, "Finished downloading " + split[split.length] + ". Loading dependecy"); Bukkit.getServer().getPluginManager().loadPlugin(new File(path)); return true; } catch (MalformedURLException ex) { Logger.getLogger(PluginDownloader.class.getName()).log(Level.SEVERE, null, ex); } catch (IOException ex) { Logger.getLogger(PluginDownloader.class.getName()).log(Level.SEVERE, null, ex); } catch (InvalidPluginException ex) { Logger.getLogger(PluginDownloader.class.getName()).log(Level.SEVERE, null, ex); } catch (InvalidDescriptionException ex) { Logger.getLogger(PluginDownloader.class.getName()).log(Level.SEVERE, null, ex); } catch (UnknownDependencyException ex) { Logger.getLogger(PluginDownloader.class.getName()).log(Level.SEVERE, null, ex); } finally { try { in.close(); } catch (IOException ex) { Logger.getLogger(PluginDownloader.class.getName()).log(Level.SEVERE, null, ex); } } return false; }
And load it with:
this.getServer().getPluginManager().loadPlugin(new File(path));
Legacy Code
This code is here for historical purposes:
if (!this.getServer().getPluginManager().isPluginEnabled("SQLibrary")) { try { info("Downloadind dependecy: SQLibrary"); URL url = new URL("http://repo.dakanilabs.com/content/repositories/public/lib/PatPeter/SQLibrary/SQLibrary/maven-metadata.xml"); URLConnection urlConnection = url.openConnection(); InputStream in = new BufferedInputStream(urlConnection.getInputStream()); DocumentBuilderFactory dbFactory = DocumentBuilderFactory.newInstance(); DocumentBuilder dBuilder = dbFactory.newDocumentBuilder(); Document doc = dBuilder.parse(in); final String version = doc.getElementsByTagName("latest").item(0).getTextContent(); in.close(); url = new URL("http://repo.dakanilabs.com/content/repositories/public/lib/PatPeter/SQLibrary/SQLibrary/" + version + "/SQLibrary-" + version + ".jar"); final String path = plugin.getDataFolder().getParentFile().getAbsoluteFile() + "/SQLibrary-" + version + ".jar"; ReadableByteChannel rbc = Channels.newChannel(url.openStream()); FileOutputStream fos = new FileOutputStream(path); fos.getChannel().transferFrom(rbc, 0, Long.MAX_VALUE); info("Finished downloading SQLibrary-" + version + ". Loading dependecy"); this.getServer().getPluginManager().loadPlugin(new File(path)); info("Loaded SQLibrary"); } catch (MalformedURLException ex) { error("Failed to download dependency", ex); } catch (IOException ex) { error("Failed to download dependency", ex); } catch (ParserConfigurationException ex) { error("Failed to download dependency", ex); } catch (SAXException ex) { error("Failed to download dependency", ex); } catch (InvalidPluginException ex) { error("Failed to load dependency", ex); } catch (InvalidDescriptionException ex) { error("Failed to load dependency", ex); } catch (UnknownDependencyException ex) { error("Failed to load dependency", ex); } }
I just want to say using this code to download the library is against the guidelines for submission and this code should not be offered. you cannot automatically download dependencies.
"You should not automatically download plugin dependencies. If your plugin has a dependency on another plugin hosted on BukkitDev, provide a link to that plugin and state it is a dependency."
@bgsteiner
We can thank the BukkitDev Nazis for that one.
Heil BükkitDevStaffen!