Skip to content

Commit a4ee2e5

Browse files
committed
Use PaperPC's Fill v3 downloads API
1 parent 6963886 commit a4ee2e5

18 files changed

+7165
-337
lines changed

src/main/java/me/itzg/helpers/paper/InstallPaperCommand.java

Lines changed: 37 additions & 72 deletions
Original file line numberDiff line numberDiff line change
@@ -4,13 +4,11 @@
44
import java.io.IOException;
55
import java.net.URI;
66
import java.nio.file.Path;
7-
import java.util.Arrays;
87
import java.util.Collections;
98
import java.util.Objects;
109
import java.util.concurrent.Callable;
1110
import java.util.regex.Matcher;
1211
import java.util.regex.Pattern;
13-
import java.util.stream.Collectors;
1412
import lombok.Builder;
1513
import lombok.extern.slf4j.Slf4j;
1614
import me.itzg.helpers.errors.GenericException;
@@ -19,15 +17,14 @@
1917
import me.itzg.helpers.files.ManifestException;
2018
import me.itzg.helpers.files.Manifests;
2119
import me.itzg.helpers.files.ResultsFileWriter;
22-
import me.itzg.helpers.http.FailedRequestException;
2320
import me.itzg.helpers.http.Fetch;
21+
import me.itzg.helpers.http.FileDownloadStatusHandler;
2422
import me.itzg.helpers.http.SharedFetch;
2523
import me.itzg.helpers.http.SharedFetchArgs;
2624
import me.itzg.helpers.json.ObjectMappers;
27-
import me.itzg.helpers.paper.model.ReleaseChannel;
25+
import me.itzg.helpers.paper.PaperDownloadsClient.VersionBuildFile;
2826
import me.itzg.helpers.paper.model.VersionMeta;
2927
import me.itzg.helpers.sync.MultiCopyManifest;
30-
import org.jetbrains.annotations.NotNull;
3128
import picocli.CommandLine;
3229
import picocli.CommandLine.ArgGroup;
3330
import picocli.CommandLine.Command;
@@ -84,14 +81,14 @@ public void setVersion(String version) {
8481
Integer build;
8582

8683
@Option(names = "--channel", defaultValue = "default")
87-
ReleaseChannel channel;
84+
RequestedChannel channel;
8885
}
8986
}
9087

9188
@Option(names = {"--output-directory", "-o"}, defaultValue = ".")
9289
Path outputDirectory;
9390

94-
@Option(names = "--base-url", defaultValue = "https://api.papermc.io")
91+
@Option(names = "--base-url", defaultValue = "https://fill.papermc.io")
9592
String baseUrl;
9693

9794
@Option(names = "--results-file", description = ResultsFileWriter.OPTION_DESCRIPTION, paramLabel = "FILE")
@@ -123,13 +120,12 @@ public Integer call() throws Exception {
123120
else {
124121
if (requestCheckUpdates) {
125122
return checkForUpdates(client, oldManifest,
126-
inputs.coordinates.project, inputs.coordinates.version, inputs.coordinates.build,
127-
inputs.coordinates.channel);
123+
inputs.coordinates.project, inputs.coordinates.version, inputs.coordinates.build
124+
);
128125
}
129126

130127
result = downloadUsingCoordinates(client, inputs.coordinates.project,
131-
inputs.coordinates.version, inputs.coordinates.build,
132-
inputs.coordinates.channel
128+
inputs.coordinates.version, inputs.coordinates.build
133129
)
134130
.block();
135131
}
@@ -155,8 +151,7 @@ public Integer call() throws Exception {
155151
}
156152

