[SOLVED] PlayerLoginEvent Error

Discussion in 'Plugin Development' started by dakoslug, May 23, 2012.

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

    dakoslug

    Please read the error below.

    Code:
    09:38:13 [INFO] [EmergencyDowntime] padeius was kicked due to Downtime.
    09:38:13 [SEVERE] Could not pass event PlayerLoginEvent to Essentials
    org.bukkit.event.EventException
        at org.bukkit.plugin.java.JavaPluginLoader$1.execute(JavaPluginLoader.java:303)
        at org.bukkit.plugin.RegisteredListener.callEvent(RegisteredListener.java:62)
        at org.bukkit.plugin.TimedRegisteredListener.callEvent(TimedRegisteredListener.java:30)
        at org.bukkit.plugin.SimplePluginManager.callEvent(SimplePluginManager.java:459)
        at net.minecraft.server.ServerConfigurationManager.attemptLogin(ServerConfigurationManager.java:227)
        at net.minecraft.server.NetLoginHandler.b(NetLoginHandler.java:102)
        at net.minecraft.server.NetLoginHandler.a(NetLoginHandler.java:41)
        at net.minecraft.server.NetworkListenThread.a(NetworkListenThread.java:61)
        at net.minecraft.server.MinecraftServer.w(MinecraftServer.java:558)
        at net.minecraft.server.MinecraftServer.run(MinecraftServer.java:450)
        at net.minecraft.server.ThreadServerApplication.run(SourceFile:492)
    Caused by: java.lang.NullPointerException
        at com.earth2me.essentials.EssentialsPlayerListener.onPlayerLogin(EssentialsPlayerListener.java:209)
        at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
        at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
        at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
        at java.lang.reflect.Method.invoke(Method.java:616)
        at org.bukkit.plugin.java.JavaPluginLoader$1.execute(JavaPluginLoader.java:301)
        ... 10 more
    09:38:13 [INFO] Disconnecting padeius [/174.134.72.10:51952]: Server needs downtime! Check back in a few!
    Here is my listener code.

    Code:
    @EventHandler(priority = EventPriority.LOWEST)
      public void  onPlayerLogin(PlayerLoginEvent event)
      {
        if (downtimekickall)
        {
          Player player = event.getPlayer();
          event.disallow(null, plugin.getConfig().getString("Kickmessage"));
          plugin.log.info("[EmergencyDowntime] " + player.getName() + " was kicked due to Downtime.");
        }
        if (downtimefull)
        {
          Player player = event.getPlayer();
          if (!player.hasPermission("emergencydowntime.exempt")) // ! Operator means instead of  == true its  == false
          {
              event.disallow(null, plugin.getConfig().getString("Kickmessage"));
            plugin.log.info("[EmergencyDowntime] " + player.getName() + " was kicked due to Downtime.");
          }
          else
          {
            player.sendMessage(plugin.getConfig().getString("OPWarning"));
          }
        }
      }
      @SuppressWarnings("static-access")
    public EmergencyDowntimeplayerListener(EmergencyDowntime plugin)
      {
          this.plugin = plugin;
      }
    }
    Please try and give me an understanding of what went wrong.
     
  2. Offline

    r0306

    dakoslug
    I don't know why Essentials is involved in this but I guess the reason why it's throwing the error is that you are cancelling the event of the login and when Essentials is trying to get the name of the player that joined, it gives it a nullpointer. If that is the case, you can fix it by changing the priority of this event to highest.
     
  3. Offline

    dakoslug

    But shouldn't it remain on Lowest..because my plugin does this;
    When the server needs downtime and another player joins, it checks if the player is OP...if the player is not OP..it should kick them.

    I think I believe i saw a thread saying the lowest priority plugin gets the last say after all the other plugins already did. Is this the case?]

    NVM: It's seems i got this backwards. Just to clarify...HIGHEST priority means the plugin that has teh final say?
     
  4. Offline

    r0306

    dakoslug
    Yes, that is usually the case. However, looking at the source code of Essentials, I found that it runs a delayed task after the player joins. This may be the cause of the issue. I still recommend you change priority to high.
     
  5. Offline

    dakoslug

    OK I'm now confused... quote from Dinnerbone
    He also says after Highest, nothing should change the outcome of the event. Because Monitor shouldn't change the outcome it should only...well monitor the event.
    So then highest gets the last say?
     
  6. Offline

    r0306

    dakoslug
    You know, actually, the cause of the problem is not even a matter of priority. The thing is that in a PlayerLoginEvent, players who join are registered as really joining the server. What you are doing right now is kicking them AFTER they join the server, which will cause all sorts of issues. Use PlayerPreLogin event to resolve this.

    Code:
    @EventHandler(priority = EventPriority.LOWEST)
      public void  onPlayerLogin(PlayerPreLoginEvent event)
      {
        if (downtimekickall)
        {
          String player = event.getPlayer().getName();
          Player p = Bukkit.getPlayer(player);
          event.disallow(null, plugin.getConfig().getString("Kickmessage"));
          plugin.log.info("[EmergencyDowntime] " + player.getName() + " was kicked due to Downtime.");
        }
        if (downtimefull)
        {
          String player = event.getPlayer().getName();
          if (!p.hasPermission("emergencydowntime.exempt")) // ! Operator means instead of  == true its  == false
          {
              event.disallow(null, plugin.getConfig().getString("Kickmessage"));
            plugin.log.info("[EmergencyDowntime] " + player.getName() + " was kicked due to Downtime.");
          }
          else
          {
            p.sendMessage(plugin.getConfig().getString("OPWarning"));
          }
        }
      }
      @SuppressWarnings("static-access")
    public EmergencyDowntimeplayerListener(EmergencyDowntime plugin)
      {
          this.plugin = plugin;
      }
    }
     
  7. Offline

    dakoslug

    Do you mean
    Code:
    public void  onPlayerLogin(PlayerPreLoginEvent event)
    and i'm getting errors on line 21,23,27,28,31 and 35. All related to the string player..it starts with getPlayer().
    Btw I appreciate your help.
     
  8. Offline

    r0306

    dakoslug
    Yes, there aren't Player variables in PlayerPreLoginEvent. You have to get the player's name and store it as a string. I've updated the code above as reference.
     
  9. Offline

    dakoslug


    Is this code good..?

    Code:
    package me.dakoslug.EmergencyDowntime;
    import org.bukkit.Bukkit;
    import org.bukkit.entity.Player;
    import org.bukkit.event.EventHandler;
    import org.bukkit.event.EventPriority;
    import org.bukkit.event.Listener;
    import org.bukkit.event.block.BlockPlaceEvent;
    import org.bukkit.event.player.PlayerLoginEvent;
    import org.bukkit.event.player.PlayerPreLoginEvent;
     
    public class EmergencyDowntimeplayerListener implements Listener
    {
      public static boolean downtimefull = false;
      public static boolean downtimekickall = false;
      public static boolean downtimebuild =  false;
      public static EmergencyDowntime plugin;
      @EventHandler(priority = EventPriority.LOWEST)
      public void  onPlayerLogin(PlayerPreLoginEvent event)
      {
        if (downtimekickall)
        {
          String player = event.getName();
          Player p = Bukkit.getPlayer(player);
          event.disallow(null, plugin.getConfig().getString("Kickmessage"));
          plugin.log.info("[EmergencyDowntime] " + player + " was kicked due to Downtime.");
        }
        if (downtimefull)
        {
          String player = event.getName();
          Player p = Bukkit.getPlayer(player);
          if (!p.hasPermission("emergencydowntime.exempt")) // ! Operator means instead of  == true its  == false
          {
              event.disallow(null, plugin.getConfig().getString("Kickmessage"));
            plugin.log.info("[EmergencyDowntime] " + player + " was kicked due to Downtime.");
          }
          else
          {
            p.sendMessage(plugin.getConfig().getString("OPWarning"));
          }
        }
      }
      @SuppressWarnings("static-access")
    public EmergencyDowntimeplayerListener(EmergencyDowntime plugin)
      {
          this.plugin = plugin;
      }
    }
    
    Does it work as intended?
     
  10. Offline

    r0306

    dakoslug
    Yes, it works before the JoinEvent activates so the error should be removed.
     
  11. Offline

    dakoslug

    Thanks for everything :)
     
  12. Offline

    r0306

    dakoslug
    Np. Try it out and please post the results below. :p
     
  13. Offline

    dakoslug

    It was a user reported problem.Could I re-test the issue by making a new server with essentials? Or is there some config option that created the error?
     
  14. Offline

    r0306

    dakoslug
    I'm pretty sure that the problem lies in the conflict with essentials itself unless the essentials feature was togglable. Try creating a test server with just essentials.
     
  15. Offline

    dakoslug

    I used the old version of the plugin(the one that had an error) , with essentials and it produced no errors...Hmmm

    Nvm..it does recreate the error with the old version and essentials..
    But with the new version, the plugin won't kick the player at all...why?

    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: May 25, 2016
  16. Offline

    r0306

    dakoslug
    Use your server in online mode. Offline mode won't trigger the event for some reason.
     
  17. Offline

    dakoslug

    r0306

    Did exactly what you said, but now it spits an error after putting it online mode. And it still doesnt kick the player.

     
  18. Offline

    r0306

    dakoslug
    Cast string to player instead.
    Code:
    package me.dakoslug.EmergencyDowntime;
    import org.bukkit.Bukkit;
    import org.bukkit.entity.Player;
    import org.bukkit.event.EventHandler;
    import org.bukkit.event.EventPriority;
    import org.bukkit.event.Listener;
    import org.bukkit.event.block.BlockPlaceEvent;
    import org.bukkit.event.player.PlayerLoginEvent;
    import org.bukkit.event.player.PlayerPreLoginEvent;
     
    public class EmergencyDowntimeplayerListener implements Listener
    {
      public static boolean downtimefull = false;
      public static boolean downtimekickall = false;
      public static boolean downtimebuild =  false;
      public static EmergencyDowntime plugin;
      @EventHandler(priority = EventPriority.LOWEST)
      public void  onPlayerLogin(PlayerPreLoginEvent event)
      {
        if (downtimekickall)
        {
          String player = event.getName();
          Player p = (Player) player;
          event.disallow(null, plugin.getConfig().getString("Kickmessage"));
          plugin.log.info("[EmergencyDowntime] " + player + " was kicked due to Downtime.");
        }
        if (downtimefull)
        {
          String player = event.getName();
          Player p = (Player) player;
          if (!p.hasPermission("emergencydowntime.exempt")) // ! Operator means instead of  == true its  == false
          {
              event.disallow(null, plugin.getConfig().getString("Kickmessage"));
            plugin.log.info("[EmergencyDowntime] " + player + " was kicked due to Downtime.");
          }
          else
          {
            p.sendMessage(plugin.getConfig().getString("OPWarning"));
          }
        }
      }
      @SuppressWarnings("static-access")
    public EmergencyDowntimeplayerListener(EmergencyDowntime plugin)
      {
          this.plugin = plugin;
      }
    }
     
  19. Offline

    dakoslug

    r0306
    Errors: Can not cast string from player

    (if that was pseudo code..im still learning java..so I need to be hand held a little)
     
  20. Offline

    r0306

    dakoslug
    Ok. It turns out you can't directly cast from string to player. However, you can cast an offline player to a player.
    Code:
    package me.dakoslug.EmergencyDowntime;
    import org.bukkit.Bukkit;
    import org.bukkit.entity.Player;
    import org.bukkit.event.EventHandler;
    import org.bukkit.event.EventPriority;
    import org.bukkit.event.Listener;
    import org.bukkit.event.block.BlockPlaceEvent;
    import org.bukkit.event.player.PlayerLoginEvent;
    import org.bukkit.event.player.PlayerPreLoginEvent;
     
    public class EmergencyDowntimeplayerListener implements Listener
    {
      public static boolean downtimefull = false;
      public static boolean downtimekickall = false;
      public static boolean downtimebuild =  false;
      public static EmergencyDowntime plugin;
      @EventHandler(priority = EventPriority.LOWEST)
      public void  onPlayerLogin(PlayerPreLoginEvent event)
      {
        if (downtimekickall)
        {
          String player = event.getName();
          Player p = (Player) Bukkit.getOfflinePlayer(player);
          event.disallow(null, plugin.getConfig().getString("Kickmessage"));
          plugin.log.info("[EmergencyDowntime] " + player + " was kicked due to Downtime.");
        }
        if (downtimefull)
        {
          String player = event.getName();
          Player p = (Player) player;
          if (!p.hasPermission("emergencydowntime.exempt")) // ! Operator means instead of  == true its  == false
          {
              event.disallow(null, plugin.getConfig().getString("Kickmessage"));
            plugin.log.info("[EmergencyDowntime] " + player + " was kicked due to Downtime.");
          }
          else
          {
            p.sendMessage(plugin.getConfig().getString("OPWarning"));
          }
        }
      }
      @SuppressWarnings("static-access")
    public EmergencyDowntimeplayerListener(EmergencyDowntime plugin)
      {
          this.plugin = plugin;
      }
    }
     
  21. Offline

    dakoslug

    r0306

    This time only line 30 says Can not cast string from player
    NVM..repasted the code
    Player p = (Player) Bukkit.getOfflinePlayer(player);

    Error again:

    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: May 25, 2016
  22. Offline

    r0306

    dakoslug
    Arrgh lol. Okay so it's impossible to check for permissions in this event because we can't get the player instance from the event. Can you remove these lines for now just for the sake of testing to see if the error is resolved?
    Code:
          Player p = (Player) Bukkit.getOfflinePlayer(player);
          if (!p.hasPermission("emergencydowntime.exempt")) // ! Operator means instead of  == true its  == false
          {
    
          }
            p.sendMessage(plugin.getConfig().getString("OPWarning"));
    
     
  23. Offline

    dakoslug

    Error remains :/ I guess it's not permissions of an offline player.
     
  24. Offline

    r0306

    dakoslug
    Change your code to this:
    Code:
    package me.dakoslug.EmergencyDowntime;
    import org.bukkit.Bukkit;
    import org.bukkit.entity.Player;
    import org.bukkit.event.EventHandler;
    import org.bukkit.event.EventPriority;
    import org.bukkit.event.Listener;
    import org.bukkit.event.block.BlockPlaceEvent;
    import org.bukkit.event.player.PlayerLoginEvent;
    import org.bukkit.event.player.PlayerPreLoginEvent;
     
    public class EmergencyDowntimeplayerListener implements Listener
    {
      public static boolean downtimefull = false;
      public static boolean downtimekickall = false;
      public static boolean downtimebuild =  false;
      public static EmergencyDowntime plugin;
      @EventHandler(priority = EventPriority.LOWEST)
      public void  onPlayerLogin(PlayerPreLoginEvent event)
      {
        if (downtimekickall)
        {
          String player = event.getName();
          event.disallow(null, plugin.getConfig().getString("Kickmessage"));
          plugin.log.info("[EmergencyDowntime] " + player + " was kicked due to Downtime.");
        }
        if (downtimefull)
        {
          String player = event.getName();
            event.disallow(null, plugin.getConfig().getString("Kickmessage"));
            plugin.log.info("[EmergencyDowntime] " + player + " was kicked due to Downtime.");
        }
      }
      @SuppressWarnings("static-access")
    public EmergencyDowntimeplayerListener(EmergencyDowntime plugin)
      {
          this.plugin = plugin;
      }
    }
     
  25. Offline

    dakoslug

    r0306

    Yeah! That worked..but theres no way to get permission check ?? :c
     
  26. Offline

    r0306

    dakoslug
    Well, I looked everywhere for a permissions check for OfflinePlayer or String but I guess the only way to do so would be to read the permissions file directly or to create your own config file to define who can/can't join. (However, you can use .isOp() check for OfflinePlayer).
     
  27. Offline

    dakoslug

    Then..is there a way to make the original code back in the first post work without the error?
    isOP() hmm.
    And how could I define the people allowed, which are the people who have the permission into a list which i can call on?
     
  28. Offline

    r0306

    dakoslug
    Hmm. I don't think there is a way to do that. However, on your enable method, you can call up a list of registered players and sync them with your list in config.
     
  29. Offline

    dakoslug

    Right then when they are online, see if they have the permissions. Wow this is MUCH MORE complex that it was supposed to be..woww...theres no way to keep it simple eh?
     
  30. Offline

    r0306

    dakoslug
    I'm not really aware of any other handlers that are triggered before the player joins the server. Since not many plugins use this handler, I find that using it prevents conflicts from occurring.
     
Thread Status:
Not open for further replies.

Share This Page