Skip to content

Commit 3b1d15f

Browse files
committed
fabric: skip fabricmc API call when matching version installed
1 parent 8f6b41c commit 3b1d15f

File tree

2 files changed

+58
-2
lines changed

2 files changed

+58
-2
lines changed

src/main/java/me/itzg/helpers/fabric/FabricMetaClient.java

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -37,17 +37,21 @@ static boolean nonEmptyString(String loaderVersion) {
3737
* @param version can be latest, snapshot or specific
3838
*/
3939
public Mono<String> resolveMinecraftVersion(@Nullable String version) {
40+
if (!isLatest(version) && !isSnapshot(version)) {
41+
return Mono.just(version);
42+
}
43+
4044
return sharedFetch.fetch(
4145
uriBuilder.resolve("/v2/versions/game")
4246
)
4347
.toObjectList(VersionEntry.class)
4448
.assemble()
4549
.flatMap(versionEntries -> {
46-
if (version == null || version.equalsIgnoreCase("latest")) {
50+
if (isLatest(version)) {
4751
return findFirst(versionEntries, VersionEntry::isStable)
4852
.switchIfEmpty(Mono.error(() -> new GenericException("Unable to find any stable versions")));
4953
}
50-
else if (version.equalsIgnoreCase("snapshot")) {
54+
else if (isSnapshot(version)) {
5155
return findFirst(versionEntries, versionEntry -> !versionEntry.isStable())
5256
.switchIfEmpty(Mono.error(() -> new GenericException("Unable to find any unstable versions")));
5357
}
@@ -58,6 +62,14 @@ else if (version.equalsIgnoreCase("snapshot")) {
5862
});
5963
}
6064

65+
private static boolean isSnapshot(@Nullable String version) {
66+
return version != null && version.equalsIgnoreCase("snapshot");
67+
}
68+
69+
private static boolean isLatest(@Nullable String version) {
70+
return version == null || version.equalsIgnoreCase("latest");
71+
}
72+
6173
public Mono<String> resolveLoaderVersion(String minecraftVersion, String loaderVersion) {
6274
if (nonEmptyString(loaderVersion)) {
6375
return Mono.just(loaderVersion);

src/test/java/me/itzg/helpers/fabric/FabricLauncherInstallerTest.java

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -201,6 +201,50 @@ void testUpgradeFromVersionToVersion(WireMockRuntimeInfo wmRuntimeInfo) {
201201
.doesNotExist();
202202
}
203203

204+
@Test
205+
void testNoNetworkUsageWhenVersionMatches(WireMockRuntimeInfo wmRuntimeInfo) {
206+
final WireMock wm = wmRuntimeInfo.getWireMock();
207+
wm.loadMappingsFrom("src/test/resources/fabric");
208+
209+
final FabricLauncherInstaller installer = new FabricLauncherInstaller(
210+
tempDir
211+
);
212+
installer.setFabricMetaBaseUrl(wmRuntimeInfo.getHttpBaseUrl());
213+
214+
installer.installUsingVersions(
215+
"1.19.2", null, null
216+
);
217+
218+
wm.verifyThat(
219+
// since minecraft version is pinned
220+
0,
221+
getRequestedFor(urlEqualTo("/v2/versions/game")));
222+
wm.verifyThat(
223+
// to lookup installer version
224+
1,
225+
getRequestedFor(urlEqualTo("/v2/versions/installer")));
226+
227+
final Path expectedLauncher192 = tempDir.resolve("fabric-server-mc.1.19.2-loader.0.14.12-launcher.0.11.1.jar");
228+
assertThat(expectedLauncher192)
229+
.isNotEmptyFile()
230+
.hasContent("fabric-server-mc.1.19.2-loader.0.14.12-launcher.0.11.1");
231+
232+
// Now try again with same
233+
234+
wm.resetRequests();
235+
236+
installer.installUsingVersions(
237+
"1.19.2", "0.14.12", "0.11.1"
238+
);
239+
240+
assertThat(expectedLauncher192)
241+
.isNotEmptyFile()
242+
.hasContent("fabric-server-mc.1.19.2-loader.0.14.12-launcher.0.11.1");
243+
244+
wm.verifyThat(0, getRequestedFor(urlEqualTo("/v2/versions/game")));
245+
wm.verifyThat(0, getRequestedFor(urlEqualTo("/v2/versions/installer")));
246+
}
247+
204248
@Test
205249
@EnabledIfSystemProperty(named = "testEnableManualTests", matches = "true", disabledReason = "For manual recording")
206250
void forRecordingVersionDiscovery() {

0 commit comments

Comments
 (0)