|
| 1 | +package com.github.elic0de.thejpspit.spigot.util; |
| 2 | + |
| 3 | +import com.github.elic0de.thejpspit.spigot.TheJpsPit; |
| 4 | +import com.google.gson.JsonArray; |
| 5 | +import com.google.gson.JsonElement; |
| 6 | +import com.google.gson.JsonObject; |
| 7 | +import com.google.gson.JsonParser; |
| 8 | +import java.io.File; |
| 9 | +import java.io.IOException; |
| 10 | +import java.io.InputStream; |
| 11 | +import java.io.InputStreamReader; |
| 12 | +import java.net.URL; |
| 13 | +import java.net.URLConnection; |
| 14 | +import java.nio.file.Files; |
| 15 | +import java.nio.file.Path; |
| 16 | +import java.nio.file.StandardCopyOption; |
| 17 | +import org.bukkit.ChatColor; |
| 18 | +import org.bukkit.entity.Player; |
| 19 | +import org.bukkit.scheduler.BukkitRunnable; |
| 20 | + |
| 21 | +public class DownloadUtil { |
| 22 | + |
| 23 | + private static final String GITHUB_REPO_RELEASES = "https://api.github.com/repos/JavaJava19/TheJpsPit/releases"; |
| 24 | + private static final String GITHUB_RELEASES_LATEST_URL = GITHUB_REPO_RELEASES + "/latest"; |
| 25 | + |
| 26 | + // https://www.spigotmc.org/threads/auto-updater-using-github.324545/ |
| 27 | + public static void download(final Player player){ |
| 28 | + try { |
| 29 | + final String TOKEN = TheJpsPit.getInstance().getSettings().getGithubToken(); |
| 30 | + |
| 31 | + if (TOKEN.equalsIgnoreCase("")) { |
| 32 | + player.sendMessage("トークンを設定してください"); |
| 33 | + return; |
| 34 | + } |
| 35 | + final URL api = new URL(GITHUB_RELEASES_LATEST_URL); //You want to set //this to your own project URL |
| 36 | + final URLConnection con = api.openConnection(); |
| 37 | + con.setRequestProperty("Accept", "application/vnd.github+json"); |
| 38 | + con.setRequestProperty("Authorization", "Bearer %token%".replaceAll("%token%", TOKEN)); |
| 39 | + con.setRequestProperty("X-GitHub-Api-Version", "2022-11-28"); |
| 40 | + con.setConnectTimeout(15000); |
| 41 | + con.setReadTimeout(15000); |
| 42 | + |
| 43 | + final JsonObject json = JsonParser.parseReader(new InputStreamReader(con.getInputStream())).getAsJsonObject(); |
| 44 | + |
| 45 | + if (json.has("assets")) { |
| 46 | + final JsonArray assets = json.get("assets").getAsJsonArray(); |
| 47 | + for (JsonElement asset : assets) { |
| 48 | + final JsonObject assetJson = asset.getAsJsonObject(); |
| 49 | + final String ASSETS_URL = assetJson.get("url").getAsString(); |
| 50 | + final String ASSETS_NAME = assetJson.get("name").getAsString(); |
| 51 | + final URL download = new URL(ASSETS_URL); //This is where you put your download URL for your latest release, be sure to change this to //your own. |
| 52 | + final URLConnection urlConnection = download.openConnection(); |
| 53 | + urlConnection.setRequestProperty("Authorization", "Bearer %token%".replaceAll("%token%", TOKEN)); |
| 54 | + urlConnection.setRequestProperty("Accept", "application/octet-stream"); |
| 55 | + |
| 56 | + player.sendMessage( |
| 57 | + ChatColor.GREEN + "[TheJpsPit] " + ASSETS_NAME + " をダウンロードします"); |
| 58 | + |
| 59 | + new BukkitRunnable() { |
| 60 | + @Override |
| 61 | + public void run() { |
| 62 | + try { |
| 63 | + |
| 64 | + InputStream in = urlConnection.getInputStream(); |
| 65 | + File temp = new File( |
| 66 | + "plugins/update"); //We want to make the folder if it doesn't exist //already |
| 67 | + if (!temp.exists()) { |
| 68 | + temp.mkdir(); |
| 69 | + } |
| 70 | + Path path = new File("plugins/update" + File.separator |
| 71 | + + ASSETS_NAME).toPath(); //Here you will //put your file name, it must be named the same everytime for it to properly replace the existing, it will send //the file to the updates folder, from there the server will handle the rest. |
| 72 | + Files.copy(in, path, StandardCopyOption.REPLACE_EXISTING); |
| 73 | + |
| 74 | + } catch (IOException e) { |
| 75 | + e.printStackTrace(); |
| 76 | + } |
| 77 | + } |
| 78 | + }.runTaskLaterAsynchronously(TheJpsPit.getInstance(), |
| 79 | + 0); //We want this to be an async task so we don't clog up the main thread, also ThisPlugin.getPlugin() is just a short cut for me not to have to use this, it saves me time incase you were wondering |
| 80 | + } |
| 81 | + } |
| 82 | + }catch(IOException e){ |
| 83 | + e.printStackTrace(); |
| 84 | + } |
| 85 | + } |
| 86 | +} |
0 commit comments