1. Welcome to the Site!


    Please feel free to register for an account for the site
    It provides you access to many different features and the ability to post your own threads.
    Come join and while you are at it make some money and meet other traders.

How To Make An Update Checker For MCMart In 10 Steps!

Discussion in 'Tutorials' started by GamerDuck123, May 22, 2018.

Was this helpful?

  1. Yes

    3 vote(s)
    100.0%
  2. Kind of confusing

    0 vote(s)
    0.0%
  3. No not at all

    0 vote(s)
    0.0%
  1. GamerDuck123

    GamerDuck123 Duck Dev

    Messages:
    10
    Trophy Points:
    1
    Ratings:
    +0
    Diamond Shards:
    158
    General Information​
    If you don't know what the title means, basically each time you upload a plugin there's a different version, right? Well, this tutorial will show you how you can compare the version installed on the server and match it up with the version on here, MCMart! You might say "Oh I see these all the times on spigot I can use it from there" WRONG! The tutorials you can find on spigot and bukkit all use a special API that spigot has made, this tutorial will allow you to do this with any other company other than spigot! So let's get started, shall we?

    Step 1: So I'm assuming you already have a plugin that's already posted here on MCMart, well open it up because you're going to need the link!

    Step 2: Open your workspace and go to the plugin that you want to add this update checker too.

    Step 3: Create a method in your plugin, call it anything and make sure that it is in your main class, but make sure that the return method is Boolean! EX:
    Code:
    private Boolean getDownloadChecker() {
    Step 4: Since MCMart has SSL installed you need to establish a "TrustManager" with your connection before you can connect to the website! This basically is saying that you are trustworthy and that you are not a bot and you should be able to connect to the website!
    Code:
    TrustManager[] trustAllCerts = new TrustManager[]{
                new X509ExtendedTrustManager() {
                    @Override
                    public java.security.cert.X509Certificate[] getAcceptedIssuers() {
                        return null;
                    }
    
                    @Override
                    public void checkClientTrusted(X509Certificate[] certs, String authType) {
                    }
    
                    @Override
                    public void checkServerTrusted(X509Certificate[] certs, String authType) {
                    }
    
                    @Override
                    public void checkClientTrusted(X509Certificate[] xcs, String string, Socket socket) throws CertificateException {
    
                    }
    
                    @Override
                    public void checkServerTrusted(X509Certificate[] xcs, String string, Socket socket) throws CertificateException {
    
                    }
    
                    @Override
                    public void checkClientTrusted(X509Certificate[] xcs, String string, SSLEngine ssle) throws CertificateException {
    
                    }
    
                    @Override
                    public void checkServerTrusted(X509Certificate[] xcs, String string, SSLEngine ssle) throws CertificateException {
    
                    }
    
                }
            };
    
            SSLContext sc = SSLContext.getInstance("SSL");
            sc.init(null, trustAllCerts, new java.security.SecureRandom());
            HttpsURLConnection.setDefaultSSLSocketFactory(sc.getSocketFactory());
    
            HostnameVerifier allHostsValid = new HostnameVerifier() {
                public boolean verify(String hostname, SSLSession session) {
                    return true;
                }
            };
    Step 5: Next you're going to "install" the all-trusting host verifier this is simply one line of code:
    Code:
    HttpsURLConnection.setDefaultHostnameVerifier(allHostsValid);
    Step 6: Now that you are trusted you can now connect to the website by using this code:
    Code:
    URLConnection openConnection = new URL("Insert Your Resource Link Here").openConnection();
    Step 7: Now if you leave it at this and try to get information the website will forbid you as it still thinks that it is a bot and blocks it with Connection error 403, the way to fix this is by making the connection more "realistic" by using this code:
    Code:
    openConnection.addRequestProperty("User-Agent", "Mozilla/5.0 (Windows NT 6.1; WOW64; rv:25.0) Gecko/20100101 Firefox/25.0");
    Step 8: Now you need to search the website to find your Version, this is done by looping through every line in the source code of the website and finding the one that starts with <h3>Version and then removing all the excess code around the number after Version, and you will be gifted with a double with that double you want to change it to a string and then make compare it to the version in the plugin.yml which is also a string, enough rambling on, here's the code try to dechiper it
    Code:
    BufferedReader in = new BufferedReader(new InputStreamReader(openConnection.getInputStream(), "UTF-8"));
             String inputLine;
             while ((inputLine = in.readLine()) != null) {
                if (inputLine.startsWith("<h3>Version")) {
                    String level = inputLine.replace("<h3>Version ", "").replace("</h3>", "");
                          level = ChatColor.stripColor(level);
                          double l = Double.parseDouble(level);
                          String newVersion = String.valueOf(l);
                          String oldVersion = this.getDescription().getVersion();
                      if (!newVersion.equals(oldVersion)) {
                          log.info("The plugin needs to be updated!");
                      } else {
                          log.info("Plugin is up to date");
                      }
                }
             }
    Step 9: Putting it all together.
    Code:
        private Boolean getCheckDownloadURL() {
            try {
            TrustManager[] trustAllCerts = new TrustManager[]{
                new X509ExtendedTrustManager() {
                    @Override
                    public java.security.cert.X509Certificate[] getAcceptedIssuers() {
                        return null;
                    }
    
                    @Override
                    public void checkClientTrusted(X509Certificate[] certs, String authType) {
                    }
    
                    @Override
                    public void checkServerTrusted(X509Certificate[] certs, String authType) {
                    }
    
                    @Override
                    public void checkClientTrusted(X509Certificate[] xcs, String string, Socket socket) throws CertificateException {
    
                    }
    
                    @Override
                    public void checkServerTrusted(X509Certificate[] xcs, String string, Socket socket) throws CertificateException {
    
                    }
    
                    @Override
                    public void checkClientTrusted(X509Certificate[] xcs, String string, SSLEngine ssle) throws CertificateException {
    
                    }
    
                    @Override
                    public void checkServerTrusted(X509Certificate[] xcs, String string, SSLEngine ssle) throws CertificateException {
    
                    }
    
                }
            };
    
            SSLContext sc = SSLContext.getInstance("SSL");
            sc.init(null, trustAllCerts, new java.security.SecureRandom());
            HttpsURLConnection.setDefaultSSLSocketFactory(sc.getSocketFactory());
    
            HostnameVerifier allHostsValid = new HostnameVerifier() {
                public boolean verify(String hostname, SSLSession session) {
                    return true;
                }
            };
            HttpsURLConnection.setDefaultHostnameVerifier(allHostsValid);
          
            URLConnection openConnection = new URL("https://www.mcmart.org/resources/ducks-guns.289/").openConnection();
            openConnection.addRequestProperty("User-Agent", "Mozilla/5.0 (Windows NT 6.1; WOW64; rv:25.0) Gecko/20100101 Firefox/25.0");
            
             BufferedReader in = new BufferedReader(new InputStreamReader(openConnection.getInputStream(), "UTF-8"));
             String inputLine;
             while ((inputLine = in.readLine()) != null) {
                 if (inputLine.startsWith("<h3>Version")) {
                     String level = inputLine.replace("<h3>Version ", "").replace("</h3>", "");
                           level = ChatColor.stripColor(level);
                           double l = Double.parseDouble(level);
                           String newVersion = String.valueOf(l);
                           String oldVersion = this.getDescription().getVersion();
    [CENTER]                  if (!newVersion.equals(oldVersion)) {[/CENTER]
                          log.info("The plugin needs to be updated!");
                      } else {
                          log.info("Plugin is up to date");
                      }
                 }
             }
          
    
        } catch (IOException | KeyManagementException | NoSuchAlgorithmException e)
            {
            throw new RuntimeException("Problem occurred:", e);
          }
            return false;
        
        
        }
    Step 10: Using this code will mean that it only sends messages to console letting console know that the plugin is outdated, and only when the plugin is reloaded! But to use this, just call the event in your onEnable and when it's enabled it'll send a message to console letting it know that it's outdated

    Extra​
    Wow if you read through all of that, I'm amazed! Thank you! Leave comments on how I can improve this tutorial, and if it helped or not! Thank you! Also, let me know if you would like to see more tutorials and what you want them on!

    Much Love ~ GamerDuck123​
     
    Tags:
  2. Jdsgames

    Jdsgames Manager & Scam Resolver Manager Scam Resolver Emerald Diamond

    Messages:
    311
    Trophy Points:
    38
    Ratings:
    +42
    Diamond Shards:
    738
    Very Nice.
     
  3. Bumblewasp

    Bumblewasp Server Owner

    Messages:
    10
    Trophy Points:
    1
    Ratings:
    +0
    Diamond Shards:
    127
    Going to try this out - I'll probably end up needing some help :p
     
  4. GamerDuck123

    GamerDuck123 Duck Dev

    Messages:
    10
    Trophy Points:
    1
    Ratings:
    +0
    Diamond Shards:
    158
    Haha if you need help just message me here or on discord: GamerDuck123#4262 :D
     

Share This Page

Loading...