1616import me .itzg .helpers .files .ManifestException ;
1717import me .itzg .helpers .files .Manifests ;
1818import me .itzg .helpers .files .ResultsFileWriter ;
19+ import me .itzg .helpers .http .FailedRequestException ;
1920import me .itzg .helpers .http .Fetch ;
2021import me .itzg .helpers .http .SharedFetch ;
2122import me .itzg .helpers .http .SharedFetchArgs ;
2223import me .itzg .helpers .json .ObjectMappers ;
24+ import me .itzg .helpers .paper .model .ReleaseChannel ;
2325import me .itzg .helpers .paper .model .VersionMeta ;
2426import me .itzg .helpers .sync .MultiCopyManifest ;
27+ import org .jetbrains .annotations .NotNull ;
2528import picocli .CommandLine ;
2629import picocli .CommandLine .ArgGroup ;
2730import picocli .CommandLine .Command ;
@@ -73,6 +76,9 @@ public void setVersion(String version) {
7376
7477 @ Option (names = "--build" )
7578 Integer build ;
79+
80+ @ Option (names = "--channel" , defaultValue = "default" )
81+ ReleaseChannel channel ;
7682 }
7783 }
7884
@@ -106,9 +112,11 @@ public Integer call() throws Exception {
106112 result = downloadCustom (inputs .downloadUrl );
107113 }
108114 else {
109- result = useCoordinates (client , inputs .coordinates .project ,
110- inputs .coordinates .version , inputs .coordinates .build
111- );
115+ result = downloadUsingCoordinates (client , inputs .coordinates .project ,
116+ inputs .coordinates .version , inputs .coordinates .build ,
117+ inputs .coordinates .channel
118+ )
119+ .block ();
112120 }
113121 }
114122
@@ -126,30 +134,60 @@ public Integer call() throws Exception {
126134 return ExitCode .OK ;
127135 }
128136
129- private Result useCoordinates (PaperDownloadsClient client , String project , String version , Integer build ) {
130- return resolveVersion (client , project , version )
131- .flatMap (v -> resolveBuild (client , project , v , build )
132- .flatMap (b -> {
133- log .info ("Resolved {} to version {} build {}" , project , v , b );
134-
135- return client .download (project , v , b , outputDirectory , Fetch .loggingDownloadStatusHandler (log ))
136- .map (serverJar ->
137- Result .builder ()
138- .newManifest (
139- PaperManifest .builder ()
140- .project (project )
141- .minecraftVersion (v )
142- .build (b )
143- .files (Collections .singleton (Manifests .relativize (outputDirectory , serverJar )))
144- .build ()
145- )
146- .serverJar (serverJar )
147- .version (v )
148- .build ()
149- );
150- })
151- )
152- .block ();
137+ private Mono <Result > downloadUsingCoordinates (PaperDownloadsClient client , String project ,
138+ String version , Integer build , ReleaseChannel channel
139+ ) {
140+ if (isSpecificVersion (version )) {
141+ if (build != null ) {
142+ return download (client , project , version , build )
143+ .onErrorMap (
144+ FailedRequestException ::isNotFound ,
145+ throwable -> new InvalidParameterException (
146+ String .format ("Requested version %s, build %d is not available" , version , build ))
147+ );
148+ }
149+ else {
150+ return client .getLatestBuild (project , version , channel )
151+ .onErrorMap (
152+ FailedRequestException ::isNotFound ,
153+ throwable -> new InvalidParameterException (
154+ String .format ("Requested version %s is not available" , version ))
155+ )
156+ .switchIfEmpty (Mono .error (() -> new InvalidParameterException (
157+ String .format ("No build found for version %s with channel %s" , version , channel )
158+ )))
159+ .flatMap (resolvedBuild -> download (client , project , version , resolvedBuild ));
160+ }
161+ }
162+ else {
163+ return client .getLatestVersionBuild (project , channel )
164+ .switchIfEmpty (
165+ Mono .error (() -> new InvalidParameterException ("No build found with channel " + channel ))
166+ )
167+ .flatMap (resolved -> download (client , project , resolved .getVersion (), resolved .getBuild ()));
168+ }
169+ }
170+
171+ private static boolean isSpecificVersion (String version ) {
172+ return version != null && !version .equalsIgnoreCase ("latest" );
173+ }
174+
175+ private @ NotNull Mono <Result > download (PaperDownloadsClient client , String project , String v , Integer b ) {
176+ return client .download (project , v , b , outputDirectory , Fetch .loggingDownloadStatusHandler (log ))
177+ .map (serverJar ->
178+ Result .builder ()
179+ .newManifest (
180+ PaperManifest .builder ()
181+ .project (project )
182+ .minecraftVersion (v )
183+ .build (b )
184+ .files (Collections .singleton (Manifests .relativize (outputDirectory , serverJar )))
185+ .build ()
186+ )
187+ .serverJar (serverJar )
188+ .version (v )
189+ .build ()
190+ );
153191 }
154192
155193 private Result downloadCustom (URI downloadUrl ) {
@@ -217,29 +255,4 @@ private PaperManifest loadOldManifest() {
217255 }
218256 }
219257
220- private Mono <String > resolveVersion (PaperDownloadsClient client , String project , String version ) {
221- if (version .equals ("latest" )) {
222- return client .getLatestProjectVersion (project );
223- }
224- return client .hasVersion (project , version )
225- .flatMap (exists -> exists ? Mono .just (version ) : Mono .error (() -> new InvalidParameterException (
226- String .format ("Version %s does not exist for the project %s" ,
227- version , project
228- ))));
229- }
230-
231- private Mono <Integer > resolveBuild (PaperDownloadsClient client , String project , String version , Integer build ) {
232- if (build == null ) {
233- return client .getLatestBuild (project , version );
234- }
235- else {
236- return client .hasBuild (project , version , build )
237- .flatMap (exists -> exists ? Mono .just (build ) : Mono .error (() ->
238- new InvalidParameterException (String .format ("Build %d does not exist for project %s version %s" ,
239- build , project , version
240- )
241- )
242- ));
243- }
244- }
245258}
0 commit comments