-
Notifications
You must be signed in to change notification settings - Fork 40
Expand file tree
/
Copy pathUpdateChecker.java
More file actions
45 lines (42 loc) · 1.94 KB
/
UpdateChecker.java
File metadata and controls
45 lines (42 loc) · 1.94 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
package dev.lrxh.neptune.utils;
import com.google.gson.JsonObject;
import com.google.gson.JsonParser;
import dev.lrxh.neptune.Neptune;
import dev.lrxh.neptune.configs.impl.SettingsLocale;
import lombok.experimental.UtilityClass;
import org.bukkit.Bukkit;
import java.io.IOException;
import java.net.URI;
import java.net.http.HttpClient;
import java.net.http.HttpRequest;
import java.net.http.HttpResponse;
import java.util.Objects;
@UtilityClass
public class UpdateChecker {
int behindBy = 0;
private void checkForUpdates() throws IOException, InterruptedException {
if (!SettingsLocale.CHECK_FOR_UPDATES.getBoolean()) return;
HttpRequest request = HttpRequest.newBuilder()
.uri(URI.create("https://api.github.com/repos/Solara-Development/Neptune/compare/master..." + GithubUtils.getCommitId()))
.header("Accept", "application/vnd.github+json")
.build();
HttpClient client = HttpClient.newHttpClient();
HttpResponse<String> response = client.send(request, HttpResponse.BodyHandlers.ofString());
JsonObject json = JsonParser.parseString(response.body()).getAsJsonObject();
String status = json.get("status").getAsString();
if (Objects.equals(status, "behind")) {
behindBy = json.get("behind_by").getAsInt();
Neptune.get().getLogger().info("Your Neptune version is behind by " + behindBy + (behindBy == 1 ? " version" : " versions") + "!");
Neptune.get().getLogger().info("It is recommended to update to the latest version available here: https://github.com/Solara-Development/Neptune#-installation");
}
}
public void run() {
Bukkit.getScheduler().runTaskAsynchronously(Neptune.get(), () -> {
try {
checkForUpdates();
} catch (Exception e) {
Neptune.get().getLogger().warning("Failed to check for updates! " + e.getMessage());
}
});
}
}