Skip to content

Commit

Permalink
Improve update checker to filter loader and version
Browse files Browse the repository at this point in the history
  • Loading branch information
Archy-X committed Dec 15, 2024
1 parent 8e2b087 commit c4ab4c8
Show file tree
Hide file tree
Showing 4 changed files with 56 additions and 19 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -40,8 +40,8 @@
import dev.aurelium.auraskills.bukkit.menus.util.SlateMenuHelper;
import dev.aurelium.auraskills.bukkit.message.BukkitMessageProvider;
import dev.aurelium.auraskills.bukkit.modifier.ArmorModifierListener;
import dev.aurelium.auraskills.bukkit.modifier.ItemListener;
import dev.aurelium.auraskills.bukkit.modifier.BukkitModifierManager;
import dev.aurelium.auraskills.bukkit.modifier.ItemListener;
import dev.aurelium.auraskills.bukkit.region.BukkitRegionManager;
import dev.aurelium.auraskills.bukkit.region.BukkitWorldManager;
import dev.aurelium.auraskills.bukkit.region.RegionBlockListener;
Expand All @@ -58,6 +58,7 @@
import dev.aurelium.auraskills.bukkit.user.BukkitUserManager;
import dev.aurelium.auraskills.bukkit.util.BukkitPlatformUtil;
import dev.aurelium.auraskills.bukkit.util.MetricsUtil;
import dev.aurelium.auraskills.bukkit.util.UpdateChecker;
import dev.aurelium.auraskills.bukkit.util.armor.ArmorListener;
import dev.aurelium.auraskills.common.AuraSkillsPlugin;
import dev.aurelium.auraskills.common.ability.AbilityRegistry;
Expand Down Expand Up @@ -260,6 +261,11 @@ public void onEnable() {
leaderboardManager.startLeaderboardUpdater(); // 5 minute interval
// bStats custom charts
new MetricsUtil(getInstance()).registerCustomCharts(metrics);

if (this.configBoolean(Option.CHECK_FOR_UPDATES)) {
UpdateChecker updateChecker = new UpdateChecker(this);
updateChecker.sendUpdateMessageAsync(Bukkit.getConsoleSender());
}
});
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -318,7 +318,7 @@ public void onClaimItems(Player player) {
@CommandPermission("auraskills.command.version")
public void onVersion(CommandSender sender) {
Locale locale = plugin.getLocale(sender);
new UpdateChecker(plugin).getVersion(latestVersion -> sender.sendMessage(plugin.getPrefix(locale) +
new UpdateChecker(plugin).getVersion((latestVersion, loader) -> sender.sendMessage(plugin.getPrefix(locale) +
TextUtil.replace(plugin.getMsg(CommandMessage.VERSION, locale),
"{current_version}", plugin.getDescription().getVersion(),
"{latest_version}", latestVersion)));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@
import dev.aurelium.auraskills.common.config.Option;
import dev.aurelium.auraskills.common.storage.sql.SqlStorageProvider;
import dev.aurelium.auraskills.common.user.User;
import org.bukkit.ChatColor;
import org.bukkit.entity.Player;
import org.bukkit.event.EventHandler;
import org.bukkit.event.Listener;
Expand Down Expand Up @@ -105,12 +104,7 @@ private void sendUpdateMessage(Player player) {
}
// Check for updates
UpdateChecker updateChecker = new UpdateChecker(plugin);
updateChecker.getVersion(version -> {
if (updateChecker.isOutdated(plugin.getDescription().getVersion(), version)) {
player.sendMessage(plugin.getPrefix(plugin.getDefaultLanguage()) + ChatColor.WHITE + "New update available! You are on version " + ChatColor.AQUA + plugin.getDescription().getVersion() + ChatColor.WHITE + ", latest version is " + ChatColor.AQUA + version);
player.sendMessage(plugin.getPrefix(plugin.getDefaultLanguage()) + ChatColor.WHITE + "Download it on Modrinth: " + ChatColor.YELLOW + "" + ChatColor.UNDERLINE + "https://modrinth.com/plugin/auraskills/version/latest");
}
});
updateChecker.sendUpdateMessageAsync(player);
}, 40L);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,28 +3,58 @@
import com.google.gson.JsonArray;
import com.google.gson.JsonObject;
import com.google.gson.JsonParser;
import dev.aurelium.auraskills.bukkit.AuraSkills;
import org.bukkit.Bukkit;
import org.bukkit.plugin.Plugin;
import org.bukkit.ChatColor;
import org.bukkit.command.CommandSender;
import org.bukkit.entity.Player;

import java.net.URI;
import java.net.http.HttpClient;
import java.net.http.HttpRequest;
import java.net.http.HttpResponse;
import java.util.function.Consumer;
import java.util.function.BiConsumer;

public class UpdateChecker {

private final Plugin plugin;
private final String slug;
public static final String MODRINTH_ID = "uDdZAVls";

public UpdateChecker(Plugin plugin) {
private final AuraSkills plugin;
private final String projectId;

public UpdateChecker(AuraSkills plugin) {
this.plugin = plugin;
this.slug = "auraskills";
this.projectId = MODRINTH_ID;
}

public void sendUpdateMessageAsync(CommandSender sender) {
getVersion((version, loader) -> {
if (isOutdated(plugin.getDescription().getVersion(), version)) {
final String prefix = sender instanceof Player ? plugin.getPrefix(plugin.getDefaultLanguage()) : "[AuraSkills] ";
final String downloadLink = "https://modrinth.com/plugin/" + UpdateChecker.MODRINTH_ID + "/version/" + version + "?loader=" + loader;

sender.sendMessage(prefix + ChatColor.WHITE + "New update available! You are on version " + ChatColor.AQUA + plugin.getDescription().getVersion() + ChatColor.WHITE + ", latest version is " + ChatColor.AQUA + version);
sender.sendMessage(prefix + ChatColor.WHITE + "Download it on Modrinth: " + ChatColor.YELLOW + "" + ChatColor.UNDERLINE + downloadLink);
}
});
}

public void getVersion(final Consumer<String> consumer) {
// Consumer accepts versionNumber and loader
public void getVersion(final BiConsumer<String, String> consumer) {
Bukkit.getScheduler().runTaskAsynchronously(this.plugin, () -> {
final String url = "https://api.modrinth.com/v2/project/" + slug + "/version";
String loader;
String serverName = Bukkit.getServer().getName();
if (serverName.equalsIgnoreCase("CraftBukkit") || serverName.equalsIgnoreCase("Spigot")) {
loader = "spigot";
} else {
loader = "paper";
}

final String baseUrl = "https://api.modrinth.com/v2/project/" + projectId + "/version";

final String gameVersion = VersionUtils.getVersionString(Bukkit.getBukkitVersion());
final String query = "loaders=%5B%22" + loader + "%22%5D&game_versions=%5B%22" + gameVersion + "%22%5D";
final String url = baseUrl + "?" + query;
try {
HttpClient client = HttpClient.newHttpClient();

Expand All @@ -37,13 +67,20 @@ public void getVersion(final Consumer<String> consumer) {

if (response.statusCode() == 200) {
String responseBody = response.body();
if (responseBody == null || responseBody.trim().isEmpty()) {
return;
}

// Parse the JSON response using Gson
JsonArray jsonArray = JsonParser.parseString(responseBody).getAsJsonArray();
if (jsonArray.isEmpty()) {
return;
}

JsonObject firstElement = jsonArray.get(0).getAsJsonObject();
String versionNumber = firstElement.get("version_number").getAsString();

consumer.accept(versionNumber);
consumer.accept(versionNumber, loader);
} else {
this.plugin.getLogger().info("Cannot look for updates: Request failed with status code " + response.statusCode());
}
Expand All @@ -53,7 +90,7 @@ public void getVersion(final Consumer<String> consumer) {
});
}

public boolean isOutdated(String localVersion, String resourceVersion) {
private boolean isOutdated(String localVersion, String resourceVersion) {
if (localVersion.equalsIgnoreCase(resourceVersion)) { // Versions match exactly
return false;
}
Expand Down

0 comments on commit c4ab4c8

Please sign in to comment.