Created
December 26, 2020 16:12
-
-
Save ChocolatMilka/1811959c1cbebcecebfc2eb57c96cb99 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| package wtf.listenia.core.api.injector; | |
| import de.leonhard.storage.Config; | |
| import org.json.JSONObject; | |
| import java.io.BufferedReader; | |
| import java.io.InputStreamReader; | |
| import java.net.URL; | |
| import java.net.URLConnection; | |
| import java.util.HashSet; | |
| import java.util.Set; | |
| public class GithubDownloader { | |
| public static Config temoin = new Config("addons.init", "./plugins/ListeniaCore"); | |
| public static Set<GithubDownloader> getAllAddons () { | |
| final Set<GithubDownloader> res = new HashSet<>(); | |
| for (final String project : temoin.keySet()) { | |
| res.add(new GithubDownloader(project)); | |
| } | |
| return res; | |
| } | |
| public final String repos; | |
| public final String filename; | |
| public String jarUrl; | |
| public int id; | |
| public final String projectName; | |
| public final String author; | |
| public GithubDownloader (String repos) { | |
| this.repos = repos; | |
| this.filename = repos.replace("/", "__"); | |
| final String[] infos = repos.split("/"); | |
| this.projectName = infos[1]; | |
| this.author = infos[0]; | |
| try { | |
| URL u = new URL("https://api.github.com/repos/" + repos + "/releases/latest"); | |
| URLConnection conn = u.openConnection(); | |
| BufferedReader in = new BufferedReader(new InputStreamReader(conn.getInputStream())); | |
| StringBuilder buffer = new StringBuilder(); | |
| String inputLine; | |
| while ((inputLine = in.readLine()) != null) | |
| buffer.append(inputLine); | |
| in.close(); | |
| JSONObject json = new JSONObject(buffer.toString()).getJSONArray("assets").getJSONObject(0); | |
| this.jarUrl = json.getString("browser_download_url"); | |
| this.id = json.getInt("id"); | |
| } catch (Exception exception) { | |
| exception.printStackTrace(); | |
| } | |
| } | |
| public boolean shouldUpdate () { | |
| if (temoin.getOrDefault(repos, 0) != id) { | |
| temoin.set(repos, id); | |
| return true; | |
| } | |
| return false; | |
| } | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| package wtf.listenia.core.api.injector; | |
| import java.io.BufferedInputStream; | |
| import java.io.File; | |
| import java.io.FileOutputStream; | |
| import java.net.URL; | |
| public class PluginLoader { | |
| public static void loadJar (GithubDownloader downloader) { | |
| File file = new File("./plugins/" + downloader.filename + ".jar"); | |
| file.getParentFile().mkdir(); | |
| try { | |
| if (downloader.shouldUpdate() || !file.exists()) { | |
| BufferedInputStream in = new BufferedInputStream(new URL(downloader.jarUrl).openStream()); | |
| FileOutputStream fileOutputStream = new FileOutputStream(file); | |
| System.out.printf("§3[Listenia&bCore§3] §cTéléchargement de l'addons §e%s §cde §c%s%n", downloader.projectName, downloader.author); | |
| byte[] dataBuffer = new byte[1024]; | |
| int bytesRead; | |
| while ((bytesRead = in.read(dataBuffer, 0, 1024)) != -1) { | |
| fileOutputStream.write(dataBuffer, 0, bytesRead); | |
| } | |
| fileOutputStream.close(); | |
| in.close(); | |
| } | |
| } catch (Exception e) { | |
| e.printStackTrace(); | |
| } | |
| } | |
| public static void loadAllAddons () { | |
| GithubDownloader.getAllAddons() | |
| .parallelStream() | |
| .forEach(PluginLoader::loadJar); | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment