Solved PlayerJoinEvent Not Passing.

Discussion in 'Plugin Development' started by xYourFreindx, Aug 2, 2014.

Thread Status:
Not open for further replies.
  1. I've had this problem for the past two months now, and it's been preventing me from doing any updates to dev.bukkit.

    Every time a player joins, I get this stacktrace:​
    http://pastebin.com/tZCiCQKX

    Upon inspection, you will see that the error refers to these lines:​
    Code:java
    1.  
    2. @EventHandler
    3. public void onPlayerJoin(PlayerJoinEvent e){
    4. if ((main.getConfig().getBoolean("CameraMode.Updates.NotifyOps") == true)) {
    5. if (updater.getResult().equals(UpdateResult.UPDATE_AVAILABLE)) {
    6. if (updater.getLatestType().toString().equalsIgnoreCase("release")) {
    7. if (e.getPlayer().hasPermission("cameramode.update")){
    8. e.getPlayer().sendMessage(ChatColor.DARK_AQUA + "CameraMode: " + ChatColor.AQUA + "New Update Available!");
    9. e.getPlayer().sendMessage(ChatColor.GRAY + updater.getLatestFileLink().toString());
    10. }
    11. }
    12. }
    13. }
    14. }

    Specifically, the first line that mentions the updater class.​
    I know there's nothing wrong with the updater class.​
    It was made by the bukkit dev team and has worked, as is, since before the problem arose.

    So I went back to where the updater class is initiated, thinking there might be a problem with how I instantiated it.

    Here, you can find the onEnable:​
    Code:java
    1.  
    2. //###### - Objects - ######//
    3.  
    4.  
    5. UpdateType bifff;
    6. UpdateType biff = this.getUpdater();
    7.  
    8. //############################//
    9. //#########- Start - #########//
    10. //############################//
    11.  
    12. public void onEnable() {
    13. getConfig().options().copyHeader(true);
    14. getConfig().options().copyDefaults(true);
    15. saveConfig();
    16. if (getConfig().getBoolean("CameraMode.Enabled") == false) {
    17. getLogger().info("Plugin Disable Setting Detected...");
    18. getServer().getPluginManager().disablePlugin(this);
    19. }
    20. getServer().getPluginManager().registerEvents(new Events(this), this);
    21. PluginDescriptionFile pdfFile = this.getDescription();
    22. commands.add("camera");
    23. commands.add("cameramode");
    24. for (String commands : this.commands){
    25. getCommand(commands).setExecutor(new Commands(this));
    26. }
    27.  
    28. getUpdater();
    29. getLogger().info(pdfFile.getName() + " v" + pdfFile.getVersion() + " has been enabled");
    30. if (biff != null) {
    31. @SuppressWarnings("unused")
    32. Updater updater = new Updater(this, 80542, getFile(), biff, true);
    33. }
    34. }

    The "GetUpdater()" is a method of my own making. It was the first real method I ever made, so obviously, it was, and still is, highly suspect for being the cause behind the trouble. But I can't seem to find any way to alter it in a better suited fashion.
    Here it is:​
    Code:java
    1.  
    2. public UpdateType getUpdater(){
    3. if (getConfig().getBoolean("CameraMode.Updates.AutoUpdate") == true){
    4. bifff = Updater.UpdateType.DEFAULT;
    5. }else if (getConfig().getBoolean("CameraMode.Updates.NotifyOps") == true){
    6. bifff = Updater.UpdateType.NO_DOWNLOAD;
    7. }else{
    8. bifff = null;
    9. }
    10. return bifff;
    11. }

    I will not just walk away from this issue and not include the updater.
    I take this and my plugin seriously, and I want to know what is producing the problem, and how I can fix it, so I can prevent this from ever happening again.

    These have been a long two months... And any advice you can offer would be appreciated.​
    Thanks so much.​
    xYourFreindx ​

     
  2. Offline

    The Fancy Whale

    Line 134 of events class is null.
    Code:java
    1. if (updater.getResult().equals(UpdateResult.UPDATE_AVAILABLE)) {

    is returning null. Probably from updader.getResult()
     
  3. The Fancy Whale
    It could be. Which is why I thought that the updater class may not have been initialized correctly in my main class.

    BASICALLY

    I think there's something either wrong with my getUpdater() method, or the way it is retrieved. ​

    Update....
    I have completely removed the getUpdater() method from the main class and replaced it with something simpler in the onEnable. ​
    Code:java
    1.  
    2. if (getConfig().getBoolean("CameraMode.Updates.AutoUpdate") == true){
    3. Updater updater = new Updater(this, 80542, getFile(), Updater.UpdateType.DEFAULT, true);
    4. }else if (getConfig().getBoolean("CameraMode.Updates.NotifyOps") == true){
    5. Updater updater = new Updater(this, 80542, getFile(), Updater.UpdateType.NO_DOWNLOAD, true);
    6. }else;

    And fiddled with the onPlayerjoin a bit, producing...​
    Code:java
    1. Updater updater;
    2. public Events(Updater plugin){
    3. this.updater = plugin;
    4. }
    5.  
    6. @EventHandler
    7. public void onPlayerJoin(PlayerJoinEvent e){
    8. if ((main.getConfig().getBoolean("CameraMode.Updates.NotifyOps") == true)) {
    9. if (updater.getResult().toString().equals("UPDATE_AVAILABLE")) {
    10. if (updater.getLatestType().toString().equalsIgnoreCase("release")) {
    11. if (e.getPlayer().hasPermission("cameramode.update")){
    12. e.getPlayer().sendMessage(ChatColor.DARK_AQUA + "CameraMode: " + ChatColor.AQUA + "New Update Available!");
    13. e.getPlayer().sendMessage(ChatColor.GRAY + updater.getLatestFileLink().toString());
    14. }
    15. }
    16. }
    17. }
    18. }

    But NOTHING I do seems to have any effect.
    I'm STILL getting the exact same result whenever I join the server.​
    I'm really at a loss...
    Please... ​

    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: Jun 9, 2016
  4. Offline

    fireblast709

    xYourFreindx you have two constructors in Events, obviously only one will be used in the initialization of the object. In your code you only use the plugin one, which leaves the updater null, causing the NPE.

    First create an Updater instance like you do in the end of onEnable(). Then create the Events object using a new constructor taking both a plugin instance and an Updater instance, and initialize the Events fields accordingly.
     
  5. fireblast709
    If I understand you correctly, what you're saying is that this:
    Code:java
    1.  
    2. public Events(CameraMode plugin){
    3. this.main = plugin;
    4. }
    5. public Events(Updater plugin){
    6. this.updater = plugin;
    7. }

    Is bad.

    I don't know how I would even begin to go about making a constructor for two classes. Could you give me an example?
    Also....
    If I am still understanding you right...
    I'm going to need to do this:
    Code:java
    1.  
    2. getLogger().info(pdfFile.getName() + " v" + pdfFile.getVersion() + " has been enabled");
    3. if (getConfig().getBoolean("CameraMode.Updates.AutoUpdate") == true){
    4. Updater updater = new Updater(this, 80542, getFile(), Updater.UpdateType.DEFAULT, true);
    5. }else if (getConfig().getBoolean("CameraMode.Updates.NotifyOps") == true){
    6. Updater updater = new Updater(this, 80542, getFile(), Updater.UpdateType.NO_DOWNLOAD, true);
    7. }else;
    8.  

    in my events class as well. Would that go in this "double" constructor? Or would it replace the "Updater updater;" ?

    And thank you... You've already been more help to me in the past two minutes, than anyone else has been in the past two months.
     
  6. Offline

    fireblast709

    xYourFreindx it's not bad, it simply doesn't work.
    Code:
    public Events(CameraMode plugin, Updater updater){
        this.main = plugin;
        this.updater = updater;
    }
    this does work. Also you need to define the Updater instance outside the if-else scope
    Code:
    Updater updater;
    if(...)
    {
        updater = ...;
    }
    else
    {
        updater = ...
    }
     
  7. fireblast709 I need the else if statements in order to correctly initiate the updater class, as defined by the settings in the config.
    I don't know what you want me to do, but I can't (obviously) have an else if statement hanging out in the open, outside a method.
    So what do you suggest?
     
  8. Offline

    fireblast709

    xYourFreindx as I showed, you define the Updater in the method scope do you can use it later on. You can insert as many 'else if', as long as the 'Updater updater' stays outside the if...else if...else block
     
  9. Here is what I've produced... ​
    Code:
    public class Events implements Listener {
     
        CameraMode main;
        Updater updater;
       
        public int ID;
        String reason = "You are in CameraMode!";
     
        public Events(CameraMode plugin, Updater updater){
            this.main = plugin;
     
            if (main.getConfig().getBoolean("CameraMode.Updates.AutoUpdate") == true){
                this.updater = new Updater(main, 80542, getFile(), Updater.UpdateType.DEFAULT, true);
            }else if (main.getConfig().getBoolean("CameraMode.Updates.NotifyOps") == true){
                this.updater = new Updater(main, 80542, getFile(), Updater.UpdateType.NO_DOWNLOAD, true);
            }
        }
    But "getFile()" is highlighted in red.
    getFile is how the updater can tell what file to replace with the new update. And can only be accessed in the main class.

    Also. In my onEnable, where I register my events, it is now highlighted in red, saying that "Events(CameraMode is not defined". ​

    fireblast709 Forgot to tag you. :p
    Thank you so much for your patience.

    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: Jun 9, 2016
  10. Offline

    fireblast709

    xYourFreindx i would create the Updater instance in the main class, then pass it to Events through the constructor
    Code:
    Updater updater;
    if(main.getConfig().getBoolean("CameraMode.Updates.AutoUpdate") == true)
    {
        updater = new Updater(main, 80542, getFile(), Updater.UpdateType.DEFAULT, true);
    }
    else if (main.getConfig().getBoolean("CameraMode.Updates.NotifyOps") == true)
    {
        updater = new Updater(main, 80542, getFile(), Updater.UpdateType.NO_DOWNLOAD, true);
    }
    Events events = new Events(this, updater);
     
  11. Thanks to the invaluable help of fireblast709 , I've come up with a solution to all my problems.
    I've set it up so that the main class does all the updater work, and instead of getting the information from the updater itself, the event class will instead take the info from an arrayList, in which the main class has stored the info telling the playerjoin event what it should do.

    Main:​
    Code:
        public ArrayList<String> updates = new ArrayList<String>();
       
        //###### - Objects - ######//
       
     
        String reason = "You are in CameraMode!";
     
     
        public ArrayList<String> commands = new ArrayList<String>();
       
       
        //############################//
        //#########- Start - #########//
        //############################//
       
        @SuppressWarnings("unused")
        public void onEnable() {
            getConfig().options().copyHeader(true);
            getConfig().options().copyDefaults(true);
            saveConfig();
            if (getConfig().getBoolean("CameraMode.Enabled") == false) {
                getLogger().info("Plugin Disable Setting Detected...");
                getServer().getPluginManager().disablePlugin(this);
            }
            getServer().getPluginManager().registerEvents(new Events(this), this);
            PluginDescriptionFile pdfFile = this.getDescription();
            commands.add("camera");
            commands.add("cameramode");
            for (String commands : this.commands){
                getCommand(commands).setExecutor(new Commands(this));
            }
            getLogger().info(pdfFile.getName() + " v" + pdfFile.getVersion() + " has been enabled");
            if (getConfig().getBoolean("CameraMode.Updates.AutoUpdate") == true){
                Updater updater = new Updater(this, 80542, getFile(), Updater.UpdateType.DEFAULT, true);
            }else if (getConfig().getBoolean("CameraMode.Updates.NotifyOps") == true){
                Updater updater = new Updater(this, 80542, getFile(), Updater.UpdateType.NO_DOWNLOAD, true);
                if (updater.getResult().equals(Updater.UpdateResult.UPDATE_AVAILABLE) && updater.getLatestType().toString().equalsIgnoreCase("release")) {
                    updates.add(updater.getLatestFileLink().toString());
                }
            }else{
            }
        }
        public void onDisable(){
            reloadConfig();
            PluginDescriptionFile pdfFile = this.getDescription();
            getLogger().info(pdfFile.getName() + " v" + pdfFile.getVersion() + " has been disabled");
            updates.clear();
        }
    PlayerJoinEvent​
    Code:java
    1.  
    2. @EventHandler
    3. public void onPlayerJoin(PlayerJoinEvent e){
    4. if (main.getConfig().getBoolean("CameraMode.Updates.NotifyOps") == true) {
    5. if (!(main.updates.isEmpty())) {
    6. if (e.getPlayer().hasPermission("cameramode.update")){
    7. e.getPlayer().sendMessage(ChatColor.DARK_AQUA + "CameraMode: " + ChatColor.AQUA + "New Update Available!");
    8. e.getPlayer().sendMessage(ChatColor.GRAY + main.updates.get(0));
    9. }
    10. }
    11. }
    12. }

    As you can see, the playerjoinevent no longer has to interact with the updater class in any way, concentrating everything to one area, and limiting the amount of code necessary.

    Thanks again to all who've assisted me in this two month long process.
    I'm glad it's over. ​
     
Thread Status:
Not open for further replies.

Share This Page