Player joining and leaving functions?

Discussion in 'Plugin Development' started by Satros, Apr 6, 2011.

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

    Satros

    What I need is to have data that is read in for each player at the plug-in start from a file, and for any other player whose data isn't read in that joins, their data should be a default value.

    I am planning on using a hashmap that uses a players name as a key to look up the associated data.
    The names that have associated data will have their data added to the hashmap as it is read in from a file.

    The problem is, is that when a player joins the game I need their data to be added to the hashmap and removed if they don't use certain commands again when they leave.

    How can I get when a player joins(and their name) and when they leave?
    Do functions/listeners exist for when this happens, if so how do I go about using them?

    psuedocode for what i'm looking to do ideally would be something like this:
    Code:
    whenPlayerJoins(Player)
    {
      if (not in hashmap already)
         add player to hashmap with default values
    }
    whenPlayerLeaves(Player)
    {
      if(player has default values in hashmap)
         remove player from hashmap
    }
     
  2. Offline

    willurd

    You need to create a PlayerListener subclass and override the onPlayerJoin and onPlayerQuit methods, like so:

    Code:
    import org.bukkit.entity.Player;
    import org.bukkit.event.player.PlayerListener;
    import org.bukkit.event.player.PlayerJoinEvent;
    import org.bukkit.event.player.PlayerQuitEvent;
    
    public class YourPlayerListener extends PlayerListener {
        @Override
        public void onPlayerJoin(PlayerJoinEvent event) {
            // Called when a player joins a server
            Player player = event.getPlayer();
            String joinMessage = event.getJoinMessage();
        }
        @Override
        public void onPlayerQuit(PlayerQuitEvent event) {
            // Called when a player leaves a server
            Player player = event.getPlayer();
            String quitMessage = event.getQuitMessage();
        }
    }
    
    Then register the events in your plugin's onEnable method:

    Code:
    PluginManager pm = getServer().getPluginManager();
    YourPlayerListener pl = new YourPlayerListener(this);
    pm.registerEvent(Event.Type.PLAYER_JOIN, pl, Priority.Normal, this);
    pm.registerEvent(Event.Type.PLAYER_QUIT, pl, Priority.Normal, this);
    
    EDIT: Messed up the PlayerQuitEvent import.
     
    Satros likes this.
  3. Offline

    Satros

    Thank you this is exactly what I was looking for!

    EDIT:
    Also in case anyone else has a similar problem it should be noted:
    the correct import from the above code should be PlayerQuitEvent, not PlayerLeaveEvent.

    Code:
    import org.bukkit.entity.Player;
    import org.bukkit.event.player.PlayerListener;
    import org.bukkit.event.player.PlayerJoinEvent;
    import org.bukkit.event.player.PlayerQuitEvent;
    
    public class YourPlayerListener extends PlayerListener {
        @Override
        public void onPlayerJoin(PlayerJoinEvent event) {
            // Called when a player joins a server
            Player player = event.getPlayer();
            String joinMessage = event.getJoinMessage();
        }
        @Override
        public void onPlayerQuit(PlayerQuitEvent event) {
            // Called when a player leaves a server
            Player player = event.getPlayer();
            String quitMessage = event.getQuitMessage();
        }
    }
    
     
  4. Offline

    willurd

    np! I also edited my original comment with information on registering the events.

    Woops! Editing my post. Thanks.

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

Share This Page