Solved Update checker: only works on reload

Discussion in 'Plugin Development' started by michaelkrauty, Sep 9, 2013.

Thread Status:
Not open for further replies.
  1. Offline

    michaelkrauty

    I'm adding an update checker to my plugin. So far I have this:

    Code:java
    1.  
    2. Server server = Bukkit.getServer();
    3. ConsoleCommandSender console = server.getConsoleSender();
    4.  
    5. public void onEnable(){
    6. PluginManager plm = this.getServer().getPluginManager();
    7. plm.registerEvents(new JoinListener(), this);
    8. getConfig().options().copyDefaults(true);
    9. saveConfig();
    10.  
    11. this.updateChecker = new UpdateChecker(this, "/*link to download*/");
    12. this.updateChecker.updateNeeded();
    13. if(getConfig().getString("checkupdate") == "true"){
    14. if(this.updateChecker.updateNeeded() == true){
    15. console.sendMessage(ChatColor.GREEN + "[E] A new version is available: " + this.updateChecker.getVersion());
    16. console.sendMessage(ChatColor.GREEN + "[E] Get it from: " + this.updateChecker.getLink());
    17. }
    18. if(this.updateChecker.updateNeeded() == false){
    19. console.sendMessage(ChatColor.GREEN + "[E] E is up to date! (Version " + this.updateChecker.getVersion() + ")");
    20. }
    21. }
    22. console.sendMessage(ChatColor.GREEN + "[E] Enabled E v" + this.updateChecker.getVersion());
    23. }


    In UpdateChecker.class:
    Code:java
    1. public class UpdateChecker {
    2.  
    3. private E plugin;
    4. private URL filesFeed;
    5.  
    6. private String version;
    7. private String link;
    8.  
    9. public UpdateChecker(E plugin, String url){
    10. this.plugin = plugin;
    11.  
    12. try{
    13. this.filesFeed = new URL(url);
    14. e.printStackTrace();
    15. }
    16. }
    17.  
    18. public boolean updateNeeded(){
    19. try{
    20. InputStream input = this.filesFeed.openConnection().getInputStream();
    21. Document document = DocumentBuilderFactory.newInstance().newDocumentBuilder().parse(input);
    22.  
    23. Node latestFile = document.getElementsByTagName("item").item(0);
    24. NodeList children = latestFile.getChildNodes();
    25.  
    26. this.version = children.item(1).getTextContent().replaceAll("[a-zA-Z ]", "");
    27. this.link = children.item(3).getTextContent();
    28.  
    29. if(plugin.getDescription().getVersion().equals(this.version)){
    30. return false;
    31. }
    32. }catch (Exception e){
    33. e.printStackTrace();
    34. }
    35. return true;
    36. }
    37.  
    38. public String getVersion(){
    39. return this.version;
    40. }
    41.  
    42. public String getLink(){
    43. return this.link;
    44. }
    45.  
    46. }


    It's entirely based off of BetterPHP's tutorial on update checking. When I followed along with the tutorial, everything worked fine... but as soon as I add color, I get this error:
    Code:
    2013-09-09 22:07:16 [INFO] [E] Enabling E v1.5.1
    2013-09-09 22:07:16 [SEVERE] Error occurred while enabling E v1.5.1 (Is it up to date?)
    java.lang.NullPointerException
        at me.michaelkrauty.E.E.onEnable(E.java:48)
        at org.bukkit.plugin.java.JavaPlugin.setEnabled(JavaPlugin.java:217)
        at org.bukkit.plugin.java.JavaPluginLoader.enablePlugin(JavaPluginLoader.java:457)
        at org.bukkit.plugin.SimplePluginManager.enablePlugin(SimplePluginManager.java:381)
        at org.bukkit.craftbukkit.v1_6_R2.CraftServer.loadPlugin(CraftServer.java:282)
        at org.bukkit.craftbukkit.v1_6_R2.CraftServer.enablePlugins(CraftServer.java:264)
        at net.minecraft.server.v1_6_R2.MinecraftServer.l(MinecraftServer.java:313)
        at net.minecraft.server.v1_6_R2.MinecraftServer.f(MinecraftServer.java:290)
        at net.minecraft.server.v1_6_R2.MinecraftServer.a(MinecraftServer.java:250)
        at net.minecraft.server.v1_6_R2.DedicatedServer.init(DedicatedServer.java:151)
        at net.minecraft.server.v1_6_R2.MinecraftServer.run(MinecraftServer.java:391)
        at net.minecraft.server.v1_6_R2.ThreadServerApplication.run(SourceFile:582)


    However, if I reload the server, the update checker works just fine, and prints the appropriate messages to the console as it should (with color)
    Any ideas on how to fix this? Any help is appreciated.
     
  2. Offline

    chasechocolate

    What's line 48?
     
  3. Offline

    Tarestudio

    michaelkrauty
    Some hints to you code in general:
    - String should be compared with equals (First Code, Line 13)
    - 'if ( something == true)' is redundant. if needs a true or false in it, so if somthing it true, there is no need to check it. 'if (something)' is enough
    - If you want to do something for the opposite of an if-condition, dont check against false, just use else. 'if (something) { [code for true] } else { [code for false] }
    - I think First Code, Line 12 can be removed.

    For your problem: What chasechocolate said.
     
  4. Offline

    michaelkrauty

    Line 48 in the main class is
    Code:java
    1. console.sendMessage(ChatColor.GREEN + "[E] E is up to date! (Version " + this.updateChecker.getVersion() + ")");


    I don't get what's wrong with that...

    And now the error points out line 49
    Code:java
    1. /*47*/ if(!this.updateChecker.updateNeeded()){
    2. /*48*/ console.sendMessage(ChatColor.GREEN + "[E] E is up to date! (Version " + this.updateChecker.getVersion() + ")");
    3. /*49*/ }


    I just got it...
    Code:java
    1.  
    2. Server server = Bukkit.getServer();
    3. ConsoleCommandSender console = server.getConsoleSender();

    had to be IN onEnable()
    Thanks for the help

    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: Jun 4, 2016
Thread Status:
Not open for further replies.

Share This Page