★ Update API

May 29, 2017
★ Update API
  • Use this API to check if there are newer versions of a plugin available / uploaded to
    the CTSNC Marketplace or to get the current plugin version.


    ★ Video Tutorials ★
    (German | English)


    1. Just copy the code below into the main or a seperate class
    Code:
        public static void update() {
    /*
    * Check if the current version is a snapshot
    * If yes: Update will be skipped to
    * prevent errors
    */
    if(Main.instance.getDescription().getVersion().contains("SNAPSHOT")) {
    Bukkit.getConsoleSender().sendMessage(Main.instance.prefix + "§cSNAPSHOT build - No update service available!");
    return;
    }
    /*
    * rsid = Resource Id
    * Replace the id with the one of the resource
    * you want to check for new updates
    */
    int rsid = 8;
    int localversion = getVersionNumber(Main.instance.getDescription().getVersion());
    try {
    /*
    * Open connection to the
    * Marketplace API to get
    * the newest resource version
    */
    HttpURLConnection con = (HttpURLConnection) new URL("http://herbystar.eu/api.php").openConnection();
    con.setDoOutput(true);
    con.setRequestMethod("POST");
    con.getOutputStream().write(("action=getResource&value=" + rsid + "&hash=1D21F3EB829FBBDA95851D5D5F7A4F17D91F69C63D953FA56EE1B5BE54").getBytes("UTF-8"));
    String content = new BufferedReader(new InputStreamReader(con.getInputStream())).readLine();
    content = formatString(content);
    int onlineVersion = getVersionNumber(content);
    /*
    * Compare local & online version
    * Print message if a update
    * is available or not
    * Switch boolean (Main class)
    */
    if(onlineVersion > localversion) {
    Main.instance.UpdateAviable = true;
    Bukkit.getConsoleSender().sendMessage("§c[§eCTSNC§c] §a-=> Update is available! <=-");
    } else {
    Main.instance.UpdateAviable = false;
    Bukkit.getConsoleSender().sendMessage("§c[§eCTSNC§c] §c-=> No Update is available! <=-");
    }
    } catch (MalformedURLException e) {
    e.printStackTrace();
    } catch (IOException e) {
    e.printStackTrace();
    }
    }

    /*
    * Format the string to parse him into an int
    */
    private static String formatString(String string) {
    String formatted = string;
    formatted = formatted.replace("[", "");
    formatted = formatted.replace("]", "");
    formatted = formatted.replace("\"", "");
    return formatted;
    }

    /*
    * Parse the formatted string into
    * an int to compare the local
    * and new version
    */
    private static int getVersionNumber(String version) {
    return Integer.parseInt(version.replace(".", ""));
    }


    2. Replace the rsid (resource id) with your marketplace plugin id


    rsid.png



    3. Use the before created update method in the OnEnable part of your plugin

    OnEnable.png



    4. If a new version is available it will be printed out to the console, else
    a message will be send that there is no newer version


    NotificationConsole.png



    5. You can use additionally a boolean to safe if a update was
    available or not (as shown in point 4.), to use
    it in other parts of your plugin
    (The PlayerJoinEvent for example)


    NotificationPlayerJoinEvent.png
  • Loading...