Updater Checker

Discussion in 'Plugin Development' started by Straiker123, Dec 28, 2018.

Thread Status:
Not open for further replies.
  1. Hello i tried to make Update Checker for my plugin..

    Here is my Main class (Only OnEnable):
    Code:
    @Override
    public void onEnable() {
        if (getConfig().getBoolean("Updater") == true) {
            Updater updater = new Updater(this, 309885);
            try {
                if (updater.checkForUpdates()) {
                    getServer().getConsoleSender().sendMessage(ChatColor.RED + "=-=-=-=-=-=-=-=-=--=-=-=");
                    getServer().getConsoleSender().sendMessage(ChatColor.RED + "[ChatSecurity] New update!");
                    getServer().getConsoleSender().sendMessage(ChatColor.RED + "Download new version here:");
                    getServer().getConsoleSender().sendMessage(ChatColor.LIGHT_PURPLE + "https://dev.bukkit.org/projects/chat-security/files");
                    getServer().getConsoleSender().sendMessage(ChatColor.RED + "=-=-=-=-=-=-=-=-=--=-=-=");
                } else {
                    getServer().getConsoleSender().sendMessage("[ChatSecurity] Plugin is up to date! - "
                                    + getDescription().getVersion());
                }
            } catch (Exception e) {
                getLogger().info("Could not check for updates! Stacktrace:");
                e.printStackTrace();
            }
        }


    Here is my Updater class:
    Code:
    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 Updater {
    
        private int project = 309885;
        private URL checkURL;
        private String newVersion = "";
        private JavaPlugin plugin;
    
        public Updater(JavaPlugin plugin, int projectID) {
            this.plugin = plugin;
            this.newVersion = plugin.getDescription().getVersion();
            this.project = projectID;
            try {
                this.checkURL = new URL(
                        "https://dev.bukkit.org/projects/chat-security/files/."+projectID);
            } catch (MalformedURLException e) {
                e.printStackTrace();
            }
        }
    
        public int getProjectID() {
            return project;
        }
    
        public JavaPlugin getPlugin() {
            return plugin;
        }
    
        public String getLatestVersion() {
            return newVersion;
        }
    
        public String getResourceURL() {
            return "https://dev.bukkit.org/projects/chat-security/files/." + project;
        }
    
        public boolean checkForUpdates() throws Exception {
            URLConnection con = checkURL.openConnection();
            this.newVersion = new BufferedReader(new InputStreamReader(con.getInputStream()))
                    .readLine();
            return !plugin.getDescription().getVersion().equals(newVersion);
        }
    }

    Here is error what i get when i start server:
    Code:
    [20:23:42 WARN]: java.io.IOException: Server returned HTTP response code: 403 fo
    r URL: https://dev.bukkit.org/projects/chat-security/files/.309885
    [20:23:42 WARN]:        at sun.net.www.protocol.http.HttpURLConnection.getInputS
    tream0(Unknown Source)
    [20:23:42 WARN]:        at sun.net.www.protocol.http.HttpURLConnection.getInputS
    tream(Unknown Source)
    [20:23:42 WARN]:        at sun.net.www.protocol.https.HttpsURLConnectionImpl.get
    InputStream(Unknown Source)
    [20:23:42 WARN]:        at ChatSecurity.Updater.checkForUpdates(Updater.java:48)
    
    [20:23:42 WARN]:        at ChatSecurity.ChatSecurity.onEnable(ChatSecurity.java:
    25)
    [20:23:42 WARN]:        at org.bukkit.plugin.java.JavaPlugin.setEnabled(JavaPlug
    in.java:264)
    [20:23:42 WARN]:        at org.bukkit.plugin.java.JavaPluginLoader.enablePlugin(
    JavaPluginLoader.java:339)
    [20:23:42 WARN]:        at org.bukkit.plugin.SimplePluginManager.enablePlugin(Si
    mplePluginManager.java:403)
    [20:23:42 WARN]:        at org.bukkit.craftbukkit.v1_13_R2.CraftServer.enablePlu
    gin(CraftServer.java:431)
    [20:23:42 WARN]:        at org.bukkit.craftbukkit.v1_13_R2.CraftServer.enablePlu
    gins(CraftServer.java:345)
    [20:23:42 WARN]:        at org.bukkit.craftbukkit.v1_13_R2.CraftServer.reload(Cr
    aftServer.java:804)
    [20:23:42 WARN]:        at org.bukkit.Bukkit.reload(Bukkit.java:569)
    [20:23:42 WARN]:        at org.bukkit.command.defaults.ReloadCommand.execute(Rel
    oadCommand.java:27)
    [20:23:42 WARN]:        at org.bukkit.command.SimpleCommandMap.dispatch(SimpleCo
    mmandMap.java:141)
    [20:23:42 WARN]:        at org.bukkit.craftbukkit.v1_13_R2.CraftServer.dispatchC
    ommand(CraftServer.java:699)
    [20:23:42 WARN]:        at org.bukkit.craftbukkit.v1_13_R2.CraftServer.dispatchS
    erverCommand(CraftServer.java:684)
    [20:23:42 WARN]:        at net.minecraft.server.v1_13_R2.DedicatedServer.aU(Dedi
    catedServer.java:459)
    [20:23:42 WARN]:        at net.minecraft.server.v1_13_R2.DedicatedServer.b(Dedic
    atedServer.java:418)
    [20:23:42 WARN]:        at net.minecraft.server.v1_13_R2.MinecraftServer.a(Minec
    raftServer.java:835)
    [20:23:42 WARN]:        at net.minecraft.server.v1_13_R2.MinecraftServer.run(Min
    ecraftServer.java:733)
    [20:23:42 WARN]:        at java.lang.Thread.run(Unknown Source)
     
  2. Offline

    timtower Administrator Administrator Moderator

    Moved to correct section
    @Straiker123 Why does the link contain a project number when the URL already covers that?
     
  3. Umm
    I modified the code for this:

    Main class (Only OnEnable):
    Code:
    @Override
    public void onEnable() {
        if (getConfig().getBoolean("Updater") == true) {
            Updater updater = new Updater(this);
            try {
                if (updater.checkForUpdates()) {
                    getServer().getConsoleSender().sendMessage(ChatColor.RED + "=-=-=-=-=-=-=-=-=--=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=");
                    getServer().getConsoleSender().sendMessage(ChatColor.RED + "You are using an older version of ChatSecurity!");
                    getServer().getConsoleSender().sendMessage(ChatColor.RED + "Download the newest version here:");
                    getServer().getConsoleSender().sendMessage(ChatColor.LIGHT_PURPLE + "https://dev.bukkit.org/projects/chat-security/files");
                    getServer().getConsoleSender().sendMessage(ChatColor.RED + "=-=-=-=-=-=-=-=-=--=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=");
                } else {
                    getServer().getConsoleSender().sendMessage("[ChatSecurity] Plugin is up to date! - "
                                    + getDescription().getVersion());
                }
            } catch (Exception e) {
                getLogger().info("Could not check for updates! Stacktrace:");
                e.printStackTrace();
            }
    }
    Updater class:
    Code:
    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 Updater {
    
        private URL checkURL;
        private String newVersion = "";
        private JavaPlugin plugin;
    
        public Updater(JavaPlugin plugin) {
            this.plugin = plugin;
            this.newVersion = plugin.getDescription().getVersion();
            try {
                this.checkURL = new URL(
                        "https://dev.bukkit.org/projects/chat-security/files");
            } catch (MalformedURLException e) {
                e.printStackTrace();
            }
        }
    
        public JavaPlugin getPlugin() {
            return plugin;
        }
    
        public String getLatestVersion() {
            return newVersion;
        }
    
        public String getResourceURL() {
            return "https://dev.bukkit.org/projects/chat-security/files";
        }
    
        public boolean checkForUpdates() throws Exception {
            URLConnection con = checkURL.openConnection();
            this.newVersion = new BufferedReader(new InputStreamReader(con.getInputStream()))
                    .readLine();
            return !plugin.getDescription().getVersion().equals(newVersion);
        }
    }
    ERROR:
    Code:
    [20:55:32 INFO]: [ChatSecurity] Could not check for updates! Stacktrace:
    [20:55:32 WARN]: java.io.IOException: Server returned HTTP response code: 403 fo
    r URL: https://dev.bukkit.org/projects/chat-security/files
    [20:55:32 WARN]:        at sun.net.www.protocol.http.HttpURLConnection.getInputS
    tream0(Unknown Source)
    [20:55:32 WARN]:        at sun.net.www.protocol.http.HttpURLConnection.getInputS
    tream(Unknown Source)
    [20:55:32 WARN]:        at sun.net.www.protocol.https.HttpsURLConnectionImpl.get
    InputStream(Unknown Source)
    [20:55:32 WARN]:        at ChatSecurity.Updater.checkForUpdates(Updater.java:42)
    
    [20:55:32 WARN]:        at ChatSecurity.ChatSecurity.onEnable(ChatSecurity.java:
    25)
    [20:55:32 WARN]:        at org.bukkit.plugin.java.JavaPlugin.setEnabled(JavaPlug
    in.java:264)
    [20:55:32 WARN]:        at org.bukkit.plugin.java.JavaPluginLoader.enablePlugin(
    JavaPluginLoader.java:339)
    [20:55:32 WARN]:        at org.bukkit.plugin.SimplePluginManager.enablePlugin(Si
    mplePluginManager.java:403)
    [20:55:32 WARN]:        at org.bukkit.craftbukkit.v1_13_R2.CraftServer.enablePlu
    gin(CraftServer.java:431)
    [20:55:32 WARN]:        at org.bukkit.craftbukkit.v1_13_R2.CraftServer.enablePlu
    gins(CraftServer.java:345)
    [20:55:32 WARN]:        at org.bukkit.craftbukkit.v1_13_R2.CraftServer.reload(Cr
    aftServer.java:804)
    [20:55:32 WARN]:        at org.bukkit.Bukkit.reload(Bukkit.java:569)
    [20:55:32 WARN]:        at org.bukkit.command.defaults.ReloadCommand.execute(Rel
    oadCommand.java:27)
    [20:55:32 WARN]:        at org.bukkit.command.SimpleCommandMap.dispatch(SimpleCo
    mmandMap.java:141)
    [20:55:32 WARN]:        at org.bukkit.craftbukkit.v1_13_R2.CraftServer.dispatchC
    ommand(CraftServer.java:699)
    [20:55:32 WARN]:        at org.bukkit.craftbukkit.v1_13_R2.CraftServer.dispatchS
    erverCommand(CraftServer.java:684)
    [20:55:32 WARN]:        at net.minecraft.server.v1_13_R2.DedicatedServer.aU(Dedi
    catedServer.java:459)
    [20:55:32 WARN]:        at net.minecraft.server.v1_13_R2.DedicatedServer.b(Dedic
    atedServer.java:418)
    [20:55:32 WARN]:        at net.minecraft.server.v1_13_R2.MinecraftServer.a(Minec
    raftServer.java:835)
    [20:55:32 WARN]:        at net.minecraft.server.v1_13_R2.MinecraftServer.run(Min
    ecraftServer.java:733)
    [20:55:32 WARN]:        at java.lang.Thread.run(Unknown Source)
     
    Last edited: Dec 28, 2018
  4. Offline

    timtower Administrator Administrator Moderator

    @Straiker123 My guess is that you need to set an user agent.
     
  5. And what is this ? O_O
     
  6. Offline

    timtower Administrator Administrator Moderator

    @Straiker123 Web requests use user agents to determine what kind of client is connecting, firefox, chrome, custom stuff.
    You don't have that set, don't know what it defaults to, server doesn't like the default.
     
  7. Oh and how do I set up?
     
  8. Offline

    timtower Administrator Administrator Moderator

Thread Status:
Not open for further replies.

Share This Page