Update checker - Am I doing this right?

Discussion in 'Spigot Plugin Development' started by DarkChaosRunner, Jun 17, 2015.

  1. Was about to start a page asking how to do this, but figured it out. I started making a plugin based on a concept, not for the fact that I needed to master the code. Therefor my knowledge of this language is minimal.

    I've been looking for a simple non library required set of code to check if there is a newer version of the plugin. since that search got me absolutely nowhere I started writing something myself. I've not seen this here before (may be blindly mistaken!). And that of course makes me wonder why, am I doing it wrong?

    Code (Text):

    String content = null;
    URLConnection connection = null;
    try {
        connection =  new URL("http://www.spigotmc.org/resources/project-manager.8271/").openConnection();
        connection.setRequestProperty("User-Agent", "Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.11 (KHTML, like Gecko) Chrome/23.0.1271.95 Safari/537.11");  
        Scanner scanner = new Scanner(connection.getInputStream());
        content = scanner.next();
    }catch ( Exception ex ) {
        System.out.println("Could not retrieve version info, please try again.");
    String siteversion = content.toString();
    PluginDescriptionFile pdf = this.getDescription();
    Pattern p = Pattern.compile("<h3>Version (\\S+)</h3>");
    Matcher m = p.matcher(siteversion);
    if (m.find()){
        String codeGroup = m.group(1);
        System.out.format("" + codeGroup);

           player.sendMessage("Version Website:" + codeGroup);
           player.sendMessage("Version Plugin:" + pdf.getVersion());                      
           player.sendMessage("not equal");
           player.sendMessage("Version Website:" + codeGroup);
           player.sendMessage("Version Plugin:" + pdf.getVersion());
    It now checks the version upon command, but going to implement this to a notification upon plugin load/log in.
    • Like Like x 1
  2. JamesJ


    Does that code work?
  3. Spigot is behind CloudFlare which prevents these types of connections when downloading a resource. If this is applied for the entire site / resource section then your code will not work.
  4. Yes it does work, it didn't work before since it was giving me a 403 error. solved that by adding this line:

    Code (Text):

    connection.setRequestProperty("User-Agent", "Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.11 (KHTML, like Gecko) Chrome/23.0.1271.95 Safari/537.11");


    As you can see the time between command execute and fetching info is minimal. (using 30mbit/s)
    #4 DarkChaosRunner, Jun 17, 2015
    Last edited: Jun 17, 2015
  5. Spigot has an api hidden somewhere
    • Agree Agree x 1
  6. Read that somewhere, but as you said hidden. Documented for sure, but not for an inexperienced Java writer like me. :(

    I'm now gonna implement this, will post the end results when I'm done. (Perhaps others can get something useful out of this)
  7. I don't believe it's documented anywhere
  8. Possibly, confused it for a sec with Maximvdw his SpigotSite API
    (which is an example of not yet do-able for me)
    • Like Like x 1