-
Notifications
You must be signed in to change notification settings - Fork 11
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Attempt to implement Mod Menu's update checker API
- Loading branch information
Showing
4 changed files
with
101 additions
and
49 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
65 changes: 65 additions & 0 deletions
65
src/main/java/io/github/gaming32/worldhost/WorldHostUpdateChecker.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,65 @@ | ||
package io.github.gaming32.worldhost; | ||
|
||
import net.minecraft.Util; | ||
import org.apache.http.impl.client.CloseableHttpClient; | ||
import org.apache.http.impl.client.HttpClients; | ||
import org.quiltmc.parsers.json.JsonReader; | ||
import org.semver4j.Semver; | ||
|
||
import java.io.IOException; | ||
import java.io.InputStreamReader; | ||
import java.io.UncheckedIOException; | ||
import java.nio.charset.StandardCharsets; | ||
import java.util.Optional; | ||
import java.util.concurrent.CompletableFuture; | ||
|
||
public class WorldHostUpdateChecker { | ||
public static final String MODRINTH_ID = "world-host"; | ||
|
||
public static CompletableFuture<Optional<String>> checkForUpdates() { | ||
return CompletableFuture.<Optional<String>>supplyAsync(() -> { | ||
try (CloseableHttpClient client = HttpClients.createMinimal()) { | ||
final String latestVersion = WorldHost.httpGet( | ||
client, "https://api.modrinth.com/v2/project/" + MODRINTH_ID + "/version", | ||
builder -> builder | ||
.addParameter("game_versions", "[\"" + WorldHost.getModVersion("minecraft") + "\"]") | ||
.addParameter("loaders", "[\"" + WorldHost.MOD_LOADER + "\"]"), | ||
input -> { | ||
try (JsonReader reader = JsonReader.json(new InputStreamReader(input, StandardCharsets.UTF_8))) { | ||
reader.beginArray(); | ||
if (!reader.hasNext()) { | ||
return null; | ||
} | ||
reader.beginObject(); | ||
while (reader.hasNext()) { | ||
final String key = reader.nextName(); | ||
if (!key.equals("version_number")) { | ||
reader.skipValue(); | ||
continue; | ||
} | ||
return reader.nextString(); | ||
} | ||
return null; | ||
} | ||
} | ||
); | ||
if (latestVersion == null) { | ||
return Optional.empty(); | ||
} | ||
if (new Semver(WorldHost.getModVersion(WorldHost.MOD_ID)).compareTo(new Semver(latestVersion)) >= 0) { | ||
return Optional.empty(); | ||
} | ||
return Optional.of(latestVersion); | ||
} catch (IOException e) { | ||
throw new UncheckedIOException(e); | ||
} | ||
}, Util.ioPool()).exceptionally(t -> { | ||
WorldHost.LOGGER.error("Failed to check for updates", t); | ||
return Optional.empty(); | ||
}); | ||
} | ||
|
||
public static String formatUpdateLink(String version) { | ||
return "https://modrinth.com/mod/" + MODRINTH_ID + "/version/" + version; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters