Skip to content

Instantly share code, notes, and snippets.

@Jaimss
Last active August 13, 2020 21:13
Show Gist options
  • Save Jaimss/f67ec1d63e043abbe5fc7ab6a6387a6d to your computer and use it in GitHub Desktop.
Save Jaimss/f67ec1d63e043abbe5fc7ab6a6387a6d to your computer and use it in GitHub Desktop.

UpdateChecker

Get latest spigot version of a resource using Spiget.

Language Support

This has both Kotlin & Java Options. Just use whatever language your plugin is in!

Your Resource ID

To get your resource id, go to your resource page. For example, PlaceholderAPI is at

Then the numbers at the end are your resource id. In the PlacholderAPI example above, the resource id is 6245.

Usage

First, you will need to include KHttp in your project. I did this via jitpack, but on their wiki they have other options if you prefer those.

Kotlin:
class Plugin : JavaPlugin() {

    override fun onEnable() {
        if (getLatestVersion(6245) != this.description.version) {
            // they need to update
            // maybe log to console and let them know
        }
    }

}
Java:
public final class Plugin extends JavaPlugin {

    @Override
    public void onEnable() {
        if (YourUtilsClass.getLatestVersion(6245) != this.getDescription().getVersion()) {
            // you gotta update
            // log to console
        }
    }
    
}

This was just an example usage. This could also be run when an admin logs in to the server, or any other time you want!

import khttp.KHttp;
import khttp.responses.Response;
import org.jetbrains.annotations.Nullable;
import org.json.JSONObject;
public class TestingJava {
@Nullable
public String getLatestVersion(int resourceId) {
Response r = KHttp.get("https://api.spiget.org/v2/resources/" + resourceId + "/versions/latest");
if (r.getStatusCode() != 200) return null;
JSONObject json = r.getJsonObject();
return json.getString("name");
}
}
import khttp.get
fun getLatestVersion(resourceId: Int): String? {
val r = get("https://api.spiget.org/v2/resources/$resourceId/versions/latest")
if (r.statusCode != 200) return null
val json = r.jsonObject
return json.getString("name")
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment