Skip to content

Commit 83340cd

Browse files
committed
Fix #1165: Ensure that version checker always finds a newer version
1 parent ff6ffe9 commit 83340cd

File tree

1 file changed

+42
-34
lines changed

1 file changed

+42
-34
lines changed

src/client/java/aztech/modern_industrialization/misc/version/VersionEvents.java

Lines changed: 42 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,8 @@
2929
import com.google.gson.JsonElement;
3030
import com.google.gson.JsonObject;
3131
import com.google.gson.JsonParser;
32+
import java.io.BufferedReader;
33+
import java.io.InputStreamReader;
3234
import java.net.URL;
3335
import java.net.URLConnection;
3436
import java.text.SimpleDateFormat;
@@ -43,7 +45,9 @@
4345
import net.neoforged.fml.ModList;
4446
import net.neoforged.neoforge.client.event.ClientPlayerNetworkEvent;
4547
import net.neoforged.neoforge.common.NeoForge;
48+
import org.apache.maven.artifact.versioning.DefaultArtifactVersion;
4649
import org.jetbrains.annotations.NotNull;
50+
import org.jetbrains.annotations.Nullable;
4751

4852
public class VersionEvents {
4953

@@ -58,16 +62,15 @@ public int compareTo(@NotNull VersionEvents.Version o) {
5862
}
5963
}
6064

61-
private static Version fetchVersion(boolean isIncludeAlphaVersion) throws Exception {
65+
@Nullable
66+
private static Version fetchLatestVersion(boolean isIncludeAlphaVersion) throws Exception {
6267
String mcVersion = ModList.get().getModContainerById("minecraft").get().getModInfo().getVersion().toString();
6368

64-
URLConnection connection;
65-
connection = new URL(url).openConnection();
66-
try (Scanner scanner = new Scanner(connection.getInputStream())) {
67-
PriorityQueue<Version> queue = new PriorityQueue<>();
69+
URLConnection connection = new URL(url).openConnection();
70+
try (var reader = new BufferedReader(new InputStreamReader(connection.getInputStream()))) {
71+
JsonObject jo = (JsonObject) JsonParser.parseReader(reader);
6872

69-
String response = scanner.useDelimiter("\\A").next();
70-
JsonObject jo = (JsonObject) JsonParser.parseString(response);
73+
List<Version> versions = new ArrayList<>();
7174

7275
for (JsonElement file : jo.getAsJsonArray("files")) {
7376
JsonObject fileAsJsonObject = file.getAsJsonObject();
@@ -88,45 +91,50 @@ private static Version fetchVersion(boolean isIncludeAlphaVersion) throws Except
8891
String date = fileAsJsonObject.get("uploaded_at").getAsString();
8992

9093
if (isIncludeAlphaVersion || !type.equals(alphaPostfix)) {
91-
queue.add(new Version(name, url, format.parse(date)));
94+
versions.add(new Version(name, url, format.parse(date)));
9295
}
9396
}
9497

95-
if (!queue.isEmpty()) {
96-
return queue.poll();
98+
if (!versions.isEmpty()) {
99+
return Collections.min(versions);
97100
}
98-
99101
}
100102
return null;
101103
}
102104

103105
public static void startVersionCheck(ModContainer miContainer, LocalPlayer player) {
104106
new Thread(() -> {
105107
try {
106-
if (MIClientConfig.INSTANCE.newVersionMessage.getAsBoolean()) {
107-
String currentVersion = miContainer.getModInfo().getVersion().toString();
108-
Version lastVersion = fetchVersion(currentVersion.contains(alphaPostfix));
109-
110-
if (lastVersion != null) {
111-
String lastVersionString = lastVersion.name.replaceFirst("Modern Industrialization v", "").strip();
112-
113-
if (!lastVersionString.equals(currentVersion)) {
114-
String url = lastVersion.url;
115-
116-
Style styleClick = Style.EMPTY.withClickEvent(new ClickEvent(ClickEvent.Action.OPEN_URL, url))
117-
.applyFormat(ChatFormatting.UNDERLINE).applyFormat(ChatFormatting.GREEN).withHoverEvent(new HoverEvent(
118-
HoverEvent.Action.SHOW_TEXT, MIText.ClickUrl.text()));
119-
120-
Minecraft.getInstance().execute(() -> {
121-
if (Minecraft.getInstance().player == player) {
122-
player.displayClientMessage(
123-
MIText.NewVersion.text(lastVersionString,
124-
MIText.CurseForge.text().setStyle(styleClick)),
125-
false);
126-
}
127-
});
108+
if (!MIClientConfig.INSTANCE.newVersionMessage.getAsBoolean()) {
109+
return;
110+
}
111+
112+
String currentVersionString = miContainer.getModInfo().getVersion().toString();
113+
var currentVersion = new DefaultArtifactVersion(currentVersionString);
114+
var latestVersion = fetchLatestVersion(currentVersionString.contains(alphaPostfix));
115+
116+
if (latestVersion == null) {
117+
return;
118+
}
119+
120+
String latestVersionString = latestVersion.name.replaceFirst("Modern Industrialization v", "").strip();
121+
var latestArtifactVersion = new DefaultArtifactVersion(latestVersionString);
122+
123+
if (latestArtifactVersion.compareTo(currentVersion) > 0) {
124+
String url = latestVersion.url;
125+
126+
Style styleClick = Style.EMPTY.withClickEvent(new ClickEvent(ClickEvent.Action.OPEN_URL, url))
127+
.applyFormat(ChatFormatting.UNDERLINE).applyFormat(ChatFormatting.GREEN).withHoverEvent(new HoverEvent(
128+
HoverEvent.Action.SHOW_TEXT, MIText.ClickUrl.text()));
129+
130+
Minecraft.getInstance().execute(() -> {
131+
if (Minecraft.getInstance().player == player) {
132+
player.displayClientMessage(
133+
MIText.NewVersion.text(latestVersionString,
134+
MIText.CurseForge.text().setStyle(styleClick)),
135+
false);
128136
}
129-
}
137+
});
130138
}
131139
} catch (Exception e) {
132140
MI.LOGGER.error("Failed to get release information from Curseforge.", e);

0 commit comments

Comments
 (0)