Skip to content

Commit 40c929d

Browse files
committed
feat: update UpdateChecker to support major.minor version checks and simplify version compatibility logic
1 parent 0a9a7a5 commit 40c929d

2 files changed

Lines changed: 20 additions & 39 deletions

File tree

core/build.gradle.kts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -99,7 +99,7 @@ tasks.shadowJar {
9999
exclude("org/slf4j/**")
100100
mergeServiceFiles()
101101

102-
// destinationDirectory.set(file("C:\\Users\\USER\\Desktop\\TestServer\\plugins"))
102+
// destinationDirectory.set(file("C:\\Users\\USER\\Desktop\\26_2_TestServer\\plugins"))
103103
}
104104

105105
tasks.build {

core/src/main/java/github/nighter/smartspawner/updates/UpdateChecker.java

Lines changed: 19 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -22,23 +22,23 @@
2222
import java.net.HttpURLConnection;
2323
import java.net.URI;
2424
import java.time.LocalDate;
25-
import java.util.ArrayList;
2625
import java.util.HashMap;
27-
import java.util.List;
2826
import java.util.Map;
27+
import java.util.Set;
2928
import java.util.UUID;
3029
import java.util.concurrent.CompletableFuture;
3130
import java.util.stream.Collectors;
3231

3332
public class UpdateChecker implements Listener {
3433
private final JavaPlugin plugin;
3534
private final String projectId = "9tQwxSFr";
35+
// Only keep supported major.minor branches here. Patch versions are accepted automatically.
36+
private static final Set<String> SUPPORTED_MAJOR_VERSIONS = Set.of("1.21", "26.1");
3637
private boolean updateAvailable = false;
3738
private final String currentVersion;
3839
private String latestVersion = "";
3940
private String downloadUrl = "";
4041
private boolean serverVersionSupported = true;
41-
private JsonArray latestSupportedVersions = null;
4242

4343
// ANSI codes for console output
4444
private static final String RESET = "\u001B[0m";
@@ -54,6 +54,7 @@ public class UpdateChecker implements Listener {
5454
public UpdateChecker(JavaPlugin plugin) {
5555
this.plugin = plugin;
5656
this.currentVersion = plugin.getPluginMeta().getVersion();
57+
this.serverVersionSupported = isServerVersionSupported();
5758
plugin.getServer().getPluginManager().registerEvents(this, plugin);
5859

5960
checkForUpdates().thenAccept(hasUpdate -> {
@@ -96,30 +97,20 @@ private void displayUnsupportedVersionMessage() {
9697
}
9798

9899
private String getSupportedVersionsString() {
99-
if (latestSupportedVersions == null || latestSupportedVersions.isEmpty()) {
100+
if (SUPPORTED_MAJOR_VERSIONS.isEmpty()) {
100101
return "N/A";
101102
}
102-
List<String> versions = new ArrayList<>();
103-
for (JsonElement element : latestSupportedVersions) {
104-
versions.add(element.getAsString());
105-
}
106-
return String.join(", ", versions);
103+
return SUPPORTED_MAJOR_VERSIONS.stream()
104+
.sorted()
105+
.map(major -> major + ".x")
106+
.collect(Collectors.joining(", "));
107107
}
108108

109-
private boolean isServerVersionSupported(JsonObject latestVersionObj) {
109+
private boolean isServerVersionSupported() {
110110
try {
111-
String serverVersion = Bukkit.getVersion();
112-
JsonArray gameVersions = latestVersionObj.getAsJsonArray("game_versions");
113-
if (gameVersions == null || gameVersions.isEmpty()) {
114-
return true;
115-
}
116-
String cleanServerVersion = extractMinecraftVersion(serverVersion);
117-
for (JsonElement versionElement : gameVersions) {
118-
if (isVersionCompatible(cleanServerVersion, versionElement.getAsString())) {
119-
return true;
120-
}
121-
}
122-
return false;
111+
String cleanServerVersion = extractMinecraftVersion(Bukkit.getVersion());
112+
String majorVersion = extractMajorVersion(cleanServerVersion);
113+
return majorVersion != null && SUPPORTED_MAJOR_VERSIONS.contains(majorVersion);
123114
} catch (Exception e) {
124115
plugin.getLogger().warning("Error checking server version compatibility: " + e.getMessage());
125116
return true;
@@ -144,21 +135,12 @@ private String extractMinecraftVersion(String serverVersion) {
144135
return serverVersion;
145136
}
146137

147-
private boolean isVersionCompatible(String serverVersion, String supportedVersion) {
148-
try {
149-
if (serverVersion.equals(supportedVersion)) {
150-
return true;
151-
}
152-
String[] serverParts = serverVersion.split("\\.");
153-
String[] supportedParts = supportedVersion.split("\\.");
154-
if (serverParts.length >= 2 && supportedParts.length >= 2) {
155-
return Integer.parseInt(serverParts[0]) == Integer.parseInt(supportedParts[0])
156-
&& Integer.parseInt(serverParts[1]) == Integer.parseInt(supportedParts[1]);
157-
}
158-
return false;
159-
} catch (NumberFormatException e) {
160-
return serverVersion.equals(supportedVersion);
138+
private String extractMajorVersion(String minecraftVersion) {
139+
String[] parts = minecraftVersion.split("\\.");
140+
if (parts.length < 2) {
141+
return null;
161142
}
143+
return parts[0] + "." + parts[1];
162144
}
163145

164146
public CompletableFuture<Boolean> checkForUpdates() {
@@ -210,8 +192,7 @@ public CompletableFuture<Boolean> checkForUpdates() {
210192
latestVersion = latestVersionObj.get("version_number").getAsString();
211193
downloadUrl = "https://modrinth.com/plugin/" + projectId + "/version/" + latestVersion;
212194

213-
serverVersionSupported = isServerVersionSupported(latestVersionObj);
214-
latestSupportedVersions = latestVersionObj.getAsJsonArray("game_versions");
195+
serverVersionSupported = isServerVersionSupported();
215196

216197
updateAvailable = new Version(latestVersion).compareTo(new Version(currentVersion)) > 0;
217198
return updateAvailable;

0 commit comments

Comments
 (0)