Skip to content

Commit

Permalink
Updater
Browse files Browse the repository at this point in the history
  • Loading branch information
HaHaWTH committed Dec 30, 2024
1 parent fa0c79c commit a992b13
Show file tree
Hide file tree
Showing 4 changed files with 109 additions and 0 deletions.
22 changes: 22 additions & 0 deletions src/main/java/io/wdsj/hybridfix/HybridFix.java
Original file line number Diff line number Diff line change
@@ -1,8 +1,11 @@
package io.wdsj.hybridfix;

import io.wdsj.hybridfix.config.Settings;
import io.wdsj.hybridfix.util.Updater;
import io.wdsj.hybridfix.util.Utils;
import net.minecraftforge.fml.common.Mod;
import net.minecraftforge.fml.common.event.FMLPreInitializationEvent;
import net.minecraftforge.fml.common.event.FMLServerStartedEvent;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;

Expand All @@ -29,4 +32,23 @@ public void onPreInit(FMLPreInitializationEvent event) {
}
HybridFixServer.preInit();
}


@Mod.EventHandler
public void onServerStartComplete(FMLServerStartedEvent event) {
if (Settings.checkForUpdates) {
Utils.ioWorker().submit(() -> {
LOGGER.info("Checking for updates...");
if (Updater.isUpdateAvailable()) {
LOGGER.warn("There is a new version available: {}, you're on: {}", Updater.getLatestVersion(), Updater.getCurrentVersion());
} else {
if (!Updater.isErred()) {
LOGGER.info("You are running the latest version.");
} else {
LOGGER.info("Unable to fetch version info.");
}
}
});
}
}
}
4 changes: 4 additions & 0 deletions src/main/java/io/wdsj/hybridfix/config/Settings.java
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,10 @@

@Config(modid = HybridFix.MOD_ID)
public class Settings {
@Config.Comment("Check for updates on startup.")
@Config.RequiresMcRestart
public static boolean checkForUpdates = true;

@Config.Comment("Fix Simple Difficulty(And other similar mods) thirst not getting reset on respawn.")
@Config.RequiresMcRestart
public static boolean fixCapabilityReset = true;
Expand Down
68 changes: 68 additions & 0 deletions src/main/java/io/wdsj/hybridfix/util/Updater.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
package io.wdsj.hybridfix.util;

import com.google.gson.JsonObject;
import com.google.gson.JsonParser;
import io.wdsj.hybridfix.HybridFix;

import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.URI;
import java.net.URL;

public class Updater {
private static String currentVersion = HybridFix.VERSION;
private static String latestVersion;
private static boolean isUpdateAvailable = false;
private static boolean isErred = false;
private static final String RELEASE_URL = "https://api.github.com/repos/HaHaWTH/HybridFix/releases/latest";

/**
* Check if there is an update available
* Note: This method will perform a network request!
* @return true if there is an update available, false otherwise
*/
public static boolean isUpdateAvailable() {
currentVersion = HybridFix.VERSION;
URI uri = URI.create(RELEASE_URL);
try {
URL url = uri.toURL();
HttpURLConnection conn = (HttpURLConnection) url.openConnection();
conn.setConnectTimeout(10000);
conn.setReadTimeout(10000);
conn.setRequestMethod("GET");
conn.setRequestProperty("Accept", "application/vnd.github+json");
try (InputStreamReader reader = new InputStreamReader(conn.getInputStream())) {
JsonObject jsonObject = new JsonParser().parse(reader).getAsJsonObject();
String latest = jsonObject.get("tag_name").getAsString();
latestVersion = latest;
isUpdateAvailable = !currentVersion.equals(latest);
reader.close();
return isUpdateAvailable;
}
} catch (Exception e) {
isErred = true;
isUpdateAvailable = false;
return false;
}
}

public static String getLatestVersion() {
return latestVersion;
}
public static String getCurrentVersion() {
return currentVersion;
}

/**
* Returns true if there is an update available, false otherwise
* Must be called after {@link Updater#isUpdateAvailable()}
* @return A boolean indicating whether there is an update available
*/
public static boolean hasUpdate() {
return isUpdateAvailable;
}

public static boolean isErred() {
return isErred;
}
}
15 changes: 15 additions & 0 deletions src/main/java/io/wdsj/hybridfix/util/Utils.java
Original file line number Diff line number Diff line change
@@ -1,8 +1,23 @@
package io.wdsj.hybridfix.util;

import com.google.common.util.concurrent.ThreadFactoryBuilder;

import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

public class Utils {
public static final String OBC_PACKAGE = "org.bukkit.craftbukkit.v1_12_R1";
public static final boolean isMohist = isClassLoaded("com.mohistmc.MohistMC");
private static final ExecutorService ioWorker = Executors.newCachedThreadPool(
new ThreadFactoryBuilder()
.setNameFormat("HybridFix I/O worker-%d")
.setPriority(Thread.NORM_PRIORITY - 2)
.build()
);

public static ExecutorService ioWorker() {
return ioWorker;
}

public static boolean isClassLoaded(String className) {
try {
Expand Down

0 comments on commit a992b13

Please sign in to comment.