99import java .net .http .HttpResponse ;
1010import java .nio .charset .StandardCharsets ;
1111import java .time .Duration ;
12+ import java .util .Optional ;
1213import net .fabricmc .fabric .api .event .lifecycle .v1 .ServerLifecycleEvents ;
1314import net .fabricmc .loader .api .FabricLoader ;
1415import net .fabricmc .loader .api .Version ;
@@ -29,25 +30,29 @@ public static void checkForUpdates() {
2930 EssentialCommands .LOGGER .warn ("Failed to check for Essential Commands updates." );
3031 return ;
3132 }
33+ String currentVersionStr = modMetadata .getVersion ().getFriendlyString ();
3234
3335 // The MC version the server/client is currently running, e.g. "26.1" or "1.21.6".
3436 String mcVersion = FabricLoader .getInstance ()
3537 .getModContainer ("minecraft" )
3638 .map (c -> c .getMetadata ().getVersion ().getFriendlyString ())
3739 .orElse (null );
3840 if (mcVersion == null ) {
39- EssentialCommands .LOGGER .warn ("Could not determine Minecraft version; skipping update check." );
41+ EssentialCommands .LOGGER .warn (
42+ "Could not determine Minecraft version; skipping update check."
43+ );
4044 return ;
4145 }
4246
4347 // game_versions and loaders are JSON-array query params, so they must be URL-encoded.
4448 String gameVersionsParam = encode ("[\" %s\" ]" .formatted (mcVersion ));
4549 String loadersParam = encode ("[\" fabric\" ]" );
46- String uri = "https://api.modrinth.com/v2/project/%s/version?game_versions=%s&loaders=%s" .formatted (
47- MODRINTH_SLUG ,
48- gameVersionsParam ,
49- loadersParam
50- );
50+ String uri =
51+ "https://api.modrinth.com/v2/project/%s/version?game_versions=%s&loaders=%s" .formatted (
52+ MODRINTH_SLUG ,
53+ gameVersionsParam ,
54+ loadersParam
55+ );
5156
5257 client
5358 .sendAsync (
@@ -56,52 +61,68 @@ public static void checkForUpdates() {
5661 .version (HttpClient .Version .HTTP_2 )
5762 .header (
5863 "User-Agent" ,
59- "jpcode.dev/essential-commands/%s" .formatted (modMetadata .getVersion ().getFriendlyString ())
64+ "jpcode.dev/essential-commands/%s" .formatted (
65+ modMetadata .getVersion ().getFriendlyString ()
66+ )
6067 )
6168 .GET ()
6269 .build (),
6370 HttpResponse .BodyHandlers .ofString ()
6471 )
65- .thenAcceptAsync ((HttpResponse <String > response ) -> {
66- if (response .statusCode () != 200 ) {
67- EssentialCommands .LOGGER .warn (
68- "Update check failed: Modrinth returned status {}." ,
69- response .statusCode ()
70- );
72+ .thenApply (response -> parseLatestVersionFromResponse (response , mcVersion ))
73+ .thenAccept (latestVersionStr -> {
74+ if (latestVersionStr .isEmpty ()) {
7175 return ;
7276 }
77+ compareAndNotify (currentVersionStr , latestVersionStr .get ());
78+ });
79+ }
7380
74- // Response is an array of version objects, ordered newest-first by date_published.
75- JsonArray versions = JsonParser .parseString (response .body ()).getAsJsonArray ();
76- if (versions .isEmpty ()) {
77- EssentialCommands .LOGGER .info ("No Essential Commands release found for Minecraft {}." , mcVersion );
78- return ;
79- }
81+ private static Optional <String > parseLatestVersionFromResponse (
82+ HttpResponse <String > response ,
83+ String mcVersion
84+ ) {
85+ if (response .statusCode () != 200 ) {
86+ EssentialCommands .LOGGER .warn (
87+ "Update check failed: Modrinth returned status {}." ,
88+ response .statusCode ()
89+ );
90+ return Optional .empty ();
91+ }
8092
81- String latestVersionStr = versions .get (0 ).getAsJsonObject ().get ("version_number" ).getAsString ();
82- String currentVersionStr = modMetadata .getVersion ().getFriendlyString ();
93+ JsonArray versions = JsonParser .parseString (response .body ()).getAsJsonArray ();
94+ if (versions .isEmpty ()) {
95+ EssentialCommands .LOGGER .info (
96+ "No Essential Commands release found for Minecraft {}." ,
97+ mcVersion
98+ );
99+ return Optional .empty ();
100+ }
101+ return Optional .of (versions .get (0 ).getAsJsonObject ().get ("version_number" ).getAsString ());
102+ }
83103
84- try {
85- Version currentVers = Version .parse (stripMinecraftVersion (currentVersionStr ));
86- Version latestVers = Version .parse (stripMinecraftVersion (latestVersionStr ));
87- if (latestVers .compareTo (currentVers ) > 0 ) {
88- String updateMessage = String .format (
89- "A new version of Essential Commands is available. Current: '%s' Latest: '%s'. Get the new version at %s" ,
90- currentVersionStr ,
91- latestVersionStr ,
92- "https://modrinth.com/mod/essential-commands"
93- );
94- EssentialCommands .LOGGER .info (updateMessage );
95- ServerLifecycleEvents .SERVER_STARTED .register (server ->
96- EssentialCommands .LOGGER .info (updateMessage )
97- );
98- } else {
99- EssentialCommands .LOGGER .info ("Essential Commands is up to date!" );
100- }
101- } catch (VersionParsingException e ) {
102- e .printStackTrace ();
103- }
104- });
104+ private static void compareAndNotify (String currentVersionStr , String latestVersionStr ) {
105+ try {
106+ Version currentVers = Version .parse (stripMinecraftVersion (currentVersionStr ));
107+ Version latestVers = Version .parse (stripMinecraftVersion (latestVersionStr ));
108+
109+ if (latestVers .compareTo (currentVers ) > 0 ) {
110+ String updateMessage = String .format (
111+ "A new version of Essential Commands is available. Current: '%s' Latest: '%s'. Get the new version at %s" ,
112+ currentVersionStr ,
113+ latestVersionStr ,
114+ "https://modrinth.com/mod/essential-commands"
115+ );
116+ EssentialCommands .LOGGER .info (updateMessage );
117+ ServerLifecycleEvents .SERVER_STARTED .register (server ->
118+ EssentialCommands .LOGGER .info (updateMessage )
119+ );
120+ } else {
121+ EssentialCommands .LOGGER .info ("Essential Commands is up to date!" );
122+ }
123+ } catch (VersionParsingException e ) {
124+ e .printStackTrace ();
125+ }
105126 }
106127
107128 private static String encode (String s ) {
0 commit comments