Solved How can I add an update checker for my plugin?

Discussion in 'Spigot Plugin Development' started by ZMarcGamerPvP, Aug 14, 2018.

Thread Status:
Not open for further replies.
  1. Hello guys;

    I need help, how could I add an update checker for my plugin?

    Thanks in advance!
     
  2. I've already done that, but I do not know how to do it and how to add it.
     
  3. There are literally sourcecodes given, where you just need to enter your Plugin.
    Then just call the methods, done.
     
  4. Yes, I just found a topic that works for me!
    Thanks equally!
     
  5. What is this error?

    Code (Java):
    [18:21:07 INFO]: [CommandProtect] Enabling CommandProtect v1.4.1
    [18:21:07 ERROR]: Error occurred while enabling CommandProtect v1.4.1 (Is it up to date?)
    java.lang.UnsupportedOperationException: Not supported yet.
            at com.zmarcgm.commandprotect.UpdateChecker.<init>(UpdateChecker.java:34) ~[?:?]
            at com.zmarcgm.commandprotect.Main.onEnable(Main.java:28) ~[?:?]
            at org.bukkit.plugin.java.JavaPlugin.setEnabled(JavaPlugin.java:321) ~[spigot-1.8.8.jar:git-Spigot-21fe707-e1ebe52]
            at org.bukkit.plugin.java.JavaPluginLoader.enablePlugin(JavaPluginLoader.java:340) [spigot-1.8.8.jar:git-Spigot-21fe707-e1ebe52]
            at org.bukkit.plugin.SimplePluginManager.enablePlugin(SimplePluginManager.java:405) [spigot-1.8.8.jar:git-Spigot-21fe707-e1ebe52]
            at org.bukkit.craftbukkit.v1_8_R3.CraftServer.loadPlugin(CraftServer.java:357) [spigot-1.8.8.jar:git-Spigot-21fe707-e1ebe52]
            at org.bukkit.craftbukkit.v1_8_R3.CraftServer.enablePlugins(CraftServer.java:317) [spigot-1.8.8.jar:git-Spigot-21fe707-e1ebe52]
            at net.minecraft.server.v1_8_R3.MinecraftServer.s(MinecraftServer.java:414) [spigot-1.8.8.jar:git-Spigot-21fe707-e1ebe52]
            at net.minecraft.server.v1_8_R3.MinecraftServer.k(MinecraftServer.java:378) [spigot-1.8.8.jar:git-Spigot-21fe707-e1ebe52]
            at net.minecraft.server.v1_8_R3.MinecraftServer.a(MinecraftServer.java:333) [spigot-1.8.8.jar:git-Spigot-21fe707-e1ebe52]
            at net.minecraft.server.v1_8_R3.DedicatedServer.init(DedicatedServer.java:263) [spigot-1.8.8.jar:git-Spigot-21fe707-e1ebe52]
            at net.minecraft.server.v1_8_R3.MinecraftServer.run(MinecraftServer.java:525) [spigot-1.8.8.jar:git-Spigot-21fe707-e1ebe52]
    Im using this code: https://www.spigotmc.org/threads/resource-update-checker.239647/
     
  6. Strahan

    Benefactor

    Are you sure the method that guy uses is still available? I followed the example to test it out but it kept crapping out throwing NPE on "if (version.length() <= 7) {" after it attempts to get current version from Spigot. Watched it make the request with Wireshark so I could see specifically what it was sending, saw in the packet Spigot returned a 301 so it makes me think the way it's accessing Spigot is no longer valid.

    [​IMG]
     
  7. So, could you help me implement it correctly? I would really appreciate it
     
  8. I i am using inventivetalent´s Spiget.
    Works like a charm.
     
  9. Can you help me implement it please?
     
  10. I use this in my RandomTP plugin.
    Code (Java):

    import java.io.BufferedReader;
    import java.io.InputStreamReader;
    import java.net.MalformedURLException;
    import java.net.URL;
    import java.net.URLConnection;

    public class UpdateChecker {
        private URL checkURL;
        private String newVersion;
        private RandomTP plugin;

        public UpdateChecker(RandomTP plugin, int projectID) {
            this.plugin = plugin;
            newVersion = plugin.getDescription().getVersion();
            plugin.log("§cChecking for updates...");
            try {
                checkURL = new URL("https://api.spigotmc.org/legacy/update.php?resource=" + projectID);
                try {
                    this.broadCastResult();
                } catch (Exception e) {
                    plugin.log("§cCouldn't check for updates, don't you have internet connection?");
                }
            }catch(MalformedURLException e) {
                plugin.log("§cCouldn't check for updates, don't you have internet connection?");
            }
       
        }
        public String getNewVersion() throws Exception {
            URLConnection con = checkURL.openConnection();
            newVersion = new BufferedReader(new InputStreamReader(con.getInputStream())).readLine();
            return newVersion;
        }
        public int versionCompare(String paramString1, String paramString2) {
            String[] arrayOfString1 = paramString1.split("\\.");
            String[] arrayOfString2 = paramString2.split("\\.");
            int i = 0;
            while ((i < arrayOfString1.length) && (i < arrayOfString2.length) && (arrayOfString1[i].equals(arrayOfString2[i]))) {
                i++;
            }
            if ((i < arrayOfString1.length) && (i < arrayOfString2.length)) {
                int j = Integer.valueOf(arrayOfString1[i]).compareTo(Integer.valueOf(arrayOfString2[i]));

                return Integer.signum(j);
            }
            return Integer.signum(arrayOfString1.length - arrayOfString2.length);
        }
        public void broadCastResult() throws Exception {
            String newvers = this.getNewVersion();
            try {
                switch (versionCompare(plugin.getDescription().getVersion(),newvers)) {
                case 0:
                    plugin.log("§aYou are using the last stable version of the plugin §6"+newvers);
                    plugin.state="§a§lStable";
                    break;
                case 1:
                    plugin.log("§bYou are using one of the beta snapshots, you might be my developer, published version §6"+newvers+
                            " §b& yours is §6"+plugin.getDescription().getVersion());
                    plugin.state="§b§lSnapshot";
                    break;
                case - 1 :
                    plugin.log("§cYour version is outdated, please download our new update version §a"+newvers);
                    plugin.state="§c§lOutdated";
                    break;
                default:
                    plugin.log("§cCouldn't check for updates, don't you have internet connection?");
                }
            } catch(Exception known) {
                plugin.log("§cCouldn't check for updates, don't you have internet connection?");
            }
        }
    }
    Then to check for updates do
    Code (Text):
    new UpdateChecker(plugin,your_resource_id);
    Just replace RandomTP with your main class name, this is valid only if you use versions like 1.3.4 or 1.5 not beta_1.5.3_snapshot
     
  11. Can you help me implement it in my code?
     
  12. Sure, what's you main class name?
     
  13. The name of the main class is: Main
     
  14. ok seriously if you know how to code you can adapt that code - I cannot understand why you are still asking to be spoonfed after being spoonfed.
     
    • Agree Agree x 2
  15. People are learning and if they need help it's ok, not everyone can easily understand.
     
  16. Okay give me a minute and I write you the code okay?
     
  17. Okay! Thanks you very much!
     
  18. major difference between what you're talking about, which is called teaching, and what you're actually doing, which is called spoonfeeding. if explain your code and show him what you're doing, he's learning, but right now he's just copy and pasting.
     
  19. Create a new class called UpdateChecker and paste this whole code then I'll ontinue helping you!
    Code (Java):

    import java.io.BufferedReader;
    import java.io.InputStreamReader;
    import java.net.MalformedURLException;
    import java.net.URL;
    import java.net.URLConnection;

    import org.bukkit.plugin.java.JavaPlugin;

    public class UpdateChecker {
        private URL checkURL;
        private String newVersion;
        private JavaPlugin plugin;

        public UpdateChecker(JavaPlugin plugin, int projectID) {
            this.plugin = plugin;
            newVersion = plugin.getDescription().getVersion();
            plugin.getServer().getConsoleSender().sendMessage("Checking for updates"); // we send the starting message to console
            try {
                checkURL = new URL("https://api.spigotmc.org/legacy/update.php?resource=" + projectID);
                try {
                    this.broadCastResult();
                } catch (Exception e) {
                    plugin.getServer().getConsoleSender().sendMessage("Couldn't check for updates");
                    // If the person using the plugin has not internet version check cannot be done
                }
            }catch(MalformedURLException e) {
                plugin.getServer().getConsoleSender().sendMessage("Couldn't check for updates");
                // If the resource id is wrong version check cannot be done
            }
           
        }
       
        // gets code from the website, if couldn't check version by the connection it will throw an exception
        public String getNewVersion() throws Exception {
            URLConnection con = checkURL.openConnection();
            newVersion = new BufferedReader(new InputStreamReader(con.getInputStream())).readLine();
            return newVersion;
        }
       
        // we compare the new and the online version to see what happens, it's a little bit hard to explain fully what this code does
        public int versionCompare(String paramString1, String paramString2) {
            String[] arrayOfString1 = paramString1.split("_")[0].split("\\.");
            String[] arrayOfString2 = paramString2.split("_")[0].split("\\.");
            int i = 0;
            while ((i < arrayOfString1.length) && (i < arrayOfString2.length) && (arrayOfString1[i].equals(arrayOfString2[i]))) {
                i++;
            }
            if ((i < arrayOfString1.length) && (i < arrayOfString2.length)) {
                int j = Integer.valueOf(arrayOfString1[i]).compareTo(Integer.valueOf(arrayOfString2[i]));

                return Integer.signum(j);
            }
            return Integer.signum(arrayOfString1.length - arrayOfString2.length);
        }
       
        // we send the console or the player or whoever we want to know that's there's or not an update
        public void broadCastResult() throws Exception {
            String publishedvers = this.getNewVersion();
            try {
                String pluginvers = plugin.getDescription().getVersion();
                switch (versionCompare(pluginvers,publishedvers)) {
                case 0:
                    plugin.getServer().getConsoleSender().sendMessage("You are using the last stable version of the plugin: " + publishedvers);
                    // If == 0 the online published version is the same as the player is using
                    break;
                case 1:
                    plugin.getServer().getConsoleSender().sendMessage("You have a version which isn't published your version: " + pluginvers
                            + " the published one " + publishedvers);
                    // If == 1 the online version is old and you have a new one because you are maybe the developer
                    break;
                case - 1 :
                    plugin.getServer().getConsoleSender().sendMessage("Your plugin is NOT updated please update to the last version: "
                            + publishedvers);
                    // If == -1 the version that he is using is old and he need to update
                    break;
                default:
                    plugin.getServer().getConsoleSender().sendMessage("Couldn't check for updates");
                }
            } catch(Exception known) {
                plugin.getServer().getConsoleSender().sendMessage("Couldn't check for updates");
            }
        }
    }
     
Thread Status:
Not open for further replies.