Breakdown of PlayerJoinEvent, PlayerPreLoginEvent and PlayerLoginEvent

Discussion in 'Resources' started by feildmaster, Sep 22, 2011.

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

    feildmaster

    A few people wonder what the difference is between the events PlayerJoinEvent, PlayerPreLoginEvent and PlayerLoginEvent.

    The short and simple answer is: They are all called on successful login.
    • First: PlayerPreLoginEvent stores raw data (Name and IP) and can be used to ban players.
    • Second: PlayerLoginEvent detects if the player should be allowed to join (checking White list, Ban list, IP Ban list, Max Players) then letting plugins edit the result.
    • Lastly, PlayerJoinEvent gets called if the player is allowed to join, and you can set the join message. :D
    PlayerPreLoginEvent
    This event stores the following data:
    • result: Is the player allowed to login? (default "Allowed")
    • message: message to display on "kick"
    • name: Users name attempting to login
    • ipAddress: Address of user trying to login
    This is not a "player" event, and as such .getPlayer() doesn't exist.

    PlayerLoginEvent
    Now here's what I've been finding people do wrong, in my opinion. People use PlayerLogin as a place to set player information. Now, I could say this is alright, except that I must say this is completely wrong for you to do. This event should be used to validate the players login, and apply permissions to a player. What you should not try to do is set player information (eg: DisplayName). Yes, it is a convenient place for you to set the display name, but you should do this in the PlayerJoinEvent, where the player actually joins the server.
    The following data is stored:
    • Player: the player logging in
    • result: The result of login
      • ALLOWED
      • KICK_FULL (server full)
      • KICK_BANNED (banned ip/name)
      • KICK_WHITELIST (not on whitelist)
      • KICK_OTHER (other, can set by plugins)
    • message: The message to display on "kick"
    An example of code for applying permissions:
    Code:
    // register code in main java file
    getServer().getPluginManager().registerEvent(Event.Type.PLAYER_LOGIN, new playerListener(this), Event.Priority.Monitor, this); // and yes. I'm serious about using monitor.
    
    public void onPlayerLogin(PlayerLoginEvent event) {
        if(event.getResult() == PlayerLoginEvent.Result.ALLOWED)
            registerPlayer(event.getPlayer());
    }
    
    PlayerJoinEvent
    This is where you should play around with the player information. Data stored:
    • Player: The player joining
    • Message: the message to broadcast on join. (set to null for no message)
    I hope this was helpful to some people. Like.... @codename_B :p
     
    Gnat008, Maulss, Tim Visee and 2 others like this.
  2. Offline

    codename_B

  3. Offline

    feildmaster

    @codename_B : Still missing the most vital "if(event.getResult() != PlayerLoginEvent.Result.ALLOWED) return;"

    EDIT: Oh I see. You use "low"
     
  4. Offline

    iffa

    Humm, isn't PreLogin only called if the server is online-mode=true?
     
  5. Offline

    feildmaster

    I didn't check. And for the sake of bukkit promoting online-mode, it doesn't matter. :p
     
  6. Offline

    nickrak

    Just curious, could you force a player to be logged in as someone else if you changed the name property in PlayerPreLoginEvent?
     
  7. Offline

    feildmaster

    You can not setName, you can only get.
     
  8. Offline

    nickrak

    Breaks all the rules about accessing out of scope, and would technically change the name variable, but would it change the name the that actually logs in, and if so, would the minecraft.net premium account check happen before or after this?
    Code:java
    1. public void onPlayerPreLogin(PlayerPreLoginEvent event)
    2. {
    3. try
    4. {
    5. PlayerPreLoginEvent.class
    6. .getField("name")
    7. .set(event, "newusername");
    8. }
    9. catch (Exception e)
    10. {
    11. // PlayerPreLoginEvent has no field called "name"
    12. }
    13. }
     
  9. Offline

    feildmaster

    It would not. Since the event doesn't allow setting of the field normally, bukkit doesn't get the field to use as the players name. It's simply a holder for people to know who is logging in if they listen to the event.
     
  10. Offline

    thehutch

    May I ask which event you would use to check if this player is new aka checking up their player.dat file? which event would this not be created in?
     
  11. Offline

    feildmaster

    I have not looked into this, I could look for it after I wake up in a few hours though.

    This is a wild guess: but logically thinking, it would be created on player join... but I don't know. I will try and see.
     
  12. Offline

    thehutch

    Thank you :)
     
Thread Status:
Not open for further replies.

Share This Page