Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- # AUTOMATIC UPDATER & DOWNLOADER OF THEAPI
- # Our discord: https://discord.gg/8YtfC234dA
- public static void downloadAndUpdate() {
- if(Bukkit.getPluginManager().getPlugin("TheAPI")==null) {
- try {
- HttpURLConnection conn = (HttpURLConnection)new URL("https://api.spiget.org/v2/resources/72679/download").openConnection();
- conn.setRequestProperty("User-Agent", "DevTec-JavaClient");
- conn.setRequestProperty("Accept-Encoding", "gzip");
- conn.setRequestMethod("GET");
- conn.setConnectTimeout(20000);
- conn.setReadTimeout(20000);
- conn.connect();
- File f = new File("plugins/TheAPI.jar");
- if(!f.exists()) {
- f.getParentFile().mkdirs();
- f.createNewFile();
- }
- ReadableByteChannel rbc = Channels.newChannel(conn.getInputStream());
- FileOutputStream fos = new FileOutputStream(f);
- fos.getChannel().transferFrom(rbc, 0, Long.MAX_VALUE);
- fos.close();
- rbc.close();
- //load TheAPI
- File file = new File("plugins/TheAPI.jar");
- Bukkit.getPluginManager().enablePlugin(Bukkit.getPluginManager().loadPlugin(file));
- }catch(Exception e) {
- }
- } //else download update if needed
- else {
- try {
- if(new SpigotUpdateChecker(Bukkit.getPluginManager().getPlugin("TheAPI").getDescription().getVersion(), 72679).checkForUpdates()==Version.NEW) {
- try {
- HttpURLConnection conn = (HttpURLConnection)new URL("https://api.spiget.org/v2/resources/72679/download").openConnection();
- conn.setRequestProperty("User-Agent", "DevTec-JavaClient");
- conn.setRequestProperty("Accept-Encoding", "gzip");
- conn.setRequestMethod("GET");
- conn.setConnectTimeout(20000);
- conn.setReadTimeout(20000);
- conn.connect();
- File f = new File("plugins/update/TheAPI.jar");
- if(!f.exists()) {
- f.getParentFile().mkdirs();
- f.createNewFile();
- }
- ReadableByteChannel rbc = Channels.newChannel(conn.getInputStream());
- FileOutputStream fos = new FileOutputStream(f);
- fos.getChannel().transferFrom(rbc, 0, Long.MAX_VALUE);
- fos.close();
- rbc.close();
- }catch(Exception e) {
- }
- }
- }catch(Exception e) {
- }
- }
- }
- public static String fromStream(InputStream stream) {
- try {
- BufferedReader br = new BufferedReader(new InputStreamReader(stream, StandardCharsets.UTF_8), 8192);
- StringBuilder sb = new StringBuilder(512);
- String content;
- while ((content = br.readLine()) != null)
- sb.append(content).append(System.lineSeparator());
- br.close();
- return sb.toString();
- } catch (Exception e) {
- return null;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement