Skip to content

Commit 6cc9dff

Browse files
committed
Merge remote-tracking branch 'origin/main'
2 parents 1da7838 + 8597d0a commit 6cc9dff

5 files changed

Lines changed: 116 additions & 2 deletions

File tree

.github/workflows/build.yml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,8 @@ jobs:
1919
with:
2020
java-version: 21
2121
distribution: "temurin"
22+
- name: Inject Version File
23+
run: echo "${{ github.run_number }}" > src/main/resources/flint_version.txt
2224
- name: Build with Gradle
2325
run: ./gradlew build '-Pversion=${{ github.run_number }}'
2426
- name: Upload Artifact

src/main/java/dev/dfonline/flint/Flint.java

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
import dev.dfonline.flint.feature.trait.TickedFeature;
1818
import dev.dfonline.flint.feature.trait.TooltipRenderFeature;
1919
import dev.dfonline.flint.feature.trait.WorldRenderFeature;
20+
import dev.dfonline.flint.util.FlintUpdate;
2021
import dev.dfonline.flint.util.Logger;
2122
import dev.dfonline.flint.util.result.EventResult;
2223
import net.fabricmc.api.ClientModInitializer;
@@ -57,6 +58,8 @@ public void onInitializeClient() {
5758
// FlintAPI.setDebugging(true);
5859
// FlintAPI.confirmLocationWithLocate();
5960

61+
FlintUpdate.fetchLatestRelease();
62+
6063
FEATURE_MANAGER.registerAll(
6164
// Debug
6265
new StateDebugDisplayFeature(),
@@ -70,7 +73,6 @@ public void onInitializeClient() {
7073
new ModeTrackerFeature(),
7174
new GetActionDumpFeature(),
7275
new FlintCommandFeature()
73-
7476
);
7577

7678
this.registerEventCallbacks();

src/main/java/dev/dfonline/flint/feature/impl/ModeTrackerFeature.java

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
import dev.dfonline.flint.hypercube.Mode;
99
import dev.dfonline.flint.hypercube.Plot;
1010
import dev.dfonline.flint.hypercube.PlotSize;
11+
import dev.dfonline.flint.util.FlintUpdate;
1112
import dev.dfonline.flint.util.Logger;
1213
import dev.dfonline.flint.util.result.EventResult;
1314
import net.minecraft.block.BlockState;
@@ -41,6 +42,7 @@ public class ModeTrackerFeature implements PacketListeningFeature, TickedFeature
4142
private PendingModeSwitchAction pendingAction = PendingModeSwitchAction.CLEAR_TITLE;
4243
private static boolean hasQueuedLocate = false;
4344
private static Mode queuedMode = null;
45+
private static boolean sentUpdateMessageThisSession = false;
4446

4547
@Override
4648
public boolean alwaysOn() {
@@ -131,6 +133,12 @@ public void tick() {
131133
});
132134
}
133135
if (queuedMode != null) {
136+
if (!sentUpdateMessageThisSession) {
137+
138+
FlintUpdate.sendUpdateMessage();
139+
140+
sentUpdateMessageThisSession = true;
141+
}
134142
setMode(queuedMode);
135143
queuedMode = null;
136144
}
@@ -181,6 +189,7 @@ public void tick() {
181189
@Override
182190
public void onDisconnect() {
183191
setMode(Mode.NONE);
192+
sentUpdateMessageThisSession = false;
184193
}
185194

186195
private enum PendingModeSwitchAction {
Lines changed: 99 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,99 @@
1+
package dev.dfonline.flint.util;
2+
3+
import com.google.gson.JsonObject;
4+
import com.google.gson.JsonParser;
5+
import dev.dfonline.flint.Flint;
6+
import dev.dfonline.flint.util.message.impl.prefix.InfoMessage;
7+
import net.fabricmc.loader.api.FabricLoader;
8+
import net.kyori.adventure.text.Component;
9+
import net.kyori.adventure.text.event.ClickEvent;
10+
import net.kyori.adventure.text.event.HoverEvent;
11+
12+
import java.io.BufferedReader;
13+
import java.io.IOException;
14+
import java.io.InputStream;
15+
import java.io.InputStreamReader;
16+
import java.net.URI;
17+
import java.net.http.HttpClient;
18+
import java.net.http.HttpRequest;
19+
import java.net.http.HttpResponse;
20+
21+
public final class FlintUpdate {
22+
23+
private static final Logger LOGGER = Logger.of(FlintUpdate.class);
24+
private static final String MOD_REPOSITORY = "DFOnline/Flint";
25+
private static final String MODRINTH_URL = "https://modrinth.com/mod/flint/versions";
26+
private static final String MOD_VERSION = getCurrentVersion();
27+
private static String latestVersion;
28+
29+
private FlintUpdate() {
30+
}
31+
32+
public static void fetchLatestRelease() {
33+
String url = String.format("https://api.github.com/repos/%s/releases/latest", MOD_REPOSITORY);
34+
35+
try (HttpClient client = HttpClient.newHttpClient()) {
36+
HttpRequest request = HttpRequest.newBuilder()
37+
.uri(URI.create(url))
38+
.build();
39+
40+
client.sendAsync(request, HttpResponse.BodyHandlers.ofString())
41+
.thenApply(HttpResponse::body)
42+
.thenAccept(responseBody -> {
43+
JsonObject jsonResponse = JsonParser.parseString(responseBody).getAsJsonObject();
44+
latestVersion = jsonResponse.get("tag_name").getAsString();
45+
LOGGER.info("Latest version: {}", latestVersion);
46+
})
47+
.exceptionally(e -> {
48+
LOGGER.error("Error while fetching version", e);
49+
return null;
50+
});
51+
}
52+
}
53+
54+
private static String getCurrentVersion() {
55+
try (InputStream input = Flint.class.getClassLoader().getResourceAsStream("flint_version.txt")) {
56+
if (input == null) {
57+
return "unknown";
58+
}
59+
try (BufferedReader reader = new BufferedReader(new InputStreamReader(input))) {
60+
return reader.readLine();
61+
}
62+
} catch (IOException e) {
63+
if (FabricLoader.getInstance().isDevelopmentEnvironment()) {
64+
return "unknown";
65+
}
66+
LOGGER.error("Failed to get mod version", e);
67+
return "error";
68+
}
69+
}
70+
71+
72+
public static void sendUpdateMessage() {
73+
if (MOD_VERSION.equals(latestVersion)) {
74+
return;
75+
}
76+
77+
try {
78+
// Latest version starts with a v.
79+
int versionInt = Integer.parseInt(latestVersion.substring(1));
80+
// Current version does not start with a v.
81+
int modInt = Integer.parseInt(MOD_VERSION);
82+
// The string parsed to an integer, now compare.
83+
if (versionInt <= modInt) {
84+
return;
85+
}
86+
// We are outdated, inform the user.
87+
if (Flint.getClient().player != null) {
88+
Flint.getUser().sendMessage(new InfoMessage("flint.update",
89+
Component.text("v" + MOD_VERSION),
90+
Component.text(latestVersion),
91+
Component.translatable("flint.update.link", PaletteColor.SKY_LIGHT_2)
92+
.clickEvent(ClickEvent.openUrl(MODRINTH_URL))
93+
.hoverEvent(HoverEvent.showText(Component.text(MODRINTH_URL, PaletteColor.GRAY_LIGHT)))
94+
));
95+
}
96+
} catch (NumberFormatException ignored) {
97+
}
98+
}
99+
}

src/main/resources/assets/flint/lang/en_us.json

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,5 +11,7 @@
1111
"flint.command.flint.action_dump.progress": "Reading action dump, %s second(s) elapsed, %s characters",
1212
"flint.command.flint.action_dump.success": "Action dump written in %s seconds! lines: %s, length: %s",
1313
"flint.locate.timeout.title": "Locate timeout",
14-
"flint.locate.timeout.description": "Failed to locate after %s second(s)."
14+
"flint.locate.timeout.description": "Failed to locate after %s second(s).",
15+
"flint.update": "There is a new Flint release! Current: %s, Latest: %s. %s",
16+
"flint.update.link": "Click here to download"
1517
}

0 commit comments

Comments
 (0)