Skip to content

Commit a19291b

Browse files
committed
Add update checker
1 parent 37e7ef1 commit a19291b

4 files changed

Lines changed: 83 additions & 9 deletions

File tree

src/main/java/net/vulkanmod/Initializer.java

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
import net.fabricmc.loader.api.FabricLoader;
66
import net.vulkanmod.config.Config;
77
import net.vulkanmod.config.Platform;
8+
import net.vulkanmod.config.UpdateChecker;
89
import net.vulkanmod.config.video.VideoModeManager;
910
import net.vulkanmod.render.chunk.build.frapi.VulkanModRenderer;
1011
import org.apache.logging.log4j.LogManager;
@@ -39,6 +40,8 @@ public void onInitializeClient() {
3940
CONFIG = loadConfig(configPath);
4041

4142
Renderer.register(VulkanModRenderer.INSTANCE);
43+
44+
UpdateChecker.checkForUpdates();
4245
}
4346

4447
private static Config loadConfig(Path path) {
Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
package net.vulkanmod.config;
2+
3+
import com.google.gson.JsonArray;
4+
import com.google.gson.JsonObject;
5+
import com.google.gson.JsonParser;
6+
import net.fabricmc.loader.api.Version;
7+
import net.fabricmc.loader.api.VersionParsingException;
8+
import net.fabricmc.loader.impl.util.version.VersionParser;
9+
import net.minecraft.SharedConstants;
10+
import net.vulkanmod.Initializer;
11+
12+
import java.io.IOException;
13+
import java.net.HttpURLConnection;
14+
import java.net.URL;
15+
import java.util.concurrent.CompletableFuture;
16+
17+
public abstract class UpdateChecker {
18+
private static boolean updateAvailable = false;
19+
20+
public static void checkForUpdates() {
21+
CompletableFuture.supplyAsync(() -> {
22+
try {
23+
String req = "https://api.modrinth.com/v2/project/vulkanmod/version?include_changelog=false";
24+
String mcVersion = SharedConstants.getCurrentVersion().name();
25+
req += "&game_versions=%s".formatted(mcVersion);
26+
27+
URL url = new URL(req);
28+
HttpURLConnection http = (HttpURLConnection)url.openConnection();
29+
var inputStream = http.getInputStream();
30+
31+
JsonObject data = JsonParser.parseString("{ versions: " + new String(inputStream.readAllBytes()) + "}").getAsJsonObject();
32+
JsonArray versions = data.getAsJsonArray("versions");
33+
http.disconnect();
34+
35+
String version = String.valueOf(versions.get(0).getAsJsonObject().get("version_number")).replace("\"", "");
36+
37+
var currentVersion = VersionParser.parseSemantic(Initializer.getVersion());
38+
updateAvailable = currentVersion.compareTo(Version.parse(version)) < 0;
39+
40+
if (updateAvailable) {
41+
Initializer.LOGGER.info("Update available!");
42+
}
43+
}
44+
catch (IOException e) {
45+
Initializer.LOGGER.info("Error occurred, skipping update check.");
46+
}
47+
catch (VersionParsingException e) {
48+
Initializer.LOGGER.info("Unable to parse version, skipping update check.");
49+
}
50+
51+
return null;
52+
});
53+
}
54+
55+
public static boolean isUpdateAvailable() {
56+
return updateAvailable;
57+
}
58+
}

src/main/java/net/vulkanmod/config/gui/VOptionScreen.java

Lines changed: 21 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package net.vulkanmod.config.gui;
22

33
import com.google.common.collect.Lists;
4+
import net.minecraft.ChatFormatting;
45
import net.minecraft.Util;
56
import net.minecraft.client.gui.GuiGraphics;
67
import net.minecraft.client.gui.components.events.GuiEventListener;
@@ -12,6 +13,7 @@
1213
import net.minecraft.resources.ResourceLocation;
1314
import net.minecraft.util.FormattedCharSequence;
1415
import net.vulkanmod.Initializer;
16+
import net.vulkanmod.config.UpdateChecker;
1517
import net.vulkanmod.config.gui.render.GuiRenderer;
1618
import net.vulkanmod.config.gui.widget.VAbstractWidget;
1719
import net.vulkanmod.config.gui.widget.VButtonWidget;
@@ -24,6 +26,7 @@
2426
import java.util.List;
2527

2628
public class VOptionScreen extends Screen {
29+
public final static int MARGIN = 20;
2730
public final static int RED = ColorUtil.ARGB.pack(0.3f, 0.0f, 0.0f, 0.8f);
2831
final ResourceLocation ICON = ResourceLocation.fromNamespaceAndPath("vulkanmod", "vlogo_transparent.png");
2932

@@ -88,15 +91,13 @@ protected void init() {
8891
int bottom = 60;
8992
int itemHeight = 20;
9093

91-
int leftMargin = 100;
92-
// int listWidth = (int) (this.width * 0.65f);
93-
int listWidth = Math.min((int) (this.width * 0.65f), 420);
94+
int leftMargin = MARGIN + 90;
95+
int listWidth = Math.min(this.width - leftMargin - MARGIN, 420);
9496
int listHeight = this.height - top - bottom;
9597

9698
this.buildLists(leftMargin, top, listWidth, listHeight, itemHeight);
9799

98100
int x = leftMargin + listWidth + 10;
99-
// int width = Math.min(this.width - this.tooltipX - 10, 200);
100101
int width = this.width - x - 10;
101102
int y = 50;
102103

@@ -147,8 +148,7 @@ private void buildPage() {
147148
this.pageButtons.clear();
148149
this.clearWidgets();
149150

150-
// this.addPageButtons(20, 6, 60, 20, false);
151-
this.addPageButtons(10, 40, 80, 22, true);
151+
this.addPageButtons(MARGIN, 40, 80, 22, true);
152152

153153
VOptionList currentList = this.optionPages.get(this.currentListIdx).getOptionList();
154154
this.addWidget(currentList);
@@ -197,6 +197,19 @@ private void addButtons() {
197197
this.addWidget(this.applyButton);
198198
this.addWidget(this.doneButton);
199199
this.addWidget(this.supportButton);
200+
201+
if (UpdateChecker.isUpdateAvailable()) {
202+
buttonWidth = minecraft.font.width(Component.translatable("vulkanmod.options.buttons.update_available")) + 10;
203+
var updateButton = new VButtonWidget(
204+
x0 - buttonWidth - buttonMargin, 6,
205+
buttonWidth, buttonHeight,
206+
Component.translatable("vulkanmod.options.buttons.update_available").withStyle(ChatFormatting.UNDERLINE),
207+
button -> Util.getPlatform().openUri("https://modrinth.com/mod/vulkanmod")
208+
);
209+
210+
this.buttons.add(updateButton);
211+
this.addWidget(updateButton);
212+
}
200213
}
201214

202215
@Override
@@ -235,9 +248,8 @@ public void render(GuiGraphics guiGraphics, int mouseX, int mouseY, float delta)
235248
GuiRenderer.guiGraphics = guiGraphics;
236249
VRenderSystem.enableBlend();
237250

238-
int size = minecraft.font.lineHeight * 4;
239-
240-
guiGraphics.blit(RenderPipelines.GUI_TEXTURED, ICON, 30, 4, 0f, 0f, size, size, size, size);
251+
int size = 36;
252+
guiGraphics.blit(RenderPipelines.GUI_TEXTURED, ICON, MARGIN + 40 - 18, 4, 0f, 0f, size, size, size, size);
241253

242254
VOptionList currentList = this.optionPages.get(this.currentListIdx).getOptionList();
243255
currentList.updateState(mouseX, mouseY);

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88

99
"vulkanmod.options.buttons.apply": "Apply",
1010
"vulkanmod.options.buttons.kofi": "Support me",
11+
"vulkanmod.options.buttons.update_available": "Update available!",
1112

1213
"vulkanmod.options.advCulling": "Advanced Chunk Culling",
1314
"vulkanmod.options.advCulling.aggressive": "Aggressive",

0 commit comments

Comments
 (0)