Use modrinth for update checker

This commit is contained in:
PiggyPiglet
2026-02-03 16:52:25 +08:00
parent 2a3f4482a0
commit d561afbb63

View File

@@ -26,6 +26,8 @@ import java.net.URL;
import java.util.Arrays;
import javax.net.ssl.HttpsURLConnection;
import com.google.gson.JsonElement;
import com.google.gson.JsonParser;
import me.clip.placeholderapi.PlaceholderAPIPlugin;
import me.clip.placeholderapi.scheduler.scheduling.schedulers.TaskScheduler;
import me.clip.placeholderapi.util.Msg;
@@ -36,12 +38,13 @@ import org.bukkit.event.Listener;
import org.bukkit.event.player.PlayerJoinEvent;
public class UpdateChecker implements Listener {
private static final String MODRINTH_URL = "https://api.modrinth.com/v2/project/lKEzGugV/version";
private static final int RESOURCE_ID = 6245;
private final PlaceholderAPIPlugin plugin;
private final TaskScheduler scheduler;
private final String pluginVersion;
private String spigotVersion;
private String modrinthVersion;
private boolean updateAvailable;
public UpdateChecker(PlaceholderAPIPlugin plugin) {
@@ -54,27 +57,27 @@ public class UpdateChecker implements Listener {
return updateAvailable;
}
public String getSpigotVersion() {
return spigotVersion;
public String getModrinthVersion() {
return modrinthVersion;
}
public void fetch() {
scheduler.runTaskAsynchronously(() -> {
try {
HttpsURLConnection con = (HttpsURLConnection) new URL(
"https://api.spigotmc.org/legacy/update.php?resource=" + RESOURCE_ID).openConnection();
HttpsURLConnection con = (HttpsURLConnection) new URL(MODRINTH_URL).openConnection();
con.setRequestMethod("GET");
spigotVersion = new BufferedReader(new InputStreamReader(con.getInputStream())).readLine();
final JsonElement json = JsonParser.parseReader(new BufferedReader(new InputStreamReader(con.getInputStream())));
modrinthVersion = json.getAsJsonArray().get(0).getAsJsonObject().get("version_number").getAsString();
} catch (Exception ex) {
plugin.getLogger().info("Failed to check for updates on spigot.");
plugin.getLogger().info("Failed to check for updates on modrinth.");
return;
}
if (spigotVersion == null || spigotVersion.isEmpty()) {
if (modrinthVersion == null || modrinthVersion.isEmpty()) {
return;
}
updateAvailable = spigotIsNewer();
updateAvailable = modrinthIsNewer();
if (!updateAvailable) {
return;
@@ -82,21 +85,21 @@ public class UpdateChecker implements Listener {
scheduler.runTask(() -> {
plugin.getLogger()
.info("An update for PlaceholderAPI (v" + getSpigotVersion() + ") is available at:");
.info("An update for PlaceholderAPI (v" + getModrinthVersion() + ") is available at:");
plugin.getLogger()
.info("https://www.spigotmc.org/resources/placeholderapi." + RESOURCE_ID + "/");
.info("https://modrinth.com/plugin/placeholderapi");
Bukkit.getPluginManager().registerEvents(this, plugin);
});
});
}
private boolean spigotIsNewer() {
if (spigotVersion == null || spigotVersion.isEmpty()) {
private boolean modrinthIsNewer() {
if (modrinthVersion == null || modrinthVersion.isEmpty()) {
return false;
}
int[] plV = toReadable(pluginVersion);
int[] spV = toReadable(spigotVersion);
int[] spV = toReadable(modrinthVersion);
if (plV[0] < spV[0]) {
return true;
@@ -119,10 +122,9 @@ public class UpdateChecker implements Listener {
public void onJoin(PlayerJoinEvent e) {
if (e.getPlayer().hasPermission("placeholderapi.updatenotify")) {
Msg.msg(e.getPlayer(),
"&bAn update for &fPlaceholder&7API &e(&fPlaceholder&7API &fv" + getSpigotVersion()
"&bAn update for &fPlaceholder&7API &e(&fPlaceholder&7API &fv" + getModrinthVersion()
+ "&e)"
, "&bis available at &ehttps://www.spigotmc.org/resources/placeholderapi." + RESOURCE_ID
+ "/");
, "&bis available at &ehttps://modrinth.com/plugin/placeholderapi");
}
}
}