77import org .bukkit .plugin .java .JavaPlugin ;
88import org .bukkit .scheduler .BukkitRunnable ;
99import org .jetbrains .annotations .Nullable ;
10+ import org .json .JSONArray ;
1011import org .json .JSONObject ;
1112
1213import java .io .InputStream ;
@@ -19,6 +20,11 @@ public UpdateChecker(JavaPlugin plugin) {
1920 }
2021
2122 private final JavaPlugin plugin ;
23+ private static final String PLUGIN_ID = "2bq9PFVl" ;
24+
25+ private String getUrl () {
26+ return "https://modrinth.com/plugin/" + PLUGIN_ID + "/changelog" ;
27+ }
2228
2329 @ EventHandler (ignoreCancelled = true )
2430 public void onJoin (PlayerJoinEvent event ) {
@@ -29,29 +35,30 @@ public void onJoin(PlayerJoinEvent event) {
2935 new BukkitRunnable () {
3036 @ Override
3137 public void run () {
32- String latest = getLatestVersion ();
3338 String current = plugin .getDescription ().getVersion ();
39+ String latest = getLatestVersion ();
3440
3541 if (latest == null ) return ;
3642 if (!current .equals (latest )) event .getPlayer ()
37- .sendMessage (ChatColor .YELLOW + "An update is available for " + pluginName + "! " + ChatColor .GOLD + "(" + current + " → " + latest + ")\n " + ChatColor .DARK_AQUA + "https://github.com/SilverstoneMC/" + pluginName + "/releases/latest" );
43+ .sendMessage (ChatColor .YELLOW + "An update is available for " + pluginName + "! " + ChatColor .GOLD + "(" + current + " → " + latest + ")\n " + ChatColor .DARK_AQUA + getUrl () );
3844 }
3945 }.runTaskAsynchronously (plugin );
4046 }
4147
42- public @ Nullable String getLatestVersion () {
43- String pluginName = plugin .getDescription ().getName ();
44-
48+ @ Nullable
49+ public String getLatestVersion () {
4550 try {
4651 // Send the request
47- InputStream url = new URI ("https://api.github .com/repos/SilverstoneMC /" + pluginName + "/releases/latest" )
48- .toURL (). openStream ();
52+ InputStream url = new URI ("https://api.modrinth .com/v2/project /" + PLUGIN_ID + "/version" ). toURL ( )
53+ .openStream ();
4954
5055 // Read the response
51- JSONObject response = new JSONObject (new String (url .readAllBytes (), StandardCharsets .UTF_8 ));
56+ JSONObject response = new JSONArray (new String (
57+ url .readAllBytes (),
58+ StandardCharsets .UTF_8 )).getJSONObject (0 );
5259 url .close ();
5360
54- return response .getString ("tag_name " );
61+ return response .getString ("version_number " );
5562
5663 } catch (Exception e ) {
5764 e .printStackTrace ();
@@ -65,6 +72,6 @@ public void logUpdate(String current, String latest) {
6572
6673 plugin .getLogger ()
6774 .warning ("An update is available for " + pluginName + "! (" + current + " → " + latest + ")" );
68- plugin .getLogger ().warning ("https://github.com/SilverstoneMC/" + pluginName + "/releases/latest" );
75+ plugin .getLogger ().warning (getUrl () );
6976 }
7077}
0 commit comments