Skip to content
This repository was archived by the owner on Jan 2, 2023. It is now read-only.

Commit 1a53f08

Browse files
committed
Add update checker
1 parent 618136c commit 1a53f08

File tree

2 files changed

+60
-0
lines changed

2 files changed

+60
-0
lines changed
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
package com.cosmiiko.bsodod;
2+
3+
import org.apache.commons.io.IOUtils;
4+
import java.io.IOException;
5+
import java.net.URL;
6+
import java.nio.charset.Charset;
7+
import java.util.regex.Matcher;
8+
import java.util.regex.Pattern;
9+
10+
public class UpdateChecker {
11+
static String GetLatestVersion() {
12+
String gradleFile = null;
13+
try {
14+
gradleFile = IOUtils.toString(
15+
new URL("https://raw.githubusercontent.com/Cosmiiko/MC-BSODOnDeath/main/build.gradle"),
16+
Charset.defaultCharset()
17+
);
18+
} catch (IOException e) {
19+
return null;
20+
}
21+
22+
if (gradleFile == null) return null;
23+
24+
Pattern pattern = Pattern.compile("version = '(.*)'");
25+
Matcher matcher = pattern.matcher(gradleFile);
26+
27+
if (matcher.find())
28+
{
29+
return matcher.group(1);
30+
}
31+
else
32+
{
33+
return null;
34+
}
35+
}
36+
37+
static String GetCurrentVersion()
38+
{
39+
return UpdateChecker.class.getPackage().getImplementationVersion();
40+
}
41+
}

src/main/java/com/cosmiiko/bsodod/WarningScreen.java

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,25 @@ public void init()
3131
textLines.add("By clicking 'I understand', you acknowledge the risks involved in");
3232
textLines.add("playing with such a mod.");
3333

34+
String latestVersion = UpdateChecker.GetLatestVersion();
35+
String currentVersion = UpdateChecker.GetCurrentVersion();
36+
37+
if (latestVersion != null && currentVersion != null)
38+
{
39+
if (!latestVersion.equals(currentVersion))
40+
{
41+
textLines.add(TextFormatting.RED + "Your mod version is outdated (v" + currentVersion + ", latest is v" + latestVersion + ").");
42+
textLines.add(TextFormatting.RED + "This version may contain bugs or accidental blue screens.");
43+
}
44+
else
45+
{
46+
textLines.add(TextFormatting.GREEN + "Your mod version is up-to-date.");
47+
}
48+
}
49+
else
50+
{
51+
textLines.add(TextFormatting.GOLD + "Could not check for updates. Proceed with caution.");
52+
}
3453

3554
textVOffset = this.height/2 - (textLines.size()*LINESIZE + WARNINGSIZE)/2;
3655

0 commit comments

Comments
 (0)