Check if player is new to server?

Discussion in 'Plugin Development' started by NerdsWBNerds, Jan 12, 2012.

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

    NerdsWBNerds

    How can I check when a player logs into the server if he has been on the server before or not?
     
  2. Offline

    wwsean08

    a few ways:
    1. Keep a list of all the players who have ever been on the server in your config file
    2. Check to see if they have a data file in any of the worlds folders
    3. I don't know but i wanted there to be a few so I threw in 3 anyways.
     
  3. Offline

    obnoxint

    This doesn't work, because the moment the player attempts to login, the server creates the according data file.

    Suggestion #1 is a good one. You can use this class, if you like to:


    Code:
    package net.obnoxint.bdev.Gift;
    
    import java.io.Serializable;
    
    import org.bukkit.Bukkit;
    import org.bukkit.entity.Player;
    
    /**
     * KnownPlayer
     *
     * @author obnoxint ([email protected])
     * @version 0.0.1
     */
    public final class KnownPlayer implements Serializable {
    
        private static final long serialVersionUID = 7172171081728068484L;
    
        private final long firstSeenDate;
    
        private final String playerName;
    
        /**
         * @param player an instance of Player.
         */
        KnownPlayer(Player player) {
            firstSeenDate = System.currentTimeMillis();
            playerName = player.getName();
        }
    
        @Override
        public boolean equals(Object obj) {
            if (obj instanceof String) {
                return ((String) obj).equals(getPlayerName());
            } else if (obj instanceof Player) {
                return ((Player) obj).getName().equals(getPlayerName());
            } else if (obj instanceof KnownPlayer) {
                return ((KnownPlayer) obj).getPlayerName().equals(getPlayerName());
            } else {
                return false;
            }
        }
    
        /**
         * @return the time in ms when this KnownPlayer has been seen for its first time.
         */
        public long getFirstSeenDate() {
            return firstSeenDate;
        }
    
        /**
         * @return the Player object corresponding to the instance of KnownPlayer.
         */
        public Player getPlayer() {
            return Bukkit.getPlayer(playerName);
        }
    
        /**
         * @return the name of the player.
         */
        public String getPlayerName() {
            return playerName;
        }
    
    }
    
    Use a HashSet to store the instances and ObjectOutputStream/ObjectInputStream in order to save/load the HashSet to/from a file. Use the contains()-method of the HashSet-object in order to determine if the player is already known and if not, create a new instance of KnownPlayer and add it to the HashSet.
     
  4. Offline

    wwsean08

    ah ok, i've never tested it before but i figured you might be able to get away with it using the PlayerPreLoginEvent
     
  5. Offline

    obnoxint

    Oh... well... this is something I overlooked. I don't know if it works, but you can try if you like to and tell us the results.
     
  6. Offline

    wwsean08

    i'll write a quick plugin and test it
     
  7. Offline

    tkausl

    You can use the Method:
    Code:
     public boolean hasPlayedBefore() {
            return hasPlayedBefore;
        }
    from CraftPlayer...
     
  8. Offline

    wwsean08

    and the on player prelogin event will also work, just finished testing it out
     
    obnoxint likes this.
  9. Offline

    obnoxint

    Great. Good to know. Thanks for the information.
     
  10. Offline

    wwsean08

    no problem
     
  11. Offline

    stelar7

  12. This will not work on servers with onlineMode = false.
    Test this:
    getServer().getOfflinePlayer(player.getName()).hasPlayedBefore();
     
  13. Offline

    wwsean08

    true, i always forget about offline mode because i deal with an online mode server
     
Thread Status:
Not open for further replies.

Share This Page