001    package org.bukkit.event.player;
002    
003    import java.net.InetAddress;
004    
005    import org.bukkit.entity.Player;
006    import org.bukkit.event.HandlerList;
007    
008    /**
009     * Stores details for players attempting to log in
010     */
011    public class PlayerLoginEvent extends PlayerEvent {
012        private static final HandlerList handlers = new HandlerList();
013        private final InetAddress address;
014        private final String hostname;
015        private Result result = Result.ALLOWED;
016        private String message = "";
017    
018        /**
019         * @deprecated Address should be provided in other constructor
020         */
021        @Deprecated
022        public PlayerLoginEvent(final Player player) {
023            this(player, "", null);
024        }
025    
026        /**
027         * @deprecated Address should be provided in other constructor
028         */
029        @Deprecated
030        public PlayerLoginEvent(final Player player, final String hostname) {
031            this(player, hostname, null);
032        }
033    
034        /**
035         * This constructor defaults message to an empty string, and result to
036         * ALLOWED
037         *
038         * @param player The {@link Player} for this event
039         * @param hostname The hostname that was used to connect to the server
040         * @param address The address the player used to connect, provided for
041         *     timing issues
042         */
043        public PlayerLoginEvent(final Player player, final String hostname, final InetAddress address) {
044            super(player);
045            this.hostname = hostname;
046            this.address = address;
047        }
048    
049        /**
050         * @deprecated Address and hostname should be provided in other
051         *     constructor
052         */
053        @Deprecated
054        public PlayerLoginEvent(final Player player, final Result result, final String message) {
055            this(player, "", null, result, message);
056        }
057    
058        /**
059         * This constructor pre-configures the event with a result and message
060         *
061         * @param player The {@link Player} for this event
062         * @param hostname The hostname that was used to connect to the server
063         * @param address The address the player used to connect, provided for
064         *     timing issues
065         * @param result The result status for this event
066         * @param message The message to be displayed if result denies login
067         */
068        public PlayerLoginEvent(final Player player, String hostname, final InetAddress address, final Result result, final String message) {
069            this(player, hostname, address);
070            this.result = result;
071            this.message = message;
072        }
073    
074        /**
075         * Gets the current result of the login, as an enum
076         *
077         * @return Current Result of the login
078         */
079        public Result getResult() {
080            return result;
081        }
082    
083        /**
084         * Sets the new result of the login, as an enum
085         *
086         * @param result New result to set
087         */
088        public void setResult(final Result result) {
089            this.result = result;
090        }
091    
092        /**
093         * Gets the current kick message that will be used if getResult() !=
094         * Result.ALLOWED
095         *
096         * @return Current kick message
097         */
098        public String getKickMessage() {
099            return message;
100        }
101    
102        /**
103         * Sets the kick message to display if getResult() != Result.ALLOWED
104         *
105         * @param message New kick message
106         */
107        public void setKickMessage(final String message) {
108            this.message = message;
109        }
110    
111        /**
112         * Gets the hostname that the player used to connect to the server, or
113         * blank if unknown
114         *
115         * @return The hostname
116         */
117        public String getHostname() {
118            return hostname;
119        }
120    
121        /**
122         * Allows the player to log in
123         */
124        public void allow() {
125            result = Result.ALLOWED;
126            message = "";
127        }
128    
129        /**
130         * Disallows the player from logging in, with the given reason
131         *
132         * @param result New result for disallowing the player
133         * @param message Kick message to display to the user
134         */
135        public void disallow(final Result result, final String message) {
136            this.result = result;
137            this.message = message;
138        }
139    
140        /**
141         * Gets the {@link InetAddress} for the Player associated with this event.
142         * This method is provided as a workaround for player.getAddress()
143         * returning null during PlayerLoginEvent.
144         *
145         * @return The address for this player. For legacy compatibility, this may
146         *     be null.
147         */
148        public InetAddress getAddress() {
149            return address;
150        }
151    
152        @Override
153        public HandlerList getHandlers() {
154            return handlers;
155        }
156    
157        public static HandlerList getHandlerList() {
158            return handlers;
159        }
160    
161        /**
162         * Basic kick reasons for communicating to plugins
163         */
164        public enum Result {
165    
166            /**
167             * The player is allowed to log in
168             */
169            ALLOWED,
170            /**
171             * The player is not allowed to log in, due to the server being full
172             */
173            KICK_FULL,
174            /**
175             * The player is not allowed to log in, due to them being banned
176             */
177            KICK_BANNED,
178            /**
179             * The player is not allowed to log in, due to them not being on the
180             * white list
181             */
182            KICK_WHITELIST,
183            /**
184             * The player is not allowed to log in, for reasons undefined
185             */
186            KICK_OTHER
187        }
188    }