Solved Checking if there is a new update when a player joins.

Discussion in 'Plugin Development' started by StephenTheHero, May 29, 2014.

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

    StephenTheHero

    Hello developers!
    I'm trying to make it so when a player joins the server with a certain permission and there is a new update for my plugin they will get a message but I've had no luck finding out how.
    What I have so far is a listener then says the message when a player joins with that perm.

    Code:java
    1. package me.StephenTheHero._____.Listeners;
    2.  
    3. import me.StephenTheHero._____.MainFile;
    4.  
    5. import org.bukkit.Bukkit;
    6. import org.bukkit.ChatColor;
    7. import org.bukkit.entity.Player;
    8. import org.bukkit.event.EventHandler;
    9. import org.bukkit.event.Listener;
    10. import org.bukkit.event.player.PlayerJoinEvent;
    11.  
    12. public class UpdateJoinListener implements Listener {
    13. public MainFile plugin;
    14.  
    15. public UpdateJoinListener(MainFile plugin) {
    16. this.plugin = plugin;
    17. }
    18.  
    19. @EventHandler
    20. public void onPlayerJoin(final PlayerJoinEvent event)
    21. {
    22. Bukkit.getServer().getScheduler().scheduleSyncDelayedTask(plugin, new Runnable()
    23. {
    24. public void run()
    25. {
    26.  
    27. Player player = event.getPlayer();
    28. if (player.isOp() || (player.hasPermission("_____.Update")) || (player.hasPermission("_____.*")))
    29. {
    30.  
    31. if (MainFile.update)
    32. {
    33. player.sendMessage(ChatColor.DARK_GRAY + "---------------------------------------------------");
    34. player.sendMessage(ChatColor.GREEN + "[_____] " + ChatColor.GOLD + "An update is available: " + ChatColor.BOLD + MainFile.name + ChatColor.RESET + ChatColor.GOLD + "!");
    35. player.sendMessage(ChatColor.GREEN + "[_____ " + ChatColor.GOLD + "Please update by going to: " + ChatColor.BOLD + "-LINK-");
    36. player.sendMessage(ChatColor.DARK_GRAY + "---------------------------------------------------");
    37.  
    38. }
    39. else
    40. {
    41. player.sendMessage(ChatColor.GREEN + "[_____] " + ChatColor.BOLD + "" + ChatColor.RED + "" + ChatColor.STRIKETHROUGH + "ERROR");
    42. }
    43.  
    44. }
    45. }
    46. }, 22L);
    47. }
    48. }

    As you can see, I have "If (MainFile.update)"
    but thats where I'm stuck, this is my main file,

    Code:java
    1. package me.StephenTheHero._____;
    2.  
    3. import java.io.IOException;
    4.  
    5. import me.StephenTheHero._____.Commands._____Command;
    6. import me.StephenTheHero._____.Listeners.UpdateJoinListener;
    7. import me.StephenTheHero._____.Metrics.Metrics;
    8. import me.StephenTheHero._____.Updater.Updater;
    9. import me.StephenTheHero._____.Updater.Updater.ReleaseType;
    10. import me.StephenTheHero._____.Updater.Updater.UpdateResult;
    11.  
    12. import org.bukkit.Bukkit;
    13. import org.bukkit.ChatColor;
    14. import org.bukkit.plugin.java.JavaPlugin;
    15.  
    16. public class MainFile extends JavaPlugin
    17. {
    18.  
    19.  
    20.  
    21. public static boolean update = false;
    22. public static String name = "";
    23. public static ReleaseType type = null;
    24. public static String version = "";
    25. public static String link = "";
    26. // You would want to make getter methods in your class, this is just for simplicity.
    27.  
    28.  

    I have no idea how to make it so it will only show the message if there is a update, I know I've done this wrong with the Boolean I knew before I did it, it was just for testing. Anyone?

    I'm using this updater: https://forums.bukkit.org/threads/u...ant-auto-updating-for-your-plugins-new.96681/

    The name of my plugin in the code has been replaced with '_____' just because I don't want people knowing what I'm working on, sorry.
     
  2. Offline

    L33m4n123

  3. Offline

    Garris0n

    So you know, you shouldn't put any capital letters in your package names. They should be all lowercase.
     
  4. Offline

    StephenTheHero

    I know, that's the API/updater I'm using, I just don't know how to make it so it'll only send a message if there is a update available.


    Why? I've always done this and it seems fine.
     
  5. Offline

    L33m4n123


    Then read the first post again I'd say

    Show Spoiler
     
  6. Offline

    StephenTheHero


    Yea, I know how to check for a update but it just doesn't work this is what I'm getting at:
    The PlayerJoinEvent is in another class and I have no idea how to fix this....

    [​IMG]
     
  7. Offline

    Garris0n

    There are naming conventions for many reasons. In this case, it can appear as if you're accessing inner classes instead of classes in packages among other things. If you don't follow the naming conventions it starts to confuse people and they won't want to read your code.
     
    desht likes this.
  8. Offline

    StephenTheHero

    Umm, okay?
     
  9. Offline

    Garris0n

    So fix it.
     
  10. Offline

    StephenTheHero

    I honestly have no idea what you were going on about. xD
     
  11. Offline

    Garris0n

    Packages aren't supposed to have capital letters in them. That is their naming convention. Just like variables and methods areLikeThis and classes AreLikeThis and constants ARE_LIKE_THIS.
     
  12. Offline

    desht

    Recommend you read http://google-styleguide.googlecode.com/svn/trunk/javaguide.html#s5-naming - there are other Java coding style guides, but the recommendation to use lower-case package names is pretty much universal.

    Naming conventions are there for a reason and Garris0n explained it pretty well. Package names should be all lower-case - just because your code compiles and runs with badly-named packages doesn't make it good practice.
     
    Garris0n likes this.
  13. Offline

    Garris0n

    Huh, that one is better than the wikipedia one.

    I used to link to Oracle's docs but their page mysteriously disappeared one day and the wikipedia one doesn't actually have packages on it (I actually meant to add it...I should do that).
     
  14. Offline

    Konkz

    Method names:
    onPlayerJoin = Right
    onplayerjoin = Wrong
    OnPlayerJoin = Wrong
    ONplayerJOIN = Wrong etc.

    Package Names:
    package = Right
    Package = Wrong
    PACKAGE = Wrong

    Class Names:
    MyClass = Right
    myclass = Wrong
    MYclass = Wrong
    MYCLAS = Wrong
     
  15. Offline

    StephenTheHero

  16. Offline

    Konkz

    Using this does not work inside a runnable, one way to do it is get instance of the class. Eg: MyClass.getInstance().getFile();
     
  17. Offline

    StephenTheHero

    It still doesn't work without the runnable.
     
  18. Offline

    ChazSchmidt

    StephenTheHero Here's what I do in my plugin

    onEnable:
    Code:java
    1. Updater updater = new Updater(this, {YOUR NUM HERE}, getFile(),
    2. Updater.UpdateType.NO_DOWNLOAD, false);
    3. log.info("[SneakAttack] Updater Result: " + updater.getResult());
    4. updateStatus = updater.getResult().toString();


    onJoining (in main) :
    Code:
        @EventHandler
        public void onLogin(PlayerJoinEvent e) {
            Player player = e.getPlayer();
            if (player.isOp()) {
                if (updateStatus.equals(Updater.UpdateResult.UPDATE_AVAILABLE
                        .toString())) {
                    player.sendMessage(ChatColor.BOLD
                            + "A new version of SneakAttack is available. Visit Bukkit or Curse to download the latest version.");
                }
            }
        }
    
     
  19. Offline

    StephenTheHero

    Guys, I've found out how to fix this...
    I changed:
    Code:java
    1. public static boolean update = false;
    to
    Code:java
    1. public static boolean update;
    so it linked to:
    Code:java
    1. update = updater.getResult() == Updater.UpdateResult.UPDATE_AVAILABLE; // Determine if there is an update ready for us
    2. name = updater.getLatestName(); // Get the latest name
    3. version = updater.getLatestGameVersion(); // Get the latest game version
    4. type = updater.getLatestType(); // Get the latest file's type
    5. link = updater.getLatestFileLink(); // Get the latest link
    in my onEnable()

    if anyone is having the same problem with this update just ask me! I know how to do this all now.
     
Thread Status:
Not open for further replies.

Share This Page