157153
private Integer checkForUpdates(PaperDownloadsClient client, PaperManifest oldManifest,
158-
String project, String version, Integer build,
159-
ReleaseChannel channel
154+
String project, String version, Integer build
160155
) {
161156
if (oldManifest != null && oldManifest.getCustomDownloadUrl() != null) {
162157
log.info("Using custom download URL before");
@@ -173,7 +168,7 @@ private Integer checkForUpdates(PaperDownloadsClient client, PaperManifest oldMa
173168
}
174169
}
175170
else {
176-
return client.getLatestBuild(project, version, channel)
171+
return client.getLatestBuild(project, version)
177172
.map(resolvedBuild -> {
178173
if (oldManifest == null) {
179174
return logVersion(project, version, resolvedBuild);
@@ -185,14 +180,11 @@ private Integer checkForUpdates(PaperDownloadsClient client, PaperManifest oldMa
185180
return ExitCode.SOFTWARE;
186181
}
187182
})
188-
.switchIfEmpty(Mono.error(() -> new InvalidParameterException(
189-
String.format("No build found for version %s with channel %s", version, channel)
190-
)))
191183
.block();
192184
}
193185
}
194186
else {
195-
return client.getLatestVersionBuild(project, channel)
187+
return client.getLatestVersionBuild(project)
196188
.map(versionBuild -> {
197189
if (oldManifest == null) {
198190
return logVersion(project, versionBuild.getVersion(), versionBuild.getBuild());
@@ -204,9 +196,6 @@ private Integer checkForUpdates(PaperDownloadsClient client, PaperManifest oldMa
204196
return ExitCode.SOFTWARE;
205197
}
206198
})
207-
.switchIfEmpty(
208-
Mono.error(() -> new InvalidParameterException("No build found with channel " + channel))
209-
)
210199
.block();
211200
}
212201
return ExitCode.SOFTWARE;
@@ -234,72 +223,48 @@ private static boolean mismatchingVersions(PaperManifest oldManifest, String pro
234223
}
235224

236225
private Mono<Result> downloadUsingCoordinates(PaperDownloadsClient client, String project,
237-
String version, Integer build, ReleaseChannel channel
226+
String version, Integer build
238227
) {
228+
return
229+
assembleDownload(client, project, version, build)
230+
.map(result ->
231+
Result.builder()
232+
.newManifest(
233+
PaperManifest.builder()
234+
.project(project)
235+
.minecraftVersion(result.getVersion())
236+
.build(result.getBuild())
237+
.files(Collections.singleton(Manifests.relativize(outputDirectory, result.getFile())))
238+
.build()
239+
)
240+
.serverJar(result.getFile())
241+
.version(result.getVersion())
242+
.build()
243+
);
244+
}
245+
246+
private Mono<VersionBuildFile> assembleDownload(PaperDownloadsClient client, String project, String version,
247+
Integer build
248+
) {
249+
final FileDownloadStatusHandler downloadStatusHandler = Fetch.loggingDownloadStatusHandler(log);
250+
239251
if (isSpecificVersion(version)) {
240252
if (build != null) {
241-
return download(client, project, version, build)
242-
.onErrorMap(
243-
FailedRequestException::isNotFound,
244-
throwable -> new InvalidParameterException(
245-
String.format("Requested version %s, build %d is not available", version, build))
246-
);
253+
return client.download(project, outputDirectory, downloadStatusHandler, version, build);
247254
}
248255
else {
249-
return client.getLatestBuild(project, version, channel)
250-
.onErrorMap(
251-
FailedRequestException::isNotFound,
252-
throwable -> new InvalidParameterException(
253-
String.format("Requested version %s is not available", version))
254-
)
255-
.switchIfEmpty(Mono.error(() -> new InvalidParameterException(
256-
String.format("No build found for version %s with channel '%s'. Perhaps try with a different channel: %s",
257-
version, channel, channelsExcept(channel))
258-
)))
259-
.flatMap(resolvedBuild -> download(client, project, version, resolvedBuild));
256+
return client.downloadLatestBuild(project, outputDirectory, downloadStatusHandler, version);
260257
}
261258
}
262259
else {
263-
return client.getLatestVersionBuild(project, channel)
264-
.switchIfEmpty(
265-
Mono.error(() -> new InvalidParameterException(
266-
String.format("No build found with channel '%s'. Perhaps try a different channel: %s",
267-
channel, channelsExcept(channel)
268-
)))
269-
)
270-
.flatMap(resolved -> download(client, project, resolved.getVersion(), resolved.getBuild()));
260+
return client.downloadLatest(project, outputDirectory, downloadStatusHandler);
271261
}
272262
}
273263

274-
private String channelsExcept(ReleaseChannel channel) {
275-
return Arrays.stream(ReleaseChannel.values())
276-
.filter(c -> !Objects.equals(c, channel))
277-
.map(ReleaseChannel::toString)
278-
.collect(Collectors.joining(", "));
279-
}
280-
281264
private static boolean isSpecificVersion(String version) {
282265
return version != null && !version.equalsIgnoreCase("latest");
283266
}
284267

285-
private @NotNull Mono<Result> download(PaperDownloadsClient client, String project, String v, Integer b) {
286-
return client.download(project, v, b, outputDirectory, Fetch.loggingDownloadStatusHandler(log))
287-
.map(serverJar ->
288-
Result.builder()
289-
.newManifest(
290-
PaperManifest.builder()
291-
.project(project)
292-
.minecraftVersion(v)
293-
.build(b)
294-
.files(Collections.singleton(Manifests.relativize(outputDirectory, serverJar)))
295-
.build()
296-
)
297-
.serverJar(serverJar)
298-
.version(v)
299-
.build()
300-
);
301-
}
302-
303268
private Result downloadCustom(URI downloadUrl) {
304269
try (SharedFetch sharedFetch = Fetch.sharedFetch("install-paper", sharedFetchArgs.options())) {
305270
return sharedFetch

0 commit comments

Comments
 (0)