|
1 | 1 | package ch.njol.skript.test.platform;
|
2 | 2 |
|
| 3 | +import ch.njol.skript.test.utils.TestResults; |
3 | 4 | import com.google.gson.Gson;
|
4 |
| -import com.google.gson.JsonArray; |
5 |
| -import com.google.gson.JsonElement; |
6 | 5 | import com.google.gson.JsonObject;
|
7 |
| - |
8 |
| -import ch.njol.skript.test.utils.TestResults; |
9 |
| - |
10 | 6 | import org.jetbrains.annotations.Nullable;
|
11 | 7 |
|
12 | 8 | import java.io.File;
|
13 | 9 | import java.io.IOException;
|
14 | 10 | import java.io.InputStream;
|
15 | 11 | import java.io.InputStreamReader;
|
16 | 12 | import java.lang.ProcessBuilder.Redirect;
|
| 13 | +import java.net.URI; |
17 | 14 | import java.net.URISyntaxException;
|
18 | 15 | import java.net.URL;
|
| 16 | +import java.net.http.HttpClient; |
| 17 | +import java.net.http.HttpRequest; |
| 18 | +import java.net.http.HttpResponse; |
| 19 | +import java.net.http.HttpResponse.BodyHandlers; |
19 | 20 | import java.nio.charset.StandardCharsets;
|
20 | 21 | import java.nio.file.Files;
|
21 | 22 | import java.nio.file.Path;
|
@@ -93,33 +94,26 @@ public String getSource() {
|
93 | 94 | return source;
|
94 | 95 | }
|
95 | 96 |
|
96 |
| - private void generateSource() throws IOException { |
| 97 | + private void generateSource() throws IOException, InterruptedException { |
97 | 98 | if (source != null)
|
98 | 99 | return;
|
99 | 100 |
|
100 |
| - String stringUrl = "https://api.papermc.io/v2/projects/paper/versions/" + version; |
101 |
| - URL url = new URL(stringUrl); |
102 |
| - JsonObject jsonObject; |
103 |
| - try (InputStream is = url.openStream()) { |
104 |
| - InputStreamReader reader = new InputStreamReader(is, StandardCharsets.UTF_8); |
105 |
| - jsonObject = gson.fromJson(reader, JsonObject.class); |
| 101 | + HttpClient client = HttpClient.newHttpClient(); |
| 102 | + HttpRequest buildRequest = HttpRequest.newBuilder() |
| 103 | + .uri(URI.create("https://fill.papermc.io/v3/projects/paper/versions/" + version + "/builds/latest")) |
| 104 | + .header("User-Agent", "SkriptLang/Skript/{@version} (admin@skriptlang.org)") |
| 105 | + .GET() |
| 106 | + .build(); |
| 107 | + HttpResponse<InputStream> buildResponse = client.send(buildRequest, BodyHandlers.ofInputStream()); |
| 108 | + JsonObject buildObject; |
| 109 | + try (InputStreamReader reader = new InputStreamReader(buildResponse.body(), StandardCharsets.UTF_8)) { |
| 110 | + buildObject = gson.fromJson(reader, JsonObject.class); |
106 | 111 | }
|
107 |
| - |
108 |
| - JsonArray jsonArray = jsonObject.get("builds").getAsJsonArray(); |
109 |
| - |
110 |
| - int latestBuild = -1; |
111 |
| - for (JsonElement jsonElement : jsonArray) { |
112 |
| - int build = jsonElement.getAsInt(); |
113 |
| - if (build > latestBuild) { |
114 |
| - latestBuild = build; |
115 |
| - } |
116 |
| - } |
117 |
| - |
118 |
| - if (latestBuild == -1) |
119 |
| - throw new IllegalStateException("No builds for this version"); |
120 |
| - |
121 |
| - source = "https://api.papermc.io/v2/projects/paper/versions/" + version + "/builds/" + latestBuild |
122 |
| - + "/downloads/paper-" + version + "-" + latestBuild + ".jar"; |
| 112 | + String downloadURL = buildObject.getAsJsonObject("downloads") |
| 113 | + .getAsJsonObject("server:default") |
| 114 | + .get("url").getAsString(); |
| 115 | + assert downloadURL != null && !downloadURL.isEmpty(); |
| 116 | + source = downloadURL; |
123 | 117 | }
|
124 | 118 | }
|
125 | 119 |
|
|
0 commit comments